]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
62caadc1b9fe2873dd9cf15bad559bf9a0413cf9
[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
11 // these store the movement direction at the time of the dodge action happening.
12 .float dodging_direction_x;
13 .float dodging_direction_y;
14
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;
18
19 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
20 .float dodging_action;
21
22 // the jump part of the dodge cannot be ramped
23 .float dodging_single_action;
24
25 void dodging_Initialize() {
26         // print("dodging_Initialize\n");
27
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;
37 }
38
39 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
40         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
41         return 0;
42 }
43
44 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
45         // print("dodging_PlayerPhysics\n");
46
47         float common_factor;
48
49         if (g_dodging == 0)
50                 return 0;
51
52         // when swimming, no dodging allowed..
53         if (self.waterlevel >= WATERLEVEL_SWIMMING) {
54                 self.dodging_action = 0;
55                 return 0;
56         }
57
58         // make sure v_up, v_right and v_forward are sane
59         makevectors(self.angles);
60
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");
65
66         // if ramp time is smaller than frametime we get problems ;D
67         if (common_factor > 1) 
68                 common_factor = 1;
69
70
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;
75
76                 self.velocity = 
77                           self.velocity 
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);
80         }
81
82         // the up part of the dodge is a single shot action
83         if (self.dodging_single_action == 1) {
84                 self.velocity = 
85                           self.velocity 
86                         + (cvar("sv_dodging_up_speed") * v_up);
87
88                 if (cvar("sv_dodging_sound") == 1)
89                         PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
90
91                 setanim(self, self.anim_jump, TRUE, FALSE, TRUE);
92
93                 self.dodging_single_action = 0;
94         }
95
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")))
98         {
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;
103         }
104
105         return 0;
106 }
107
108
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)
112                 return 0;
113
114         vector trace_start;
115         vector trace_end;
116
117         trace_start = self.origin;
118
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)
122                 return 1;
123
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)
127                 return 1;
128
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)
132                 return 1;
133
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)
137                 return 1;
138
139         return 0;
140 }
141
142 float check_close_to_ground(float threshold) {
143         vector trace_start;
144         vector trace_end;
145
146         // determine height above ground is below a threshold
147         trace_start = self.origin;
148         trace_end = self.origin - (1000*v_up);
149
150         tracebox(trace_start, self.mins, self.maxs, trace_end, TRUE, self);
151
152         // check if the trace hit anything at all
153         if (trace_fraction > 1)
154                 return 0;
155
156         if(self.origin_z - trace_endpos_z < threshold) 
157                 return 1;
158
159         return 0;
160 }
161
162
163 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
164         // print("dodging_PlayerPhysics\n");
165
166         float length;
167         float dodge_detected;
168         if (g_dodging == 0)
169                 return 0;
170
171         dodge_detected = 0;
172
173         // no dodging and jumping at the same time..
174         if (self.BUTTON_JUMP)
175                 return 0;
176
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"))
179                 return 0;
180
181         if (check_close_to_ground(cvar("sv_dodging_height_threshold")) != 1 
182                 && check_close_to_wall(cvar("sv_dodging_wall_distance_threshold")) != 1)
183                 return 0;
184
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) { 
189                                 dodge_detected = 1;
190                                 self.dodging_direction_x = 1.0;
191                         }
192                         self.last_FORWARD_KEY_time = time;
193                 }
194         }
195
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)        { 
200                                 dodge_detected = 1;
201                                 self.dodging_direction_x = -1.0;
202                         }
203                         self.last_BACKWARD_KEY_time = time;
204                 }
205         }
206
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)   { 
211                                 dodge_detected = 1;
212                                 self.dodging_direction_y = 1.0;
213                         }
214                         self.last_RIGHT_KEY_time = time;
215                 }
216         }
217
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)    { 
222                                 dodge_detected = 1;
223                                 self.dodging_direction_y = -1.0;
224                         }
225                         self.last_LEFT_KEY_time = time;
226                 }
227         }
228
229
230
231         if (dodge_detected == 1) {
232                 if (self.movement_z > 0)
233                         self.movement_z = 0;
234
235                 self.last_dodging_time = time;
236
237                 self.dodging_action = 1;
238                 self.dodging_single_action = 1;
239
240                 // normalize the dodging_direction vector.. (unlike UT99) XD
241                 length = length + self.dodging_direction_x * self.dodging_direction_x;
242                 length = length + self.dodging_direction_y * self.dodging_direction_y;
243                 length = sqrt(length);
244
245                 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
246                 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
247         }
248
249         return 0;
250 }
251
252 MUTATOR_DEFINITION(dodging)
253 {
254         // we need to be called before GetPressedKey does its thing so we can
255         // detect state changes and therefore dodging actions..
256         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
257
258         // in the physics hook we actually implement the dodge..
259         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
260
261         // get timeout information from the client, so the client can configure it..
262         MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
263
264         // this just turns on the cvar.
265         MUTATOR_ONADD
266         {
267                 g_dodging = 1;
268                 dodging_Initialize();
269         }
270
271         // this just turns off the cvar.
272         MUTATOR_ONREMOVE
273         {        
274                 g_dodging = 0;
275         }
276
277         return 0;
278 }