]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/sv_dodging.qc
declare vars explicitly
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / dodging / sv_dodging.qc
1 #include "sv_dodging.qh"
2
3 // TODO the CSQC blocks in this sv_ file are currently not compiled but will be when dodging prediction gets enabled
4
5 #define PHYS_DODGING                                            g_dodging
6 #define PHYS_DODGING_DELAY                                      autocvar_sv_dodging_delay
7 #define PHYS_DODGING_DISTANCE_THRESHOLD         autocvar_sv_dodging_wall_distance_threshold
8 #define PHYS_DODGING_FROZEN_DOUBLETAP           autocvar_sv_dodging_frozen_doubletap
9 #define PHYS_DODGING_HEIGHT_THRESHOLD           autocvar_sv_dodging_height_threshold
10 #define PHYS_DODGING_HORIZ_SPEED_MIN            autocvar_sv_dodging_horiz_speed_min
11 #define PHYS_DODGING_HORIZ_SPEED_MAX            autocvar_sv_dodging_horiz_speed_max
12 #define PHYS_DODGING_HORIZ_FORCE_SLOWEST        autocvar_sv_dodging_horiz_force_slowest
13 #define PHYS_DODGING_HORIZ_FORCE_FASTEST        autocvar_sv_dodging_horiz_force_fastest
14 #define PHYS_DODGING_HORIZ_FORCE_FROZEN         autocvar_sv_dodging_horiz_force_frozen
15 #define PHYS_DODGING_RAMP_TIME                          autocvar_sv_dodging_ramp_time
16 #define PHYS_DODGING_UP_SPEED                           autocvar_sv_dodging_up_speed
17 #define PHYS_DODGING_WALL                                       autocvar_sv_dodging_wall_dodging
18 #define PHYS_DODGING_AIR                                        autocvar_sv_dodging_air_dodging
19 #define PHYS_DODGING_MAXSPEED                           autocvar_sv_dodging_maxspeed
20 #define PHYS_DODGING_AIR_MAXSPEED                       autocvar_sv_dodging_air_maxspeed
21
22 // we ran out of stats slots! TODO: re-enable this when prediction is available for dodging
23 #if 0
24 #define PHYS_DODGING                                            STAT(DODGING, this)
25 #define PHYS_DODGING_DELAY                                      STAT(DODGING_DELAY, this)
26 #define PHYS_DODGING_DISTANCE_THRESHOLD         STAT(DODGING_DISTANCE_THRESHOLD, this)
27 #define PHYS_DODGING_FROZEN_DOUBLETAP           STAT(DODGING_FROZEN_DOUBLETAP, this)
28 #define PHYS_DODGING_HEIGHT_THRESHOLD           STAT(DODGING_HEIGHT_THRESHOLD, this)
29 #define PHYS_DODGING_HORIZ_SPEED_MIN            STAT(DODGING_HORIZ_SPEED_MIN, this)
30 #define PHYS_DODGING_HORIZ_SPEED_MAX            STAT(DODGING_HORIZ_SPEED_MAX, this)
31 #define PHYS_DODGING_HORIZ_FORCE_SLOWEST        STAT(DODGING_HORIZ_FORCE_SLOWEST, this)
32 #define PHYS_DODGING_HORIZ_FORCE_FASTEST        STAT(DODGING_HORIZ_FORCE_FASTEST, this)
33 #define PHYS_DODGING_HORIZ_FORCE_FROZEN         STAT(DODGING_HORIZ_FORCE_FROZEN, this)
34 #define PHYS_DODGING_RAMP_TIME                          STAT(DODGING_RAMP_TIME, this)
35 #define PHYS_DODGING_UP_SPEED                           STAT(DODGING_UP_SPEED, this)
36 #define PHYS_DODGING_WALL                                       STAT(DODGING_WALL, this)
37 #define PHYS_DODGING_AIR                                        STAT(DODGING_AIR, this)
38 #define PHYS_DODGING_MAXSPEED                           STAT(DODGING_MAXSPEED, this)
39 #define PHYS_DODGING_AIR_MAXSPEED                       STAT(DODGING_AIR_MAXSPEED, this)
40 #endif
41
42 #ifdef CSQC
43         #define PHYS_DODGING_FRAMETIME                          (1 / (frametime <= 0 ? 60 : frametime))
44         #define PHYS_DODGING_TIMEOUT(s)                         STAT(DODGING_TIMEOUT)
45         #define PHYS_DODGING_PRESSED_KEYS(s)            (s).pressedkeys
46 #elif defined(SVQC)
47         #define PHYS_DODGING_FRAMETIME                          sys_frametime
48         #define PHYS_DODGING_TIMEOUT(s)                         CS(s).cvar_cl_dodging_timeout
49         #define PHYS_DODGING_PRESSED_KEYS(s)            CS(s).pressedkeys
50 #endif
51
52 #ifdef SVQC
53
54 bool autocvar_sv_dodging_sound;
55
56 #include <common/animdecide.qh>
57 #include <common/physics/player.qh>
58
59 .float cvar_cl_dodging_timeout;
60
61 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
62 {
63         // this just turns on the cvar.
64         MUTATOR_ONADD
65         {
66                 g_dodging = cvar("g_dodging");
67         }
68
69         // this just turns off the cvar.
70         MUTATOR_ONROLLBACK_OR_REMOVE
71         {
72                 g_dodging = 0;
73         }
74
75         return false;
76 }
77
78 #elif defined(CSQC)
79 REGISTER_MUTATOR(dodging, true);
80 #endif
81
82 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
83 .float dodging_action;
84
85 // the jump part of the dodge cannot be ramped
86 .float dodging_single_action;
87
88 // these are used to store the last key press time for each of the keys..
89 .float last_FORWARD_KEY_time;
90 .float last_BACKWARD_KEY_time;
91 .float last_LEFT_KEY_time;
92 .float last_RIGHT_KEY_time;
93
94 // these store the movement direction at the time of the dodge action happening.
95 .vector dodging_direction;
96
97 // this indicates the last time a dodge was executed. used to check if another one is allowed
98 // and to ramp up the dodge acceleration in the physics hook.
99 .float last_dodging_time;
100
101 // the total speed that will be added over the ramp time
102 .float dodging_force_total;
103 // the part of total yet to be added
104 .float dodging_force_remaining;
105
106 #ifdef CSQC
107 .int pressedkeys;
108 #endif
109
110 #define X(dir) \
111         tracebox(this.origin, this.mins, this.maxs, this.origin + threshold * dir, true, this); \
112         if (trace_fraction < 1 && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) \
113                 return true;
114
115 // returns true if the player is close to a wall
116 bool is_close_to_wall(entity this, float threshold, vector forward, vector right)
117 {
118         X(right);
119         X(-right);
120         X(forward);
121         X(-forward);
122
123         return false;
124 }
125
126 bool is_close_to_ground(entity this, float threshold, vector up)
127 {
128         if (IS_ONGROUND(this)) return true;
129         X(-up); // necessary for dodging down a slope using doubletap (using `+dodge` works anyway)
130
131         return false;
132 }
133
134 #undef X
135
136 float determine_force(entity player) {
137         if (PHYS_FROZEN(player)) return PHYS_DODGING_HORIZ_FORCE_FROZEN;
138
139         float horiz_vel = vlen(vec2(player.velocity));
140         return map_bound_ranges(horiz_vel,
141                                 PHYS_DODGING_HORIZ_SPEED_MIN, PHYS_DODGING_HORIZ_SPEED_MAX,
142                                 PHYS_DODGING_HORIZ_FORCE_SLOWEST, PHYS_DODGING_HORIZ_FORCE_FASTEST);
143 }
144
145 bool PM_dodging_checkpressedkeys(entity this)
146 {
147         bool frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN(this));
148         bool frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_DOUBLETAP);
149
150         float tap_direction_x = 0;
151         float tap_direction_y = 0;
152         bool dodge_detected = false;
153         vector mymovement = PHYS_CS(this).movement;
154
155         #define X(COND,BTN,RESULT)                                                                                                                                                              \
156         if (mymovement_##COND) {                                                                                                                                                                \
157                 /* is this a state change? */                                                                                                                                           \
158                 if(!(PHYS_DODGING_PRESSED_KEYS(this) & KEY_##BTN) || frozen_no_doubletap) {                                                     \
159                         tap_direction_##RESULT;                                                                                                                                                 \
160                         if ((time - this.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(this) || frozen_no_doubletap) {  \
161                                 dodge_detected = true;                                                                                                                                          \
162                         } else if(PHYS_INPUT_BUTTON_DODGE(this)) {                                                                                                              \
163                                 dodge_detected = true;                                                                                                                                          \
164                         }                                                                                                                                                                                               \
165                         this.last_##BTN##_KEY_time = time;                                                                                                                              \
166                 }                                                                                                                                                                                                       \
167         }
168         X(x < 0, BACKWARD,      x--);
169         X(x > 0, FORWARD,       x++);
170         X(y < 0, LEFT,          y--);
171         X(y > 0, RIGHT,         y++);
172         #undef X
173
174         if (!dodge_detected) return false;
175
176         // this check has to be after checking keys:
177         // the first key press of the double tap is allowed to be before dodging delay,
178         // only the second has to be after, otherwise +dodge gives an advantage because typical repress time is 0.1 s
179         // or higher which means players using +dodge would be able to do it more often
180         if ((time - this.last_dodging_time) < PHYS_DODGING_DELAY)
181                 return false;
182
183         vector forward, right, up;
184         MAKE_VECTORS(this.angles, forward, right, up);
185
186         bool can_dodge = (is_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD, up) && (PHYS_DODGING_MAXSPEED == 0 || vdist(this.velocity, <, PHYS_DODGING_MAXSPEED)));
187         bool can_wall_dodge = (PHYS_DODGING_WALL && is_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD, forward, right));
188         bool can_air_dodge = (PHYS_DODGING_AIR && (PHYS_DODGING_AIR_MAXSPEED == 0 || vdist(this.velocity, <, PHYS_DODGING_AIR_MAXSPEED)));
189         if (!can_dodge && !can_wall_dodge && !can_air_dodge) return false;
190
191         this.last_dodging_time = time;
192
193         this.dodging_action = 1;
194         this.dodging_single_action = 1;
195
196         this.dodging_force_total = determine_force(this);
197         this.dodging_force_remaining = this.dodging_force_total;
198
199         this.dodging_direction.x = tap_direction_x;
200         this.dodging_direction.y = tap_direction_y;
201
202         // normalize the dodging_direction vector.. (unlike UT99) XD
203         float length = sqrt(this.dodging_direction.x ** 2 + this.dodging_direction.y ** 2);
204
205         this.dodging_direction.x = this.dodging_direction.x / length;
206         this.dodging_direction.y = this.dodging_direction.y / length;
207
208         return true;
209 }
210
211 void PM_dodging(entity this)
212 {
213         // can't use return value from PM_dodging_checkpressedkeys because they're called from different hooks
214         if (!this.dodging_action) return;
215
216         // when swimming or dead, no dodging allowed..
217         if (this.waterlevel >= WATERLEVEL_SWIMMING || IS_DEAD(this))
218         {
219                 this.dodging_action = 0;
220                 this.dodging_direction.x = 0;
221                 this.dodging_direction.y = 0;
222                 return;
223         }
224
225         vector forward, right, up;
226         if(PHYS_DODGING_AIR)
227                 MAKE_VECTORS(this.v_angle, forward, right, up);
228         else
229                 MAKE_VECTORS(this.angles, forward, right, up);
230
231         // fraction of the force to apply each frame
232         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
233         // will be called ramp_time/frametime times = 2 times. so, we need to
234         // add 0.5 * the total speed each frame until the dodge action is done..
235         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
236         // NOTE: depending on cl_netfps the client may (and probably will) send more input frames during each server frame
237         // but common_factor uses server frame rate so players with higher cl_netfps will ramp slightly faster
238
239         float velocity_increase = min(common_factor * this.dodging_force_total, this.dodging_force_remaining);
240         this.dodging_force_remaining -= velocity_increase;
241         this.velocity += this.dodging_direction.x * velocity_increase * forward
242                        + this.dodging_direction.y * velocity_increase * right;
243
244         // the up part of the dodge is a single shot action
245         if (this.dodging_single_action == 1)
246         {
247                 UNSET_ONGROUND(this);
248
249                 this.velocity += PHYS_DODGING_UP_SPEED * up;
250
251 #ifdef SVQC
252                 if (autocvar_sv_dodging_sound)
253                         PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
254
255                 animdecide_setaction(this, ANIMACTION_JUMP, true);
256 #endif
257
258                 this.dodging_single_action = 0;
259         }
260
261         if(this.dodging_force_remaining <= 0)
262         {
263                 // reset state so next dodge can be done correctly
264                 this.dodging_action = 0;
265                 this.dodging_direction.x = 0;
266                 this.dodging_direction.y = 0;
267         }
268 }
269
270 #ifdef CSQC
271 void PM_dodging_GetPressedKeys(entity this)
272 {
273         PM_dodging_checkpressedkeys(this);
274
275         int keys = this.pressedkeys;
276         keys = BITSET(keys, KEY_FORWARD,        PHYS_CS(this).movement.x > 0);
277         keys = BITSET(keys, KEY_BACKWARD,       PHYS_CS(this).movement.x < 0);
278         keys = BITSET(keys, KEY_RIGHT,          PHYS_CS(this).movement.y > 0);
279         keys = BITSET(keys, KEY_LEFT,           PHYS_CS(this).movement.y < 0);
280
281         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
282         keys = BITSET(keys, KEY_CROUCH,         PHYS_INPUT_BUTTON_CROUCH(this));
283         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
284         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
285         this.pressedkeys = keys;
286 }
287 #endif
288
289 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
290 {
291         entity player = M_ARGV(0, entity);
292
293 #ifdef CSQC
294         PM_dodging_GetPressedKeys(player);
295 #endif
296         PM_dodging(player);
297 }
298
299 #ifdef SVQC
300
301 REPLICATE(cvar_cl_dodging_timeout, float, "cl_dodging_timeout");
302
303 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
304 {
305         entity player = M_ARGV(0, entity);
306
307         PM_dodging_checkpressedkeys(player);
308 }
309
310 #endif