]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
DODGING: much saner dodge velocity ramping code
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_dodging.qc
1
2 .float cvar_cl_dodging_timeout;
3
4
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;
11
12 // these store the movement direction at the time of the dodge action happening.
13 .float dodging_direction_x;
14 .float dodging_direction_y;
15
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;
19
20 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
21 .float dodging_action;
22
23 // This is the velocity gain to be added over the ramp time.
24 // It will decrease from frame to frame during dodging_action = 1
25 // until it's 0.
26 .float dodging_velocity_gain;
27
28 // the jump part of the dodge cannot be ramped
29 .float dodging_single_action;
30
31 void dodging_Initialize() {
32         // print("dodging_Initialize\n");
33
34         self.last_FORWARD_KEY_time = 0;
35         self.last_BACKWARD_KEY_time = 0;
36         self.last_RIGHT_KEY_time = 0;
37         self.last_LEFT_KEY_time = 0;
38         self.last_JUMP_KEY_time = 0;
39         self.last_dodging_time = 0;
40         self.dodging_action = 0;
41         self.dodging_velocity_gain = 0;
42         self.dodging_single_action = 0;
43         self.dodging_direction_x = 0;
44         self.dodging_direction_y = 0;
45 }
46
47 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
48         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
49         return 0;
50 }
51
52 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
53         // print("dodging_PlayerPhysics\n");
54
55         float common_factor;
56         float new_velocity_gain;
57         float velocity_difference;
58         float clean_up_and_do_nothing;
59
60         new_velocity_gain = 0;
61         clean_up_and_do_nothing = 0;
62
63         if (g_dodging == 0)
64                 clean_up_and_do_nothing = 1;
65
66         // when swimming, no dodging allowed..
67         if (self.waterlevel >= WATERLEVEL_SWIMMING)
68                 clean_up_and_do_nothing = 1;
69
70         if (clean_up_and_do_nothing != 0) {
71                 self.dodging_action = 0;
72                 self.dodging_direction_x = 0;
73                 self.dodging_direction_y = 0;
74                 return 0;
75         }
76
77         // make sure v_up, v_right and v_forward are sane
78         makevectors(self.angles);
79
80         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code 
81         // will be called ramp_time/frametime times = 2 times. so, we need to 
82         // add 0.5 * the total speed each frame until the dodge action is done..
83         common_factor = sys_frametime / cvar("sv_dodging_ramp_time");
84
85         // if ramp time is smaller than frametime we get problems ;D
86         if (common_factor > 1) 
87                 common_factor = 1;
88
89         new_velocity_gain = self.dodging_velocity_gain - (common_factor * cvar("sv_dodging_horiz_speed"));
90         if (new_velocity_gain < 0)
91                 new_velocity_gain = 0;
92
93         velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
94
95         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
96         if (self.dodging_action == 1) {
97                 //disable jump key during dodge accel phase
98                 if (self.movement_z > 0) self.movement_z = 0;
99
100                 self.velocity = 
101                           self.velocity 
102                         + ((self.dodging_direction_y * velocity_difference) * v_right)
103                         + ((self.dodging_direction_x * velocity_difference) * v_forward);
104
105                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
106         }
107
108         // the up part of the dodge is a single shot action
109         if (self.dodging_single_action == 1) {
110                 self.velocity = 
111                           self.velocity 
112                         + (cvar("sv_dodging_up_speed") * v_up);
113
114                 if (cvar("sv_dodging_sound") == 1)
115                         PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
116
117                 setanim(self, self.anim_jump, TRUE, FALSE, TRUE);
118
119                 self.dodging_single_action = 0;
120         }
121
122         // are we done with the dodging ramp yet?
123         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > cvar("sv_dodging_ramp_time")))
124         {
125                 // reset state so next dodge can be done correctly
126                 self.dodging_action = 0;
127                 self.dodging_direction_x = 0;
128                 self.dodging_direction_y = 0;
129         }
130
131         return 0;
132 }
133
134
135 // returns 1 if the player is close to a wall
136 float check_close_to_wall(float threshold) {
137         if (cvar("sv_dodging_wall_dodging") == 0)
138                 return 0;
139
140         vector trace_start;
141         vector trace_end;
142
143         trace_start = self.origin;
144
145         trace_end = self.origin + (1000*v_right);
146         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
147         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
148                 return 1;
149
150         trace_end = self.origin - (1000*v_right);
151         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
152         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
153                 return 1;
154
155         trace_end = self.origin + (1000*v_forward);
156         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
157         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
158                 return 1;
159
160         trace_end = self.origin - (1000*v_forward);
161         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
162         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
163                 return 1;
164
165         return 0;
166 }
167
168 float check_close_to_ground(float threshold) {
169         vector trace_start;
170         vector trace_end;
171
172         // determine height above ground is below a threshold
173         trace_start = self.origin;
174         trace_end = self.origin - (1000*v_up);
175
176         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
177
178         // check if the trace hit anything at all
179         if (trace_fraction > 1)
180                 return 0;
181
182         if(self.origin_z - trace_endpos_z < threshold) 
183                 return 1;
184
185         return 0;
186 }
187
188
189 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
190         // print("dodging_PlayerPhysics\n");
191
192         float length;
193         float move_direction_x;
194         float move_direction_y;
195
196         move_direction_x = 0;
197         move_direction_y = 0;
198
199         float dodge_detected;
200         if (g_dodging == 0)
201                 return 0;
202
203         dodge_detected = 0;
204
205         // no dodging and jumping at the same time..
206         if (self.BUTTON_JUMP)
207                 return 0;
208
209         // first check if the last dodge is far enough back in time so we can dodge again
210         if ((time - self.last_dodging_time) < cvar("sv_dodging_delay"))
211                 return 0;
212
213         if (check_close_to_ground(cvar("sv_dodging_height_threshold")) != 1 
214                 && check_close_to_wall(cvar("sv_dodging_wall_distance_threshold")) != 1)
215                 return 0;
216
217         // remember last jump key time, so we can check in dodging code, if it
218         // was pressed between the two dodges..
219         if (self.BUTTON_JUMP)
220                 self.last_JUMP_KEY_time = time;
221
222         if (self.movement_x > 0) {
223                 move_direction_x = 1.0;
224                 // is this a state change?
225                 if (!(self.pressedkeys & KEY_FORWARD)) {
226                         if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) { 
227                                 dodge_detected = 1;
228                         }
229                         self.last_FORWARD_KEY_time = time;
230                 }
231         }
232
233         if (self.movement_x < 0) {
234                 move_direction_x = -1.0;
235                 // is this a state change?
236                 if (!(self.pressedkeys & KEY_BACKWARD)) {
237                         if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout)        { 
238                                 dodge_detected = 1;
239                         }
240                         self.last_BACKWARD_KEY_time = time;
241                 }
242         }
243
244         if (self.movement_y > 0) {
245                 move_direction_y = 1.0;
246                 // is this a state change?
247                 if (!(self.pressedkeys & KEY_RIGHT)) {
248                         if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout)   { 
249                                 dodge_detected = 1;
250                         }
251                         self.last_RIGHT_KEY_time = time;
252                 }
253         }
254
255         if (self.movement_y < 0) {
256                 move_direction_y = -1.0;
257                 // is this a state change?
258                 if (!(self.pressedkeys & KEY_LEFT)) {
259                         if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout)    { 
260                                 dodge_detected = 1;
261                         }
262                         self.last_LEFT_KEY_time = time;
263                 }
264         }
265
266
267
268         if (dodge_detected == 1) {
269                 // If the player pressed JUMP between the two taps, disallow dodging,
270                 // cause he obviously wants to jump instead
271                 if ((time - self.last_JUMP_KEY_time) < self.cvar_cl_dodging_timeout)
272                         return 0;
273
274                 self.last_dodging_time = time;
275
276                 self.dodging_action = 1;
277                 self.dodging_single_action = 1;
278
279                 self.dodging_velocity_gain = cvar("sv_dodging_horiz_speed");
280
281                 self.dodging_direction_x = move_direction_x;
282                 self.dodging_direction_y = move_direction_y;
283
284                 // normalize the dodging_direction vector.. (unlike UT99) XD
285                 length = length + self.dodging_direction_x * self.dodging_direction_x;
286                 length = length + self.dodging_direction_y * self.dodging_direction_y;
287                 length = sqrt(length);
288
289                 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
290                 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
291         }
292
293         return 0;
294 }
295
296 MUTATOR_DEFINITION(dodging)
297 {
298         // we need to be called before GetPressedKey does its thing so we can
299         // detect state changes and therefore dodging actions..
300         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
301
302         // in the physics hook we actually implement the dodge..
303         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
304
305         // get timeout information from the client, so the client can configure it..
306         MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
307
308         // this just turns on the cvar.
309         MUTATOR_ONADD
310         {
311                 g_dodging = 1;
312                 dodging_Initialize();
313         }
314
315         // this just turns off the cvar.
316         MUTATOR_ONREMOVE
317         {        
318                 g_dodging = 0;
319         }
320
321         return 0;
322 }