]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_dodging.qc
1 #include "mutator_dodging.qh"
2 #include "../_all.qh"
3
4 #include "mutator.qh"
5
6 #include "../../common/animdecide.qh"
7
8 .float cvar_cl_dodging_timeout;
9
10
11 // these are used to store the last key press time for each of the keys..
12 .float last_FORWARD_KEY_time;
13 .float last_BACKWARD_KEY_time;
14 .float last_LEFT_KEY_time;
15 .float last_RIGHT_KEY_time;
16
17 // these store the movement direction at the time of the dodge action happening.
18 .vector dodging_direction;
19
20 // this indicates the last time a dodge was executed. used to check if another one is allowed
21 // and to ramp up the dodge acceleration in the physics hook.
22 .float last_dodging_time;
23
24 // This is the velocity gain to be added over the ramp time.
25 // It will decrease from frame to frame during dodging_action = 1
26 // until it's 0.
27 .float dodging_velocity_gain;
28
29 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
30         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
31         return 0;
32 }
33
34 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
35         // print("dodging_PlayerPhysics\n");
36
37         float common_factor;
38         float new_velocity_gain;
39         float velocity_difference;
40         float clean_up_and_do_nothing;
41         float horiz_speed = autocvar_sv_dodging_horiz_speed;
42
43         if(self.frozen)
44                 horiz_speed = autocvar_sv_dodging_horiz_speed_frozen;
45
46     if (self.deadflag != DEAD_NO)
47         return 0;
48
49         new_velocity_gain = 0;
50         clean_up_and_do_nothing = 0;
51
52         if (g_dodging == 0)
53                 clean_up_and_do_nothing = 1;
54
55         // when swimming, no dodging allowed..
56         if (self.waterlevel >= WATERLEVEL_SWIMMING)
57                 clean_up_and_do_nothing = 1;
58
59         if (clean_up_and_do_nothing != 0) {
60                 self.dodging_action = 0;
61                 self.dodging_direction_x = 0;
62                 self.dodging_direction_y = 0;
63                 return 0;
64         }
65
66         // make sure v_up, v_right and v_forward are sane
67         makevectors(self.angles);
68
69         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
70         // will be called ramp_time/frametime times = 2 times. so, we need to
71         // add 0.5 * the total speed each frame until the dodge action is done..
72         common_factor = sys_frametime / autocvar_sv_dodging_ramp_time;
73
74         // if ramp time is smaller than frametime we get problems ;D
75         if (common_factor > 1)
76                 common_factor = 1;
77
78         new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
79         if (new_velocity_gain < 0)
80                 new_velocity_gain = 0;
81
82         velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
83
84         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
85         if (self.dodging_action == 1) {
86                 //disable jump key during dodge accel phase
87                 if (self.movement.z > 0) self.movement_z = 0;
88
89                 self.velocity =
90                           self.velocity
91                         + ((self.dodging_direction.y * velocity_difference) * v_right)
92                         + ((self.dodging_direction.x * velocity_difference) * v_forward);
93
94                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
95         }
96
97         // the up part of the dodge is a single shot action
98         if (self.dodging_single_action == 1) {
99                 self.flags &= ~FL_ONGROUND;
100
101                 self.velocity =
102                           self.velocity
103                         + (autocvar_sv_dodging_up_speed * v_up);
104
105                 if (autocvar_sv_dodging_sound)
106                         PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
107
108                 animdecide_setaction(self, ANIMACTION_JUMP, true);
109
110                 self.dodging_single_action = 0;
111         }
112
113         // are we done with the dodging ramp yet?
114         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > autocvar_sv_dodging_ramp_time))
115         {
116                 // reset state so next dodge can be done correctly
117                 self.dodging_action = 0;
118                 self.dodging_direction_x = 0;
119                 self.dodging_direction_y = 0;
120         }
121
122         return 0;
123 }
124
125
126 // returns 1 if the player is close to a wall
127 bool check_close_to_wall(float threshold)
128 {
129         if(!autocvar_sv_dodging_wall_dodging)
130                 return false;
131
132         vector trace_start;
133         vector trace_end;
134
135         trace_start = self.origin;
136
137         trace_end = self.origin + (1000*v_right);
138         tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
139         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
140                 return true;
141
142         trace_end = self.origin - (1000*v_right);
143         tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
144         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
145                 return true;
146
147         trace_end = self.origin + (1000*v_forward);
148         tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
149         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
150                 return true;
151
152         trace_end = self.origin - (1000*v_forward);
153         tracebox(trace_start, self.mins, self.maxs, trace_end, true, self);
154         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)
155                 return true;
156
157         return false;
158 }
159
160 bool check_close_to_ground(float threshold)
161 {
162         if (self.flags & FL_ONGROUND)
163                 return true;
164
165         return false;
166 }
167
168
169 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
170         // print("dodging_PlayerPhysics\n");
171
172         float length;
173         vector tap_direction = '0 0 0';
174
175         float frozen_dodging, frozen_no_doubletap;
176         frozen_dodging = (self.frozen && autocvar_sv_dodging_frozen);
177         frozen_no_doubletap = (frozen_dodging && !autocvar_sv_dodging_frozen_doubletap);
178
179         float dodge_detected;
180         if (g_dodging == 0)
181                 return 0;
182
183         dodge_detected = 0;
184
185         // first check if the last dodge is far enough back in time so we can dodge again
186         if ((time - self.last_dodging_time) < autocvar_sv_dodging_delay)
187                 return 0;
188
189         if (check_close_to_ground(autocvar_sv_dodging_height_threshold) != 1
190                 && check_close_to_wall(autocvar_sv_dodging_wall_distance_threshold) != 1)
191                 return 0;
192
193         if (self.movement.x > 0) {
194                 // is this a state change?
195                 if (!(self.pressedkeys & KEY_FORWARD) || frozen_no_doubletap) {
196                         if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) {
197                                 tap_direction.x = 1.0;
198                                 dodge_detected = 1;
199                         }
200                         self.last_FORWARD_KEY_time = time;
201                 }
202         }
203
204         if (self.movement.x < 0) {
205                 // is this a state change?
206                 if (!(self.pressedkeys & KEY_BACKWARD) || frozen_no_doubletap) {
207                         tap_direction.x = -1.0;
208                         if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout)        {
209                                 dodge_detected = 1;
210                         }
211                         self.last_BACKWARD_KEY_time = time;
212                 }
213         }
214
215         if (self.movement.y > 0) {
216                 // is this a state change?
217                 if (!(self.pressedkeys & KEY_RIGHT) || frozen_no_doubletap) {
218                         tap_direction.y = 1.0;
219                         if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout)   {
220                                 dodge_detected = 1;
221                         }
222                         self.last_RIGHT_KEY_time = time;
223                 }
224         }
225
226         if (self.movement.y < 0) {
227                 // is this a state change?
228                 if (!(self.pressedkeys & KEY_LEFT) || frozen_no_doubletap) {
229                         tap_direction.y = -1.0;
230                         if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout)    {
231                                 dodge_detected = 1;
232                         }
233                         self.last_LEFT_KEY_time = time;
234                 }
235         }
236
237         if (dodge_detected == 1) {
238                 self.last_dodging_time = time;
239
240                 self.dodging_action = 1;
241                 self.dodging_single_action = 1;
242
243                 self.dodging_velocity_gain = autocvar_sv_dodging_horiz_speed;
244
245                 self.dodging_direction_x = tap_direction.x;
246                 self.dodging_direction_y = tap_direction.y;
247
248                 // normalize the dodging_direction vector.. (unlike UT99) XD
249                 length =          self.dodging_direction.x * self.dodging_direction.x;
250                 length = length + self.dodging_direction.y * self.dodging_direction.y;
251                 length = sqrt(length);
252
253                 self.dodging_direction_x = self.dodging_direction.x * 1.0/length;
254                 self.dodging_direction_y = self.dodging_direction.y * 1.0/length;
255         }
256
257         return 0;
258 }
259
260 MUTATOR_DEFINITION(mutator_dodging)
261 {
262         // we need to be called before GetPressedKey does its thing so we can
263         // detect state changes and therefore dodging actions..
264         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
265
266         // in the physics hook we actually implement the dodge..
267         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
268
269         // get timeout information from the client, so the client can configure it..
270         MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
271
272         // this just turns on the cvar.
273         MUTATOR_ONADD
274         {
275                 g_dodging = 1;
276         }
277
278         // this just turns off the cvar.
279         MUTATOR_ONROLLBACK_OR_REMOVE
280         {
281                 g_dodging = 0;
282         }
283
284         MUTATOR_ONREMOVE
285         {
286         }
287
288         return 0;
289 }