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