]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/sv_dodging.qc
from fixme to todo
[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                        autocvar_sv_dodging_horiz_speed
11 #define PHYS_DODGING_HORIZ_SPEED_FROZEN         autocvar_sv_dodging_horiz_speed_frozen
12 #define PHYS_DODGING_RAMP_TIME                          autocvar_sv_dodging_ramp_time
13 #define PHYS_DODGING_UP_SPEED                           autocvar_sv_dodging_up_speed
14 #define PHYS_DODGING_WALL                                       autocvar_sv_dodging_wall_dodging
15 #define PHYS_DODGING_AIR                                        autocvar_sv_dodging_air_dodging
16 #define PHYS_DODGING_MAXSPEED                           autocvar_sv_dodging_maxspeed // TODO separate max air speed, fix frozen dodging
17
18 // we ran out of stats slots! TODO: re-enable this when prediction is available for dodging
19 #if 0
20 #define PHYS_DODGING                                            STAT(DODGING, this)
21 #define PHYS_DODGING_DELAY                                      STAT(DODGING_DELAY, this)
22 #define PHYS_DODGING_DISTANCE_THRESHOLD         STAT(DODGING_DISTANCE_THRESHOLD, this)
23 #define PHYS_DODGING_FROZEN_DOUBLETAP           STAT(DODGING_FROZEN_DOUBLETAP, this)
24 #define PHYS_DODGING_HEIGHT_THRESHOLD           STAT(DODGING_HEIGHT_THRESHOLD, this)
25 #define PHYS_DODGING_HORIZ_SPEED                        STAT(DODGING_HORIZ_SPEED, this)
26 #define PHYS_DODGING_HORIZ_SPEED_FROZEN         STAT(DODGING_HORIZ_SPEED_FROZEN, this)
27 #define PHYS_DODGING_RAMP_TIME                          STAT(DODGING_RAMP_TIME, this)
28 #define PHYS_DODGING_UP_SPEED                           STAT(DODGING_UP_SPEED, this)
29 #define PHYS_DODGING_WALL                                       STAT(DODGING_WALL, this)
30 #define PHYS_DODGING_AIR                                        STAT(DODGING_AIR, this)
31 #define PHYS_DODGING_MAXSPEED                           STAT(DODGING_MAXSPEED, this)
32 #endif
33
34 #ifdef CSQC
35         #define PHYS_DODGING_FRAMETIME                          (1 / (frametime <= 0 ? 60 : frametime))
36         #define PHYS_DODGING_TIMEOUT(s)                         STAT(DODGING_TIMEOUT)
37         #define PHYS_DODGING_PRESSED_KEYS(s)            (s).pressedkeys
38 #elif defined(SVQC)
39         #define PHYS_DODGING_FRAMETIME                          sys_frametime
40         #define PHYS_DODGING_TIMEOUT(s)                         CS(s).cvar_cl_dodging_timeout
41         #define PHYS_DODGING_PRESSED_KEYS(s)            CS(s).pressedkeys
42 #endif
43
44 #ifdef SVQC
45
46 bool autocvar_sv_dodging_sound;
47
48 #include <common/animdecide.qh>
49 #include <common/physics/player.qh>
50
51 .float cvar_cl_dodging_timeout;
52
53 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
54 {
55         // this just turns on the cvar.
56         MUTATOR_ONADD
57         {
58                 g_dodging = cvar("g_dodging");
59         }
60
61         // this just turns off the cvar.
62         MUTATOR_ONROLLBACK_OR_REMOVE
63         {
64                 g_dodging = 0;
65         }
66
67         return false;
68 }
69
70 #elif defined(CSQC)
71 REGISTER_MUTATOR(dodging, true);
72 #endif
73
74 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
75 .float dodging_action;
76
77 // the jump part of the dodge cannot be ramped
78 .float dodging_single_action;
79
80 // these are used to store the last key press time for each of the keys..
81 .float last_FORWARD_KEY_time;
82 .float last_BACKWARD_KEY_time;
83 .float last_LEFT_KEY_time;
84 .float last_RIGHT_KEY_time;
85
86 // these store the movement direction at the time of the dodge action happening.
87 .vector dodging_direction;
88
89 // this indicates the last time a dodge was executed. used to check if another one is allowed
90 // and to ramp up the dodge acceleration in the physics hook.
91 .float last_dodging_time;
92
93 // This is the velocity gain to be added over the ramp time.
94 // It will decrease from frame to frame during dodging_action = 1
95 // until it's 0.
96 //.float dodging_velocity_gain;
97
98 .float dodging_force_total;
99 .float dodging_force_remaining;
100
101 #ifdef CSQC // TODO what is this? 1) CSQC in sv_ file never even gets compiled 2) pressedkeys is only defined on server so this would be always 0
102 .int pressedkeys;
103 #endif
104
105 #define X(dir) \
106         tracebox(this.origin, this.mins, this.maxs, this.origin + threshold * dir, true, this); \
107         if (trace_fraction < 1 && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) \
108                 return true;
109
110 // returns true if the player is close to a wall
111 bool is_close_to_wall(entity this, float threshold)
112 {
113         X(v_right);
114         X(-v_right);
115         X(v_forward);
116         X(-v_forward);
117
118         return false;
119 }
120
121 bool is_close_to_ground(entity this, float threshold)
122 {
123         if (IS_ONGROUND(this)) return true;
124         X(-v_up); // necessary for dodging down a slope using doubletap (using `+dodge` works anyway)
125
126         return false;
127 }
128
129 #undef X
130
131 // TODO use real cvars
132 /*float autocvar_velocity_min = 200;
133 float autocvar_velocity_max = 700;
134 float autocvar_force_min = 50;
135 float autocvar_force_max = 350;*/
136 float determine_speed(entity player) {
137         float x = PHYS_FROZEN(player) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
138         x = x; // unused
139         return x;
140
141         /*float horiz_vel = vlen(vec2(player.velocity));
142         // force min and max are inverted - the faster you are the wekaer dodging becomes
143         // TODO document cvars in cfg
144         return map_bound_ranges(horiz_vel, autocvar_velocity_min, autocvar_velocity_max, autocvar_force_max, autocvar_force_min);*/
145 }
146
147 bool PM_dodging_checkpressedkeys(entity this)
148 {
149         // first check if the last dodge is far enough back in time so we can dodge again
150         if ((time - this.last_dodging_time) < PHYS_DODGING_DELAY)
151                 return false;
152
153         bool frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN(this));
154         bool frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_DOUBLETAP);
155
156         float tap_direction_x = 0;
157         float tap_direction_y = 0;
158         bool dodge_detected = false;
159         vector mymovement = PHYS_CS(this).movement;
160
161         #define X(COND,BTN,RESULT)                                                                                                                      \
162         if (mymovement_##COND)                                                                                          \
163                 /* is this a state change? */                                                                                                   \
164                 if(!(PHYS_DODGING_PRESSED_KEYS(this) & KEY_##BTN) || frozen_no_doubletap) {             \
165                         tap_direction_##RESULT;                                                                                                 \
166                         if ((time - this.last_##BTN##_KEY_time) < PHYS_DODGING_TIMEOUT(this) || frozen_no_doubletap)    \
167                                 dodge_detected = true;                                                                                          \
168                         if(PHYS_INPUT_BUTTON_DODGE(this))                                                                               \
169                                 dodge_detected = true;                                                                                          \
170                         this.last_##BTN##_KEY_time = time;                                                                              \
171                 }
172         X(x < 0, BACKWARD,      x--);
173         X(x > 0, FORWARD,       x++);
174         X(y < 0, LEFT,          y--);
175         X(y > 0, RIGHT,         y++);
176         #undef X
177
178         if (!dodge_detected) return false;
179
180         makevectors(this.angles);
181
182         bool wall_dodge = false;
183
184         if(!PHYS_DODGING_AIR)
185         if(!is_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD))
186         {
187                 wall_dodge = (PHYS_DODGING_WALL && is_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD));
188                 if(!wall_dodge) // we're not on the ground, and wall dodging isn't allowed, end it!
189                         return false;
190         }
191
192         if(!wall_dodge && PHYS_DODGING_MAXSPEED && vdist(this.velocity, >, PHYS_DODGING_MAXSPEED))
193                 return false;
194
195         this.last_dodging_time = time;
196
197         this.dodging_action = 1;
198         this.dodging_single_action = 1;
199
200         //this.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
201         this.dodging_force_total = determine_speed(this);
202         this.dodging_force_remaining = this.dodging_force_total;
203
204         this.dodging_direction.x = tap_direction_x;
205         this.dodging_direction.y = tap_direction_y;
206
207         // normalize the dodging_direction vector.. (unlike UT99) XD
208         float length = sqrt(this.dodging_direction.x ** 2 + this.dodging_direction.y ** 2);
209
210         this.dodging_direction_x = this.dodging_direction_x / length;
211         this.dodging_direction_y = this.dodging_direction_y / length;
212
213         return true;
214 }
215
216 void PM_dodging(entity this)
217 {
218         // can't use return value from PM_dodging_checkpressedkeys because they're called from different hooks
219         if (!this.dodging_action) return;
220
221         // when swimming or dead, no dodging allowed..
222         if (this.waterlevel >= WATERLEVEL_SWIMMING || IS_DEAD(this))
223         {
224                 this.dodging_action = 0;
225                 this.dodging_direction_x = 0;
226                 this.dodging_direction_y = 0;
227                 return;
228         }
229
230         // make sure v_up, v_right and v_forward are sane
231         if(PHYS_DODGING_AIR)
232                 makevectors(this.v_angle);
233         else
234                 makevectors(this.angles);
235
236         // fraction of the force to apply each frame
237         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
238         // will be called ramp_time/frametime times = 2 times. so, we need to
239         // add 0.5 * the total speed each frame until the dodge action is done..
240         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
241         common_factor /= 2; // FIXME this is WRONG but this function is called twice per frame!!!
242
243         // if ramp time is smaller than frametime we get problems ;D
244         common_factor = min(common_factor, 1);
245
246         /*float horiz_speed = determine_speed(this); // TODO kill this
247         //LOG_INFOF("velocity %f -> force %f\n", vlen(vec2(this.velocity)), horiz_speed);
248         float new_velocity_gain = this.dodging_velocity_gain - (common_factor * horiz_speed);
249         new_velocity_gain = max(0, new_velocity_gain);
250
251         float velocity_difference = this.dodging_velocity_gain - new_velocity_gain;*/
252
253         /*float velocity_increase = min(common_factor * this.dodging_force_total, this.dodging_force_remaining;
254         this.dodging_force_remaining -= velocity_increase;*/
255
256         /*this.velocity += ((this.dodging_direction_y * velocity_difference) * v_right)
257                                 + ((this.dodging_direction_x * velocity_difference) * v_forward);
258
259         this.dodging_velocity_gain = this.dodging_velocity_gain - velocity_difference;*/
260
261         float velocity_increase = common_factor * this.dodging_force_total;
262         LOG_INFOF("time %f velocity_increase: %f\n", time, velocity_increase);
263         this.velocity += this.dodging_direction_x * velocity_increase * v_forward
264                        + this.dodging_direction_y * velocity_increase * v_right;
265
266         // the up part of the dodge is a single shot action
267         if (this.dodging_single_action == 1)
268         {
269                 UNSET_ONGROUND(this);
270
271                 this.velocity += PHYS_DODGING_UP_SPEED * v_up;
272
273 #ifdef SVQC
274                 if (autocvar_sv_dodging_sound)
275                         PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
276
277                 animdecide_setaction(this, ANIMACTION_JUMP, true);
278 #endif
279
280                 this.dodging_single_action = 0;
281         }
282
283         // are we done with the dodging ramp yet?
284         if((time - this.last_dodging_time) > PHYS_DODGING_RAMP_TIME)
285         {
286                 // reset state so next dodge can be done correctly
287                 this.dodging_action = 0;
288                 this.dodging_direction.x = 0;
289                 this.dodging_direction.y = 0;
290         }
291 }
292
293 #ifdef CSQC
294 void PM_dodging_GetPressedKeys(entity this)
295 {
296         PM_dodging_checkpressedkeys(this);
297
298         int keys = this.pressedkeys;
299         keys = BITSET(keys, KEY_FORWARD,        PHYS_CS(this).movement.x > 0);
300         keys = BITSET(keys, KEY_BACKWARD,       PHYS_CS(this).movement.x < 0);
301         keys = BITSET(keys, KEY_RIGHT,          PHYS_CS(this).movement.y > 0);
302         keys = BITSET(keys, KEY_LEFT,           PHYS_CS(this).movement.y < 0);
303
304         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
305         keys = BITSET(keys, KEY_CROUCH,         PHYS_INPUT_BUTTON_CROUCH(this));
306         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
307         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
308         this.pressedkeys = keys;
309 }
310 #endif
311
312 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
313 {
314         entity player = M_ARGV(0, entity);
315         //LOG_INFOF("player %s, physics %f\n", player.netname, time);
316
317 #ifdef CSQC
318         PM_dodging_GetPressedKeys(player);
319 #endif
320         PM_dodging(player);
321 }
322
323 #ifdef SVQC
324
325 REPLICATE(cvar_cl_dodging_timeout, float, "cl_dodging_timeout");
326
327 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
328 {
329         entity player = M_ARGV(0, entity);
330         //LOG_INFOF("player %s, keys %f\n", player.netname, time);
331
332         PM_dodging_checkpressedkeys(player);
333 }
334
335 #endif