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)
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
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;
48 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
49 .float dodging_action;
51 // the jump part of the dodge cannot be ramped
52 .float dodging_single_action;
54 #include "../../../animdecide.qh"
55 #include "../../../physics.qh"
57 .float cvar_cl_dodging_timeout;
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;
71 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
73 // this just turns on the cvar.
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);
91 // this just turns off the cvar.
92 MUTATOR_ONROLLBACK_OR_REMOVE
101 REGISTER_MUTATOR(dodging, true);
104 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
105 .float dodging_action;
107 // the jump part of the dodge cannot be ramped
108 .float dodging_single_action;
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;
117 // these store the movement direction at the time of the dodge action happening.
118 .vector dodging_direction;
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;
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
127 .float dodging_velocity_gain;
133 void dodging_UpdateStats(entity this)
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;
149 // returns 1 if the player is close to a wall
150 bool check_close_to_wall(entity this, float threshold)
152 if (PHYS_DODGING_WALL == 0) { return false; }
155 tracebox(this.origin, this.mins, this.maxs, this.origin + OFFSET, true, this); \
156 if (trace_fraction < 1 && vlen (this.origin - trace_endpos) < threshold) \
167 bool check_close_to_ground(entity this, float threshold)
169 return IS_ONGROUND(this) ? true : false;
172 float PM_dodging_checkpressedkeys(entity this)
177 float frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN);
178 float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
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)
184 makevectors(this.angles);
186 if (check_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD) != 1
187 && check_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
190 float tap_direction_x = 0;
191 float tap_direction_y = 0;
192 bool dodge_detected = false;
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; \
203 X(x < 0, BACKWARD, x--);
204 X(x > 0, FORWARD, x++);
206 X(y > 0, RIGHT, y++);
211 this.last_dodging_time = time;
213 this.dodging_action = 1;
214 this.dodging_single_action = 1;
216 this.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
218 this.dodging_direction_x = tap_direction_x;
219 this.dodging_direction_y = tap_direction_y;
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);
226 this.dodging_direction_x = this.dodging_direction_x * 1.0 / length;
227 this.dodging_direction_y = this.dodging_direction_y * 1.0 / length;
233 void PM_dodging(entity this)
238 dodging_UpdateStats(this);
243 // when swimming, no dodging allowed..
244 if (this.waterlevel >= WATERLEVEL_SWIMMING)
246 this.dodging_action = 0;
247 this.dodging_direction_x = 0;
248 this.dodging_direction_y = 0;
252 // make sure v_up, v_right and v_forward are sane
253 makevectors(this.angles);
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;
260 // if ramp time is smaller than frametime we get problems ;D
261 common_factor = min(common_factor, 1);
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);
267 float velocity_difference = this.dodging_velocity_gain - new_velocity_gain;
269 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
270 if (this.dodging_action == 1)
272 //disable jump key during dodge accel phase
273 if(this.movement_z > 0) { this.movement_z = 0; }
275 this.velocity += ((this.dodging_direction_y * velocity_difference) * v_right)
276 + ((this.dodging_direction_x * velocity_difference) * v_forward);
278 this.dodging_velocity_gain = this.dodging_velocity_gain - velocity_difference;
281 // the up part of the dodge is a single shot action
282 if (this.dodging_single_action == 1)
284 UNSET_ONGROUND(this);
286 this.velocity += PHYS_DODGING_UP_SPEED * v_up;
289 if (autocvar_sv_dodging_sound)
290 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
292 animdecide_setaction(this, ANIMACTION_JUMP, true);
295 this.dodging_single_action = 0;
298 // are we done with the dodging ramp yet?
299 if((this.dodging_action == 1) && ((time - this.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
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;
308 void PM_dodging_GetPressedKeys(entity this)
311 if(!PHYS_DODGING) { return; }
313 PM_dodging_checkpressedkeys(this);
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);
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;
329 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
331 // print("dodging_PlayerPhysics\n");
332 PM_dodging_GetPressedKeys(self);
339 MUTATOR_HOOKFUNCTION(dodging, GetCvars)
341 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
345 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
347 PM_dodging_checkpressedkeys(self);