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