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