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