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 // the jump part of the dodge cannot be ramped
23 .float dodging_single_action;
25 void dodging_Initialize() {
26 // print("dodging_Initialize\n");
28 self.last_FORWARD_KEY_time = 0;
29 self.last_BACKWARD_KEY_time = 0;
30 self.last_RIGHT_KEY_time = 0;
31 self.last_LEFT_KEY_time = 0;
32 self.last_dodging_time = 0;
33 self.dodging_action = 0;
34 self.dodging_single_action = 0;
35 self.dodging_direction_x = 0;
36 self.dodging_direction_y = 0;
39 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
40 GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
44 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
45 // print("dodging_PlayerPhysics\n");
52 // when swimming, no dodging allowed..
53 if (self.waterlevel >= WATERLEVEL_SWIMMING) {
54 self.dodging_action = 0;
58 // make sure v_up, v_right and v_forward are sane
59 makevectors(self.angles);
61 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
62 // will be called ramp_time/frametime times = 2 times. so, we need to
63 // add 0.5 * the total speed each frame until the dodge action is done..
64 common_factor = sys_frametime / cvar("sv_dodging_ramp_time");
66 // if ramp time is smaller than frametime we get problems ;D
67 if (common_factor > 1)
71 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
72 if (self.dodging_action == 1) {
73 //disable jump key during dodge accel phase
74 if (self.movement_z > 0) self.movement_z = 0;
78 + (common_factor * (self.dodging_direction_y * cvar("sv_dodging_horiz_speed")) * v_right)
79 + (common_factor * (self.dodging_direction_x * cvar("sv_dodging_horiz_speed")) * v_forward);
82 // the up part of the dodge is a single shot action
83 if (self.dodging_single_action == 1) {
86 + (cvar("sv_dodging_up_speed") * v_up);
88 if (cvar("sv_dodging_sound") == 1)
89 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
91 setanim(self, self.anim_jump, TRUE, FALSE, TRUE);
93 self.dodging_single_action = 0;
96 // are we done with the dodging ramp yet?
97 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
99 // reset state so next dodge can be done correctly
100 self.dodging_action = 0;
101 self.dodging_direction_x = 0;
102 self.dodging_direction_y = 0;
109 // returns 1 if the player is close to a wall
110 float check_close_to_wall(float threshold) {
111 if (cvar("sv_dodging_wall_dodging") == 0)
117 trace_start = self.origin;
119 trace_end = self.origin + (1000*v_right);
120 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
121 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
124 trace_end = self.origin - (1000*v_right);
125 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
126 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
129 trace_end = self.origin + (1000*v_forward);
130 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
131 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
134 trace_end = self.origin - (1000*v_forward);
135 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
136 if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
142 float check_close_to_ground(float threshold) {
146 // determine height above ground is below a threshold
147 trace_start = self.origin;
148 trace_end = self.origin - (1000*v_up);
150 tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
152 // check if the trace hit anything at all
153 if (trace_fraction > 1)
156 if(self.origin_z - trace_endpos_z < threshold)
163 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
164 // print("dodging_PlayerPhysics\n");
167 float dodge_detected;
173 // no dodging and jumping at the same time..
174 if (self.BUTTON_JUMP)
177 // first check if the last dodge is far enough back in time so we can dodge again
178 if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
181 if (check_close_to_ground(cvar("sv_dodging_height_threshold")) != 1
182 && check_close_to_wall(cvar("sv_dodging_wall_distance_threshold")) != 1)
185 if (self.movement_x > 0) {
186 // is this a state change?
187 if (!(self.pressedkeys & KEY_FORWARD)) {
188 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
190 self.dodging_direction_x = 1.0;
192 self.last_FORWARD_KEY_time = time;
196 if (self.movement_x < 0) {
197 // is this a state change?
198 if (!(self.pressedkeys & KEY_BACKWARD)) {
199 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
201 self.dodging_direction_x = -1.0;
203 self.last_BACKWARD_KEY_time = time;
207 if (self.movement_y > 0) {
208 // is this a state change?
209 if (!(self.pressedkeys & KEY_RIGHT)) {
210 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
212 self.dodging_direction_y = 1.0;
214 self.last_RIGHT_KEY_time = time;
218 if (self.movement_y < 0) {
219 // is this a state change?
220 if (!(self.pressedkeys & KEY_LEFT)) {
221 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
223 self.dodging_direction_y = -1.0;
225 self.last_LEFT_KEY_time = time;
231 if (dodge_detected == 1) {
232 self.last_dodging_time = time;
234 self.dodging_action = 1;
235 self.dodging_single_action = 1;
237 // normalize the dodging_direction vector.. (unlike UT99) XD
238 length = length + self.dodging_direction_x * self.dodging_direction_x;
239 length = length + self.dodging_direction_y * self.dodging_direction_y;
240 length = sqrt(length);
242 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
243 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
249 MUTATOR_DEFINITION(dodging)
251 // we need to be called before GetPressedKey does its thing so we can
252 // detect state changes and therefore dodging actions..
253 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
255 // in the physics hook we actually implement the dodge..
256 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
258 // get timeout information from the client, so the client can configure it..
259 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
261 // this just turns on the cvar.
265 dodging_Initialize();
268 // this just turns off the cvar.