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