2 .float cvar_cl_dodging_timeout;
5 // these are used to store the last key press time for each of the keys..
6 .float last_FORWARD_KEY_time;
7 .float last_BACKWARD_KEY_time;
8 .float last_LEFT_KEY_time;
9 .float last_RIGHT_KEY_time;
11 // these store the movement direction at the time of the dodge action happening.
12 .float dodging_direction_x;
13 .float dodging_direction_y;
15 // this indicates the last time a dodge was executed. used to check if another one is allowed
16 // and to ramp up the dodge acceleration in the physics hook.
17 .float last_dodging_time;
19 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
20 .float dodging_action;
22 // This is the velocity gain to be added over the ramp time.
23 // It will decrease from frame to frame during dodging_action = 1
25 .float dodging_velocity_gain;
27 // the jump part of the dodge cannot be ramped
28 .float dodging_single_action;
30 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
31 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
35 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
36 // print("dodging_PlayerPhysics\n");
39 float new_velocity_gain;
40 float velocity_difference;
41 float clean_up_and_do_nothing;
43 if (self.deadflag != DEAD_NO)
46 new_velocity_gain = 0;
47 clean_up_and_do_nothing = 0;
50 clean_up_and_do_nothing = 1;
52 // when swimming, no dodging allowed..
53 if (self.waterlevel >= WATERLEVEL_SWIMMING)
54 clean_up_and_do_nothing = 1;
56 if (clean_up_and_do_nothing != 0) {
57 self.dodging_action = 0;
58 self.dodging_direction_x = 0;
59 self.dodging_direction_y = 0;
63 // make sure v_up, v_right and v_forward are sane
64 makevectors(self.angles);
66 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
67 // will be called ramp_time/frametime times = 2 times. so, we need to
68 // add 0.5 * the total speed each frame until the dodge action is done..
69 common_factor = sys_frametime / autocvar_sv_dodging_ramp_time;
71 // if ramp time is smaller than frametime we get problems ;D
72 if (common_factor > 1)
75 new_velocity_gain = self.dodging_velocity_gain - (common_factor * autocvar_sv_dodging_horiz_speed);
76 if (new_velocity_gain < 0)
77 new_velocity_gain = 0;
79 velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
81 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
82 if (self.dodging_action == 1) {
83 //disable jump key during dodge accel phase
84 if (self.movement_z > 0) self.movement_z = 0;
88 + ((self.dodging_direction_y * velocity_difference) * v_right)
89 + ((self.dodging_direction_x * velocity_difference) * v_forward);
91 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
94 // the up part of the dodge is a single shot action
95 if (self.dodging_single_action == 1) {
96 self.flags &~= FL_ONGROUND;
100 + (autocvar_sv_dodging_up_speed * v_up);
102 if (autocvar_sv_dodging_sound == 1)
103 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
105 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
107 self.dodging_single_action = 0;
110 // are we done with the dodging ramp yet?
111 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > autocvar_sv_dodging_ramp_time))
113 // reset state so next dodge can be done correctly
114 self.dodging_action = 0;
115 self.dodging_direction_x = 0;
116 self.dodging_direction_y = 0;
123 // returns 1 if the player is close to a wall
124 float check_close_to_wall(float threshold) {
125 if (autocvar_sv_dodging_wall_dodging == 0)
131 trace_start = self.origin;
133 trace_end = self.origin + (1000*v_right);
134 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
135 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
138 trace_end = self.origin - (1000*v_right);
139 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
140 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
143 trace_end = self.origin + (1000*v_forward);
144 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
145 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
148 trace_end = self.origin - (1000*v_forward);
149 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
150 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
156 float check_close_to_ground(float threshold) {
157 if (self.flags & FL_ONGROUND)
164 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
165 // print("dodging_PlayerPhysics\n");
168 float tap_direction_x;
169 float tap_direction_y;
174 float dodge_detected;
180 // first check if the last dodge is far enough back in time so we can dodge again
181 if ((time - self.last_dodging_time) < autocvar_sv_dodging_delay)
184 if (check_close_to_ground(autocvar_sv_dodging_height_threshold) != 1
185 && check_close_to_wall(autocvar_sv_dodging_wall_distance_threshold) != 1)
188 if (self.movement_x > 0) {
189 // is this a state change?
190 if (!(self.pressedkeys & KEY_FORWARD)) {
191 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
192 tap_direction_x = 1.0;
195 self.last_FORWARD_KEY_time = time;
199 if (self.movement_x < 0) {
200 // is this a state change?
201 if (!(self.pressedkeys & KEY_BACKWARD)) {
202 tap_direction_x = -1.0;
203 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
206 self.last_BACKWARD_KEY_time = time;
210 if (self.movement_y > 0) {
211 // is this a state change?
212 if (!(self.pressedkeys & KEY_RIGHT)) {
213 tap_direction_y = 1.0;
214 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
217 self.last_RIGHT_KEY_time = time;
221 if (self.movement_y < 0) {
222 // is this a state change?
223 if (!(self.pressedkeys & KEY_LEFT)) {
224 tap_direction_y = -1.0;
225 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
228 self.last_LEFT_KEY_time = time;
232 if (dodge_detected == 1) {
233 self.last_dodging_time = time;
235 self.dodging_action = 1;
236 self.dodging_single_action = 1;
238 self.dodging_velocity_gain = autocvar_sv_dodging_horiz_speed;
240 self.dodging_direction_x = tap_direction_x;
241 self.dodging_direction_y = tap_direction_y;
243 // normalize the dodging_direction vector.. (unlike UT99) XD
244 length = self.dodging_direction_x * self.dodging_direction_x;
245 length = length + self.dodging_direction_y * self.dodging_direction_y;
246 length = sqrt(length);
248 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
249 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
255 MUTATOR_DEFINITION(mutator_dodging)
257 // we need to be called before GetPressedKey does its thing so we can
258 // detect state changes and therefore dodging actions..
259 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
261 // in the physics hook we actually implement the dodge..
262 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
264 // get timeout information from the client, so the client can configure it..
265 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
267 // this just turns on the cvar.
273 // this just turns off the cvar.
274 MUTATOR_ONROLLBACK_OR_REMOVE