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;
10 .float last_JUMP_KEY_time;
12 // these store the movement direction at the time of the dodge action happening.
13 .float dodging_direction_x;
14 .float dodging_direction_y;
16 // this indicates the last time a dodge was executed. used to check if another one is allowed
17 // and to ramp up the dodge acceleration in the physics hook.
18 .float last_dodging_time;
20 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
21 .float dodging_action;
23 // the jump part of the dodge cannot be ramped
24 .float dodging_single_action;
26 void dodging_Initialize() {
27 // print("dodging_Initialize\n");
29 self.last_FORWARD_KEY_time = 0;
30 self.last_BACKWARD_KEY_time = 0;
31 self.last_RIGHT_KEY_time = 0;
32 self.last_LEFT_KEY_time = 0;
33 self.last_JUMP_KEY_time = 0;
34 self.last_dodging_time = 0;
35 self.dodging_action = 0;
36 self.dodging_single_action = 0;
37 self.dodging_direction_x = 0;
38 self.dodging_direction_y = 0;
41 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
42 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
46 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
47 // print("dodging_PlayerPhysics\n");
51 float clean_up_and_do_nothing;
53 clean_up_and_do_nothing = 0;
56 clean_up_and_do_nothing = 1;
58 // when swimming, no dodging allowed..
59 if (self.waterlevel >= WATERLEVEL_SWIMMING)
60 clean_up_and_do_nothing = 1;
62 if (clean_up_and_do_nothing != 0) {
63 self.dodging_action = 0;
67 // make sure v_up, v_right and v_forward are sane
68 makevectors(self.angles);
70 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
71 // will be called ramp_time/frametime times = 2 times. so, we need to
72 // add 0.5 * the total speed each frame until the dodge action is done..
73 common_factor = sys_frametime / cvar("sv_dodging_ramp_time");
75 // if ramp time is smaller than frametime we get problems ;D
76 if (common_factor > 1)
80 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
81 if (self.dodging_action == 1) {
82 //disable jump key during dodge accel phase
83 if (self.movement_z > 0) self.movement_z = 0;
87 + (common_factor * (self.dodging_direction_y * cvar("sv_dodging_horiz_speed")) * v_right)
88 + (common_factor * (self.dodging_direction_x * cvar("sv_dodging_horiz_speed")) * v_forward);
91 // the up part of the dodge is a single shot action
92 if (self.dodging_single_action == 1) {
95 + (cvar("sv_dodging_up_speed") * v_up);
97 if (cvar("sv_dodging_sound") == 1)
98 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
100 setanim(self, self.anim_jump, TRUE, FALSE, TRUE);
102 self.dodging_single_action = 0;
105 // are we done with the dodging ramp yet?
106 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
108 // reset state so next dodge can be done correctly
109 self.dodging_action = 0;
110 self.dodging_direction_x = 0;
111 self.dodging_direction_y = 0;
118 // returns 1 if the player is close to a wall
119 float check_close_to_wall(float threshold) {
120 if (cvar("sv_dodging_wall_dodging") == 0)
126 trace_start = self.origin;
128 trace_end = self.origin + (1000*v_right);
129 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
130 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
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_forward);
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)
151 float check_close_to_ground(float threshold) {
155 // determine height above ground is below a threshold
156 trace_start = self.origin;
157 trace_end = self.origin - (1000*v_up);
159 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
161 // check if the trace hit anything at all
162 if (trace_fraction > 1)
165 if(self.origin_z - trace_endpos_z < threshold)
172 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
173 // print("dodging_PlayerPhysics\n");
176 float dodge_detected;
182 // no dodging and jumping at the same time..
183 if (self.BUTTON_JUMP)
186 // first check if the last dodge is far enough back in time so we can dodge again
187 if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
190 if (check_close_to_ground(cvar("sv_dodging_height_threshold")) != 1
191 && check_close_to_wall(cvar("sv_dodging_wall_distance_threshold")) != 1)
194 // remember last jump key time, so we can check in dodging code, if it
195 // was pressed between the two dodges..
196 if (self.BUTTON_JUMP)
197 self.last_JUMP_KEY_time = time;
199 if (self.movement_x > 0) {
200 // is this a state change?
201 if (!(self.pressedkeys & KEY_FORWARD)) {
202 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
204 self.dodging_direction_x = 1.0;
206 self.last_FORWARD_KEY_time = time;
210 if (self.movement_x < 0) {
211 // is this a state change?
212 if (!(self.pressedkeys & KEY_BACKWARD)) {
213 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
215 self.dodging_direction_x = -1.0;
217 self.last_BACKWARD_KEY_time = time;
221 if (self.movement_y > 0) {
222 // is this a state change?
223 if (!(self.pressedkeys & KEY_RIGHT)) {
224 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
226 self.dodging_direction_y = 1.0;
228 self.last_RIGHT_KEY_time = time;
232 if (self.movement_y < 0) {
233 // is this a state change?
234 if (!(self.pressedkeys & KEY_LEFT)) {
235 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
237 self.dodging_direction_y = -1.0;
239 self.last_LEFT_KEY_time = time;
245 if (dodge_detected == 1) {
246 // If the player pressed JUMP between the two taps, disallow dodging,
247 // cause he obviously wants to jump instead
248 if ((time - self.last_JUMP_KEY_time) < self.cvar_cl_dodging_timeout)
251 self.last_dodging_time = time;
253 self.dodging_action = 1;
254 self.dodging_single_action = 1;
256 // normalize the dodging_direction vector.. (unlike UT99) XD
257 length = length + self.dodging_direction_x * self.dodging_direction_x;
258 length = length + self.dodging_direction_y * self.dodging_direction_y;
259 length = sqrt(length);
261 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
262 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
268 MUTATOR_DEFINITION(dodging)
270 // we need to be called before GetPressedKey does its thing so we can
271 // detect state changes and therefore dodging actions..
272 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
274 // in the physics hook we actually implement the dodge..
275 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
277 // get timeout information from the client, so the client can configure it..
278 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
280 // this just turns on the cvar.
284 dodging_Initialize();
287 // this just turns off the cvar.