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