]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/sv_dodging.qc
scale dodging force according to currect velocity
[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 // TODO separate max air speed, fix frozen dodging
20
21 // we ran out of stats slots! TODO: re-enable this when prediction is available for dodging
22 #if 0
23 #define PHYS_DODGING                                            STAT(DODGING, this)
24 #define PHYS_DODGING_DELAY                                      STAT(DODGING_DELAY, this)
25 #define PHYS_DODGING_DISTANCE_THRESHOLD         STAT(DODGING_DISTANCE_THRESHOLD, this)
26 #define PHYS_DODGING_FROZEN_DOUBLETAP           STAT(DODGING_FROZEN_DOUBLETAP, this)
27 #define PHYS_DODGING_HEIGHT_THRESHOLD           STAT(DODGING_HEIGHT_THRESHOLD, this)
28 #define PHYS_DODGING_HORIZ_SPEED_MIN            STAT(DODGING_HORIZ_SPEED_MIN, this)
29 #define PHYS_DODGING_HORIZ_SPEED_MAX            STAT(DODGING_HORIZ_SPEED_MAX, this)
30 #define PHYS_DODGING_HORIZ_FORCE_SLOWEST        STAT(DODGING_HORIZ_FORCE_SLOWEST, this)
31 #define PHYS_DODGING_HORIZ_FORCE_FASTEST        STAT(DODGING_HORIZ_FORCE_FASTEST, this)
32 #define PHYS_DODGING_HORIZ_FORCE_FROZEN         STAT(DODGING_HORIZ_FORCE_FROZEN, this)
33 #define PHYS_DODGING_RAMP_TIME                          STAT(DODGING_RAMP_TIME, this)
34 #define PHYS_DODGING_UP_SPEED                           STAT(DODGING_UP_SPEED, this)
35 #define PHYS_DODGING_WALL                                       STAT(DODGING_WALL, this)
36 #define PHYS_DODGING_AIR                                        STAT(DODGING_AIR, this)
37 #define PHYS_DODGING_MAXSPEED                           STAT(DODGING_MAXSPEED, this)
38 #endif
39
40 #ifdef CSQC
41         #define PHYS_DODGING_FRAMETIME                          (1 / (frametime <= 0 ? 60 : frametime))
42         #define PHYS_DODGING_TIMEOUT(s)                         STAT(DODGING_TIMEOUT)
43         #define PHYS_DODGING_PRESSED_KEYS(s)            (s).pressedkeys
44 #elif defined(SVQC)
45         #define PHYS_DODGING_FRAMETIME                          sys_frametime
46         #define PHYS_DODGING_TIMEOUT(s)                         CS(s).cvar_cl_dodging_timeout
47         #define PHYS_DODGING_PRESSED_KEYS(s)            CS(s).pressedkeys
48 #endif
49
50 #ifdef SVQC
51
52 bool autocvar_sv_dodging_sound;
53
54 #include <common/animdecide.qh>
55 #include <common/physics/player.qh>
56
57 .float cvar_cl_dodging_timeout;
58
59 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
60 {
61         // this just turns on the cvar.
62         MUTATOR_ONADD
63         {
64                 g_dodging = cvar("g_dodging");
65         }
66
67         // this just turns off the cvar.
68         MUTATOR_ONROLLBACK_OR_REMOVE
69         {
70                 g_dodging = 0;
71         }
72
73         return false;
74 }
75
76 #elif defined(CSQC)
77 REGISTER_MUTATOR(dodging, true);
78 #endif
79
80 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
81 .float dodging_action;
82
83 // the jump part of the dodge cannot be ramped
84 .float dodging_single_action;
85
86 // these are used to store the last key press time for each of the keys..
87 .float last_FORWARD_KEY_time;
88 .float last_BACKWARD_KEY_time;
89 .float last_LEFT_KEY_time;
90 .float last_RIGHT_KEY_time;
91
92 // these store the movement direction at the time of the dodge action happening.
93 .vector dodging_direction;
94
95 // this indicates the last time a dodge was executed. used to check if another one is allowed
96 // and to ramp up the dodge acceleration in the physics hook.
97 .float last_dodging_time;
98
99 // the total speed that will be added over the ramp time
100 .float dodging_force_total;
101 // the part of total yet to be added
102 .float dodging_force_remaining;
103
104 #ifdef CSQC
105 .int pressedkeys;
106 #endif
107
108 #define X(dir) \
109         tracebox(this.origin, this.mins, this.maxs, this.origin + threshold * dir, true, this); \
110         if (trace_fraction < 1 && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) \
111                 return true;
112
113 // returns true if the player is close to a wall
114 bool is_close_to_wall(entity this, float threshold)
115 {
116         X(v_right);
117         X(-v_right);
118         X(v_forward);
119         X(-v_forward);
120
121         return false;
122 }
123
124 bool is_close_to_ground(entity this, float threshold)
125 {
126         if (IS_ONGROUND(this)) return true;
127         X(-v_up); // necessary for dodging down a slope using doubletap (using `+dodge` works anyway)
128
129         return false;
130 }
131
132 #undef X
133
134 float determine_force(entity player) {
135         if (PHYS_FROZEN(player)) return PHYS_DODGING_HORIZ_FORCE_FROZEN;
136
137         float horiz_vel = vlen(vec2(player.velocity));
138         return map_bound_ranges(horiz_vel,
139                                 PHYS_DODGING_HORIZ_SPEED_MIN, PHYS_DODGING_HORIZ_SPEED_MAX,
140                                 PHYS_DODGING_HORIZ_FORCE_SLOWEST, PHYS_DODGING_HORIZ_FORCE_FASTEST);
141 }
142
143 bool PM_dodging_checkpressedkeys(entity this)
144 {
145         // first check if the last dodge is far enough back in time so we can dodge again
146         if ((time - this.last_dodging_time) < PHYS_DODGING_DELAY)
147                 return false;
148
149         bool frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN(this));
150         bool frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_DOUBLETAP);
151
152         float tap_direction_x = 0;
153         float tap_direction_y = 0;
154         bool dodge_detected = false;
155         vector mymovement = PHYS_CS(this).movement;
156
157         #define X(COND,BTN,RESULT)                                                                                                                      \
158         if (mymovement_##COND)                                                                                          \
159                 /* is this a state change? */                                                                                                   \
160                 if(!(PHYS_DODGING_PRESSED_KEYS(this) & KEY_##BTN) || frozen_no_doubletap) {             \
161                         tap_direction_##RESULT;                                                                                                 \
162                         if ((time - this.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(this) || frozen_no_doubletap)    \
163                                 dodge_detected = true;                                                                                          \
164                         if(PHYS_INPUT_BUTTON_DODGE(this))                                                                               \
165                                 dodge_detected = true;                                                                                          \
166                         this.last_##BTN##_KEY_time = time;                                                                              \
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         makevectors(this.angles);
177
178         bool wall_dodge = false;
179
180         if(!PHYS_DODGING_AIR)
181         if(!is_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD)) // TODO first check wall wallldodging - this fails for sloped walls
182         {
183                 wall_dodge = (PHYS_DODGING_WALL && is_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD));
184                 if(!wall_dodge) // we're not on the ground, and wall dodging isn't allowed, end it!
185                         return false;
186         }
187
188         if(!wall_dodge && PHYS_DODGING_MAXSPEED && vdist(this.velocity, >, PHYS_DODGING_MAXSPEED))
189                 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         // make sure v_up, v_right and v_forward are sane
226         if(PHYS_DODGING_AIR)
227                 makevectors(this.v_angle);
228         else
229                 makevectors(this.angles);
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         //common_factor /= 2; // FIXME this is WRONG but this function is called twice per frame!!!
237
238         // if ramp time is smaller than frametime we get problems ;D
239         common_factor = min(common_factor, 1); // TODO necessary with dodging_force_remaining?
240
241         float velocity_increase = min(common_factor * this.dodging_force_total, this.dodging_force_remaining);
242         this.dodging_force_remaining -= velocity_increase;
243         LOG_INFOF("time %f velocity_increase: %f\n", time, velocity_increase);
244         this.velocity += this.dodging_direction.x * velocity_increase * v_forward
245                        + this.dodging_direction.y * velocity_increase * v_right;
246
247         // the up part of the dodge is a single shot action
248         if (this.dodging_single_action == 1)
249         {
250                 UNSET_ONGROUND(this);
251
252                 this.velocity += PHYS_DODGING_UP_SPEED * v_up;
253
254 #ifdef SVQC
255                 if (autocvar_sv_dodging_sound)
256                         PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
257
258                 animdecide_setaction(this, ANIMACTION_JUMP, true);
259 #endif
260
261                 this.dodging_single_action = 0;
262         }
263
264         if(this.dodging_force_remaining <= 0)
265         {
266                 // reset state so next dodge can be done correctly
267                 this.dodging_action = 0;
268                 this.dodging_direction.x = 0;
269                 this.dodging_direction.y = 0;
270         }
271 }
272
273 #ifdef CSQC
274 void PM_dodging_GetPressedKeys(entity this)
275 {
276         PM_dodging_checkpressedkeys(this);
277
278         int keys = this.pressedkeys;
279         keys = BITSET(keys, KEY_FORWARD,        PHYS_CS(this).movement.x > 0);
280         keys = BITSET(keys, KEY_BACKWARD,       PHYS_CS(this).movement.x < 0);
281         keys = BITSET(keys, KEY_RIGHT,          PHYS_CS(this).movement.y > 0);
282         keys = BITSET(keys, KEY_LEFT,           PHYS_CS(this).movement.y < 0);
283
284         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
285         keys = BITSET(keys, KEY_CROUCH,         PHYS_INPUT_BUTTON_CROUCH(this));
286         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
287         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
288         this.pressedkeys = keys;
289 }
290 #endif
291
292 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
293 {
294         entity player = M_ARGV(0, entity);
295         //LOG_INFOF("player %s, physics %f\n", player.netname, time);
296
297 #ifdef CSQC
298         PM_dodging_GetPressedKeys(player);
299 #endif
300         PM_dodging(player);
301 }
302
303 #ifdef SVQC
304
305 REPLICATE(cvar_cl_dodging_timeout, float, "cl_dodging_timeout");
306
307 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
308 {
309         entity player = M_ARGV(0, entity);
310         //LOG_INFOF("player %s, keys %f\n", player.netname, time);
311
312         PM_dodging_checkpressedkeys(player);
313 }
314
315 #endif