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