Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_dodging.qc
1 #ifdef CSQC
2         #define PHYS_DODGING_FRAMETIME                          (1 / (frametime <= 0 ? 60 : frametime))
3         #define PHYS_DODGING                                            getstati(STAT_DODGING)
4         #define PHYS_DODGING_DELAY                                      getstatf(STAT_DODGING_DELAY)
5         #define PHYS_DODGING_TIMEOUT(s)                         getstatf(STAT_DODGING_TIMEOUT)
6         #define PHYS_DODGING_HORIZ_SPEED_FROZEN         getstatf(STAT_DODGING_HORIZ_SPEED_FROZEN)
7         #define PHYS_DODGING_FROZEN_NODOUBLETAP         getstati(STAT_DODGING_FROZEN_NO_DOUBLETAP)
8         #define PHYS_DODGING_HORIZ_SPEED                        getstatf(STAT_DODGING_HORIZ_SPEED)
9         #define PHYS_DODGING_PRESSED_KEYS(s)            s.pressedkeys
10         #define PHYS_DODGING_HEIGHT_THRESHOLD           getstatf(STAT_DODGING_HEIGHT_THRESHOLD)
11         #define PHYS_DODGING_DISTANCE_THRESHOLD         getstatf(STAT_DODGING_DISTANCE_THRESHOLD)
12         #define PHYS_DODGING_RAMP_TIME                          getstatf(STAT_DODGING_RAMP_TIME)
13         #define PHYS_DODGING_UP_SPEED                           getstatf(STAT_DODGING_UP_SPEED)
14         #define PHYS_DODGING_WALL                                       getstatf(STAT_DODGING_WALL)
15 #elif defined(SVQC)
16         #define PHYS_DODGING_FRAMETIME                          sys_frametime
17         #define PHYS_DODGING                                            g_dodging
18         #define PHYS_DODGING_DELAY                                      autocvar_sv_dodging_delay
19         #define PHYS_DODGING_TIMEOUT(s)                         s.cvar_cl_dodging_timeout
20         #define PHYS_DODGING_HORIZ_SPEED_FROZEN         autocvar_sv_dodging_horiz_speed_frozen
21         #define PHYS_DODGING_FROZEN_NODOUBLETAP         autocvar_sv_dodging_frozen_doubletap
22         #define PHYS_DODGING_HORIZ_SPEED                        autocvar_sv_dodging_horiz_speed
23         #define PHYS_DODGING_PRESSED_KEYS(s)            s.pressedkeys
24         #define PHYS_DODGING_HEIGHT_THRESHOLD           autocvar_sv_dodging_height_threshold
25         #define PHYS_DODGING_DISTANCE_THRESHOLD         autocvar_sv_dodging_wall_distance_threshold
26         #define PHYS_DODGING_RAMP_TIME                          autocvar_sv_dodging_ramp_time
27         #define PHYS_DODGING_UP_SPEED                           autocvar_sv_dodging_up_speed
28         #define PHYS_DODGING_WALL                                       autocvar_sv_dodging_wall_dodging
29 #endif
30
31 #ifdef SVQC
32 #include "mutator_dodging.qh"
33 #include "../_all.qh"
34
35 #include "mutator.qh"
36
37 #include "../../common/animdecide.qh"
38
39 .float cvar_cl_dodging_timeout;
40
41 .float stat_dodging;
42 .float stat_dodging_delay;
43 .float stat_dodging_horiz_speed_frozen;
44 .float stat_dodging_frozen_nodoubletap;
45 .float stat_dodging_frozen;
46 .float stat_dodging_horiz_speed;
47 .float stat_dodging_height_threshold;
48 .float stat_dodging_distance_threshold;
49 .float stat_dodging_ramp_time;
50 .float stat_dodging_up_speed;
51 .float stat_dodging_wall;
52
53 #endif
54
55 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
56 .float dodging_action;
57
58 // the jump part of the dodge cannot be ramped
59 .float dodging_single_action;
60
61
62 // these are used to store the last key press time for each of the keys..
63 .float last_FORWARD_KEY_time;
64 .float last_BACKWARD_KEY_time;
65 .float last_LEFT_KEY_time;
66 .float last_RIGHT_KEY_time;
67
68 // these store the movement direction at the time of the dodge action happening.
69 .vector dodging_direction;
70
71 // this indicates the last time a dodge was executed. used to check if another one is allowed
72 // and to ramp up the dodge acceleration in the physics hook.
73 .float last_dodging_time;
74
75 // This is the velocity gain to be added over the ramp time.
76 // It will decrease from frame to frame during dodging_action = 1
77 // until it's 0.
78 .float dodging_velocity_gain;
79
80 #ifdef CSQC
81 .int pressedkeys;
82
83 #elif defined(SVQC)
84
85 void dodging_UpdateStats()
86 {
87         self.stat_dodging = PHYS_DODGING;
88         self.stat_dodging_delay = PHYS_DODGING_DELAY;
89         self.stat_dodging_horiz_speed_frozen = PHYS_DODGING_HORIZ_SPEED_FROZEN;
90         self.stat_dodging_frozen = PHYS_DODGING_FROZEN;
91         self.stat_dodging_frozen_nodoubletap = PHYS_DODGING_FROZEN_NODOUBLETAP;
92         self.stat_dodging_height_threshold = PHYS_DODGING_HEIGHT_THRESHOLD;
93         self.stat_dodging_distance_threshold = PHYS_DODGING_DISTANCE_THRESHOLD;
94         self.stat_dodging_ramp_time = PHYS_DODGING_RAMP_TIME;
95         self.stat_dodging_up_speed = PHYS_DODGING_UP_SPEED;
96         self.stat_dodging_wall = PHYS_DODGING_WALL;
97 }
98
99 void dodging_Initialize()
100 {
101         addstat(STAT_DODGING, AS_INT, stat_dodging);
102         addstat(STAT_DODGING_DELAY, AS_FLOAT, stat_dodging_delay);
103         addstat(STAT_DODGING_TIMEOUT, AS_FLOAT, cvar_cl_dodging_timeout); // we stat this, so it is updated on the client when updated on server (otherwise, chaos)
104         addstat(STAT_DODGING_FROZEN_NO_DOUBLETAP, AS_INT, stat_dodging_frozen_nodoubletap);
105         addstat(STAT_DODGING_HORIZ_SPEED_FROZEN, AS_FLOAT, stat_dodging_horiz_speed_frozen);
106         addstat(STAT_DODGING_FROZEN, AS_INT, stat_dodging_frozen);
107         addstat(STAT_DODGING_HORIZ_SPEED, AS_FLOAT, stat_dodging_horiz_speed);
108         addstat(STAT_DODGING_HEIGHT_THRESHOLD, AS_FLOAT, stat_dodging_height_threshold);
109         addstat(STAT_DODGING_DISTANCE_THRESHOLD, AS_FLOAT, stat_dodging_distance_threshold);
110         addstat(STAT_DODGING_RAMP_TIME, AS_FLOAT, stat_dodging_ramp_time);
111         addstat(STAT_DODGING_UP_SPEED, AS_FLOAT, stat_dodging_up_speed);
112         addstat(STAT_DODGING_WALL, AS_FLOAT, stat_dodging_wall);
113 }
114
115 #endif
116
117 // returns 1 if the player is close to a wall
118 bool check_close_to_wall(float threshold)
119 {
120         if (PHYS_DODGING_WALL == 0) { return false; }
121
122         #define X(OFFSET)                                                                                                                               \
123         tracebox(self.origin, self.mins, self.maxs, self.origin + OFFSET, true, self);  \
124         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)                \
125                 return true;
126         X(1000*v_right);
127         X(-1000*v_right);
128         X(1000*v_forward);
129         X(-1000*v_forward);
130         #undef X
131
132         return false;
133 }
134
135 bool check_close_to_ground(float threshold)
136 {
137         return IS_ONGROUND(self) ? true : false;
138 }
139
140 float PM_dodging_checkpressedkeys()
141 {
142         if(!PHYS_DODGING)
143                 return false;
144
145         float frozen_dodging = (PHYS_FROZEN(self) && PHYS_DODGING_FROZEN);
146         float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
147
148         // first check if the last dodge is far enough back in time so we can dodge again
149         if ((time - self.last_dodging_time) < PHYS_DODGING_DELAY)
150                 return false;
151
152         makevectors(self.angles);
153
154         if (check_close_to_ground(PHYS_DODGING_HEIGHT_THRESHOLD) != 1
155                 && check_close_to_wall(PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
156                 return true;
157
158         float tap_direction_x = 0;
159         float tap_direction_y = 0;
160         float dodge_detected = 0;
161
162         #define X(COND,BTN,RESULT)                                                                                                                      \
163         if (self.movement_##COND)                                                                                               \
164                 /* is this a state change? */                                                                                                   \
165                 if(!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_##BTN) || frozen_no_doubletap) {             \
166                                 tap_direction_##RESULT;                                                                                                 \
167                                 if ((time - self.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(self))   \
168                                         dodge_detected = 1;                                                                                                     \
169                                 self.last_##BTN##_KEY_time = time;                                                                              \
170                 }
171         X(x < 0, BACKWARD,      x--);
172         X(x > 0, FORWARD,       x++);
173         X(y < 0, LEFT,          y--);
174         X(y > 0, RIGHT,         y++);
175         #undef X
176
177         if (dodge_detected == 1)
178         {
179                 self.last_dodging_time = time;
180
181                 self.dodging_action = 1;
182                 self.dodging_single_action = 1;
183
184                 self.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
185
186                 self.dodging_direction_x = tap_direction_x;
187                 self.dodging_direction_y = tap_direction_y;
188
189                 // normalize the dodging_direction vector.. (unlike UT99) XD
190                 float length = self.dodging_direction_x * self.dodging_direction_x
191                                         + self.dodging_direction_y * self.dodging_direction_y;
192                 length = sqrt(length);
193
194                 self.dodging_direction_x = self.dodging_direction_x * 1.0 / length;
195                 self.dodging_direction_y = self.dodging_direction_y * 1.0 / length;
196                 return true;
197         }
198         return false;
199 }
200
201 void PM_dodging()
202 {
203         if (!PHYS_DODGING)
204                 return;
205
206 #ifdef SVQC
207         dodging_UpdateStats();
208 #endif
209
210     if (PHYS_DEAD(self))
211         return;
212
213         // when swimming, no dodging allowed..
214         if (self.waterlevel >= WATERLEVEL_SWIMMING)
215         {
216                 self.dodging_action = 0;
217                 self.dodging_direction_x = 0;
218                 self.dodging_direction_y = 0;
219                 return;
220         }
221
222         // make sure v_up, v_right and v_forward are sane
223         makevectors(self.angles);
224
225         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
226         // will be called ramp_time/frametime times = 2 times. so, we need to
227         // add 0.5 * the total speed each frame until the dodge action is done..
228         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
229
230         // if ramp time is smaller than frametime we get problems ;D
231         common_factor = min(common_factor, 1);
232
233         float horiz_speed = PHYS_FROZEN(self) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
234         float new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
235         new_velocity_gain = max(0, new_velocity_gain);
236
237         float velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
238
239         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
240         if (self.dodging_action == 1)
241         {
242                 //disable jump key during dodge accel phase
243                 if(self.movement_z > 0) { self.movement_z = 0; }
244
245                 self.velocity += ((self.dodging_direction_y * velocity_difference) * v_right)
246                                         + ((self.dodging_direction_x * velocity_difference) * v_forward);
247
248                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
249         }
250
251         // the up part of the dodge is a single shot action
252         if (self.dodging_single_action == 1)
253         {
254                 UNSET_ONGROUND(self);
255
256                 self.velocity += PHYS_DODGING_UP_SPEED * v_up;
257
258 #ifdef SVQC
259                 if (autocvar_sv_dodging_sound)
260                         PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
261
262                 animdecide_setaction(self, ANIMACTION_JUMP, true);
263 #endif
264
265                 self.dodging_single_action = 0;
266         }
267
268         // are we done with the dodging ramp yet?
269         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
270         {
271                 // reset state so next dodge can be done correctly
272                 self.dodging_action = 0;
273                 self.dodging_direction_x = 0;
274                 self.dodging_direction_y = 0;
275         }
276 }
277
278 #ifdef SVQC
279
280 MUTATOR_HOOKFUNCTION(dodging_GetCvars)
281 {
282         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
283         return false;
284 }
285
286 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics)
287 {
288         // print("dodging_PlayerPhysics\n");
289         PM_dodging();
290
291         return false;
292 }
293
294 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys)
295 {
296         PM_dodging_checkpressedkeys();
297
298         return false;
299 }
300
301 MUTATOR_DEFINITION(mutator_dodging)
302 {
303         // we need to be called before GetPressedKey does its thing so we can
304         // detect state changes and therefore dodging actions..
305         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
306
307         // in the physics hook we actually implement the dodge..
308         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
309
310         // get timeout information from the client, so the client can configure it..
311         MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
312
313         // this just turns on the cvar.
314         MUTATOR_ONADD
315         {
316                 g_dodging = cvar("g_dodging");
317                 dodging_Initialize();
318         }
319
320         // this just turns off the cvar.
321         MUTATOR_ONROLLBACK_OR_REMOVE
322         {
323                 g_dodging = 0;
324         }
325
326         return false;
327 }
328 #endif