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