3 #define PHYS_DODGING STAT(DODGING, this)
4 #define PHYS_DODGING_DELAY STAT(DODGING_DELAY, this)
5 #define PHYS_DODGING_DISTANCE_THRESHOLD STAT(DODGING_DISTANCE_THRESHOLD, this)
6 #define PHYS_DODGING_FROZEN_NODOUBLETAP STAT(DODGING_FROZEN_NO_DOUBLETAP, this)
7 #define PHYS_DODGING_HEIGHT_THRESHOLD STAT(DODGING_HEIGHT_THRESHOLD, this)
8 #define PHYS_DODGING_HORIZ_SPEED STAT(DODGING_HORIZ_SPEED, this)
9 #define PHYS_DODGING_HORIZ_SPEED_FROZEN STAT(DODGING_HORIZ_SPEED_FROZEN, this)
10 #define PHYS_DODGING_RAMP_TIME STAT(DODGING_RAMP_TIME, this)
11 #define PHYS_DODGING_UP_SPEED STAT(DODGING_UP_SPEED, this)
12 #define PHYS_DODGING_WALL STAT(DODGING_WALL, this)
13 #define PHYS_DODGING_PRESSED_KEYS(s) (s).pressedkeys
16 #define PHYS_DODGING_FRAMETIME (1 / (frametime <= 0 ? 60 : frametime))
17 #define PHYS_DODGING_TIMEOUT(s) STAT(DODGING_TIMEOUT)
19 #define PHYS_DODGING_FRAMETIME sys_frametime
20 #define PHYS_DODGING_TIMEOUT(s) s.cvar_cl_dodging_timeout
27 bool autocvar_sv_dodging_sound;
29 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
30 .float dodging_action;
32 // the jump part of the dodge cannot be ramped
33 .float dodging_single_action;
35 #include "../../../animdecide.qh"
36 #include "../../../physics.qh"
38 .float cvar_cl_dodging_timeout = _STAT(DODGING_TIMEOUT);
40 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
42 // this just turns on the cvar.
45 g_dodging = cvar("g_dodging");
48 // this just turns off the cvar.
49 MUTATOR_ONROLLBACK_OR_REMOVE
59 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
60 .float dodging_action;
62 // the jump part of the dodge cannot be ramped
63 .float dodging_single_action;
66 // these are used to store the last key press time for each of the keys..
67 .float last_FORWARD_KEY_time;
68 .float last_BACKWARD_KEY_time;
69 .float last_LEFT_KEY_time;
70 .float last_RIGHT_KEY_time;
72 // these store the movement direction at the time of the dodge action happening.
73 .vector dodging_direction;
75 // this indicates the last time a dodge was executed. used to check if another one is allowed
76 // and to ramp up the dodge acceleration in the physics hook.
77 .float last_dodging_time;
79 // This is the velocity gain to be added over the ramp time.
80 // It will decrease from frame to frame during dodging_action = 1
82 .float dodging_velocity_gain;
88 // returns 1 if the player is close to a wall
89 bool check_close_to_wall(float threshold)
91 if (PHYS_DODGING_WALL == 0) { return false; }
94 tracebox(self.origin, self.mins, self.maxs, self.origin + OFFSET, true, self); \
95 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold) \
106 bool check_close_to_ground(float threshold)
108 return IS_ONGROUND(self) ? true : false;
111 float PM_dodging_checkpressedkeys()
116 float frozen_dodging = (PHYS_FROZEN(self) && PHYS_DODGING_FROZEN);
117 float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
119 // first check if the last dodge is far enough back in time so we can dodge again
120 if ((time - self.last_dodging_time) < PHYS_DODGING_DELAY)
123 makevectors(self.angles);
125 if (check_close_to_ground(PHYS_DODGING_HEIGHT_THRESHOLD) != 1
126 && check_close_to_wall(PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
129 float tap_direction_x = 0;
130 float tap_direction_y = 0;
131 float dodge_detected = 0;
133 #define X(COND,BTN,RESULT) \
134 if (self.movement_##COND) \
135 /* is this a state change? */ \
136 if(!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_##BTN) || frozen_no_doubletap) { \
137 tap_direction_##RESULT; \
138 if ((time - self.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(self)) \
139 dodge_detected = 1; \
140 self.last_##BTN##_KEY_time = time; \
142 X(x < 0, BACKWARD, x--);
143 X(x > 0, FORWARD, x++);
145 X(y > 0, RIGHT, y++);
148 if (dodge_detected == 1)
150 self.last_dodging_time = time;
152 self.dodging_action = 1;
153 self.dodging_single_action = 1;
155 self.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
157 self.dodging_direction_x = tap_direction_x;
158 self.dodging_direction_y = tap_direction_y;
160 // normalize the dodging_direction vector.. (unlike UT99) XD
161 float length = self.dodging_direction_x * self.dodging_direction_x
162 + self.dodging_direction_y * self.dodging_direction_y;
163 length = sqrt(length);
165 self.dodging_direction_x = self.dodging_direction_x * 1.0 / length;
166 self.dodging_direction_y = self.dodging_direction_y * 1.0 / length;
180 // when swimming, no dodging allowed..
181 if (self.waterlevel >= WATERLEVEL_SWIMMING)
183 self.dodging_action = 0;
184 self.dodging_direction_x = 0;
185 self.dodging_direction_y = 0;
189 // make sure v_up, v_right and v_forward are sane
190 makevectors(self.angles);
192 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
193 // will be called ramp_time/frametime times = 2 times. so, we need to
194 // add 0.5 * the total speed each frame until the dodge action is done..
195 float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
197 // if ramp time is smaller than frametime we get problems ;D
198 common_factor = min(common_factor, 1);
200 float horiz_speed = PHYS_FROZEN(self) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
201 float new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
202 new_velocity_gain = max(0, new_velocity_gain);
204 float velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
206 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
207 if (self.dodging_action == 1)
209 //disable jump key during dodge accel phase
210 if(self.movement_z > 0) { self.movement_z = 0; }
212 self.velocity += ((self.dodging_direction_y * velocity_difference) * v_right)
213 + ((self.dodging_direction_x * velocity_difference) * v_forward);
215 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
218 // the up part of the dodge is a single shot action
219 if (self.dodging_single_action == 1)
221 UNSET_ONGROUND(self);
223 self.velocity += PHYS_DODGING_UP_SPEED * v_up;
226 if (autocvar_sv_dodging_sound)
227 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
229 animdecide_setaction(self, ANIMACTION_JUMP, true);
232 self.dodging_single_action = 0;
235 // are we done with the dodging ramp yet?
236 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
238 // reset state so next dodge can be done correctly
239 self.dodging_action = 0;
240 self.dodging_direction_x = 0;
241 self.dodging_direction_y = 0;
247 MUTATOR_HOOKFUNCTION(dodging, GetCvars)
249 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
253 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
255 // print("dodging_PlayerPhysics\n");
261 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
263 PM_dodging_checkpressedkeys();