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