]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics.qc
Finish off the last of the stats
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics.qc
1 #include "physics.qh"
2 #include "triggers/include.qh"
3 #include "viewloc.qh"
4
5 #ifdef SVQC
6
7 #include "../server/miscfunctions.qh"
8 #include "triggers/trigger/viewloc.qh"
9
10 // client side physics
11 bool Physics_Valid(string thecvar)
12 {
13         return autocvar_g_physics_clientselect && strhasword(autocvar_g_physics_clientselect_options, thecvar);
14 }
15
16 float Physics_ClientOption(entity pl, string option)
17 {
18         if(Physics_Valid(pl.cvar_cl_physics))
19         {
20                 string s = sprintf("g_physics_%s_%s", pl.cvar_cl_physics, option);
21                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
22                         return cvar(s);
23         }
24         if(autocvar_g_physics_clientselect && autocvar_g_physics_clientselect_default)
25         {
26                 string s = sprintf("g_physics_%s_%s", autocvar_g_physics_clientselect_default, option);
27                 if(cvar_type(s) & CVAR_TYPEFLAG_EXISTS)
28                         return cvar(s);
29         }
30         return cvar(strcat("sv_", option));
31 }
32
33 void Physics_AddStats()
34 {
35         // static view offset and hitbox vectors
36         // networked for all you bandwidth pigs out there
37         addstat(STAT_PL_VIEW_OFS1, AS_FLOAT, stat_pl_view_ofs_x);
38         addstat(STAT_PL_VIEW_OFS2, AS_FLOAT, stat_pl_view_ofs_y);
39         addstat(STAT_PL_VIEW_OFS3, AS_FLOAT, stat_pl_view_ofs_z);
40         addstat(STAT_PL_CROUCH_VIEW_OFS1, AS_FLOAT, stat_pl_crouch_view_ofs_x);
41         addstat(STAT_PL_CROUCH_VIEW_OFS2, AS_FLOAT, stat_pl_crouch_view_ofs_y);
42         addstat(STAT_PL_CROUCH_VIEW_OFS3, AS_FLOAT, stat_pl_crouch_view_ofs_z);
43
44         addstat(STAT_PL_MIN1, AS_FLOAT, stat_pl_min_x);
45         addstat(STAT_PL_MIN2, AS_FLOAT, stat_pl_min_y);
46         addstat(STAT_PL_MIN3, AS_FLOAT, stat_pl_min_z);
47         addstat(STAT_PL_MAX1, AS_FLOAT, stat_pl_max_x);
48         addstat(STAT_PL_MAX2, AS_FLOAT, stat_pl_max_y);
49         addstat(STAT_PL_MAX3, AS_FLOAT, stat_pl_max_z);
50         addstat(STAT_PL_CROUCH_MIN1, AS_FLOAT, stat_pl_crouch_min_x);
51         addstat(STAT_PL_CROUCH_MIN2, AS_FLOAT, stat_pl_crouch_min_y);
52         addstat(STAT_PL_CROUCH_MIN3, AS_FLOAT, stat_pl_crouch_min_z);
53         addstat(STAT_PL_CROUCH_MAX1, AS_FLOAT, stat_pl_crouch_max_x);
54         addstat(STAT_PL_CROUCH_MAX2, AS_FLOAT, stat_pl_crouch_max_y);
55         addstat(STAT_PL_CROUCH_MAX3, AS_FLOAT, stat_pl_crouch_max_z);
56
57         // g_movementspeed hack
58         addstat(STAT_MOVEVARS_MAXSPEED, AS_FLOAT, stat_sv_maxspeed);
59         addstat(STAT_MOVEVARS_AIRACCEL_QW, AS_FLOAT, stat_sv_airaccel_qw);
60
61         // new properties
62         addstat(STAT_MOVEVARS_JUMPVELOCITY, AS_FLOAT, stat_sv_jumpvelocity);
63         addstat(STAT_MOVEVARS_MAXAIRSTRAFESPEED, AS_FLOAT, stat_sv_maxairstrafespeed);
64         addstat(STAT_MOVEVARS_MAXAIRSPEED, AS_FLOAT, stat_sv_maxairspeed);
65         addstat(STAT_MOVEVARS_AIRSTRAFEACCELERATE, AS_FLOAT, stat_sv_airstrafeaccelerate);
66         addstat(STAT_MOVEVARS_WARSOWBUNNY_TURNACCEL, AS_FLOAT, stat_sv_warsowbunny_turnaccel);
67         addstat(STAT_MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, AS_FLOAT, stat_sv_airaccel_sideways_friction);
68         addstat(STAT_MOVEVARS_AIRCONTROL, AS_FLOAT, stat_sv_aircontrol);
69         addstat(STAT_MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, AS_FLOAT, stat_sv_warsowbunny_airforwardaccel);
70         addstat(STAT_MOVEVARS_WARSOWBUNNY_TOPSPEED, AS_FLOAT, stat_sv_warsowbunny_topspeed);
71         addstat(STAT_MOVEVARS_WARSOWBUNNY_ACCEL, AS_FLOAT, stat_sv_warsowbunny_accel);
72         addstat(STAT_MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, AS_FLOAT, stat_sv_warsowbunny_backtosideratio);
73         addstat(STAT_MOVEVARS_FRICTION, AS_FLOAT, stat_sv_friction);
74         addstat(STAT_MOVEVARS_ACCELERATE, AS_FLOAT, stat_sv_accelerate);
75         addstat(STAT_MOVEVARS_STOPSPEED, AS_FLOAT, stat_sv_stopspeed);
76         addstat(STAT_MOVEVARS_AIRACCELERATE, AS_FLOAT, stat_sv_airaccelerate);
77         addstat(STAT_MOVEVARS_AIRSTOPACCELERATE, AS_FLOAT, stat_sv_airstopaccelerate);
78 }
79
80 void Physics_UpdateStats(float maxspd_mod)
81 {SELFPARAM();
82         // blah
83         self.stat_pl_view_ofs = PL_VIEW_OFS;
84         self.stat_pl_crouch_view_ofs = PL_CROUCH_VIEW_OFS;
85
86         self.stat_pl_min = PL_MIN;
87         self.stat_pl_max = PL_MAX;
88         self.stat_pl_crouch_min = PL_CROUCH_MIN;
89         self.stat_pl_crouch_max = PL_CROUCH_MAX;
90
91
92         self.stat_sv_airaccel_qw = AdjustAirAccelQW(Physics_ClientOption(self, "airaccel_qw"), maxspd_mod);
93         STAT(MOVEVARS_AIRSTRAFEACCEL_QW, this) = (Physics_ClientOption(self, "airstrafeaccel_qw"))
94                 ? AdjustAirAccelQW(Physics_ClientOption(self, "airstrafeaccel_qw"), maxspd_mod)
95                 : 0;
96         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, this) = Physics_ClientOption(self, "airspeedlimit_nonqw") * maxspd_mod;
97         self.stat_sv_maxspeed = Physics_ClientOption(self, "maxspeed") * maxspd_mod; // also slow walking
98         STAT(MOVEVARS_HIGHSPEED, this) = PHYS_HIGHSPEED; // TODO: remove this!
99
100         STAT(DOUBLEJUMP, this) = PHYS_DOUBLEJUMP;
101
102         STAT(JETPACK_ANTIGRAVITY, this) = PHYS_JETPACK_ANTIGRAVITY;
103         STAT(JETPACK_ACCEL_UP, this) = PHYS_JETPACK_ACCEL_UP;
104         STAT(JETPACK_ACCEL_SIDE, this) = PHYS_JETPACK_ACCEL_SIDE;
105         STAT(JETPACK_MAXSPEED_SIDE, this) = PHYS_JETPACK_MAXSPEED_SIDE;
106         STAT(JETPACK_MAXSPEED_UP, this) = PHYS_JETPACK_MAXSPEED_UP;
107         STAT(JETPACK_FUEL, this) = PHYS_JETPACK_FUEL;
108
109         STAT(MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, this) = PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS;
110
111         STAT(MOVEVARS_FRICTION_ONLAND, this) = PHYS_FRICTION_ONLAND;
112         STAT(MOVEVARS_FRICTION_SLICK, this) = PHYS_FRICTION_SLICK;
113
114         STAT(MOVEVARS_JUMPSTEP, this) = PHYS_JUMPSTEP;
115
116         STAT(GAMEPLAYFIX_STEPDOWN, this) = GAMEPLAYFIX_STEPDOWN;
117
118         STAT(NOSTEP, this) = PHYS_NOSTEP;
119
120         STAT(GAMEPLAYFIX_EASIERWATERJUMP, this) = GAMEPLAYFIX_EASIERWATERJUMP;
121         STAT(GAMEPLAYFIX_DOWNTRACEONGROUND, this) = GAMEPLAYFIX_DOWNTRACEONGROUND;
122         STAT(GAMEPLAYFIX_STEPMULTIPLETIMES, this) = GAMEPLAYFIX_STEPMULTIPLETIMES;
123         STAT(GAMEPLAYFIX_UNSTICKPLAYERS, this) = GAMEPLAYFIX_UNSTICKPLAYERS;
124
125
126         // old stats
127         // fix some new settings
128         STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, this) = Physics_ClientOption(self, "airaccel_qw_stretchfactor");
129         self.stat_sv_maxairstrafespeed = Physics_ClientOption(self, "maxairstrafespeed");
130         self.stat_sv_maxairspeed = Physics_ClientOption(self, "maxairspeed");
131         self.stat_sv_airstrafeaccelerate = Physics_ClientOption(self, "airstrafeaccelerate");
132         self.stat_sv_warsowbunny_turnaccel = Physics_ClientOption(self, "warsowbunny_turnaccel");
133         self.stat_sv_airaccel_sideways_friction = Physics_ClientOption(self, "airaccel_sideways_friction");
134         self.stat_sv_aircontrol = Physics_ClientOption(self, "aircontrol");
135         STAT(MOVEVARS_AIRCONTROL_POWER, this) = Physics_ClientOption(self, "aircontrol_power");
136         STAT(MOVEVARS_AIRCONTROL_PENALTY, this) = Physics_ClientOption(self, "aircontrol_penalty");
137         self.stat_sv_warsowbunny_airforwardaccel = Physics_ClientOption(self, "warsowbunny_airforwardaccel");
138         self.stat_sv_warsowbunny_topspeed = Physics_ClientOption(self, "warsowbunny_topspeed");
139         self.stat_sv_warsowbunny_accel = Physics_ClientOption(self, "warsowbunny_accel");
140         self.stat_sv_warsowbunny_backtosideratio = Physics_ClientOption(self, "warsowbunny_backtosideratio");
141         self.stat_sv_friction = Physics_ClientOption(self, "friction");
142         self.stat_sv_accelerate = Physics_ClientOption(self, "accelerate");
143         self.stat_sv_stopspeed = Physics_ClientOption(self, "stopspeed");
144         self.stat_sv_airaccelerate = Physics_ClientOption(self, "airaccelerate");
145         self.stat_sv_airstopaccelerate = Physics_ClientOption(self, "airstopaccelerate");
146         self.stat_sv_jumpvelocity = Physics_ClientOption(self, "jumpvelocity");
147
148         STAT(MOVEVARS_TRACK_CANJUMP, this) = Physics_ClientOption(self, "track_canjump");
149
150         self.stat_gameplayfix_upvelocityclearsonground = UPWARD_VELOCITY_CLEARS_ONGROUND;
151 }
152 #endif
153
154 float IsMoveInDirection(vector mv, float ang) // key mix factor
155 {
156         if (mv_x == 0 && mv_y == 0)
157                 return 0; // avoid division by zero
158         ang -= RAD2DEG * atan2(mv_y, mv_x);
159         ang = remainder(ang, 360) / 45;
160         return ang > 1 ? 0 : ang < -1 ? 0 : 1 - fabs(ang);
161 }
162
163 float GeomLerp(float a, float lerp, float b)
164 {
165         return a == 0 ? (lerp < 1 ? 0 : b)
166                 : b == 0 ? (lerp > 0 ? 0 : a)
167                 : a * pow(fabs(b / a), lerp);
168 }
169
170 noref float pmove_waterjumptime;
171
172 const float unstick_count = 27;
173 vector unstick_offsets[unstick_count] =
174 {
175 // 1 no nudge (just return the original if this test passes)
176         '0.000   0.000  0.000',
177 // 6 simple nudges
178         ' 0.000  0.000  0.125', '0.000  0.000 -0.125',
179         '-0.125  0.000  0.000', '0.125  0.000  0.000',
180         ' 0.000 -0.125  0.000', '0.000  0.125  0.000',
181 // 4 diagonal flat nudges
182         '-0.125 -0.125  0.000', '0.125 -0.125  0.000',
183         '-0.125  0.125  0.000', '0.125  0.125  0.000',
184 // 8 diagonal upward nudges
185         '-0.125  0.000  0.125', '0.125  0.000  0.125',
186         ' 0.000 -0.125  0.125', '0.000  0.125  0.125',
187         '-0.125 -0.125  0.125', '0.125 -0.125  0.125',
188         '-0.125  0.125  0.125', '0.125  0.125  0.125',
189 // 8 diagonal downward nudges
190         '-0.125  0.000 -0.125', '0.125  0.000 -0.125',
191         ' 0.000 -0.125 -0.125', '0.000  0.125 -0.125',
192         '-0.125 -0.125 -0.125', '0.125 -0.125 -0.125',
193         '-0.125  0.125 -0.125', '0.125  0.125 -0.125',
194 };
195
196 void PM_ClientMovement_Unstick()
197 {SELFPARAM();
198         float i;
199         for (i = 0; i < unstick_count; i++)
200         {
201                 vector neworigin = unstick_offsets[i] + self.origin;
202                 tracebox(neworigin, PL_CROUCH_MIN, PL_CROUCH_MAX, neworigin, MOVE_NORMAL, self);
203                 if (!trace_startsolid)
204                 {
205                         setorigin(self, neworigin);
206                         return;// true;
207                 }
208         }
209 }
210
211 void PM_ClientMovement_UpdateStatus(bool ground)
212 {SELFPARAM();
213         // make sure player is not stuck
214         PM_ClientMovement_Unstick();
215
216         // set crouched
217         if (PHYS_INPUT_BUTTON_CROUCH(self))
218         {
219                 // wants to crouch, this always works..
220                 if (!IS_DUCKED(self))
221                         SET_DUCKED(self);
222         }
223         else
224         {
225                 // wants to stand, if currently crouching we need to check for a
226                 // low ceiling first
227                 if (IS_DUCKED(self))
228                 {
229                         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, MOVE_NORMAL, self);
230                         if (!trace_startsolid)
231                                 UNSET_DUCKED(self);
232                 }
233         }
234
235         // set onground
236         vector origin1 = self.origin + '0 0 1';
237         vector origin2 = self.origin - '0 0 1';
238
239         if(ground)
240         {
241                 tracebox(origin1, self.mins, self.maxs, origin2, MOVE_NORMAL, self);
242                 if (trace_fraction < 1.0 && trace_plane_normal_z > 0.7)
243                 {
244                         SET_ONGROUND(self);
245
246                         // this code actually "predicts" an impact; so let's clip velocity first
247                         float f = self.velocity * trace_plane_normal;
248                         self.velocity -= f * trace_plane_normal;
249                 }
250                 else
251                         UNSET_ONGROUND(self);
252         }
253
254         // set watertype/waterlevel
255         origin1 = self.origin;
256         origin1_z += self.mins_z + 1;
257         self.waterlevel = WATERLEVEL_NONE;
258
259         int thepoint = pointcontents(origin1);
260
261         self.watertype = (thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME);
262
263         if(self.watertype)
264         {
265                 self.waterlevel = WATERLEVEL_WETFEET;
266                 origin1_z = self.origin_z + (self.mins_z + self.maxs_z) * 0.5;
267                 thepoint = pointcontents(origin1);
268                 if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
269                 {
270                         self.waterlevel = WATERLEVEL_SWIMMING;
271                         origin1_z = self.origin_z + 22;
272                         thepoint = pointcontents(origin1);
273                         if(thepoint == CONTENT_WATER || thepoint == CONTENT_LAVA || thepoint == CONTENT_SLIME)
274                                 self.waterlevel = WATERLEVEL_SUBMERGED;
275                 }
276         }
277
278         if(IS_ONGROUND(self) || self.velocity_z <= 0 || pmove_waterjumptime <= 0)
279                 pmove_waterjumptime = 0;
280 }
281
282 void PM_ClientMovement_Move()
283 {SELFPARAM();
284 #ifdef CSQC
285         int bump;
286         float t;
287         float f;
288         vector neworigin;
289         vector currentorigin2;
290         vector neworigin2;
291         vector primalvelocity;
292
293         vector trace1_endpos = '0 0 0';
294         vector trace2_endpos = '0 0 0';
295         vector trace3_endpos = '0 0 0';
296         float trace1_fraction = 0;
297         float trace2_fraction = 0;
298         float trace3_fraction = 0;
299         vector trace1_plane_normal = '0 0 0';
300         vector trace2_plane_normal = '0 0 0';
301         vector trace3_plane_normal = '0 0 0';
302
303
304         PM_ClientMovement_UpdateStatus(false);
305         primalvelocity = self.velocity;
306         for(bump = 0, t = PHYS_INPUT_TIMELENGTH; bump < 8 && (self.velocity * self.velocity) > 0; bump++)
307         {
308                 neworigin = self.origin + t * self.velocity;
309                 tracebox(self.origin, self.mins, self.maxs, neworigin, MOVE_NORMAL, self);
310                 trace1_endpos = trace_endpos;
311                 trace1_fraction = trace_fraction;
312                 trace1_plane_normal = trace_plane_normal;
313                 if(trace1_fraction < 1 && trace1_plane_normal_z == 0)
314                 {
315                         // may be a step or wall, try stepping up
316                         // first move forward at a higher level
317                         currentorigin2 = self.origin;
318                         currentorigin2_z += PHYS_STEPHEIGHT;
319                         neworigin2 = neworigin;
320                         neworigin2_z += PHYS_STEPHEIGHT;
321                         tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
322                         trace2_endpos = trace_endpos;
323                         trace2_fraction = trace_fraction;
324                         trace2_plane_normal = trace_plane_normal;
325                         if(!trace_startsolid)
326                         {
327                                 // then move down from there
328                                 currentorigin2 = trace2_endpos;
329                                 neworigin2 = trace2_endpos;
330                                 neworigin2_z = self.origin_z;
331                                 tracebox(currentorigin2, self.mins, self.maxs, neworigin2, MOVE_NORMAL, self);
332                                 trace3_endpos = trace_endpos;
333                                 trace3_fraction = trace_fraction;
334                                 trace3_plane_normal = trace_plane_normal;
335                                 // accept the new trace if it made some progress
336                                 if(fabs(trace3_endpos_x - trace1_endpos_x) >= 0.03125 || fabs(trace3_endpos_y - trace1_endpos_y) >= 0.03125)
337                                 {
338                                         trace1_endpos = trace2_endpos;
339                                         trace1_fraction = trace2_fraction;
340                                         trace1_plane_normal = trace2_plane_normal;
341                                         trace1_endpos = trace3_endpos;
342                                 }
343                         }
344                 }
345
346                 // check if it moved at all
347                 if(trace1_fraction >= 0.001)
348                         setorigin(self, trace1_endpos);
349
350                 // check if it moved all the way
351                 if(trace1_fraction == 1)
352                         break;
353
354                 // this is only really needed for nogravityonground combined with gravityunaffectedbyticrate
355                 // <LordHavoc> I'm pretty sure I commented it out solely because it seemed redundant
356                 // this got commented out in a change that supposedly makes the code match QW better
357                 // so if this is broken, maybe put it in an if(cls.protocol != PROTOCOL_QUAKEWORLD) block
358                 if(trace1_plane_normal_z > 0.7)
359                         SET_ONGROUND(self);
360
361                 t -= t * trace1_fraction;
362
363                 f = (self.velocity * trace1_plane_normal);
364                 self.velocity = self.velocity + -f * trace1_plane_normal;
365         }
366         if(pmove_waterjumptime > 0)
367                 self.velocity = primalvelocity;
368 #endif
369 }
370
371 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
372 {SELFPARAM();
373         float k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
374         if (k <= 0)
375                 return;
376
377         k *= bound(0, wishspeed / PHYS_MAXAIRSPEED(self), 1);
378
379         float zspeed = self.velocity_z;
380         self.velocity_z = 0;
381         float xyspeed = vlen(self.velocity);
382         self.velocity = normalize(self.velocity);
383
384         float dot = self.velocity * wishdir;
385
386         if (dot > 0) // we can't change direction while slowing down
387         {
388                 k *= pow(dot, PHYS_AIRCONTROL_POWER) * PHYS_INPUT_TIMELENGTH;
389                 xyspeed = max(0, xyspeed - PHYS_AIRCONTROL_PENALTY * sqrt(max(0, 1 - dot*dot)) * k/32);
390                 k *= PHYS_AIRCONTROL;
391                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
392         }
393
394         self.velocity = self.velocity * xyspeed;
395         self.velocity_z = zspeed;
396 }
397
398 float AdjustAirAccelQW(float accelqw, float factor)
399 {
400         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
401 }
402
403 // example config for alternate speed clamping:
404 //   sv_airaccel_qw 0.8
405 //   sv_airaccel_sideways_friction 0
406 //   prvm_globalset server speedclamp_mode 1
407 //     (or 2)
408 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
409 {SELFPARAM();
410         float speedclamp = stretchfactor > 0 ? stretchfactor
411         : accelqw < 0 ? 1 // full clamping, no stretch
412         : -1; // no clamping
413
414         accelqw = fabs(accelqw);
415
416         if (GAMEPLAYFIX_Q2AIRACCELERATE)
417                 wishspeed0 = wishspeed; // don't need to emulate this Q1 bug
418
419         float vel_straight = self.velocity * wishdir;
420         float vel_z = self.velocity_z;
421         vector vel_xy = vec2(self.velocity);
422         vector vel_perpend = vel_xy - vel_straight * wishdir;
423
424         float step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
425
426         float vel_xy_current  = vlen(vel_xy);
427         if (speedlimit)
428                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
429         float vel_xy_forward =  vel_xy_current  + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
430         float vel_xy_backward = vel_xy_current  - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
431         vel_xy_backward = max(0, vel_xy_backward); // not that it REALLY occurs that this would cause wrong behaviour afterwards
432         vel_straight =          vel_straight    + bound(0, wishspeed - vel_straight,   step) * accelqw + step * (1 - accelqw);
433
434         if (sidefric < 0 && (vel_perpend*vel_perpend))
435                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
436         {
437                 float f = max(0, 1 + PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
438                 float fmin = (vel_xy_backward * vel_xy_backward - vel_straight * vel_straight) / (vel_perpend * vel_perpend);
439                 // assume: fmin > 1
440                 // vel_xy_backward*vel_xy_backward - vel_straight*vel_straight > vel_perpend*vel_perpend
441                 // vel_xy_backward*vel_xy_backward > vel_straight*vel_straight + vel_perpend*vel_perpend
442                 // vel_xy_backward*vel_xy_backward > vel_xy * vel_xy
443                 // obviously, this cannot be
444                 if (fmin <= 0)
445                         vel_perpend *= f;
446                 else
447                 {
448                         fmin = sqrt(fmin);
449                         vel_perpend *= max(fmin, f);
450                 }
451         }
452         else
453                 vel_perpend *= max(0, 1 - PHYS_INPUT_TIMELENGTH * wishspeed * sidefric);
454
455         vel_xy = vel_straight * wishdir + vel_perpend;
456
457         if (speedclamp >= 0)
458         {
459                 float vel_xy_preclamp;
460                 vel_xy_preclamp = vlen(vel_xy);
461                 if (vel_xy_preclamp > 0) // prevent division by zero
462                 {
463                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
464                         if (vel_xy_current < vel_xy_preclamp)
465                                 vel_xy *= (vel_xy_current / vel_xy_preclamp);
466                 }
467         }
468
469         self.velocity = vel_xy + vel_z * '0 0 1';
470 }
471
472 void PM_AirAccelerate(vector wishdir, float wishspeed)
473 {SELFPARAM();
474         if (wishspeed == 0)
475                 return;
476
477         vector curvel = self.velocity;
478         curvel_z = 0;
479         float curspeed = vlen(curvel);
480
481         if (wishspeed > curspeed * 1.01)
482                 wishspeed = min(wishspeed, curspeed + PHYS_WARSOWBUNNY_AIRFORWARDACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
483         else
484         {
485                 float f = max(0, (PHYS_WARSOWBUNNY_TOPSPEED - curspeed) / (PHYS_WARSOWBUNNY_TOPSPEED - PHYS_MAXSPEED(self)));
486                 wishspeed = max(curspeed, PHYS_MAXSPEED(self)) + PHYS_WARSOWBUNNY_ACCEL * f * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH;
487         }
488         vector wishvel = wishdir * wishspeed;
489         vector acceldir = wishvel - curvel;
490         float addspeed = vlen(acceldir);
491         acceldir = normalize(acceldir);
492
493         float accelspeed = min(addspeed, PHYS_WARSOWBUNNY_TURNACCEL * PHYS_MAXSPEED(self) * PHYS_INPUT_TIMELENGTH);
494
495         if (PHYS_WARSOWBUNNY_BACKTOSIDERATIO < 1)
496         {
497                 vector curdir = normalize(curvel);
498                 float dot = acceldir * curdir;
499                 if (dot < 0)
500                         acceldir -= (1 - PHYS_WARSOWBUNNY_BACKTOSIDERATIO) * dot * curdir;
501         }
502
503         self.velocity += accelspeed * acceldir;
504 }
505
506
507 /*
508 =============
509 PlayerJump
510
511 When you press the jump key
512 returns true if handled
513 =============
514 */
515 bool PlayerJump ()
516 {SELFPARAM();
517         if (PHYS_FROZEN(self))
518                 return true; // no jumping in freezetag when frozen
519
520 #ifdef SVQC
521         if (self.player_blocked)
522                 return true; // no jumping while blocked
523 #endif
524
525         bool doublejump = false;
526         float mjumpheight = PHYS_JUMPVELOCITY;
527 #ifdef CSQC
528         player_multijump = doublejump;
529         player_jumpheight = mjumpheight;
530 #endif
531
532         if (MUTATOR_CALLHOOK(PlayerJump, doublejump, mjumpheight)
533 #ifdef CSQC
534                 || PM_multijump_checkjump()
535 #endif
536                 ) { return true; }
537
538         doublejump = player_multijump;
539         mjumpheight = player_jumpheight;
540
541         if (PHYS_DOUBLEJUMP)
542         {
543                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
544                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
545                 {
546                         doublejump = true;
547
548                         // we MUST clip velocity here!
549                         float f;
550                         f = self.velocity * trace_plane_normal;
551                         if (f < 0)
552                                 self.velocity -= f * trace_plane_normal;
553                 }
554         }
555
556         if (self.waterlevel >= WATERLEVEL_SWIMMING)
557         {
558                 self.velocity_z = PHYS_MAXSPEED(self) * 0.7;
559                 return true;
560         }
561
562         if (!doublejump)
563                 if (!IS_ONGROUND(self))
564                         return IS_JUMP_HELD(self);
565
566         bool track_jump = PHYS_CL_TRACK_CANJUMP(self);
567         if(PHYS_TRACK_CANJUMP(self))
568                 track_jump = true;
569
570         if (track_jump)
571                 if (IS_JUMP_HELD(self))
572                         return true;
573
574         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
575         // velocity bounds.  Final velocity is bound between (jumpheight *
576         // min + jumpheight) and (jumpheight * max + jumpheight);
577
578         if(PHYS_JUMPSPEEDCAP_MIN != "")
579         {
580                 float minjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MIN);
581
582                 if (self.velocity_z < minjumpspeed)
583                         mjumpheight += minjumpspeed - self.velocity_z;
584         }
585
586         if(PHYS_JUMPSPEEDCAP_MAX != "")
587         {
588                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
589                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
590
591                 if (!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS))
592                 {
593                         float maxjumpspeed = mjumpheight * stof(PHYS_JUMPSPEEDCAP_MAX);
594
595                         if (self.velocity_z > maxjumpspeed)
596                                 mjumpheight -= self.velocity_z - maxjumpspeed;
597                 }
598         }
599
600         if (!WAS_ONGROUND(self))
601         {
602 #ifdef SVQC
603                 if(autocvar_speedmeter)
604                         LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
605 #endif
606                 if(self.lastground < time - 0.3)
607                 {
608                         self.velocity_x *= (1 - PHYS_FRICTION_ONLAND);
609                         self.velocity_y *= (1 - PHYS_FRICTION_ONLAND);
610                 }
611 #ifdef SVQC
612                 if(self.jumppadcount > 1)
613                         LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
614                 self.jumppadcount = 0;
615 #endif
616         }
617
618         self.velocity_z += mjumpheight;
619
620         UNSET_ONGROUND(self);
621         SET_JUMP_HELD(self);
622
623 #ifdef SVQC
624
625         self.oldvelocity_z = self.velocity_z;
626
627         animdecide_setaction(self, ANIMACTION_JUMP, true);
628
629         if (autocvar_g_jump_grunt)
630                 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
631 #endif
632         return true;
633 }
634
635 void CheckWaterJump()
636 {SELFPARAM();
637 // check for a jump-out-of-water
638         makevectors(self.v_angle);
639         vector start = self.origin;
640         start_z += 8;
641         v_forward_z = 0;
642         normalize(v_forward);
643         vector end = start + v_forward*24;
644         traceline (start, end, true, self);
645         if (trace_fraction < 1)
646         {       // solid at waist
647                 start_z = start_z + self.maxs_z - 8;
648                 end = start + v_forward*24;
649                 self.movedir = trace_plane_normal * -50;
650                 traceline(start, end, true, self);
651                 if (trace_fraction == 1)
652                 {       // open at eye level
653                         self.velocity_z = 225;
654                         self.flags |= FL_WATERJUMP;
655                         SET_JUMP_HELD(self);
656 #ifdef SVQC
657                         self.teleport_time = time + 2;  // safety net
658 #elif defined(CSQC)
659                         pmove_waterjumptime = time + 2;
660 #endif
661                 }
662         }
663 }
664
665
666 #ifdef SVQC
667         #define JETPACK_JUMP(s) s.cvar_cl_jetpack_jump
668 #elif defined(CSQC)
669         float autocvar_cl_jetpack_jump;
670         #define JETPACK_JUMP(s) autocvar_cl_jetpack_jump
671 #endif
672 .float jetpack_stopped;
673 // Hack: shouldn't need to know about this
674 .float multijump_count;
675 void CheckPlayerJump()
676 {SELFPARAM();
677 #ifdef SVQC
678         float was_flying = ITEMS_STAT(self) & IT_USING_JETPACK;
679 #endif
680         if (JETPACK_JUMP(self) < 2)
681                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
682
683         if(PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self))
684         {
685                 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
686                 float activate = JETPACK_JUMP(self) && air_jump && PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_JETPACK(self);
687                 float has_fuel = !PHYS_JETPACK_FUEL || PHYS_AMMO_FUEL(self) || ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO;
688
689                 if (!(ITEMS_STAT(self) & ITEM_Jetpack.m_itemid)) { }
690                 else if (self.jetpack_stopped) { }
691                 else if (!has_fuel)
692                 {
693 #ifdef SVQC
694                         if (was_flying) // TODO: ran out of fuel message
695                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
696                         else if (activate)
697                                 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
698 #endif
699                         self.jetpack_stopped = true;
700                         ITEMS_STAT(self) &= ~IT_USING_JETPACK;
701                 }
702                 else if (activate && !PHYS_FROZEN(self))
703                         ITEMS_STAT(self) |= IT_USING_JETPACK;
704         }
705         else
706         {
707                 self.jetpack_stopped = false;
708                 ITEMS_STAT(self) &= ~IT_USING_JETPACK;
709         }
710         if (!PHYS_INPUT_BUTTON_JUMP(self))
711                 UNSET_JUMP_HELD(self);
712
713         if (self.waterlevel == WATERLEVEL_SWIMMING)
714                 CheckWaterJump();
715 }
716
717 float racecar_angle(float forward, float down)
718 {
719         if (forward < 0)
720         {
721                 forward = -forward;
722                 down = -down;
723         }
724
725         float ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
726
727         float angle_mult = forward / (800 + forward);
728
729         if (ret > 180)
730                 return ret * angle_mult + 360 * (1 - angle_mult);
731         else
732                 return ret * angle_mult;
733 }
734
735 void RaceCarPhysics()
736 {SELFPARAM();
737 #ifdef SVQC
738         // using this move type for "big rigs"
739         // the engine does not push the entity!
740
741         vector rigvel;
742
743         vector angles_save = self.angles;
744         float accel = bound(-1, self.movement.x / PHYS_MAXSPEED(self), 1);
745         float steer = bound(-1, self.movement.y / PHYS_MAXSPEED(self), 1);
746
747         if (g_bugrigs_reverse_speeding)
748         {
749                 if (accel < 0)
750                 {
751                         // back accel is DIGITAL
752                         // to prevent speedhack
753                         if (accel < -0.5)
754                                 accel = -1;
755                         else
756                                 accel = 0;
757                 }
758         }
759
760         self.angles_x = 0;
761         self.angles_z = 0;
762         makevectors(self.angles); // new forward direction!
763
764         if (IS_ONGROUND(self) || g_bugrigs_air_steering)
765         {
766                 float myspeed = self.velocity * v_forward;
767                 float upspeed = self.velocity * v_up;
768
769                 // responsiveness factor for steering and acceleration
770                 float f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
771                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
772
773                 float steerfactor;
774                 if (myspeed < 0 && g_bugrigs_reverse_spinning)
775                         steerfactor = -myspeed * g_bugrigs_steer;
776                 else
777                         steerfactor = -myspeed * f * g_bugrigs_steer;
778
779                 float accelfactor;
780                 if (myspeed < 0 && g_bugrigs_reverse_speeding)
781                         accelfactor = g_bugrigs_accel;
782                 else
783                         accelfactor = f * g_bugrigs_accel;
784                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
785
786                 if (accel < 0)
787                 {
788                         if (myspeed > 0)
789                         {
790                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
791                         }
792                         else
793                         {
794                                 if (!g_bugrigs_reverse_speeding)
795                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
796                         }
797                 }
798                 else
799                 {
800                         if (myspeed >= 0)
801                         {
802                                 myspeed = max(0, myspeed - PHYS_INPUT_TIMELENGTH * g_bugrigs_friction_floor);
803                         }
804                         else
805                         {
806                                 if (g_bugrigs_reverse_stopping)
807                                         myspeed = 0;
808                                 else
809                                         myspeed = min(0, myspeed + PHYS_INPUT_TIMELENGTH * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
810                         }
811                 }
812                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
813                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
814
815                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
816                 makevectors(self.angles); // new forward direction!
817
818                 myspeed += accel * accelfactor * PHYS_INPUT_TIMELENGTH;
819
820                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
821         }
822         else
823         {
824                 float myspeed = vlen(self.velocity);
825
826                 // responsiveness factor for steering and acceleration
827                 float f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
828                 float steerfactor = -myspeed * f;
829                 self.angles_y += steer * PHYS_INPUT_TIMELENGTH * steerfactor; // apply steering
830
831                 rigvel = self.velocity;
832                 makevectors(self.angles); // new forward direction!
833         }
834
835         rigvel *= max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * PHYS_INPUT_TIMELENGTH);
836         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
837         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
838         //MAXIMA: solve(total_acceleration(v) = 0, v);
839
840         if (g_bugrigs_planar_movement)
841         {
842                 vector rigvel_xy, neworigin, up;
843                 float mt;
844
845                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
846                 rigvel_xy = vec2(rigvel);
847
848                 if (g_bugrigs_planar_movement_car_jumping)
849                         mt = MOVE_NORMAL;
850                 else
851                         mt = MOVE_NOMONSTERS;
852
853                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
854                 up = trace_endpos - self.origin;
855
856                 // BUG RIGS: align the move to the surface instead of doing collision testing
857                 // can we move?
858                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * PHYS_INPUT_TIMELENGTH, mt, self);
859
860                 // align to surface
861                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * PHYS_INPUT_TIMELENGTH, mt, self);
862
863                 if (trace_fraction < 0.5)
864                 {
865                         trace_fraction = 1;
866                         neworigin = self.origin;
867                 }
868                 else
869                         neworigin = trace_endpos;
870
871                 if (trace_fraction < 1)
872                 {
873                         // now set angles_x so that the car points parallel to the surface
874                         self.angles = vectoangles(
875                                         '1 0 0' * v_forward_x * trace_plane_normal_z
876                                         +
877                                         '0 1 0' * v_forward_y * trace_plane_normal_z
878                                         +
879                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
880                                         );
881                         SET_ONGROUND(self);
882                 }
883                 else
884                 {
885                         // now set angles_x so that the car points forward, but is tilted in velocity direction
886                         UNSET_ONGROUND(self);
887                 }
888
889                 self.velocity = (neworigin - self.origin) * (1.0 / PHYS_INPUT_TIMELENGTH);
890                 self.movetype = MOVETYPE_NOCLIP;
891         }
892         else
893         {
894                 rigvel_z -= PHYS_INPUT_TIMELENGTH * PHYS_GRAVITY; // 4x gravity plays better
895                 self.velocity = rigvel;
896                 self.movetype = MOVETYPE_FLY;
897         }
898
899         trace_fraction = 1;
900         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
901         if (trace_fraction != 1)
902         {
903                 self.angles = vectoangles2(
904                                 '1 0 0' * v_forward_x * trace_plane_normal_z
905                                 +
906                                 '0 1 0' * v_forward_y * trace_plane_normal_z
907                                 +
908                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
909                                 trace_plane_normal
910                                 );
911         }
912         else
913         {
914                 vector vel_local;
915
916                 vel_local_x = v_forward * self.velocity;
917                 vel_local_y = v_right * self.velocity;
918                 vel_local_z = v_up * self.velocity;
919
920                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
921                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
922         }
923
924         // smooth the angles
925         vector vf1, vu1, smoothangles;
926         makevectors(self.angles);
927         float f = bound(0, PHYS_INPUT_TIMELENGTH * g_bugrigs_angle_smoothing, 1);
928         if (f == 0)
929                 f = 1;
930         vf1 = v_forward * f;
931         vu1 = v_up * f;
932         makevectors(angles_save);
933         vf1 = vf1 + v_forward * (1 - f);
934         vu1 = vu1 + v_up * (1 - f);
935         smoothangles = vectoangles2(vf1, vu1);
936         self.angles_x = -smoothangles_x;
937         self.angles_z =  smoothangles_z;
938 #endif
939 }
940
941 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
942 .float specialcommand_pos;
943 void SpecialCommand()
944 {
945 #ifdef SVQC
946 #ifdef TETRIS
947         TetrisImpulse();
948 #else
949         if (!CheatImpulse(99))
950                 LOG_INFO("A hollow voice says \"Plugh\".\n");
951 #endif
952 #endif
953 }
954
955 float PM_check_specialcommand(float buttons)
956 {SELFPARAM();
957 #ifdef SVQC
958         string c;
959         if (!buttons)
960                 c = "x";
961         else if (buttons == 1)
962                 c = "1";
963         else if (buttons == 2)
964                 c = " ";
965         else if (buttons == 128)
966                 c = "s";
967         else if (buttons == 256)
968                 c = "w";
969         else if (buttons == 512)
970                 c = "a";
971         else if (buttons == 1024)
972                 c = "d";
973         else
974                 c = "?";
975
976         if (c == substring(specialcommand, self.specialcommand_pos, 1))
977         {
978                 self.specialcommand_pos += 1;
979                 if (self.specialcommand_pos >= strlen(specialcommand))
980                 {
981                         self.specialcommand_pos = 0;
982                         SpecialCommand();
983                         return true;
984                 }
985         }
986         else if (self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
987                 self.specialcommand_pos = 0;
988 #endif
989         return false;
990 }
991
992 void PM_check_nickspam()
993 {SELFPARAM();
994 #ifdef SVQC
995         if (time >= self.nickspamtime)
996                 return;
997         if (self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
998         {
999                 // slight annoyance for nick change scripts
1000                 self.movement = -1 * self.movement;
1001                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
1002
1003                 if (self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
1004                 {
1005                         self.v_angle_x = random() * 360;
1006                         self.v_angle_y = random() * 360;
1007                         // at least I'm not forcing retardedview by also assigning to angles_z
1008                         self.fixangle = true;
1009                 }
1010         }
1011 #endif
1012 }
1013
1014 void PM_check_punch()
1015 {SELFPARAM();
1016 #ifdef SVQC
1017         if (self.punchangle != '0 0 0')
1018         {
1019                 float f = vlen(self.punchangle) - 10 * PHYS_INPUT_TIMELENGTH;
1020                 if (f > 0)
1021                         self.punchangle = normalize(self.punchangle) * f;
1022                 else
1023                         self.punchangle = '0 0 0';
1024         }
1025
1026         if (self.punchvector != '0 0 0')
1027         {
1028                 float f = vlen(self.punchvector) - 30 * PHYS_INPUT_TIMELENGTH;
1029                 if (f > 0)
1030                         self.punchvector = normalize(self.punchvector) * f;
1031                 else
1032                         self.punchvector = '0 0 0';
1033         }
1034 #endif
1035 }
1036
1037 void PM_check_spider()
1038 {SELFPARAM();
1039 #ifdef SVQC
1040         if (time >= self.spider_slowness)
1041                 return;
1042         PHYS_MAXSPEED(self) *= 0.5; // half speed while slow from spider
1043         PHYS_MAXAIRSPEED(self) *= 0.5;
1044         PHYS_AIRSPEEDLIMIT_NONQW(self) *= 0.5;
1045         PHYS_AIRSTRAFEACCELERATE(self) *= 0.5;
1046 #endif
1047 }
1048
1049 // predict frozen movement, as frozen players CAN move in some cases
1050 void PM_check_frozen()
1051 {SELFPARAM();
1052         if (!PHYS_FROZEN(self))
1053                 return;
1054         if (PHYS_DODGING_FROZEN
1055 #ifdef SVQC
1056         && IS_REAL_CLIENT(self)
1057 #endif
1058         )
1059         {
1060                 self.movement_x = bound(-5, self.movement.x, 5);
1061                 self.movement_y = bound(-5, self.movement.y, 5);
1062                 self.movement_z = bound(-5, self.movement.z, 5);
1063         }
1064         else
1065                 self.movement = '0 0 0';
1066
1067         vector midpoint = ((self.absmin + self.absmax) * 0.5);
1068         if (pointcontents(midpoint) == CONTENT_WATER)
1069         {
1070                 self.velocity = self.velocity * 0.5;
1071
1072                 if (pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
1073                         self.velocity_z = 200;
1074         }
1075 }
1076
1077 void PM_check_hitground()
1078 {SELFPARAM();
1079 #ifdef SVQC
1080         if (IS_ONGROUND(self))
1081         if (IS_PLAYER(self)) // no fall sounds for observers thank you very much
1082         if (self.wasFlying)
1083         {
1084                 self.wasFlying = 0;
1085                 if (self.waterlevel < WATERLEVEL_SWIMMING)
1086                 if (time >= self.ladder_time)
1087                 if (!self.hook)
1088                 {
1089                         self.nextstep = time + 0.3 + random() * 0.1;
1090                         trace_dphitq3surfaceflags = 0;
1091                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1092                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
1093                         {
1094                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
1095                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1096                                 else
1097                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
1098                         }
1099                 }
1100         }
1101 #endif
1102 }
1103
1104 void PM_check_blocked()
1105 {SELFPARAM();
1106 #ifdef SVQC
1107         if (!self.player_blocked)
1108                 return;
1109         self.movement = '0 0 0';
1110         self.disableclientprediction = 1;
1111 #endif
1112 }
1113
1114 void PM_check_vortex()
1115 {SELFPARAM();
1116 #ifdef SVQC
1117         // WEAPONTODO
1118         float xyspeed = vlen(vec2(self.velocity));
1119         if (self.weapon == WEP_VORTEX.m_id && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1120         {
1121                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1122                 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1123                 float f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1124                 // add the extra charge
1125                 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * PHYS_INPUT_TIMELENGTH);
1126         }
1127 #endif
1128 }
1129
1130 void PM_fly(float maxspd_mod)
1131 {SELFPARAM();
1132         // noclipping or flying
1133         UNSET_ONGROUND(self);
1134
1135         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1136         makevectors(self.v_angle);
1137         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1138         vector wishvel = v_forward * self.movement.x
1139                                         + v_right * self.movement.y
1140                                         + '0 0 1' * self.movement.z;
1141         // acceleration
1142         vector wishdir = normalize(wishvel);
1143         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1144 #ifdef SVQC
1145         if (time >= self.teleport_time)
1146 #endif
1147                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1148         PM_ClientMovement_Move();
1149 }
1150
1151 void PM_swim(float maxspd_mod)
1152 {SELFPARAM();
1153         // swimming
1154         UNSET_ONGROUND(self);
1155
1156         float jump = PHYS_INPUT_BUTTON_JUMP(self);
1157         // water jump only in certain situations
1158         // this mimics quakeworld code
1159         if (jump && self.waterlevel == WATERLEVEL_SWIMMING && self.velocity_z >= -180)
1160         {
1161                 vector yawangles = '0 1 0' * self.v_angle.y;
1162                 makevectors(yawangles);
1163                 vector forward = v_forward;
1164                 vector spot = self.origin + 24 * forward;
1165                 spot_z += 8;
1166                 traceline(spot, spot, MOVE_NOMONSTERS, self);
1167                 if (trace_startsolid)
1168                 {
1169                         spot_z += 24;
1170                         traceline(spot, spot, MOVE_NOMONSTERS, self);
1171                         if (!trace_startsolid)
1172                         {
1173                                 self.velocity = forward * 50;
1174                                 self.velocity_z = 310;
1175                                 pmove_waterjumptime = 2;
1176                                 UNSET_ONGROUND(self);
1177                                 SET_JUMP_HELD(self);
1178                         }
1179                 }
1180         }
1181         makevectors(self.v_angle);
1182         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1183         vector wishvel = v_forward * self.movement.x
1184                                         + v_right * self.movement.y
1185                                         + '0 0 1' * self.movement.z;
1186         if (wishvel == '0 0 0')
1187                 wishvel = '0 0 -60'; // drift towards bottom
1188
1189         vector wishdir = normalize(wishvel);
1190         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod) * 0.7;
1191
1192         if (IS_DUCKED(self))
1193         wishspeed *= 0.5;
1194
1195 //      if (pmove_waterjumptime <= 0) // TODO: use
1196     {
1197                 // water friction
1198                 float f = 1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION;
1199                 f = min(max(0, f), 1);
1200                 self.velocity *= f;
1201
1202                 f = wishspeed - self.velocity * wishdir;
1203                 if (f > 0)
1204                 {
1205                         float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, f);
1206                         self.velocity += accelspeed * wishdir;
1207                 }
1208
1209                 // holding jump button swims upward slowly
1210                 if (jump)
1211                 {
1212 #if 0
1213                         if (self.watertype & CONTENT_LAVA)
1214                                 self.velocity_z =  50;
1215                         else if (self.watertype & CONTENT_SLIME)
1216                                 self.velocity_z =  80;
1217                         else
1218                         {
1219                                 if (IS_NEXUIZ_DERIVED(gamemode))
1220 #endif
1221                                         self.velocity_z = 200;
1222 #if 0
1223                                 else
1224                                         self.velocity_z = 100;
1225                         }
1226 #endif
1227                 }
1228         }
1229         // water acceleration
1230         PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE * maxspd_mod, 1, 0, 0, 0);
1231         PM_ClientMovement_Move();
1232 }
1233
1234 void PM_ladder(float maxspd_mod)
1235 {SELFPARAM();
1236         // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1237         UNSET_ONGROUND(self);
1238
1239         float g;
1240         g = PHYS_GRAVITY * PHYS_INPUT_TIMELENGTH;
1241         if (PHYS_ENTGRAVITY(self))
1242                 g *= PHYS_ENTGRAVITY(self);
1243         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1244         {
1245                 g *= 0.5;
1246                 self.velocity_z += g;
1247         }
1248
1249         self.velocity = self.velocity * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION);
1250         makevectors(self.v_angle);
1251         //wishvel = v_forward * self.movement.x + v_right * self.movement.y + v_up * self.movement.z;
1252         vector wishvel = v_forward * self.movement_x
1253                                         + v_right * self.movement_y
1254                                         + '0 0 1' * self.movement_z;
1255         self.velocity_z += g;
1256         if (self.ladder_entity.classname == "func_water")
1257         {
1258                 float f = vlen(wishvel);
1259                 if (f > self.ladder_entity.speed)
1260                         wishvel *= (self.ladder_entity.speed / f);
1261
1262                 self.watertype = self.ladder_entity.skin;
1263                 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1264                 if ((self.origin_z + self.view_ofs_z) < f)
1265                         self.waterlevel = WATERLEVEL_SUBMERGED;
1266                 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1267                         self.waterlevel = WATERLEVEL_SWIMMING;
1268                 else if ((self.origin_z + self.mins_z + 1) < f)
1269                         self.waterlevel = WATERLEVEL_WETFEET;
1270                 else
1271                 {
1272                         self.waterlevel = WATERLEVEL_NONE;
1273                         self.watertype = CONTENT_EMPTY;
1274                 }
1275         }
1276         // acceleration
1277         vector wishdir = normalize(wishvel);
1278         float wishspeed = min(vlen(wishvel), PHYS_MAXSPEED(self) * maxspd_mod);
1279 #ifdef SVQC
1280         if (time >= self.teleport_time)
1281 #endif
1282                 // water acceleration
1283                 PM_Accelerate(wishdir, wishspeed, wishspeed, PHYS_ACCELERATE*maxspd_mod, 1, 0, 0, 0);
1284         PM_ClientMovement_Move();
1285 }
1286
1287 void PM_jetpack(float maxspd_mod)
1288 {SELFPARAM();
1289         //makevectors(self.v_angle.y * '0 1 0');
1290         makevectors(self.v_angle);
1291         vector wishvel = v_forward * self.movement_x
1292                                         + v_right * self.movement_y;
1293         // add remaining speed as Z component
1294         float maxairspd = PHYS_MAXAIRSPEED(self) * max(1, maxspd_mod);
1295         // fix speedhacks :P
1296         wishvel = normalize(wishvel) * min(1, vlen(wishvel) / maxairspd);
1297         // add the unused velocity as up component
1298         wishvel_z = 0;
1299
1300         // if (self.BUTTON_JUMP)
1301                 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1302
1303         // it is now normalized, so...
1304         float a_side = PHYS_JETPACK_ACCEL_SIDE;
1305         float a_up = PHYS_JETPACK_ACCEL_UP;
1306         float a_add = PHYS_JETPACK_ANTIGRAVITY * PHYS_GRAVITY;
1307
1308         wishvel_x *= a_side;
1309         wishvel_y *= a_side;
1310         wishvel_z *= a_up;
1311         wishvel_z += a_add;
1312
1313         float best = 0;
1314         //////////////////////////////////////////////////////////////////////////////////////
1315         // finding the maximum over all vectors of above form
1316         // with wishvel having an absolute value of 1
1317         //////////////////////////////////////////////////////////////////////////////////////
1318         // we're finding the maximum over
1319         //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1320         // for z in the range from -1 to 1
1321         //////////////////////////////////////////////////////////////////////////////////////
1322         // maximum is EITHER attained at the single extreme point:
1323         float a_diff = a_side * a_side - a_up * a_up;
1324         float f;
1325         if (a_diff != 0)
1326         {
1327                 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1328                 if (f > -1 && f < 1) // can it be attained?
1329                 {
1330                         best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1331                         //print("middle\n");
1332                 }
1333         }
1334         // OR attained at z = 1:
1335         f = (a_up + a_add) * (a_up + a_add);
1336         if (f > best)
1337         {
1338                 best = f;
1339                 //print("top\n");
1340         }
1341         // OR attained at z = -1:
1342         f = (a_up - a_add) * (a_up - a_add);
1343         if (f > best)
1344         {
1345                 best = f;
1346                 //print("bottom\n");
1347         }
1348         best = sqrt(best);
1349         //////////////////////////////////////////////////////////////////////////////////////
1350
1351         //print("best possible acceleration: ", ftos(best), "\n");
1352
1353         float fxy, fz;
1354         fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / PHYS_JETPACK_MAXSPEED_SIDE, 1);
1355         if (wishvel_z - PHYS_GRAVITY > 0)
1356                 fz = bound(0, 1 - self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1357         else
1358                 fz = bound(0, 1 + self.velocity_z / PHYS_JETPACK_MAXSPEED_UP, 1);
1359
1360         float fvel;
1361         fvel = vlen(wishvel);
1362         wishvel_x *= fxy;
1363         wishvel_y *= fxy;
1364         wishvel_z = (wishvel_z - PHYS_GRAVITY) * fz + PHYS_GRAVITY;
1365
1366         fvel = min(1, vlen(wishvel) / best);
1367         if (PHYS_JETPACK_FUEL && !(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1368                 f = min(1, PHYS_AMMO_FUEL(self) / (PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel));
1369         else
1370                 f = 1;
1371
1372         //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1373
1374         if (f > 0 && wishvel != '0 0 0')
1375         {
1376                 self.velocity = self.velocity + wishvel * f * PHYS_INPUT_TIMELENGTH;
1377                 UNSET_ONGROUND(self);
1378
1379 #ifdef SVQC
1380                 if (!(ITEMS_STAT(self) & IT_UNLIMITED_WEAPON_AMMO))
1381                         self.ammo_fuel -= PHYS_JETPACK_FUEL * PHYS_INPUT_TIMELENGTH * fvel * f;
1382
1383                 ITEMS_STAT(self) |= IT_USING_JETPACK;
1384
1385                 // jetpack also inhibits health regeneration, but only for 1 second
1386                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1387 #endif
1388         }
1389
1390 #ifdef CSQC
1391         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1392         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1393                 self.velocity_z -= g * 0.5;
1394         else
1395                 self.velocity_z -= g;
1396         PM_ClientMovement_Move();
1397         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1398                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1399                         self.velocity_z -= g * 0.5;
1400 #endif
1401 }
1402
1403 void PM_walk(float buttons_prev, float maxspd_mod)
1404 {SELFPARAM();
1405         if (!WAS_ONGROUND(self))
1406         {
1407 #ifdef SVQC
1408                 if (autocvar_speedmeter)
1409                         LOG_TRACE(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1410 #endif
1411                 if (self.lastground < time - 0.3)
1412                         self.velocity *= (1 - PHYS_FRICTION_ONLAND);
1413 #ifdef SVQC
1414                 if (self.jumppadcount > 1)
1415                         LOG_TRACE(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1416                 self.jumppadcount = 0;
1417 #endif
1418         }
1419
1420         // walking
1421         makevectors(self.v_angle.y * '0 1 0');
1422         vector wishvel = v_forward * self.movement.x
1423                                         + v_right * self.movement.y;
1424         // acceleration
1425         vector wishdir = normalize(wishvel);
1426         float wishspeed = vlen(wishvel);
1427
1428         wishspeed = min(wishspeed, PHYS_MAXSPEED(self) * maxspd_mod);
1429         if (IS_DUCKED(self))
1430                 wishspeed *= 0.5;
1431
1432         // apply edge friction
1433         float f = vlen(vec2(self.velocity));
1434         if (f > 0)
1435         {
1436                 float realfriction;
1437                 trace_dphitq3surfaceflags = 0;
1438                 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
1439                 // TODO: apply edge friction
1440                 // apply ground friction
1441                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
1442                         realfriction = PHYS_FRICTION_SLICK;
1443                 else
1444                         realfriction = PHYS_FRICTION;
1445
1446                 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction * ((f < PHYS_STOPSPEED) ? (PHYS_STOPSPEED / f) : 1);
1447                 f = max(0, f);
1448                 self.velocity *= f;
1449                 /*
1450                    Mathematical analysis time!
1451
1452                    Our goal is to invert this mess.
1453
1454                    For the two cases we get:
1455                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED / v0) * PHYS_FRICTION)
1456                           = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1457                         v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION
1458                    and
1459                         v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1460                         v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1461
1462                    These cases would be chosen ONLY if:
1463                         v0 < PHYS_STOPSPEED
1464                         v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED * PHYS_FRICTION < PHYS_STOPSPEED
1465                         v < PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1466                    and, respectively:
1467                         v0 >= PHYS_STOPSPEED
1468                         v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION) >= PHYS_STOPSPEED
1469                         v >= PHYS_STOPSPEED * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION)
1470                  */
1471         }
1472         float addspeed = wishspeed - self.velocity * wishdir;
1473         if (addspeed > 0)
1474         {
1475                 float accelspeed = min(PHYS_ACCELERATE * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
1476                 self.velocity += accelspeed * wishdir;
1477         }
1478         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1479         if (!(GAMEPLAYFIX_NOGRAVITYONGROUND))
1480                 self.velocity_z -= g * (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1);
1481         if (self.velocity * self.velocity)
1482                 PM_ClientMovement_Move();
1483         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1484                 if (!IS_ONGROUND(self) || !GAMEPLAYFIX_NOGRAVITYONGROUND)
1485                         self.velocity_z -= g * 0.5;
1486 }
1487
1488 void PM_air(float buttons_prev, float maxspd_mod)
1489 {SELFPARAM();
1490         makevectors(self.v_angle.y * '0 1 0');
1491         vector wishvel = v_forward * self.movement.x
1492                                         + v_right * self.movement.y;
1493         // acceleration
1494         vector wishdir = normalize(wishvel);
1495         float wishspeed = vlen(wishvel);
1496
1497 #ifdef SVQC
1498         if (time >= self.teleport_time)
1499 #else
1500         if (pmove_waterjumptime <= 0)
1501 #endif
1502         {
1503                 float maxairspd = PHYS_MAXAIRSPEED(self) * min(maxspd_mod, 1);
1504
1505                 // apply air speed limit
1506                 float airaccelqw = PHYS_AIRACCEL_QW(self);
1507                 float wishspeed0 = wishspeed;
1508                 wishspeed = min(wishspeed, maxairspd);
1509                 if (IS_DUCKED(self))
1510                         wishspeed *= 0.5;
1511                 float airaccel = PHYS_AIRACCELERATE * min(maxspd_mod, 1);
1512
1513                 float accelerating = (self.velocity * wishdir > 0);
1514                 float wishspeed2 = wishspeed;
1515
1516                 // CPM: air control
1517                 if (PHYS_AIRSTOPACCELERATE)
1518                 {
1519                         vector curdir = normalize(vec2(self.velocity));
1520                         airaccel += (PHYS_AIRSTOPACCELERATE*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1521                 }
1522                 // note that for straight forward jumping:
1523                 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
1524                 // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1525                 // -->
1526                 // dv/dt = accel * maxspeed (when slow)
1527                 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1528                 // log dv/dt = logaccel + logmaxspeed (when slow)
1529                 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1530                 float strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1531                 if (PHYS_MAXAIRSTRAFESPEED)
1532                         wishspeed = min(wishspeed, GeomLerp(PHYS_MAXAIRSPEED(self)*maxspd_mod, strafity, PHYS_MAXAIRSTRAFESPEED*maxspd_mod));
1533                 if (PHYS_AIRSTRAFEACCELERATE(self))
1534                         airaccel = GeomLerp(airaccel, strafity, PHYS_AIRSTRAFEACCELERATE(self)*maxspd_mod);
1535                 if (PHYS_AIRSTRAFEACCEL_QW(self))
1536                         airaccelqw =
1537                 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(self) : PHYS_AIRACCEL_QW(self)) >= 0) ? +1 : -1)
1538                 *
1539                 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(self)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(self))));
1540                 // !CPM
1541
1542                 if (PHYS_WARSOWBUNNY_TURNACCEL && accelerating && self.movement.y == 0 && self.movement.x != 0)
1543                         PM_AirAccelerate(wishdir, wishspeed2);
1544                 else
1545                         PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, PHYS_AIRACCEL_QW_STRETCHFACTOR(self), PHYS_AIRACCEL_SIDEWAYS_FRICTION / maxairspd, PHYS_AIRSPEEDLIMIT_NONQW(self));
1546
1547                 if (PHYS_AIRCONTROL)
1548                         CPM_PM_Aircontrol(wishdir, wishspeed2);
1549         }
1550         float g = PHYS_GRAVITY * PHYS_ENTGRAVITY(self) * PHYS_INPUT_TIMELENGTH;
1551         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1552                 self.velocity_z -= g * 0.5;
1553         else
1554                 self.velocity_z -= g;
1555         PM_ClientMovement_Move();
1556         if (!IS_ONGROUND(self) || !(GAMEPLAYFIX_NOGRAVITYONGROUND))
1557                 if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
1558                         self.velocity_z -= g * 0.5;
1559 }
1560
1561 // used for calculating airshots
1562 bool IsFlying(entity a)
1563 {
1564         if(IS_ONGROUND(a))
1565                 return false;
1566         if(a.waterlevel >= WATERLEVEL_SWIMMING)
1567                 return false;
1568         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
1569         if(trace_fraction < 1)
1570                 return false;
1571         return true;
1572 }
1573
1574 void PM_Main()
1575 {SELFPARAM();
1576         int buttons = PHYS_INPUT_BUTTON_MASK(self);
1577 #ifdef CSQC
1578         self.items = getstati(STAT_ITEMS, 0, 24);
1579
1580         self.movement = PHYS_INPUT_MOVEVALUES(self);
1581
1582         vector oldv_angle = self.v_angle;
1583         vector oldangles = self.angles; // we need to save these, as they're abused by other code
1584         self.v_angle = PHYS_INPUT_ANGLES(self);
1585         self.angles = PHYS_WORLD_ANGLES(self);
1586
1587         self.team = myteam + 1; // is this correct?
1588         if (!(PHYS_INPUT_BUTTON_JUMP(self))) // !jump
1589                 UNSET_JUMP_HELD(self); // canjump = true
1590         pmove_waterjumptime -= PHYS_INPUT_TIMELENGTH;
1591
1592         PM_ClientMovement_UpdateStatus(true);
1593 #endif
1594
1595
1596 #ifdef SVQC
1597         WarpZone_PlayerPhysics_FixVAngle();
1598 #endif
1599         float maxspeed_mod = 1;
1600         maxspeed_mod *= PHYS_HIGHSPEED;
1601
1602 #ifdef SVQC
1603         Physics_UpdateStats(maxspeed_mod);
1604
1605         if (self.PlayerPhysplug)
1606                 if (self.PlayerPhysplug())
1607                         return;
1608 #endif
1609
1610 #ifdef SVQC
1611         anticheat_physics();
1612 #endif
1613
1614         if (PM_check_specialcommand(buttons))
1615                 return;
1616 #ifdef SVQC
1617         if (sv_maxidle > 0)
1618         {
1619                 if (buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
1620                         self.parm_idlesince = time;
1621         }
1622 #endif
1623         int buttons_prev = self.buttons_old;
1624         self.buttons_old = buttons;
1625         self.movement_old = self.movement;
1626         self.v_angle_old = self.v_angle;
1627
1628         PM_check_nickspam();
1629
1630         PM_check_punch();
1631 #ifdef SVQC
1632         if (IS_BOT_CLIENT(self))
1633         {
1634                 if (playerdemo_read())
1635                         return;
1636                 bot_think();
1637         }
1638
1639         if (IS_PLAYER(self))
1640 #endif
1641         {
1642                 bool not_allowed_to_move = false;
1643 #ifdef SVQC
1644                 if (time < game_starttime)
1645                         not_allowed_to_move = true;
1646 #endif
1647
1648                 if (not_allowed_to_move)
1649                 {
1650                         self.velocity = '0 0 0';
1651                         self.movetype = MOVETYPE_NONE;
1652 #ifdef SVQC
1653                         self.disableclientprediction = 2;
1654 #endif
1655                 }
1656 #ifdef SVQC
1657                 else if (self.disableclientprediction == 2)
1658                 {
1659                         if (self.movetype == MOVETYPE_NONE)
1660                                 self.movetype = MOVETYPE_WALK;
1661                         self.disableclientprediction = 0;
1662                 }
1663 #endif
1664         }
1665
1666 #ifdef SVQC
1667         if (self.movetype == MOVETYPE_NONE)
1668                 return;
1669
1670         // when we get here, disableclientprediction cannot be 2
1671         self.disableclientprediction = 0;
1672 #endif
1673
1674         viewloc_PlayerPhysics();
1675
1676         PM_check_spider();
1677
1678         PM_check_frozen();
1679
1680         PM_check_blocked();
1681
1682         maxspeed_mod = 1;
1683
1684         if (self.in_swamp)
1685                 maxspeed_mod *= self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
1686
1687         // conveyors: first fix velocity
1688         if (self.conveyor.state)
1689                 self.velocity -= self.conveyor.movedir;
1690
1691 #ifdef SVQC
1692         MUTATOR_CALLHOOK(PlayerPhysics);
1693 #endif
1694 #ifdef CSQC
1695         PM_multijump();
1696 #endif
1697
1698 //      float forcedodge = 1;
1699 //      if(forcedodge) {
1700 //#ifdef CSQC
1701 //              PM_dodging_checkpressedkeys();
1702 //#endif
1703 //              PM_dodging();
1704 //              PM_ClientMovement_Move();
1705 //              return;
1706 //      }
1707
1708 #ifdef SVQC
1709         if (!IS_PLAYER(self))
1710         {
1711                 maxspeed_mod = autocvar_sv_spectator_speed_multiplier;
1712                 if (!self.spectatorspeed)
1713                         self.spectatorspeed = maxspeed_mod;
1714                 if (self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
1715                 {
1716                         if (self.lastclassname != "player")
1717                         {
1718                                 if (self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
1719                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
1720                                 else if (self.impulse == 11)
1721                                         self.spectatorspeed = maxspeed_mod;
1722                                 else if (self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
1723                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
1724                                 else if (self.impulse >= 1 && self.impulse <= 9)
1725                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
1726                         } // otherwise just clear
1727                         self.impulse = 0;
1728                 }
1729                 maxspeed_mod = self.spectatorspeed;
1730         }
1731
1732         float spd = max(PHYS_MAXSPEED(self), PHYS_MAXAIRSPEED(self)) * maxspeed_mod;
1733         if(self.speed != spd)
1734         {
1735                 self.speed = spd;
1736                 string temps = ftos(spd);
1737                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
1738                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
1739                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
1740                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
1741         }
1742
1743         if(self.stat_jumpspeedcap_min != PHYS_JUMPSPEEDCAP_MIN)
1744         {
1745                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MIN;
1746                 stuffcmd(self, strcat("cl_jumpspeedcap_min ", PHYS_JUMPSPEEDCAP_MIN, "\n"));
1747         }
1748         if(self.stat_jumpspeedcap_max != PHYS_JUMPSPEEDCAP_MAX)
1749         {
1750                 self.stat_jumpspeedcap_min = PHYS_JUMPSPEEDCAP_MAX;
1751                 stuffcmd(self, strcat("cl_jumpspeedcap_max ", PHYS_JUMPSPEEDCAP_MAX, "\n"));
1752         }
1753 #endif
1754
1755         if(PHYS_DEAD(self))
1756         {
1757                 // handle water here
1758                 vector midpoint = ((self.absmin + self.absmax) * 0.5);
1759                 if(pointcontents(midpoint) == CONTENT_WATER)
1760                 {
1761                         self.velocity = self.velocity * 0.5;
1762
1763                         // do we want this?
1764                         //if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
1765                                 //{ self.velocity_z = 70; }
1766                 }
1767                 goto end;
1768         }
1769
1770 #ifdef SVQC
1771         if (!self.fixangle && !g_bugrigs)
1772                 self.angles = '0 1 0' * self.v_angle.y;
1773 #endif
1774
1775         PM_check_hitground();
1776
1777         if(IsFlying(self))
1778                 self.wasFlying = 1;
1779
1780         if (IS_PLAYER(self))
1781                 CheckPlayerJump();
1782
1783         if (self.flags & FL_WATERJUMP)
1784         {
1785                 self.velocity_x = self.movedir_x;
1786                 self.velocity_y = self.movedir_y;
1787                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
1788                 {
1789                         self.flags &= ~FL_WATERJUMP;
1790                         self.teleport_time = 0;
1791                 }
1792         }
1793
1794 #ifdef SVQC
1795         else if (g_bugrigs && IS_PLAYER(self))
1796                 RaceCarPhysics();
1797 #endif
1798
1799         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY || (BUFFS_STAT(self) & BUFF_FLIGHT.m_itemid))
1800                 PM_fly(maxspeed_mod);
1801
1802         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1803                 PM_swim(maxspeed_mod);
1804
1805         else if (time < self.ladder_time)
1806                 PM_ladder(maxspeed_mod);
1807
1808         else if (ITEMS_STAT(self) & IT_USING_JETPACK)
1809                 PM_jetpack(maxspeed_mod);
1810
1811         else if (IS_ONGROUND(self))
1812                 PM_walk(buttons_prev, maxspeed_mod);
1813
1814         else
1815                 PM_air(buttons_prev, maxspeed_mod);
1816
1817         PM_check_vortex();
1818
1819 :end
1820         if (IS_ONGROUND(self))
1821                 self.lastground = time;
1822
1823         // conveyors: then break velocity again
1824         if(self.conveyor.state)
1825                 self.velocity += self.conveyor.movedir;
1826
1827         self.lastflags = self.flags;
1828
1829         self.lastclassname = self.classname;
1830
1831 #ifdef CSQC
1832         self.v_angle = oldv_angle;
1833         self.angles = oldangles;
1834 #endif
1835 }
1836
1837 #ifdef SVQC
1838 void SV_PlayerPhysics()
1839 #elif defined(CSQC)
1840 void CSQC_ClientMovement_PlayerMove_Frame()
1841 #endif
1842 {SELFPARAM();
1843         PM_Main();
1844
1845 #ifdef CSQC
1846         self.pmove_flags =
1847                         ((self.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
1848                         (!(self.flags & FL_JUMPRELEASED) ? 0 : PMF_JUMP_HELD) |
1849                         ((self.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);
1850 #endif
1851 }