]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/dodging.qc
Merge branch 'master' into Mario/bulldozer
[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 #endif
101
102 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
103 .float dodging_action;
104
105 // the jump part of the dodge cannot be ramped
106 .float dodging_single_action;
107
108
109 // these are used to store the last key press time for each of the keys..
110 .float last_FORWARD_KEY_time;
111 .float last_BACKWARD_KEY_time;
112 .float last_LEFT_KEY_time;
113 .float last_RIGHT_KEY_time;
114
115 // these store the movement direction at the time of the dodge action happening.
116 .vector dodging_direction;
117
118 // this indicates the last time a dodge was executed. used to check if another one is allowed
119 // and to ramp up the dodge acceleration in the physics hook.
120 .float last_dodging_time;
121
122 // This is the velocity gain to be added over the ramp time.
123 // It will decrease from frame to frame during dodging_action = 1
124 // until it's 0.
125 .float dodging_velocity_gain;
126
127 #ifdef CSQC
128 .int pressedkeys;
129
130 #elif defined(SVQC)
131
132 void dodging_UpdateStats()
133 {SELFPARAM();
134         self.stat_dodging = PHYS_DODGING;
135         self.stat_dodging_delay = PHYS_DODGING_DELAY;
136         self.stat_dodging_horiz_speed_frozen = PHYS_DODGING_HORIZ_SPEED_FROZEN;
137         self.stat_dodging_frozen = PHYS_DODGING_FROZEN;
138         self.stat_dodging_frozen_nodoubletap = PHYS_DODGING_FROZEN_NODOUBLETAP;
139         self.stat_dodging_height_threshold = PHYS_DODGING_HEIGHT_THRESHOLD;
140         self.stat_dodging_distance_threshold = PHYS_DODGING_DISTANCE_THRESHOLD;
141         self.stat_dodging_ramp_time = PHYS_DODGING_RAMP_TIME;
142         self.stat_dodging_up_speed = PHYS_DODGING_UP_SPEED;
143         self.stat_dodging_wall = PHYS_DODGING_WALL;
144 }
145
146 #endif
147
148 // returns 1 if the player is close to a wall
149 bool check_close_to_wall(float threshold)
150 {SELFPARAM();
151         if (PHYS_DODGING_WALL == 0) { return false; }
152
153         #define X(OFFSET)                                                                                                                               \
154         tracebox(self.origin, self.mins, self.maxs, self.origin + OFFSET, true, self);  \
155         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)                \
156                 return true;
157         X(1000*v_right);
158         X(-1000*v_right);
159         X(1000*v_forward);
160         X(-1000*v_forward);
161         #undef X
162
163         return false;
164 }
165
166 bool check_close_to_ground(float threshold)
167 {SELFPARAM();
168         return IS_ONGROUND(self) ? true : false;
169 }
170
171 float PM_dodging_checkpressedkeys()
172 {SELFPARAM();
173         if(!PHYS_DODGING)
174                 return false;
175
176         float frozen_dodging = (PHYS_FROZEN(self) && PHYS_DODGING_FROZEN);
177         float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
178
179         // first check if the last dodge is far enough back in time so we can dodge again
180         if ((time - self.last_dodging_time) < PHYS_DODGING_DELAY)
181                 return false;
182
183         makevectors(self.angles);
184
185         if (check_close_to_ground(PHYS_DODGING_HEIGHT_THRESHOLD) != 1
186                 && check_close_to_wall(PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
187                 return true;
188
189         float tap_direction_x = 0;
190         float tap_direction_y = 0;
191         float dodge_detected = 0;
192
193         #define X(COND,BTN,RESULT)                                                                                                                      \
194         if (self.movement_##COND)                                                                                               \
195                 /* is this a state change? */                                                                                                   \
196                 if(!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_##BTN) || frozen_no_doubletap) {             \
197                                 tap_direction_##RESULT;                                                                                                 \
198                                 if ((time - self.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(self))   \
199                                         dodge_detected = 1;                                                                                                     \
200                                 self.last_##BTN##_KEY_time = time;                                                                              \
201                 }
202         X(x < 0, BACKWARD,      x--);
203         X(x > 0, FORWARD,       x++);
204         X(y < 0, LEFT,          y--);
205         X(y > 0, RIGHT,         y++);
206         #undef X
207
208         if (dodge_detected == 1)
209         {
210                 self.last_dodging_time = time;
211
212                 self.dodging_action = 1;
213                 self.dodging_single_action = 1;
214
215                 self.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
216
217                 self.dodging_direction_x = tap_direction_x;
218                 self.dodging_direction_y = tap_direction_y;
219
220                 // normalize the dodging_direction vector.. (unlike UT99) XD
221                 float length = self.dodging_direction_x * self.dodging_direction_x
222                                         + self.dodging_direction_y * self.dodging_direction_y;
223                 length = sqrt(length);
224
225                 self.dodging_direction_x = self.dodging_direction_x * 1.0 / length;
226                 self.dodging_direction_y = self.dodging_direction_y * 1.0 / length;
227                 return true;
228         }
229         return false;
230 }
231
232 void PM_dodging()
233 {SELFPARAM();
234         if (!PHYS_DODGING)
235                 return;
236
237 #ifdef SVQC
238         dodging_UpdateStats();
239 #endif
240
241     if (PHYS_DEAD(self))
242         return;
243
244         // when swimming, no dodging allowed..
245         if (self.waterlevel >= WATERLEVEL_SWIMMING)
246         {
247                 self.dodging_action = 0;
248                 self.dodging_direction_x = 0;
249                 self.dodging_direction_y = 0;
250                 return;
251         }
252
253         // make sure v_up, v_right and v_forward are sane
254         makevectors(self.angles);
255
256         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
257         // will be called ramp_time/frametime times = 2 times. so, we need to
258         // add 0.5 * the total speed each frame until the dodge action is done..
259         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
260
261         // if ramp time is smaller than frametime we get problems ;D
262         common_factor = min(common_factor, 1);
263
264         float horiz_speed = PHYS_FROZEN(self) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
265         float new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
266         new_velocity_gain = max(0, new_velocity_gain);
267
268         float velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
269
270         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
271         if (self.dodging_action == 1)
272         {
273                 //disable jump key during dodge accel phase
274                 if(self.movement_z > 0) { self.movement_z = 0; }
275
276                 self.velocity += ((self.dodging_direction_y * velocity_difference) * v_right)
277                                         + ((self.dodging_direction_x * velocity_difference) * v_forward);
278
279                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
280         }
281
282         // the up part of the dodge is a single shot action
283         if (self.dodging_single_action == 1)
284         {
285                 UNSET_ONGROUND(self);
286
287                 self.velocity += PHYS_DODGING_UP_SPEED * v_up;
288
289 #ifdef SVQC
290                 if (autocvar_sv_dodging_sound)
291                         PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
292
293                 animdecide_setaction(self, ANIMACTION_JUMP, true);
294 #endif
295
296                 self.dodging_single_action = 0;
297         }
298
299         // are we done with the dodging ramp yet?
300         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
301         {
302                 // reset state so next dodge can be done correctly
303                 self.dodging_action = 0;
304                 self.dodging_direction_x = 0;
305                 self.dodging_direction_y = 0;
306         }
307 }
308
309 #ifdef SVQC
310
311 MUTATOR_HOOKFUNCTION(dodging, GetCvars)
312 {
313         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
314         return false;
315 }
316
317 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
318 {
319         // print("dodging_PlayerPhysics\n");
320         PM_dodging();
321
322         return false;
323 }
324
325 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
326 {
327         PM_dodging_checkpressedkeys();
328
329         return false;
330 }
331
332 #endif
333
334 #endif