]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerdemo.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / playerdemo.qc
1 #include "playerdemo.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5 #include "defs.qh"
6 #include "playerdemo.qh"
7 #include <common/state.qh>
8 #endif
9
10 .float playerdemo_fh;
11 .float playerdemo_mode;
12 .float playerdemo_starttime;
13 .float playerdemo_time;
14 const float PLAYERDEMO_MODE_OFF = 0;
15 const float PLAYERDEMO_MODE_READING = 1;
16 const float PLAYERDEMO_MODE_WRITING = 2;
17 void playerdemo_init(entity this)
18 {
19         this.playerdemo_mode = PLAYERDEMO_MODE_OFF;
20 }
21 void playerdemo_shutdown(entity this)
22 {
23         if (this.playerdemo_mode != PLAYERDEMO_MODE_OFF) {
24                 LOG_INFO("playerdemo: ", this.netname, " closed");
25                 fclose(this.playerdemo_fh);
26         }
27         this.playerdemo_mode = 0;
28 }
29 void playerdemo_open_read(entity this, string f)
30 {
31         playerdemo_shutdown(this);
32         this.playerdemo_mode = PLAYERDEMO_MODE_READING;
33         this.playerdemo_fh = fopen(f, FILE_READ);
34         this.playerdemo_starttime = time - 1;
35         this.playerdemo_time = stof(fgets(this.playerdemo_fh));
36         this.playerdemo_time += this.playerdemo_starttime;
37         set_movetype(this, MOVETYPE_NONE);
38         LOG_INFO("playerdemo: ", this.netname, " reading from ", f);
39 }
40 void playerdemo_open_write(entity this, string f)
41 {
42         playerdemo_shutdown(this);
43         this.playerdemo_mode = PLAYERDEMO_MODE_WRITING;
44         this.playerdemo_fh = fopen(f, FILE_WRITE);
45         this.playerdemo_starttime = time - 1;
46         LOG_INFO("playerdemo: ", this.netname, " writing to ", f);
47         LOG_INFO("WARNING: playerdemo file format is incomplete and not stable yet. DO NOT RELY ON IT!");
48 }
49 #define PLAYERDEMO_FIELD(ent, func, t, f) func##t(ent, f, #f);
50 #define PLAYERDEMO_FIELDS(ent, func) \
51         PLAYERDEMO_FIELD(ent, func, originvector, origin) \
52         PLAYERDEMO_FIELD(ent, func, vector, angles) \
53         PLAYERDEMO_FIELD(ent, func, sizevector, mins) \
54         PLAYERDEMO_FIELD(ent, func, sizevector, maxs) \
55         PLAYERDEMO_FIELD(ent, func, vector, v_angle) \
56         PLAYERDEMO_FIELD(ent, func, modelstring, model) \
57         PLAYERDEMO_FIELD(ent, func, string, playermodel) \
58         PLAYERDEMO_FIELD(ent, func, float, skin) \
59         PLAYERDEMO_FIELD(ent, func, string, playerskin) \
60         PLAYERDEMO_FIELD(ent, func, float, frame) \
61         PLAYERDEMO_FIELD(ent, func, float, effects) \
62         /* PLAYERDEMO_FIELD(ent,func,float,switchweapon) */ \
63         PLAYERDEMO_FIELD(CS(ent), func, float, button0)   /* TODO: PHYS_INPUT_BUTTON_ATCK */ \
64         PLAYERDEMO_FIELD(CS(ent), func, float, button3)   /* TODO: PHYS_INPUT_BUTTON_ATCK2 */ \
65         PLAYERDEMO_FIELD(CS(ent), func, float, button5)   /* TODO: PHYS_INPUT_BUTTON_CROUCH */ \
66         PLAYERDEMO_FIELD(CS(ent), func, float, button6)   /* TODO: PHYS_INPUT_BUTTON_HOOK */ \
67         PLAYERDEMO_FIELD(CS(ent), func, float, buttonuse) /* TODO: PHYS_INPUT_BUTTON_USE */ \
68         PLAYERDEMO_FIELD(ent, func, float, flags) \
69         // end of list
70
71 void playerdemo_write_originvector(entity this, .vector f, string name)
72 {
73         fputs(this.playerdemo_fh, strcat(vtos(this.(f)), "\n"));
74 }
75 void playerdemo_write_sizevector(entity this, .vector f, string name)
76 {
77         fputs(this.playerdemo_fh, strcat(vtos(this.(f)), "\n"));
78 }
79 void playerdemo_write_vector(entity this, .vector f, string name)
80 {
81         fputs(this.playerdemo_fh, strcat(vtos(this.(f)), "\n"));
82 }
83 void playerdemo_write_string(entity this, .string f, string name)
84 {
85         fputs(this.playerdemo_fh, strcat(this.(f), "\n"));
86 }
87 void playerdemo_write_modelstring(entity this, .string f, string name)
88 {
89         fputs(this.playerdemo_fh, strcat(this.(f), "\n"));
90 }
91 void playerdemo_write_float(entity this, .float f, string name)
92 {
93         fputs(this.playerdemo_fh, strcat(ftos(this.(f)), "\n"));
94 }
95 void playerdemo_write(entity this)
96 {
97         if (this.playerdemo_mode != PLAYERDEMO_MODE_WRITING) {
98                 return;
99         }
100         fputs(this.playerdemo_fh, strcat(ftos(time - this.playerdemo_starttime), "\n"));
101         PLAYERDEMO_FIELDS(this, playerdemo_write_)
102 }
103 void playerdemo_read_originvector(entity this, .vector f, string name)
104 {
105         setorigin(this, stov(fgets(this.playerdemo_fh)));
106 }
107 void playerdemo_read_sizevector(entity this, .vector f, string name)
108 {
109         this.(f) = stov(fgets(this.playerdemo_fh));
110         setsize(this, this.mins, this.maxs);
111 }
112 void playerdemo_read_vector(entity this, .vector f, string name)
113 {
114         this.(f) = stov(fgets(this.playerdemo_fh));
115 }
116 void playerdemo_read_string(entity this, .string f, string name)
117 {
118         string s = fgets(this.playerdemo_fh);
119         if (s != this.(f)) {
120                 /*
121                 if(this.f)
122                     strunzone(this.f);
123                 */
124                 this.(f) = strzone(s);
125         }
126 }
127 void playerdemo_read_modelstring(entity this, .string f, string name)
128 {
129         string s = fgets(this.playerdemo_fh);
130         if (s != this.(f)) {
131                 _setmodel(this, s);
132         }
133 }
134 void playerdemo_read_float(entity this, .float f, string name)
135 {
136         this.(f) = stof(fgets(this.playerdemo_fh));
137 }
138 float playerdemo_read(entity this)
139 {
140         if (this.playerdemo_mode != PLAYERDEMO_MODE_READING) {
141                 return 0;
142         }
143         if (this.playerdemo_time < 0) {
144                 return 1;
145         }
146         float t;
147         t = time;
148         while (time >= this.playerdemo_time) {
149                 PLAYERDEMO_FIELDS(this, playerdemo_read_)
150                 {
151                         time = this.playerdemo_time;
152                         PlayerPreThink(this);
153                         // not running physics though... this is just so we can run weapon stuff
154                         PlayerPostThink(this);
155                 }
156                 this.playerdemo_time = stof(fgets(this.playerdemo_fh));
157                 if (this.playerdemo_time == 0) {
158                         this.playerdemo_time = -1;
159                         return 1;
160                 }
161                 this.playerdemo_time += this.playerdemo_starttime;
162         }
163         this.velocity = '0 0 0';
164         CS(this).movement = '0 0 0';
165         this.dmg_take = 0; // so screen doesn't stay blurry
166         this.dmg_save = 0;
167         this.dmg_inflictor = NULL;
168         time = t;
169         return 1;
170 }