]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/dodging/sv_dodging.qc
Merge branch 'Mario/addtolist_cleanup' into 'master'
[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_NODOUBLETAP         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_NODOUBLETAP         STAT(DODGING_FROZEN_NO_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)                         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 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
47 .float dodging_action;
48
49 // the jump part of the dodge cannot be ramped
50 .float dodging_single_action;
51
52 #include <common/animdecide.qh>
53 #include <common/physics/player.qh>
54
55 .float cvar_cl_dodging_timeout = _STAT(DODGING_TIMEOUT);
56
57 REGISTER_MUTATOR(dodging, cvar("g_dodging"))
58 {
59         // this just turns on the cvar.
60         MUTATOR_ONADD
61         {
62                 g_dodging = cvar("g_dodging");
63         }
64
65         // this just turns off the cvar.
66         MUTATOR_ONROLLBACK_OR_REMOVE
67         {
68                 g_dodging = 0;
69         }
70
71         return false;
72 }
73
74 #elif defined(CSQC)
75 REGISTER_MUTATOR(dodging, true);
76 #endif
77
78 // set to 1 to indicate dodging has started.. reset by physics hook after dodge has been done..
79 .float dodging_action;
80
81 // the jump part of the dodge cannot be ramped
82 .float dodging_single_action;
83
84
85 // these are used to store the last key press time for each of the keys..
86 .float last_FORWARD_KEY_time;
87 .float last_BACKWARD_KEY_time;
88 .float last_LEFT_KEY_time;
89 .float last_RIGHT_KEY_time;
90
91 // these store the movement direction at the time of the dodge action happening.
92 .vector dodging_direction;
93
94 // this indicates the last time a dodge was executed. used to check if another one is allowed
95 // and to ramp up the dodge acceleration in the physics hook.
96 .float last_dodging_time;
97
98 // This is the velocity gain to be added over the ramp time.
99 // It will decrease from frame to frame during dodging_action = 1
100 // until it's 0.
101 .float dodging_velocity_gain;
102
103 #ifdef CSQC
104 .int pressedkeys;
105 #endif
106
107 // returns 1 if the player is close to a wall
108 bool check_close_to_wall(entity this, float threshold)
109 {
110         if (PHYS_DODGING_WALL == 0) { return false; }
111
112 #define X(OFFSET) \
113         tracebox(this.origin, this.mins, this.maxs, this.origin + OFFSET, true, this); \
114         if(trace_fraction < 1 && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY) && vdist(this.origin - trace_endpos, <, threshold)) \
115                 return true;
116         X(1000*v_right);
117         X(-1000*v_right);
118         X(1000*v_forward);
119         X(-1000*v_forward);
120 #undef X
121
122         return false;
123 }
124
125 bool check_close_to_ground(entity this, float threshold)
126 {
127         return IS_ONGROUND(this) ? true : false;
128 }
129
130 bool PM_dodging_checkpressedkeys(entity this)
131 {
132         if(!PHYS_DODGING)
133                 return false;
134
135         bool frozen_dodging = (PHYS_FROZEN(this) && PHYS_DODGING_FROZEN(this));
136         bool frozen_no_doubletap = (frozen_dodging && !PHYS_DODGING_FROZEN_NODOUBLETAP);
137
138         // first check if the last dodge is far enough back in time so we can dodge again
139         if ((time - this.last_dodging_time) < PHYS_DODGING_DELAY)
140                 return false;
141
142         makevectors(this.angles);
143
144         bool wall_dodge = false;
145
146         if(!PHYS_DODGING_AIR)
147         if(!check_close_to_ground(this, PHYS_DODGING_HEIGHT_THRESHOLD))
148         {
149                 wall_dodge = check_close_to_wall(this, PHYS_DODGING_DISTANCE_THRESHOLD);
150                 if(!wall_dodge) // we're not on the ground, and wall dodging isn't allowed, end it!
151                         return true;
152         }
153
154         if(!wall_dodge && PHYS_DODGING_MAXSPEED && vdist(this.velocity, >, PHYS_DODGING_MAXSPEED))
155                 return false;
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                                 if(PHYS_INPUT_BUTTON_DODGE(this))                                                                               \
170                                         dodge_detected = true;                                                                                          \
171                                 this.last_##BTN##_KEY_time = time;                                                                              \
172                 }
173         X(x < 0, BACKWARD,      x--);
174         X(x > 0, FORWARD,       x++);
175         X(y < 0, LEFT,          y--);
176         X(y > 0, RIGHT,         y++);
177         #undef X
178
179         if (dodge_detected)
180         {
181                 this.last_dodging_time = time;
182
183                 this.dodging_action = 1;
184                 this.dodging_single_action = 1;
185
186                 this.dodging_velocity_gain = PHYS_DODGING_HORIZ_SPEED;
187
188                 this.dodging_direction_x = tap_direction_x;
189                 this.dodging_direction_y = tap_direction_y;
190
191                 // normalize the dodging_direction vector.. (unlike UT99) XD
192                 float length = this.dodging_direction_x * this.dodging_direction_x
193                                         + this.dodging_direction_y * this.dodging_direction_y;
194                 length = sqrt(length);
195
196                 this.dodging_direction_x = this.dodging_direction_x * 1.0 / length;
197                 this.dodging_direction_y = this.dodging_direction_y * 1.0 / length;
198                 return true;
199         }
200         return false;
201 }
202
203 void PM_dodging(entity this)
204 {
205         if (!PHYS_DODGING)
206                 return;
207
208         // when swimming or dead, no dodging allowed..
209         if (this.waterlevel >= WATERLEVEL_SWIMMING || IS_DEAD(this))
210         {
211                 this.dodging_action = 0;
212                 this.dodging_direction_x = 0;
213                 this.dodging_direction_y = 0;
214                 return;
215         }
216
217         // make sure v_up, v_right and v_forward are sane
218         if(PHYS_DODGING_AIR)
219                 makevectors(this.v_angle);
220         else
221                 makevectors(this.angles);
222
223         // if we have e.g. 0.5 sec ramptime and a frametime of 0.25, then the ramp code
224         // will be called ramp_time/frametime times = 2 times. so, we need to
225         // add 0.5 * the total speed each frame until the dodge action is done..
226         float common_factor = PHYS_DODGING_FRAMETIME / PHYS_DODGING_RAMP_TIME;
227
228         // if ramp time is smaller than frametime we get problems ;D
229         common_factor = min(common_factor, 1);
230
231         float horiz_speed = PHYS_FROZEN(this) ? PHYS_DODGING_HORIZ_SPEED_FROZEN : PHYS_DODGING_HORIZ_SPEED;
232         float new_velocity_gain = this.dodging_velocity_gain - (common_factor * horiz_speed);
233         new_velocity_gain = max(0, new_velocity_gain);
234
235         float velocity_difference = this.dodging_velocity_gain - new_velocity_gain;
236
237         // ramp up dodging speed by adding some velocity each frame.. TODO: do it! :D
238         if (this.dodging_action == 1)
239         {
240                 //disable jump key during dodge accel phase
241                 if(PHYS_CS(this).movement.z > 0) { PHYS_CS(this).movement_z = 0; }
242
243                 this.velocity += ((this.dodging_direction_y * velocity_difference) * v_right)
244                                         + ((this.dodging_direction_x * velocity_difference) * v_forward);
245
246                 this.dodging_velocity_gain = this.dodging_velocity_gain - velocity_difference;
247         }
248
249         // the up part of the dodge is a single shot action
250         if (this.dodging_single_action == 1)
251         {
252                 UNSET_ONGROUND(this);
253
254                 this.velocity += PHYS_DODGING_UP_SPEED * v_up;
255
256 #ifdef SVQC
257                 if (autocvar_sv_dodging_sound)
258                         PlayerSound(this, playersound_jump, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
259
260                 animdecide_setaction(this, ANIMACTION_JUMP, true);
261 #endif
262
263                 this.dodging_single_action = 0;
264         }
265
266         // are we done with the dodging ramp yet?
267         if((this.dodging_action == 1) && ((time - this.last_dodging_time) > PHYS_DODGING_RAMP_TIME))
268         {
269                 // reset state so next dodge can be done correctly
270                 this.dodging_action = 0;
271                 this.dodging_direction_x = 0;
272                 this.dodging_direction_y = 0;
273         }
274 }
275
276 void PM_dodging_GetPressedKeys(entity this)
277 {
278 #ifdef CSQC
279         if(!PHYS_DODGING) { return; }
280
281         PM_dodging_checkpressedkeys(this);
282
283         int keys = this.pressedkeys;
284         keys = BITSET(keys, KEY_FORWARD,        PHYS_CS(this).movement.x > 0);
285         keys = BITSET(keys, KEY_BACKWARD,       PHYS_CS(this).movement.x < 0);
286         keys = BITSET(keys, KEY_RIGHT,          PHYS_CS(this).movement.y > 0);
287         keys = BITSET(keys, KEY_LEFT,           PHYS_CS(this).movement.y < 0);
288
289         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
290         keys = BITSET(keys, KEY_CROUCH,         PHYS_INPUT_BUTTON_CROUCH(this));
291         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
292         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
293         this.pressedkeys = keys;
294 #endif
295 }
296
297 MUTATOR_HOOKFUNCTION(dodging, PlayerPhysics)
298 {
299     entity player = M_ARGV(0, entity);
300
301         // print("dodging_PlayerPhysics\n");
302         PM_dodging_GetPressedKeys(player);
303         PM_dodging(player);
304 }
305
306 #ifdef SVQC
307
308 REPLICATE(cvar_cl_dodging_timeout, float, "cl_dodging_timeout");
309
310 MUTATOR_HOOKFUNCTION(dodging, GetPressedKeys)
311 {
312         entity player = M_ARGV(0, entity);
313
314         PM_dodging_checkpressedkeys(player);
315 }
316
317 #endif