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 <common/animdecide.qh>
36 #include <common/physics/player.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
58 REGISTER_MUTATOR(dodging, true);
61 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
62 .float dodging_action;
64 // the jump part of the dodge cannot be ramped
65 .float dodging_single_action;
68 // these are used to store the last key press time for each of the keys..
69 .float last_FORWARD_KEY_time;
70 .float last_BACKWARD_KEY_time;
71 .float last_LEFT_KEY_time;
72 .float last_RIGHT_KEY_time;
74 // these store the movement direction at the time of the dodge action happening.
75 .vector dodging_direction;
77 // this indicates the last time a dodge was executed. used to check if another one is allowed
78 // and to ramp up the dodge acceleration in the physics hook.
79 .float last_dodging_time;
81 // This is the velocity gain to be added over the ramp time.
82 // It will decrease from frame to frame during dodging_action = 1
84 .float dodging_velocity_gain;
90 // returns 1 if the player is close to a wall
91 bool check_close_to_wall(entity this, float threshold)
93 if (PHYS_DODGING_WALL == 0) { return false; }
96 tracebox(this.origin, this.mins, this.maxs, this.origin + OFFSET, true, this); \
97 if (trace_fraction < 1 && vlen (this.origin - trace_endpos) < threshold) \
108 bool check_close_to_ground(entity this, float threshold)
110 return IS_ONGROUND(this) ? true : false;
113 float PM_dodging_checkpressedkeys(entity this)
118 float frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN(this));
119 float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
121 // first check if the last dodge is far enough back in time so we can dodge again
122 if ((time - this.last_dodging_time) < PHYS_DODGING_DELAY)
125 makevectors(this.angles);
127 if (check_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD) != 1
128 && check_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
131 float tap_direction_x = 0;
132 float tap_direction_y = 0;
133 bool dodge_detected = false;
135 #define X(COND,BTN,RESULT) \
136 if (this.movement_##COND) \
137 /* is this a state change? */ \
138 if(!(PHYS_DODGING_PRESSED_KEYS(this) & KEY_##BTN) || frozen_no_doubletap) { \
139 tap_direction_##RESULT; \
140 if ((time - this.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(this)) \
141 dodge_detected = true; \
142 this.last_##BTN##_KEY_time = time; \
144 X(x < 0, BACKWARD, x--);
145 X(x > 0, FORWARD, x++);
147 X(y > 0, RIGHT, y++);
152 this.last_dodging_time = time;
154 this.dodging_action = 1;
155 this.dodging_single_action = 1;
157 this.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
159 this.dodging_direction_x = tap_direction_x;
160 this.dodging_direction_y = tap_direction_y;
162 // normalize the dodging_direction vector.. (unlike UT99) XD
163 float length = this.dodging_direction_x * this.dodging_direction_x
164 + this.dodging_direction_y * this.dodging_direction_y;
165 length = sqrt(length);
167 this.dodging_direction_x = this.dodging_direction_x * 1.0 / length;
168 this.dodging_direction_y = this.dodging_direction_y * 1.0 / length;
174 void PM_dodging(entity this)
182 // when swimming, no dodging allowed..
183 if (this.waterlevel >= WATERLEVEL_SWIMMING)
185 this.dodging_action = 0;
186 this.dodging_direction_x = 0;
187 this.dodging_direction_y = 0;
191 // make sure v_up, v_right and v_forward are sane
192 makevectors(this.angles);
194 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
195 // will be called ramp_time/frametime times = 2 times. so, we need to
196 // add 0.5 * the total speed each frame until the dodge action is done..
197 float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
199 // if ramp time is smaller than frametime we get problems ;D
200 common_factor = min(common_factor, 1);
202 float horiz_speed = PHYS_FROZEN(this) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
203 float new_velocity_gain = this.dodging_velocity_gain - (common_factor * horiz_speed);
204 new_velocity_gain = max(0, new_velocity_gain);
206 float velocity_difference = this.dodging_velocity_gain - new_velocity_gain;
208 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
209 if (this.dodging_action == 1)
211 //disable jump key during dodge accel phase
212 if(this.movement_z > 0) { this.movement_z = 0; }
214 this.velocity += ((this.dodging_direction_y * velocity_difference) * v_right)
215 + ((this.dodging_direction_x * velocity_difference) * v_forward);
217 this.dodging_velocity_gain = this.dodging_velocity_gain - velocity_difference;
220 // the up part of the dodge is a single shot action
221 if (this.dodging_single_action == 1)
223 UNSET_ONGROUND(this);
225 this.velocity += PHYS_DODGING_UP_SPEED * v_up;
228 if (autocvar_sv_dodging_sound)
229 PlayerSound(this, playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
231 animdecide_setaction(this, ANIMACTION_JUMP, true);
234 this.dodging_single_action = 0;
237 // are we done with the dodging ramp yet?
238 if((this.dodging_action == 1) && ((time - this.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
240 // reset state so next dodge can be done correctly
241 this.dodging_action = 0;
242 this.dodging_direction_x = 0;
243 this.dodging_direction_y = 0;
247 void PM_dodging_GetPressedKeys(entity this)
250 if(!PHYS_DODGING) { return; }
252 PM_dodging_checkpressedkeys(this);
254 int keys = this.pressedkeys;
255 keys = BITSET(keys, KEY_FORWARD, this.movement.x > 0);
256 keys = BITSET(keys, KEY_BACKWARD, this.movement.x < 0);
257 keys = BITSET(keys, KEY_RIGHT, this.movement.y > 0);
258 keys = BITSET(keys, KEY_LEFT, this.movement.y < 0);
260 keys = BITSET(keys, KEY_JUMP, PHYS_INPUT_BUTTON_JUMP(this));
261 keys = BITSET(keys, KEY_CROUCH, PHYS_INPUT_BUTTON_CROUCH(this));
262 keys = BITSET(keys, KEY_ATCK, PHYS_INPUT_BUTTON_ATCK(this));
263 keys = BITSET(keys, KEY_ATCK2, PHYS_INPUT_BUTTON_ATCK2(this));
264 this.pressedkeys = keys;
268 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
270 // print("dodging_PlayerPhysics\n");
271 PM_dodging_GetPressedKeys(self);
278 MUTATOR_HOOKFUNCTION(dodging, GetCvars)
280 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
284 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
286 PM_dodging_checkpressedkeys(self);