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