4 .float cvar_cl_dodging_timeout;
7 .float stat_dodging_timeout;
8 .float stat_dodging_delay;
9 .float stat_dodging_horiz_speed_frozen;
10 .float stat_dodging_frozen_nodoubletap;
11 .float stat_dodging_frozen;
12 .float stat_dodging_horiz_speed;
13 .float stat_dodging_height_threshold;
14 .float stat_dodging_distance_threshold;
15 .float stat_dodging_ramp_time;
16 .float stat_dodging_up_speed;
17 .float stat_dodging_wall;
21 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
22 .float dodging_action;
24 // the jump part of the dodge cannot be ramped
25 .float dodging_single_action;
28 // these are used to store the last key press time for each of the keys..
29 .float last_FORWARD_KEY_time;
30 .float last_BACKWARD_KEY_time;
31 .float last_LEFT_KEY_time;
32 .float last_RIGHT_KEY_time;
34 // these store the movement direction at the time of the dodge action happening.
35 .float dodging_direction_x;
36 .float dodging_direction_y;
38 // this indicates the last time a dodge was executed. used to check if another one is allowed
39 // and to ramp up the dodge acceleration in the physics hook.
40 .float last_dodging_time;
42 // This is the velocity gain to be added over the ramp time.
43 // It will decrease from frame to frame during dodging_action = 1
45 .float dodging_velocity_gain;
50 #define PHYS_INPUT_MOVEVALUES(s) input_movevalues
51 #define PHYS_INPUT_BUTTONS(s) input_buttons
52 #define PHYS_INPUT_ANGLES(s) input_angles
54 #define UNSET_ONGROUND(s) s.pmove_flags &= ~PMF_ONGROUND
55 #define PHYS_FROZEN(s) getstati(STAT_FROZEN)
56 #define IS_ONGROUND(s) (s.pmove_flags & PMF_ONGROUND)
58 #define PHYS_DEAD(s) s.csqcmodel_isdead
60 #define PHYS_DODGING_FRAMETIME frametime
61 #define PHYS_DODGING getstati(STAT_DODGING)
62 #define PHYS_DODGING_DELAY getstatf(STAT_DODGING_DELAY)
63 #define PHYS_DODGING_TIMEOUT(s) getstatf(STAT_DODGING_TIMEOUT)
64 #define PHYS_DODGING_HORIZ_SPEED_FROZEN getstatf(STAT_DODGING_HORIZ_SPEED_FROZEN)
65 #define PHYS_DODGING_FROZEN getstati(STAT_DODGING_FROZEN)
66 #define PHYS_DODGING_FROZEN_NODOUBLETAP getstati(STAT_DODGING_FROZEN_NO_DOUBLETAP)
67 #define PHYS_DODGING_HORIZ_SPEED getstatf(STAT_DODGING_HORIZ_SPEED)
68 #define PHYS_DODGING_PRESSED_KEYS(s) s.pressedkeys
69 #define PHYS_DODGING_HEIGHT_THRESHOLD getstatf(STAT_DODGING_HEIGHT_THRESHOLD)
70 #define PHYS_DODGING_DISTANCE_THRESHOLD getstatf(STAT_DODGING_DISTANCE_THRESHOLD)
71 #define PHYS_DODGING_RAMP_TIME getstatf(STAT_DODGING_RAMP_TIME)
72 #define PHYS_DODGING_UP_SPEED getstatf(STAT_DODGING_UP_SPEED)
73 #define PHYS_DODGING_WALL getstatf(STAT_DODGING_WALL)
77 #define PHYS_INPUT_ANGLES(s) s.angles
78 #define PHYS_INPUT_MOVEVALUES(s) s.movement
79 #define PHYS_INPUT_BUTTONS(s) (s.BUTTON_ATCK + 2 * s.BUTTON_JUMP + 4 * s.BUTTON_ATCK2 + 8 * s.BUTTON_ZOOM + 16 * s.BUTTON_CROUCH + 32 * s.BUTTON_HOOK + 64 * s.BUTTON_USE + 128 * (s.movement_x < 0) + 256 * (s.movement_x > 0) + 512 * (s.movement_y < 0) + 1024 * (s.movement_y > 0))
80 #define UNSET_ONGROUND(s) s.flags &= ~FL_ONGROUND
81 #define PHYS_FROZEN(s) s.frozen
82 #define IS_ONGROUND(s) (s.flags & FL_ONGROUND)
84 #define PHYS_DEAD(s) s.deadflag != DEAD_NO
86 #define PHYS_DODGING_FRAMETIME sys_frametime
87 #define PHYS_DODGING g_dodging
88 #define PHYS_DODGING_DELAY autocvar_sv_dodging_delay
89 #define PHYS_DODGING_TIMEOUT(s) s.cvar_cl_dodging_timeout
90 #define PHYS_DODGING_HORIZ_SPEED_FROZEN autocvar_sv_dodging_horiz_speed_frozen
91 #define PHYS_DODGING_FROZEN autocvar_sv_dodging_frozen
92 #define PHYS_DODGING_FROZEN_NODOUBLETAP autocvar_sv_dodging_frozen_doubletap
93 #define PHYS_DODGING_HORIZ_SPEED autocvar_sv_dodging_horiz_speed
94 #define PHYS_DODGING_PRESSED_KEYS(s) s.pressedkeys
95 #define PHYS_DODGING_HEIGHT_THRESHOLD autocvar_sv_dodging_height_threshold
96 #define PHYS_DODGING_DISTANCE_THRESHOLD autocvar_sv_dodging_wall_distance_threshold
97 #define PHYS_DODGING_RAMP_TIME autocvar_sv_dodging_ramp_time
98 #define PHYS_DODGING_UP_SPEED autocvar_sv_dodging_up_speed
99 #define PHYS_DODGING_WALL autocvar_sv_dodging_wall_dodging
102 void dodging_UpdateStats()
104 self.stat_dodging = PHYS_DODGING;
105 self.stat_dodging_delay = PHYS_DODGING_DELAY;
106 self.stat_dodging_timeout = PHYS_DODGING_TIMEOUT(self);
107 self.stat_dodging_horiz_speed_frozen = PHYS_DODGING_HORIZ_SPEED_FROZEN;
108 self.stat_dodging_frozen = PHYS_DODGING_FROZEN;
109 self.stat_dodging_frozen_nodoubletap = PHYS_DODGING_FROZEN_NODOUBLETAP;
110 self.stat_dodging_height_threshold = PHYS_DODGING_HEIGHT_THRESHOLD;
111 self.stat_dodging_distance_threshold = PHYS_DODGING_DISTANCE_THRESHOLD;
112 self.stat_dodging_ramp_time = PHYS_DODGING_RAMP_TIME;
113 self.stat_dodging_up_speed = PHYS_DODGING_UP_SPEED;
114 self.stat_dodging_wall = PHYS_DODGING_WALL;
117 void dodging_Initialize()
119 addstat(STAT_DODGING, AS_INT, stat_dodging);
120 addstat(STAT_DODGING_DELAY, AS_FLOAT, stat_dodging_delay);
121 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)
122 addstat(STAT_DODGING_FROZEN_NO_DOUBLETAP, AS_INT, stat_dodging_frozen_nodoubletap);
123 addstat(STAT_DODGING_HORIZ_SPEED_FROZEN, AS_FLOAT, stat_dodging_horiz_speed_frozen);
124 addstat(STAT_DODGING_FROZEN, AS_INT, stat_dodging_frozen);
125 addstat(STAT_DODGING_HORIZ_SPEED, AS_FLOAT, stat_dodging_horiz_speed);
126 addstat(STAT_DODGING_HEIGHT_THRESHOLD, AS_FLOAT, stat_dodging_height_threshold);
127 addstat(STAT_DODGING_DISTANCE_THRESHOLD, AS_FLOAT, stat_dodging_distance_threshold);
128 addstat(STAT_DODGING_RAMP_TIME, AS_FLOAT, stat_dodging_ramp_time);
129 addstat(STAT_DODGING_UP_SPEED, AS_FLOAT, stat_dodging_up_speed);
130 addstat(STAT_DODGING_WALL, AS_FLOAT, stat_dodging_wall);
135 // instantly updates pressed keys, for use with dodging (may be out of date, but we can't care)
136 void PM_dodging_updatepressedkeys()
138 if (PHYS_INPUT_MOVEVALUES(self)_x > 0) // get if movement keys are pressed
139 { // forward key pressed
140 self.pressedkeys |= KEY_FORWARD;
141 self.pressedkeys &= ~KEY_BACKWARD;
143 else if (PHYS_INPUT_MOVEVALUES(self)_x < 0)
144 { // backward key pressed
145 self.pressedkeys |= KEY_BACKWARD;
146 self.pressedkeys &= ~KEY_FORWARD;
150 self.pressedkeys &= ~KEY_FORWARD;
151 self.pressedkeys &= ~KEY_BACKWARD;
154 if (PHYS_INPUT_MOVEVALUES(self)_y > 0)
155 { // right key pressed
156 self.pressedkeys |= KEY_RIGHT;
157 self.pressedkeys &= ~KEY_LEFT;
159 else if (PHYS_INPUT_MOVEVALUES(self)_y < 0)
160 { // left key pressed
161 self.pressedkeys |= KEY_LEFT;
162 self.pressedkeys &= ~KEY_RIGHT;
166 self.pressedkeys &= ~KEY_RIGHT;
167 self.pressedkeys &= ~KEY_LEFT;
170 if (PHYS_INPUT_BUTTONS(self) & 2) // get if jump and crouch keys are pressed
171 self.pressedkeys |= KEY_JUMP;
173 self.pressedkeys &= ~KEY_JUMP;
174 if (PHYS_INPUT_BUTTONS(self) & 16)
175 self.pressedkeys |= KEY_CROUCH;
177 self.pressedkeys &= ~KEY_CROUCH;
179 if (PHYS_INPUT_BUTTONS(self) & 1)
180 self.pressedkeys |= KEY_ATCK;
182 self.pressedkeys &= ~KEY_ATCK;
183 if (PHYS_INPUT_BUTTONS(self) & 4)
184 self.pressedkeys |= KEY_ATCK2;
186 self.pressedkeys &= ~KEY_ATCK2;
190 // returns 1 if the player is close to a wall
191 float check_close_to_wall(float threshold)
193 if (PHYS_DODGING_WALL == 0) { return 0; }
198 trace_start = self.origin;
200 trace_end = self.origin + (1000*v_right);
201 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
202 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
205 trace_end = self.origin - (1000*v_right);
206 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
207 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
210 trace_end = self.origin + (1000*v_forward);
211 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
212 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
215 trace_end = self.origin - (1000*v_forward);
216 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
217 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
223 float check_close_to_ground(float threshold)
225 if (IS_ONGROUND(self))
231 void PM_dodging_checkpressedkeys()
233 if(!PHYS_DODGING) { return; }
236 float tap_direction_x;
237 float tap_direction_y;
242 float frozen_dodging, frozen_no_doubletap;
243 frozen_dodging = (PHYS_FROZEN(self) && PHYS_DODGING_FROZEN);
244 frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
246 float dodge_detected = 0;
248 // first check if the last dodge is far enough back in time so we can dodge again
249 if ((time - self.last_dodging_time) < PHYS_DODGING_DELAY)
252 makevectors(PHYS_INPUT_ANGLES(self));
254 if (check_close_to_ground(PHYS_DODGING_HEIGHT_THRESHOLD) != 1
255 && check_close_to_wall(PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
258 if (PHYS_INPUT_MOVEVALUES(self)_x > 0)
260 // is this a state change?
261 if (!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_FORWARD) || frozen_no_doubletap)
263 if ((time - self.last_FORWARD_KEY_time) < PHYS_DODGING_TIMEOUT(self))
265 tap_direction_x = 1.0;
268 self.last_FORWARD_KEY_time = time;
272 if (PHYS_INPUT_MOVEVALUES(self)_x < 0)
274 // is this a state change?
275 if (!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_BACKWARD) || frozen_no_doubletap)
277 tap_direction_x = -1.0;
278 if ((time - self.last_BACKWARD_KEY_time) < PHYS_DODGING_TIMEOUT(self))
282 self.last_BACKWARD_KEY_time = time;
286 if (PHYS_INPUT_MOVEVALUES(self)_y > 0)
288 // is this a state change?
289 if (!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_RIGHT) || frozen_no_doubletap)
291 tap_direction_y = 1.0;
292 if ((time - self.last_RIGHT_KEY_time) < PHYS_DODGING_TIMEOUT(self))
296 self.last_RIGHT_KEY_time = time;
300 if (PHYS_INPUT_MOVEVALUES(self)_y < 0)
302 // is this a state change?
303 if (!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_LEFT) || frozen_no_doubletap)
305 tap_direction_y = -1.0;
306 if ((time - self.last_LEFT_KEY_time) < PHYS_DODGING_TIMEOUT(self))
310 self.last_LEFT_KEY_time = time;
314 if (dodge_detected == 1)
316 self.last_dodging_time = time;
318 self.dodging_action = 1;
319 self.dodging_single_action = 1;
321 self.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
323 self.dodging_direction_x = tap_direction_x;
324 self.dodging_direction_y = tap_direction_y;
326 // normalize the dodging_direction vector.. (unlike UT99) XD
327 length = self.dodging_direction_x * self.dodging_direction_x;
328 length = length + self.dodging_direction_y * self.dodging_direction_y;
329 length = sqrt(length);
331 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
332 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
336 PM_dodging_updatepressedkeys();
342 if(!PHYS_DODGING) { return; }
345 float new_velocity_gain;
346 float velocity_difference;
347 float clean_up_and_do_nothing;
348 float horiz_speed = PHYS_DODGING_HORIZ_SPEED;
351 dodging_UpdateStats();
354 if(PHYS_FROZEN(self))
355 horiz_speed = PHYS_DODGING_HORIZ_SPEED_FROZEN;
360 new_velocity_gain = 0;
361 clean_up_and_do_nothing = 0;
363 // when swimming, no dodging allowed..
364 if (self.waterlevel >= WATERLEVEL_SWIMMING)
365 clean_up_and_do_nothing = 1;
367 if (clean_up_and_do_nothing != 0)
369 self.dodging_action = 0;
370 self.dodging_direction_x = 0;
371 self.dodging_direction_y = 0;
375 // make sure v_up, v_right and v_forward are sane
376 makevectors(PHYS_INPUT_ANGLES(self));
378 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
379 // will be called ramp_time/frametime times = 2 times. so, we need to
380 // add 0.5 * the total speed each frame until the dodge action is done..
381 common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
383 // if ramp time is smaller than frametime we get problems ;D
384 if (common_factor > 1)
387 new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
388 if (new_velocity_gain < 0)
389 new_velocity_gain = 0;
391 velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
393 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
394 if (self.dodging_action == 1)
396 //disable jump key during dodge accel phase
398 if(self.movement_z > 0) self.movement_z = 0;
400 if(input_movevalues_z > 0) input_movevalues_z = 0;
405 + ((self.dodging_direction_y * velocity_difference) * v_right)
406 + ((self.dodging_direction_x * velocity_difference) * v_forward);
408 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
411 // the up part of the dodge is a single shot action
412 if (self.dodging_single_action == 1)
414 UNSET_ONGROUND(self);
418 + (PHYS_DODGING_UP_SPEED * v_up);
421 if (autocvar_sv_dodging_sound == 1)
422 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
424 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
427 self.dodging_single_action = 0;
430 // are we done with the dodging ramp yet?
431 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
433 // reset state so next dodge can be done correctly
434 self.dodging_action = 0;
435 self.dodging_direction_x = 0;
436 self.dodging_direction_y = 0;
440 PM_dodging_checkpressedkeys();
446 MUTATOR_HOOKFUNCTION(dodging_GetCvars)
448 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
452 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics)
454 // print("dodging_PlayerPhysics\n");
460 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys)
462 PM_dodging_checkpressedkeys();
467 MUTATOR_DEFINITION(mutator_dodging)
469 // we need to be called before GetPressedKey does its thing so we can
470 // detect state changes and therefore dodging actions..
471 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
473 // in the physics hook we actually implement the dodge..
474 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
476 // get timeout information from the client, so the client can configure it..
477 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
479 // this just turns on the cvar.
482 g_dodging = cvar("g_dodging");
483 dodging_Initialize();
486 // this just turns off the cvar.
487 MUTATOR_ONROLLBACK_OR_REMOVE
500 // we use this elsewhere, but let's undefine it anyway
501 #undef PHYS_DODGING_FROZEN
502 #undef PHYS_INPUT_MOVEVALUES
503 #undef PHYS_INPUT_ANGLES
504 #undef PHYS_INPUT_BUTTONS
505 #undef UNSET_ONGROUND