1 #include "mutator_dodging.qh"
6 #include "../../common/animdecide.qh"
8 .float cvar_cl_dodging_timeout;
11 // these are used to store the last key press time for each of the keys..
12 .float last_FORWARD_KEY_time;
13 .float last_BACKWARD_KEY_time;
14 .float last_LEFT_KEY_time;
15 .float last_RIGHT_KEY_time;
17 // these store the movement direction at the time of the dodge action happening.
18 .vector dodging_direction;
20 // this indicates the last time a dodge was executed. used to check if another one is allowed
21 // and to ramp up the dodge acceleration in the physics hook.
22 .float last_dodging_time;
24 // This is the velocity gain to be added over the ramp time.
25 // It will decrease from frame to frame during dodging_action = 1
27 .float dodging_velocity_gain;
29 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
30 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
34 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
35 // print("dodging_PlayerPhysics\n");
38 float new_velocity_gain;
39 float velocity_difference;
40 float clean_up_and_do_nothing;
41 float horiz_speed = autocvar_sv_dodging_horiz_speed;
44 horiz_speed = autocvar_sv_dodging_horiz_speed_frozen;
46 if (self.deadflag != DEAD_NO)
49 new_velocity_gain = 0;
50 clean_up_and_do_nothing = 0;
53 clean_up_and_do_nothing = 1;
55 // when swimming, no dodging allowed..
56 if (self.waterlevel >= WATERLEVEL_SWIMMING)
57 clean_up_and_do_nothing = 1;
59 if (clean_up_and_do_nothing != 0) {
60 self.dodging_action = 0;
61 self.dodging_direction_x = 0;
62 self.dodging_direction_y = 0;
66 // make sure v_up, v_right and v_forward are sane
67 makevectors(self.angles);
69 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
70 // will be called ramp_time/frametime times = 2 times. so, we need to
71 // add 0.5 * the total speed each frame until the dodge action is done..
72 common_factor = sys_frametime / autocvar_sv_dodging_ramp_time;
74 // if ramp time is smaller than frametime we get problems ;D
75 if (common_factor > 1)
78 new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
79 if (new_velocity_gain < 0)
80 new_velocity_gain = 0;
82 velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
84 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
85 if (self.dodging_action == 1) {
86 //disable jump key during dodge accel phase
87 if (self.movement.z > 0) self.movement_z = 0;
91 + ((self.dodging_direction.y * velocity_difference) * v_right)
92 + ((self.dodging_direction.x * velocity_difference) * v_forward);
94 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
97 // the up part of the dodge is a single shot action
98 if (self.dodging_single_action == 1) {
99 self.flags &= ~FL_ONGROUND;
103 + (autocvar_sv_dodging_up_speed * v_up);
105 if (autocvar_sv_dodging_sound)
106 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
108 animdecide_setaction(self, ANIMACTION_JUMP, true);
110 self.dodging_single_action = 0;
113 // are we done with the dodging ramp yet?
114 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > autocvar_sv_dodging_ramp_time))
116 // reset state so next dodge can be done correctly
117 self.dodging_action = 0;
118 self.dodging_direction_x = 0;
119 self.dodging_direction_y = 0;
126 // returns 1 if the player is close to a wall
127 bool check_close_to_wall(float threshold)
129 if(!autocvar_sv_dodging_wall_dodging)
135 trace_start = self.origin;
137 trace_end = self.origin + (1000*v_right);
138 tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
139 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
142 trace_end = self.origin - (1000*v_right);
143 tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
144 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
147 trace_end = self.origin + (1000*v_forward);
148 tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
149 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
152 trace_end = self.origin - (1000*v_forward);
153 tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
154 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
160 bool check_close_to_ground(float threshold)
162 if (self.flags & FL_ONGROUND)
169 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
170 // print("dodging_PlayerPhysics\n");
173 vector tap_direction = '0 0 0';
175 float frozen_dodging, frozen_no_doubletap;
176 frozen_dodging = (self.frozen && autocvar_sv_dodging_frozen);
177 frozen_no_doubletap = (frozen_dodging && !autocvar_sv_dodging_frozen_doubletap);
179 float dodge_detected;
185 // first check if the last dodge is far enough back in time so we can dodge again
186 if ((time - self.last_dodging_time) < autocvar_sv_dodging_delay)
189 if (check_close_to_ground(autocvar_sv_dodging_height_threshold) != 1
190 && check_close_to_wall(autocvar_sv_dodging_wall_distance_threshold) != 1)
193 if (self.movement.x > 0) {
194 // is this a state change?
195 if (!(self.pressedkeys & KEY_FORWARD) || frozen_no_doubletap) {
196 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
197 tap_direction.x = 1.0;
200 self.last_FORWARD_KEY_time = time;
204 if (self.movement.x < 0) {
205 // is this a state change?
206 if (!(self.pressedkeys & KEY_BACKWARD) || frozen_no_doubletap) {
207 tap_direction.x = -1.0;
208 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
211 self.last_BACKWARD_KEY_time = time;
215 if (self.movement.y > 0) {
216 // is this a state change?
217 if (!(self.pressedkeys & KEY_RIGHT) || frozen_no_doubletap) {
218 tap_direction.y = 1.0;
219 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
222 self.last_RIGHT_KEY_time = time;
226 if (self.movement.y < 0) {
227 // is this a state change?
228 if (!(self.pressedkeys & KEY_LEFT) || frozen_no_doubletap) {
229 tap_direction.y = -1.0;
230 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
233 self.last_LEFT_KEY_time = time;
237 if (dodge_detected == 1) {
238 self.last_dodging_time = time;
240 self.dodging_action = 1;
241 self.dodging_single_action = 1;
243 self.dodging_velocity_gain = autocvar_sv_dodging_horiz_speed;
245 self.dodging_direction_x = tap_direction.x;
246 self.dodging_direction_y = tap_direction.y;
248 // normalize the dodging_direction vector.. (unlike UT99) XD
249 length = self.dodging_direction.x * self.dodging_direction.x;
250 length = length + self.dodging_direction.y * self.dodging_direction.y;
251 length = sqrt(length);
253 self.dodging_direction_x = self.dodging_direction.x * 1.0/length;
254 self.dodging_direction_y = self.dodging_direction.y * 1.0/length;
260 MUTATOR_DEFINITION(mutator_dodging)
262 // we need to be called before GetPressedKey does its thing so we can
263 // detect state changes and therefore dodging actions..
264 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
266 // in the physics hook we actually implement the dodge..
267 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
269 // get timeout information from the client, so the client can configure it..
270 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
272 // this just turns on the cvar.
278 // this just turns off the cvar.
279 MUTATOR_ONROLLBACK_OR_REMOVE