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