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");
49 // is dodging enabled at all? if not, do nothing..
53 // make sure v_up, v_right and v_forward are sane
54 makevectors(self.angles);
56 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
57 // will be called ramp_time/frametime times = 2 times. so, we need to
58 // add 0.5 * the total speed each frame until the dodge action is done..
59 common_factor = sys_frametime / cvar("sv_dodging_ramp_time");
61 // if ramp time is smaller than frametime we get problems ;D
62 if (common_factor > 1)
66 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
67 if (self.dodging_action == 1) {
68 //disable jump key during dodge accel phase
69 if (self.movement_z > 0) self.movement_z = 0;
73 + (common_factor * (self.dodging_direction_y * cvar("sv_dodging_horiz_speed")) * v_right)
74 + (common_factor * (self.dodging_direction_x * cvar("sv_dodging_horiz_speed")) * v_forward);
77 // the up part of the dodge is a single shot action
78 if (self.dodging_single_action == 1) {
81 + (cvar("sv_dodging_up_speed") * v_up);
83 if (cvar("sv_dodging_sound") == 1)
84 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
86 self.dodging_single_action = 0;
89 // are we done with the dodging ramp yet?
90 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
92 // reset state so next dodge can be done correctly
93 self.dodging_action = 0;
94 self.dodging_direction_x = 0;
95 self.dodging_direction_y = 0;
101 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
102 // print("dodging_PlayerPhysics\n");
105 float dodge_detected;
108 float height_above_ground;
115 // no dodging and jumping at the same time..
116 if (self.BUTTON_JUMP)
119 // first check if the last dodge is far enough back in time so we can dodge again
120 if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
123 // determine height above ground is below a threshold
124 trace_start = self.origin;
125 trace_end = self.origin - (100*v_up);
127 traceline(trace_start, trace_end, TRUE, self);
128 height_above_ground = self.origin_z - trace_endpos_z;
130 // check if our feet are on the ground or at least close :D
131 if (height_above_ground > (fabs(PL_MIN_z) + cvar("sv_dodging_height_threshold")))
134 if (self.movement_x > 0) {
135 // is this a state change?
136 if (!(self.pressedkeys & KEY_FORWARD)) {
137 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
139 self.dodging_direction_x = 1.0;
141 self.last_FORWARD_KEY_time = time;
145 if (self.movement_x < 0) {
146 // is this a state change?
147 if (!(self.pressedkeys & KEY_BACKWARD)) {
148 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
150 self.dodging_direction_x = -1.0;
152 self.last_BACKWARD_KEY_time = time;
156 if (self.movement_y > 0) {
157 // is this a state change?
158 if (!(self.pressedkeys & KEY_RIGHT)) {
159 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
161 self.dodging_direction_y = 1.0;
163 self.last_RIGHT_KEY_time = time;
167 if (self.movement_y < 0) {
168 // is this a state change?
169 if (!(self.pressedkeys & KEY_LEFT)) {
170 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
172 self.dodging_direction_y = -1.0;
174 self.last_LEFT_KEY_time = time;
180 if (dodge_detected == 1) {
181 if (self.movement_z > 0)
184 self.last_dodging_time = time;
186 self.dodging_action = 1;
187 self.dodging_single_action = 1;
189 // normalize the dodging_direction vector.. (unlike UT99) XD
190 length = length + self.dodging_direction_x * self.dodging_direction_x;
191 length = length + self.dodging_direction_y * self.dodging_direction_y;
192 length = sqrt(length);
194 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
195 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
201 MUTATOR_DEFINITION(dodging)
203 // we need to be called before GetPressedKey does its thing so we can
204 // detect state changes and therefore dodging actions..
205 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
207 // in the physics hook we actually implement the dodge..
208 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
210 // get timeout information from the client, so the client can configure it..
211 MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
213 // this just turns on the cvar.
217 dodging_Initialize();
220 // this just turns off the cvar.