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