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