2 // these are used to store the last key press time for each of the keys..
3 .float last_FORWARD_KEY_time;
4 .float last_BACKWARD_KEY_time;
5 .float last_LEFT_KEY_time;
6 .float last_RIGHT_KEY_time;
8 // these store the movement direction at the time of the dodge action happening.
9 .float dodging_direction_x;
10 .float dodging_direction_y;
12 // this indicates the last time a dodge was executed. used to check if another one is allowed
13 // and to ramp up the dodge acceleration in the physics hook.
14 .float last_dodging_time;
16 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
17 .float dodging_action;
19 // the jump part of the dodge cannot be ramped
20 .float dodging_single_action;
22 void dodging_Initialize() {
23 self.last_FORWARD_KEY_time = 0;
24 self.last_BACKWARD_KEY_time = 0;
25 self.last_RIGHT_KEY_time = 0;
26 self.last_LEFT_KEY_time = 0;
27 self.last_dodging_time = 0;
28 self.dodging_action = 0;
29 self.dodging_single_action = 0;
30 self.dodging_direction_x = 0;
31 self.dodging_direction_y = 0;
34 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
37 // is dodging enabled at all? if not, do nothing..
41 // make sure v_up, v_right and v_forward are sane
42 makevectors(self.angles);
44 // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
45 // will be called ramp_time/frametime times = 2 times. so, we need to
46 // add 0.5 * the total speed each frame until the dodge action is done..
47 common_factor = sys_frametime / cvar("sv_dodging_ramp_time");
49 // if ramp time is smaller than frametime we get problems ;D
50 if (common_factor > 1)
54 // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
55 if (self.dodging_action == 1) {
58 + (common_factor * (self.dodging_direction_y * cvar("sv_dodging_horiz_speed")) * v_right)
59 + (common_factor * (self.dodging_direction_x * cvar("sv_dodging_horiz_speed")) * v_forward);
62 // the up part of the dodge is a single shot action
63 if (self.dodging_single_action == 1) {
66 + (cvar("sv_dodging_up_speed") * v_up);
68 self.dodging_single_action = 0;
71 // are we done with the dodging ramp yet?
72 if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
74 // reset state so next dodge can be done correctly
75 self.dodging_action = 0;
76 self.dodging_direction_x = 0;
77 self.dodging_direction_y = 0;
83 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
86 //print("dodging_hook\n");
93 // first check if the last dodge is far enough back in time so we can dodge again
94 if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
97 // check if our feet are on the ground :D
98 if (!(self.lastflags & FL_ONGROUND))
102 if (self.movement_x > 0) {
103 // is this a state change?
104 if (!(self.pressedkeys & KEY_FORWARD)) {
105 if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
107 self.dodging_direction_x = 1.0;
108 self.last_dodging_time = time;
110 self.last_FORWARD_KEY_time = time;
114 if (self.movement_x < 0) {
115 // is this a state change?
116 if (!(self.pressedkeys & KEY_BACKWARD)) {
117 if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
119 self.dodging_direction_x = -1.0;
120 self.last_dodging_time = time;
122 self.last_BACKWARD_KEY_time = time;
126 if (self.movement_y > 0) {
127 // is this a state change?
128 if (!(self.pressedkeys & KEY_RIGHT)) {
129 if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout) {
131 self.dodging_direction_y = 1.0;
132 self.last_dodging_time = time;
134 self.last_RIGHT_KEY_time = time;
138 if (self.movement_y < 0) {
139 // is this a state change?
140 if (!(self.pressedkeys & KEY_LEFT)) {
141 if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout) {
143 self.dodging_direction_y = -1.0;
144 self.last_dodging_time = time;
146 self.last_LEFT_KEY_time = time;
152 if (dodge_detected == 1) {
153 self.dodging_action = 1;
154 self.dodging_single_action = 1;
156 // normalize the dodging_direction vector.. (unlike UT99) XD
157 length = length + self.dodging_direction_x * self.dodging_direction_x;
158 length = length + self.dodging_direction_y * self.dodging_direction_y;
159 length = sqrt(length);
161 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
162 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
168 MUTATOR_DEFINITION(dodging)
170 // we need to be called before GetPressedKey does its thing so we can
171 // detect state changes and therefore dodging actions..
172 MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
174 // in the physics hook we actually implement the dodge..
175 MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
177 // this just turns on the cvar.
181 dodging_Initialize();
184 // this just turns off the cvar.