]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_dodging.qc
Merge remote branch 'origin/terencehill/WEP_constants_fix' into fruitiex/ctsfix
[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 // This is the velocity gain to be added over the ramp time.
23 // It will decrease from frame to frame during dodging_action = 1
24 // until it's 0.
25 .float dodging_velocity_gain;
26
27 // the jump part of the dodge cannot be ramped
28 .float dodging_single_action;
29
30 void dodging_Initialize() {
31         // print("dodging_Initialize\n");
32
33         self.last_FORWARD_KEY_time = 0;
34         self.last_BACKWARD_KEY_time = 0;
35         self.last_RIGHT_KEY_time = 0;
36         self.last_LEFT_KEY_time = 0;
37         self.last_dodging_time = 0;
38         self.dodging_action = 0;
39         self.dodging_velocity_gain = 0;
40         self.dodging_single_action = 0;
41         self.dodging_direction_x = 0;
42         self.dodging_direction_y = 0;
43 }
44
45 MUTATOR_HOOKFUNCTION(dodging_GetCvars) {
46         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
47         return 0;
48 }
49
50 MUTATOR_HOOKFUNCTION(dodging_PlayerPhysics) {
51         // print("dodging_PlayerPhysics\n");
52
53         float common_factor;
54         float new_velocity_gain;
55         float velocity_difference;
56         float clean_up_and_do_nothing;
57
58         new_velocity_gain = 0;
59         clean_up_and_do_nothing = 0;
60
61         if (g_dodging == 0)
62                 clean_up_and_do_nothing = 1;
63
64         // when swimming, no dodging allowed..
65         if (self.waterlevel >= WATERLEVEL_SWIMMING)
66                 clean_up_and_do_nothing = 1;
67
68         if (clean_up_and_do_nothing != 0) {
69                 self.dodging_action = 0;
70                 self.dodging_direction_x = 0;
71                 self.dodging_direction_y = 0;
72                 return 0;
73         }
74
75         // make sure v_up, v_right and v_forward are sane
76         makevectors(self.angles);
77
78         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code 
79         // will be called ramp_time/frametime times = 2 times. so, we need to 
80         // add 0.5 * the total speed each frame until the dodge action is done..
81         common_factor = sys_frametime / autocvar_sv_dodging_ramp_time;
82
83         // if ramp time is smaller than frametime we get problems ;D
84         if (common_factor > 1) 
85                 common_factor = 1;
86
87         new_velocity_gain = self.dodging_velocity_gain - (common_factor * autocvar_sv_dodging_horiz_speed);
88         if (new_velocity_gain < 0)
89                 new_velocity_gain = 0;
90
91         velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
92
93         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
94         if (self.dodging_action == 1) {
95                 //disable jump key during dodge accel phase
96                 if (self.movement_z > 0) self.movement_z = 0;
97
98                 self.velocity = 
99                           self.velocity 
100                         + ((self.dodging_direction_y * velocity_difference) * v_right)
101                         + ((self.dodging_direction_x * velocity_difference) * v_forward);
102
103                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
104         }
105
106         // the up part of the dodge is a single shot action
107         if (self.dodging_single_action == 1) {
108                 self.flags &~= FL_ONGROUND;
109
110                 self.velocity = 
111                           self.velocity 
112                         + (autocvar_sv_dodging_up_speed * v_up);
113
114                 if (autocvar_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) > autocvar_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 (autocvar_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         if (self.flags & FL_ONGROUND)
170                 return 1;
171
172         return 0;
173 }
174
175
176 MUTATOR_HOOKFUNCTION(dodging_GetPressedKeys) {
177         // print("dodging_PlayerPhysics\n");
178
179         float length;
180         float tap_direction_x;
181         float tap_direction_y;
182
183         tap_direction_x = 0;
184         tap_direction_y = 0;
185
186         float dodge_detected;
187         if (g_dodging == 0)
188                 return 0;
189
190         dodge_detected = 0;
191
192         // first check if the last dodge is far enough back in time so we can dodge again
193         if ((time - self.last_dodging_time) < autocvar_sv_dodging_delay)
194                 return 0;
195
196         if (check_close_to_ground(autocvar_sv_dodging_height_threshold) != 1 
197                 && check_close_to_wall(autocvar_sv_dodging_wall_distance_threshold) != 1)
198                 return 0;
199
200         if (self.movement_x > 0) {
201                 // is this a state change?
202                 if (!(self.pressedkeys & KEY_FORWARD)) {
203                         if ((time - self.last_FORWARD_KEY_time) < self.cvar_cl_dodging_timeout) { 
204                                 tap_direction_x = 1.0;
205                                 dodge_detected = 1;
206                         }
207                         self.last_FORWARD_KEY_time = time;
208                 }
209         }
210
211         if (self.movement_x < 0) {
212                 // is this a state change?
213                 if (!(self.pressedkeys & KEY_BACKWARD)) {
214                         tap_direction_x = -1.0;
215                         if ((time - self.last_BACKWARD_KEY_time) < self.cvar_cl_dodging_timeout)        { 
216                                 dodge_detected = 1;
217                         }
218                         self.last_BACKWARD_KEY_time = time;
219                 }
220         }
221
222         if (self.movement_y > 0) {
223                 // is this a state change?
224                 if (!(self.pressedkeys & KEY_RIGHT)) {
225                         tap_direction_y = 1.0;
226                         if ((time - self.last_RIGHT_KEY_time) < self.cvar_cl_dodging_timeout)   { 
227                                 dodge_detected = 1;
228                         }
229                         self.last_RIGHT_KEY_time = time;
230                 }
231         }
232
233         if (self.movement_y < 0) {
234                 // is this a state change?
235                 if (!(self.pressedkeys & KEY_LEFT)) {
236                         tap_direction_y = -1.0;
237                         if ((time - self.last_LEFT_KEY_time) < self.cvar_cl_dodging_timeout)    { 
238                                 dodge_detected = 1;
239                         }
240                         self.last_LEFT_KEY_time = time;
241                 }
242         }
243
244
245
246         if (dodge_detected == 1) {
247                 self.last_dodging_time = time;
248
249                 self.dodging_action = 1;
250                 self.dodging_single_action = 1;
251
252                 self.dodging_velocity_gain = autocvar_sv_dodging_horiz_speed;
253
254                 self.dodging_direction_x = tap_direction_x;
255                 self.dodging_direction_y = tap_direction_y;
256
257                 // normalize the dodging_direction vector.. (unlike UT99) XD
258                 length = length + self.dodging_direction_x * self.dodging_direction_x;
259                 length = length + self.dodging_direction_y * self.dodging_direction_y;
260                 length = sqrt(length);
261
262                 self.dodging_direction_x = self.dodging_direction_x * 1.0/length;
263                 self.dodging_direction_y = self.dodging_direction_y * 1.0/length;
264         }
265
266         return 0;
267 }
268
269 MUTATOR_DEFINITION(mutator_dodging)
270 {
271         // we need to be called before GetPressedKey does its thing so we can
272         // detect state changes and therefore dodging actions..
273         MUTATOR_HOOK(GetPressedKeys, dodging_GetPressedKeys, CBC_ORDER_ANY);
274
275         // in the physics hook we actually implement the dodge..
276         MUTATOR_HOOK(PlayerPhysics, dodging_PlayerPhysics, CBC_ORDER_ANY);
277
278         // get timeout information from the client, so the client can configure it..
279         MUTATOR_HOOK(GetCvars, dodging_GetCvars, CBC_ORDER_ANY);
280
281         // this just turns on the cvar.
282         MUTATOR_ONADD
283         {
284                 g_dodging = 1;
285                 dodging_Initialize();
286         }
287
288         // this just turns off the cvar.
289         MUTATOR_ONREMOVE
290         {        
291                 g_dodging = 0;
292         }
293
294         return 0;
295 }