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