]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/dodging.qc
4f724cfbf7148af2e51e523eb4ff02d47f99d76b
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / dodging / dodging.qc
1 #ifdef IMPLEMENTATION
2
3 #define PHYS_DODGING                                            STAT(DODGING, this)
4 #define PHYS_DODGING_DELAY                                      STAT(DODGING_DELAY, this)
5 #define PHYS_DODGING_DISTANCE_THRESHOLD         STAT(DODGING_DISTANCE_THRESHOLD, this)
6 #define PHYS_DODGING_FROZEN_NODOUBLETAP         STAT(DODGING_FROZEN_NO_DOUBLETAP, this)
7 #define PHYS_DODGING_HEIGHT_THRESHOLD           STAT(DODGING_HEIGHT_THRESHOLD, this)
8 #define PHYS_DODGING_HORIZ_SPEED                        STAT(DODGING_HORIZ_SPEED, this)
9 #define PHYS_DODGING_HORIZ_SPEED_FROZEN         STAT(DODGING_HORIZ_SPEED_FROZEN, this)
10 #define PHYS_DODGING_RAMP_TIME                          STAT(DODGING_RAMP_TIME, this)
11 #define PHYS_DODGING_UP_SPEED                           STAT(DODGING_UP_SPEED, this)
12 #define PHYS_DODGING_WALL                                       STAT(DODGING_WALL, this)
13 #define PHYS_DODGING_PRESSED_KEYS(s)            (s).pressedkeys
14
15 #ifdef CSQC
16         #define PHYS_DODGING_FRAMETIME                          (1 / (frametime <= 0 ? 60 : frametime))
17         #define PHYS_DODGING_TIMEOUT(s)                         STAT(DODGING_TIMEOUT)
18 #elif defined(SVQC)
19         #define PHYS_DODGING_FRAMETIME                          sys_frametime
20         #define PHYS_DODGING_TIMEOUT(s)                         s.cvar_cl_dodging_timeout
21
22
23 #endif
24
25 #ifdef SVQC
26
27 bool autocvar_sv_dodging_sound;
28
29 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
30 .float dodging_action;
31
32 // the jump part of the dodge cannot be ramped
33 .float dodging_single_action;
34
35 #include "../../../animdecide.qh"
36 #include "../../../physics.qh"
37
38 .float cvar_cl_dodging_timeout = _STAT(DODGING_TIMEOUT);
39
40 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
41 {
42         // this just turns on the cvar.
43         MUTATOR_ONADD
44         {
45                 g_dodging = cvar("g_dodging");
46         }
47
48         // this just turns off the cvar.
49         MUTATOR_ONROLLBACK_OR_REMOVE
50         {
51                 g_dodging = 0;
52         }
53
54         return false;
55 }
56
57 #endif
58
59 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
60 .float dodging_action;
61
62 // the jump part of the dodge cannot be ramped
63 .float dodging_single_action;
64
65
66 // these are used to store the last key press time for each of the keys..
67 .float last_FORWARD_KEY_time;
68 .float last_BACKWARD_KEY_time;
69 .float last_LEFT_KEY_time;
70 .float last_RIGHT_KEY_time;
71
72 // these store the movement direction at the time of the dodge action happening.
73 .vector dodging_direction;
74
75 // this indicates the last time a dodge was executed. used to check if another one is allowed
76 // and to ramp up the dodge acceleration in the physics hook.
77 .float last_dodging_time;
78
79 // This is the velocity gain to be added over the ramp time.
80 // It will decrease from frame to frame during dodging_action = 1
81 // until it's 0.
82 .float dodging_velocity_gain;
83
84 #ifdef CSQC
85 .int pressedkeys;
86 #endif
87
88 // returns 1 if the player is close to a wall
89 bool check_close_to_wall(float threshold)
90 {SELFPARAM();
91         if (PHYS_DODGING_WALL == 0) { return false; }
92
93         #define X(OFFSET)                                                                                                                               \
94         tracebox(self.origin, self.mins, self.maxs, self.origin + OFFSET, true, self);  \
95         if (trace_fraction < 1 && vlen (self.origin - trace_endpos) < threshold)                \
96                 return true;
97         X(1000*v_right);
98         X(-1000*v_right);
99         X(1000*v_forward);
100         X(-1000*v_forward);
101         #undef X
102
103         return false;
104 }
105
106 bool check_close_to_ground(float threshold)
107 {SELFPARAM();
108         return IS_ONGROUND(self) ? true : false;
109 }
110
111 float PM_dodging_checkpressedkeys()
112 {SELFPARAM();
113         if(!PHYS_DODGING)
114                 return false;
115
116         float frozen_dodging = (PHYS_FROZEN(self) && PHYS_DODGING_FROZEN);
117         float frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
118
119         // first check if the last dodge is far enough back in time so we can dodge again
120         if ((time - self.last_dodging_time) < PHYS_DODGING_DELAY)
121                 return false;
122
123         makevectors(self.angles);
124
125         if (check_close_to_ground(PHYS_DODGING_HEIGHT_THRESHOLD) != 1
126                 && check_close_to_wall(PHYS_DODGING_DISTANCE_THRESHOLD) != 1)
127                 return true;
128
129         float tap_direction_x = 0;
130         float tap_direction_y = 0;
131         float dodge_detected = 0;
132
133         #define X(COND,BTN,RESULT)                                                                                                                      \
134         if (self.movement_##COND)                                                                                               \
135                 /* is this a state change? */                                                                                                   \
136                 if(!(PHYS_DODGING_PRESSED_KEYS(self) & KEY_##BTN) || frozen_no_doubletap) {             \
137                                 tap_direction_##RESULT;                                                                                                 \
138                                 if ((time - self.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(self))   \
139                                         dodge_detected = 1;                                                                                                     \
140                                 self.last_##BTN##_KEY_time = time;                                                                              \
141                 }
142         X(x < 0, BACKWARD,      x--);
143         X(x > 0, FORWARD,       x++);
144         X(y < 0, LEFT,          y--);
145         X(y > 0, RIGHT,         y++);
146         #undef X
147
148         if (dodge_detected == 1)
149         {
150                 self.last_dodging_time = time;
151
152                 self.dodging_action = 1;
153                 self.dodging_single_action = 1;
154
155                 self.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
156
157                 self.dodging_direction_x = tap_direction_x;
158                 self.dodging_direction_y = tap_direction_y;
159
160                 // normalize the dodging_direction vector.. (unlike UT99) XD
161                 float length = self.dodging_direction_x * self.dodging_direction_x
162                                         + self.dodging_direction_y * self.dodging_direction_y;
163                 length = sqrt(length);
164
165                 self.dodging_direction_x = self.dodging_direction_x * 1.0 / length;
166                 self.dodging_direction_y = self.dodging_direction_y * 1.0 / length;
167                 return true;
168         }
169         return false;
170 }
171
172 void PM_dodging()
173 {SELFPARAM();
174         if (!PHYS_DODGING)
175                 return;
176
177     if (PHYS_DEAD(self))
178         return;
179
180         // when swimming, no dodging allowed..
181         if (self.waterlevel >= WATERLEVEL_SWIMMING)
182         {
183                 self.dodging_action = 0;
184                 self.dodging_direction_x = 0;
185                 self.dodging_direction_y = 0;
186                 return;
187         }
188
189         // make sure v_up, v_right and v_forward are sane
190         makevectors(self.angles);
191
192         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
193         // will be called ramp_time/frametime times = 2 times. so, we need to
194         // add 0.5 * the total speed each frame until the dodge action is done..
195         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
196
197         // if ramp time is smaller than frametime we get problems ;D
198         common_factor = min(common_factor, 1);
199
200         float horiz_speed = PHYS_FROZEN(self) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
201         float new_velocity_gain = self.dodging_velocity_gain - (common_factor * horiz_speed);
202         new_velocity_gain = max(0, new_velocity_gain);
203
204         float velocity_difference = self.dodging_velocity_gain - new_velocity_gain;
205
206         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
207         if (self.dodging_action == 1)
208         {
209                 //disable jump key during dodge accel phase
210                 if(self.movement_z > 0) { self.movement_z = 0; }
211
212                 self.velocity += ((self.dodging_direction_y * velocity_difference) * v_right)
213                                         + ((self.dodging_direction_x * velocity_difference) * v_forward);
214
215                 self.dodging_velocity_gain = self.dodging_velocity_gain - velocity_difference;
216         }
217
218         // the up part of the dodge is a single shot action
219         if (self.dodging_single_action == 1)
220         {
221                 UNSET_ONGROUND(self);
222
223                 self.velocity += PHYS_DODGING_UP_SPEED * v_up;
224
225 #ifdef SVQC
226                 if (autocvar_sv_dodging_sound)
227                         PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
228
229                 animdecide_setaction(self, ANIMACTION_JUMP, true);
230 #endif
231
232                 self.dodging_single_action = 0;
233         }
234
235         // are we done with the dodging ramp yet?
236         if((self.dodging_action == 1) && ((time - self.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
237         {
238                 // reset state so next dodge can be done correctly
239                 self.dodging_action = 0;
240                 self.dodging_direction_x = 0;
241                 self.dodging_direction_y = 0;
242         }
243 }
244
245 #ifdef SVQC
246
247 MUTATOR_HOOKFUNCTION(dodging, GetCvars)
248 {
249         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_dodging_timeout, "cl_dodging_timeout");
250         return false;
251 }
252
253 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
254 {
255         // print("dodging_PlayerPhysics\n");
256         PM_dodging();
257
258         return false;
259 }
260
261 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
262 {
263         PM_dodging_checkpressedkeys();
264
265         return false;
266 }
267
268 #endif
269
270 #endif