]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
Merge remote branch 'origin/mirceakitsune/gravity_trigger'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_physics.qc
1 .float race_penalty;
2 .float restart_jump;
3
4 float sv_accelerate;
5 float sv_friction;
6 float sv_maxspeed;
7 float sv_airaccelerate;
8 float sv_maxairspeed;
9 float sv_stopspeed;
10 float sv_gravity;
11 float sv_airaccel_sideways_friction;
12 float sv_airaccel_qw;
13 float sv_airstopaccelerate;
14 float sv_airstrafeaccelerate;
15 float sv_maxairstrafespeed;
16 float sv_aircontrol;
17 float sv_aircontrol_power;
18 float sv_warsowbunny_airforwardaccel;
19 float sv_warsowbunny_accel;
20 float sv_warsowbunny_topspeed;
21 float sv_warsowbunny_turnaccel;
22 float sv_warsowbunny_backtosideratio;
23
24 .float ladder_time;
25 .entity ladder_entity;
26 .float gravity;
27 .float swamp_slowdown;
28 .float lastflags;
29 .float lastground;
30 .float wasFlying;
31 .float spectatorspeed;
32
33 /*
34 =============
35 PlayerJump
36
37 When you press the jump key
38 =============
39 */
40 void PlayerJump (void)
41 {
42         float mjumpheight;
43
44         mjumpheight = cvar("sv_jumpvelocity");
45         if (self.waterlevel >= WATERLEVEL_SWIMMING)
46         {
47                 if (self.watertype == CONTENT_WATER)
48                         self.velocity_z = 200;
49                 else if (self.watertype == CONTENT_SLIME)
50                         self.velocity_z = 80;
51                 else
52                         self.velocity_z = 50;
53
54                 return;
55         }
56
57         if (!(self.flags & FL_ONGROUND))
58                 return;
59
60         if(!sv_pogostick)
61                 if (!(self.flags & FL_JUMPRELEASED))
62                         return;
63
64         if(self.health <= g_bloodloss)
65                 return;
66
67         if(g_runematch)
68         {
69                 if(self.runes & RUNE_SPEED)
70                 {
71                         if(self.runes & CURSE_SLOW)
72                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_combo_jumpheight");
73                         else
74                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_jumpheight");
75                 }
76                 else if(self.runes & CURSE_SLOW)
77                 {
78                         mjumpheight = mjumpheight * cvar("g_balance_curse_slow_jumpheight");
79                 }
80         }
81
82         if(g_minstagib && (self.items & IT_INVINCIBLE))
83         {
84                 mjumpheight = mjumpheight * cvar("g_minstagib_speed_jumpheight");
85         }
86
87         if(cvar_string("sv_jumpspeedcap_min") != "")
88                 self.velocity_z = max(cvar("sv_jumpvelocity") * cvar("sv_jumpspeedcap_min"), self.velocity_z);
89         if(cvar_string("sv_jumpspeedcap_max") != "") {
90                 if(trace_fraction < 1 && trace_plane_normal_z < 0.98 && cvar("sv_jumpspeedcap_max_disable_on_ramps")) {
91                         // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
92                         //print("Trace plane normal z: ", ftos(trace_plane_normal_z), ", disabling speed cap!\n");
93                 }
94                 else
95                         self.velocity_z = min(cvar("sv_jumpvelocity") * cvar("sv_jumpspeedcap_max"), self.velocity_z) + trace_ent.velocity_z;
96         }
97
98         if(!(self.lastflags & FL_ONGROUND))
99         {
100                 if(cvar("speedmeter"))
101                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
102                 if(self.lastground < time - 0.3)
103                 {
104                         self.velocity_x *= (1 - cvar("sv_friction_on_land"));
105                         self.velocity_y *= (1 - cvar("sv_friction_on_land"));
106                 }
107                 if(self.jumppadcount > 1)
108                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
109                 self.jumppadcount = 0;
110         }
111
112         self.velocity_z = self.velocity_z + mjumpheight;
113         self.oldvelocity_z = self.velocity_z;
114
115         self.flags &~= FL_ONGROUND;
116         self.flags &~= FL_JUMPRELEASED;
117
118         if (self.crouch)
119                 setanim(self, self.anim_duckjump, FALSE, TRUE, TRUE);
120         else
121                 setanim(self, self.anim_jump, FALSE, TRUE, TRUE);
122
123         if(g_jump_grunt)
124                 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
125
126         self.restart_jump = -1; // restart jump anim next time
127         // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
128 }
129
130 void CheckWaterJump()
131 {
132         local vector start, end;
133
134 // check for a jump-out-of-water
135         makevectors (self.angles);
136         start = self.origin;
137         start_z = start_z + 8;
138         v_forward_z = 0;
139         normalize(v_forward);
140         end = start + v_forward*24;
141         traceline (start, end, TRUE, self);
142         if (trace_fraction < 1)
143         {       // solid at waist
144                 start_z = start_z + self.maxs_z - 8;
145                 end = start + v_forward*24;
146                 self.movedir = trace_plane_normal * -50;
147                 traceline (start, end, TRUE, self);
148                 if (trace_fraction == 1)
149                 {       // open at eye level
150                         self.flags |= FL_WATERJUMP;
151                         self.velocity_z = 225;
152                         self.flags &~= FL_JUMPRELEASED;
153                         self.teleport_time = time + 2;  // safety net
154                         return;
155                 }
156         }
157 };
158
159 float racecar_angle(float forward, float down)
160 {
161         float ret, angle_mult;
162
163         if(forward < 0)
164         {
165                 forward = -forward;
166                 down = -down;
167         }
168
169         ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
170
171         angle_mult = forward / (800 + forward);
172
173         if(ret > 180)
174                 return ret * angle_mult + 360 * (1 - angle_mult);
175         else
176                 return ret * angle_mult;
177 }
178
179 void RaceCarPhysics()
180 {
181         // using this move type for "big rigs"
182         // the engine does not push the entity!
183
184         float accel, steer, f;
185         vector angles_save, rigvel;
186
187         angles_save = self.angles;
188         accel = bound(-1, self.movement_x / sv_maxspeed, 1);
189         steer = bound(-1, self.movement_y / sv_maxspeed, 1);
190
191         if(g_bugrigs_reverse_speeding)
192         {
193                 if(accel < 0)
194                 {
195                         // back accel is DIGITAL
196                         // to prevent speedhack
197                         if(accel < -0.5)
198                                 accel = -1;
199                         else
200                                 accel = 0;
201                 }
202         }
203
204         self.angles_x = 0;
205         self.angles_z = 0;
206         makevectors(self.angles); // new forward direction!
207
208         if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
209         {
210                 float myspeed, upspeed, steerfactor, accelfactor;
211
212                 myspeed = self.velocity * v_forward;
213                 upspeed = self.velocity * v_up;
214
215                 // responsiveness factor for steering and acceleration
216                 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
217                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
218
219                 if(myspeed < 0 && g_bugrigs_reverse_spinning)
220                         steerfactor = -myspeed * g_bugrigs_steer;
221                 else
222                         steerfactor = -myspeed * f * g_bugrigs_steer;
223
224                 if(myspeed < 0 && g_bugrigs_reverse_speeding)
225                         accelfactor = g_bugrigs_accel;
226                 else
227                         accelfactor = f * g_bugrigs_accel;
228                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
229
230                 if(accel < 0)
231                 {
232                         if(myspeed > 0)
233                         {
234                                 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
235                         }
236                         else
237                         {
238                                 if(!g_bugrigs_reverse_speeding)
239                                         myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
240                         }
241                 }
242                 else
243                 {
244                         if(myspeed >= 0)
245                         {
246                                 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
247                         }
248                         else
249                         {
250                                 if(g_bugrigs_reverse_stopping)
251                                         myspeed = 0;
252                                 else
253                                         myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
254                         }
255                 }
256                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
257                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
258
259                 self.angles_y += steer * frametime * steerfactor; // apply steering
260                 makevectors(self.angles); // new forward direction!
261
262                 myspeed += accel * accelfactor * frametime;
263
264                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
265         }
266         else
267         {
268                 myspeed = vlen(self.velocity);
269
270                 // responsiveness factor for steering and acceleration
271                 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
272                 steerfactor = -myspeed * f;
273                 self.angles_y += steer * frametime * steerfactor; // apply steering
274
275                 rigvel = self.velocity;
276                 makevectors(self.angles); // new forward direction!
277         }
278
279         rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
280         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
281         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
282         //MAXIMA: solve(total_acceleration(v) = 0, v);
283
284         if(g_bugrigs_planar_movement)
285         {
286                 vector rigvel_xy, neworigin, up;
287                 float mt;
288
289                 rigvel_z -= frametime * sv_gravity; // 4x gravity plays better
290                 rigvel_xy = rigvel;
291                 rigvel_xy_z = 0;
292
293                 if(g_bugrigs_planar_movement_car_jumping && !g_touchexplode) // touchexplode is a better way to handle collisions
294                         mt = MOVE_NORMAL;
295                 else
296                         mt = MOVE_NOMONSTERS;
297
298                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
299                 up = trace_endpos - self.origin;
300
301                 // BUG RIGS: align the move to the surface instead of doing collision testing
302                 // can we move?
303                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
304
305                 // align to surface
306                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
307
308                 if(trace_fraction < 0.5)
309                 {
310                         trace_fraction = 1;
311                         neworigin = self.origin;
312                 }
313                 else
314                         neworigin = trace_endpos;
315
316                 if(trace_fraction < 1)
317                 {
318                         // now set angles_x so that the car points parallel to the surface
319                         self.angles = vectoangles(
320                                         '1 0 0' * v_forward_x * trace_plane_normal_z
321                                         +
322                                         '0 1 0' * v_forward_y * trace_plane_normal_z
323                                         +
324                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
325                                         );
326                         self.flags |= FL_ONGROUND;
327                 }
328                 else
329                 {
330                         // now set angles_x so that the car points forward, but is tilted in velocity direction
331                         self.flags &~= FL_ONGROUND;
332                 }
333
334                 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
335                 self.movetype = MOVETYPE_NOCLIP;
336         }
337         else
338         {
339                 rigvel_z -= frametime * sv_gravity; // 4x gravity plays better
340                 self.velocity = rigvel;
341                 self.movetype = MOVETYPE_FLY;
342         }
343
344         trace_fraction = 1;
345         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
346         if(trace_fraction != 1)
347         {
348                 self.angles = vectoangles2(
349                                 '1 0 0' * v_forward_x * trace_plane_normal_z
350                                 +
351                                 '0 1 0' * v_forward_y * trace_plane_normal_z
352                                 +
353                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
354                                 trace_plane_normal
355                                 );
356         }
357         else
358         {
359                 vector vel_local;
360
361                 vel_local_x = v_forward * self.velocity;
362                 vel_local_y = v_right * self.velocity;
363                 vel_local_z = v_up * self.velocity;
364
365                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
366                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
367         }
368
369         // smooth the angles
370         vector vf1, vu1, smoothangles;
371         makevectors(self.angles);
372         f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
373         if(f == 0)
374                 f = 1;
375         vf1 = v_forward * f;
376         vu1 = v_up * f;
377         makevectors(angles_save);
378         vf1 = vf1 + v_forward * (1 - f);
379         vu1 = vu1 + v_up * (1 - f);
380         smoothangles = vectoangles2(vf1, vu1);
381         self.angles_x = -smoothangles_x;
382         self.angles_z =  smoothangles_z;
383 }
384
385 float IsMoveInDirection(vector mv, float angle) // key mix factor
386 {
387         if(mv_x == 0 && mv_y == 0)
388                 return 0; // avoid division by zero
389         angle = RAD2DEG * atan2(mv_y, mv_x);
390         angle = remainder(angle, 360) / 45;
391         if(angle >  1)
392                 return 0;
393         if(angle < -1)
394                 return 0;
395         return 1 - fabs(angle);
396 }
397
398 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
399 {
400         float zspeed, xyspeed, dot, k;
401
402 #if 0
403         // this doesn't play well with analog input
404         if(self.movement_x == 0 || self.movement_y != 0)
405                 return; // can't control movement if not moving forward or backward
406         k = 32;
407 #else
408         k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
409         if(k <= 0)
410                 return;
411 #endif
412
413         k *= bound(0, wishspeed / sv_maxairspeed, 1);
414
415         zspeed = self.velocity_z;
416         self.velocity_z = 0;
417         xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
418
419         dot = self.velocity * wishdir;
420
421         if(dot > 0) // we can't change direction while slowing down
422         {
423                 k *= fabs(sv_aircontrol)*pow(dot, sv_aircontrol_power)*frametime;
424                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
425         }
426
427         self.velocity = self.velocity * xyspeed;
428         self.velocity_z = zspeed;
429 }
430
431 // example config for alternate speed clamping:
432 //   sv_airaccel_qw 0.8
433 //   sv_airaccel_sideways_friction 0
434 //   prvm_globalset server speedclamp_mode 1
435 //     (or 2)
436 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float sidefric)
437 {
438         float vel_straight;
439         float vel_z;
440         vector vel_perpend;
441         float step;
442
443         vector vel_xy;
444         float vel_xy_current;
445         float vel_xy_backward, vel_xy_forward;
446         float speedclamp;
447
448         speedclamp = (accelqw < 0);
449         if(speedclamp)
450                 accelqw = -accelqw;
451
452         if(cvar("sv_gameplayfix_q2airaccelerate"))
453                 wishspeed0 = wishspeed;
454
455         vel_straight = self.velocity * wishdir;
456         vel_z = self.velocity_z;
457         vel_xy = self.velocity - vel_z * '0 0 1';
458         vel_perpend = vel_xy - vel_straight * wishdir;
459
460         step = accel * frametime * wishspeed0;
461
462         vel_xy_current  = vlen(vel_xy);
463         vel_xy_forward  = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
464         vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
465         if(vel_xy_backward < 0)
466                 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
467
468         vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
469
470         if(sidefric < 0 && (vel_perpend*vel_perpend))
471                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
472         {
473                 float f, fminimum;
474                 f = max(0, 1 + frametime * wishspeed * sidefric);
475                 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
476                 // this cannot be > 1
477                 if(fminimum <= 0)
478                         vel_perpend = vel_perpend * max(0, f);
479                 else
480                 {
481                         fminimum = sqrt(fminimum);
482                         vel_perpend = vel_perpend * max(fminimum, f);
483                 }
484         }
485         else
486                 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
487         
488         vel_xy = vel_straight * wishdir + vel_perpend;
489         
490         if(speedclamp)
491         {
492                 // ensure we don't get too fast or decelerate faster than we should
493                 vel_xy_current = min(vlen(vel_xy), vel_xy_forward);
494                 if(vel_xy_current > 0) // prevent division by zero
495                         vel_xy = normalize(vel_xy) * vel_xy_current;
496         }
497
498         self.velocity = vel_xy + vel_z * '0 0 1';
499 }
500
501 void PM_AirAccelerate(vector wishdir, float wishspeed)
502 {
503         vector curvel, wishvel, acceldir, curdir;
504         float addspeed, accelspeed, curspeed, f;
505         float dot;
506
507         if(wishspeed == 0)
508                 return;
509
510         curvel = self.velocity;
511         curvel_z = 0;
512         curspeed = vlen(curvel);
513
514         if(wishspeed > curspeed * 1.01)
515         {
516                 wishspeed = min(wishspeed, curspeed + sv_warsowbunny_airforwardaccel * sv_maxspeed * frametime);
517         }
518         else
519         {
520                 f = max(0, (sv_warsowbunny_topspeed - curspeed) / (sv_warsowbunny_topspeed - sv_maxspeed));
521                 wishspeed = max(curspeed, sv_maxspeed) + sv_warsowbunny_accel * f * sv_maxspeed * frametime;
522         }
523         wishvel = wishdir * wishspeed;
524         acceldir = wishvel - curvel;
525         addspeed = vlen(acceldir);
526         acceldir = normalize(acceldir);
527
528         accelspeed = min(addspeed, sv_warsowbunny_turnaccel * sv_maxspeed * frametime);
529
530         if(sv_warsowbunny_backtosideratio < 1)
531         {
532                 curdir = normalize(curvel);
533                 dot = acceldir * curdir;
534                 if(dot < 0)
535                         acceldir = acceldir - (1 - sv_warsowbunny_backtosideratio) * dot * curdir;
536         }
537
538         self.velocity += accelspeed * acceldir;
539 }
540
541 .vector movement_old;
542 .float buttons_old;
543 .vector v_angle_old;
544 .string lastclassname;
545
546 .float() PlayerPhysplug;
547
548 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
549 .float specialcommand_pos;
550 void SpecialCommand()
551 {
552 #ifdef TETRIS
553         TetrisImpulse();
554 #else
555         if(!CheatImpulse(99))
556                 print("A hollow voice says \"Plugh\".\n");
557 #endif
558 }
559
560 float speedaward_speed;
561 string speedaward_holder;
562 void race_send_speedaward(float msg)
563 {
564         // send the best speed of the round
565         WriteByte(msg, SVC_TEMPENTITY);
566         WriteByte(msg, TE_CSQC_RACE);
567         WriteByte(msg, RACE_NET_SPEED_AWARD);
568         WriteInt24_t(msg, floor(speedaward_speed+0.5));
569         WriteString(msg, speedaward_holder);
570 }
571
572 float speedaward_alltimebest;
573 string speedaward_alltimebest_holder;
574 void race_send_speedaward_alltimebest(float msg)
575 {
576         // send the best speed
577         WriteByte(msg, SVC_TEMPENTITY);
578         WriteByte(msg, TE_CSQC_RACE);
579         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
580         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
581         WriteString(msg, speedaward_alltimebest_holder);
582 }
583
584 string GetMapname(void);
585 float speedaward_lastupdate;
586 float speedaward_lastsent;
587 .float jumppadusetime;
588 void SV_PlayerPhysics()
589 {
590         local vector wishvel, wishdir, v;
591         local float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
592         string temps;
593         float buttons_prev;
594         float not_allowed_to_move;
595         string c;
596
597     if(self.PlayerPhysplug)
598         if(self.PlayerPhysplug())
599             return;
600
601         self.race_movetime_frac += frametime;
602         f = floor(self.race_movetime_frac);
603         self.race_movetime_frac -= f;
604         self.race_movetime_count += f;
605         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
606
607         anticheat_physics();
608
609         buttons = self.BUTTON_ATCK + 2 * self.BUTTON_JUMP + 4 * self.BUTTON_ATCK2 + 8 * self.BUTTON_ZOOM + 16 * self.BUTTON_CROUCH + 32 * self.BUTTON_HOOK + 64 * self.BUTTON_USE + 128 * (self.movement_x < 0) + 256 * (self.movement_x > 0) + 512 * (self.movement_y < 0) + 1024 * (self.movement_y > 0);
610
611         if(!buttons)
612                 c = "x";
613         else if(buttons == 1)
614                 c = "1";
615         else if(buttons == 2)
616                 c = " ";
617         else if(buttons == 128)
618                 c = "s";
619         else if(buttons == 256)
620                 c = "w";
621         else if(buttons == 512)
622                 c = "a";
623         else if(buttons == 1024)
624                 c = "d";
625         else
626                 c = "?";
627
628         if(c == substring(specialcommand, self.specialcommand_pos, 1))
629         {
630                 self.specialcommand_pos += 1;
631                 if(self.specialcommand_pos >= strlen(specialcommand))
632                 {
633                         self.specialcommand_pos = 0;
634                         SpecialCommand();
635                         return;
636                 }
637         }
638         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
639                 self.specialcommand_pos = 0;
640
641         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
642         {
643                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
644                         self.parm_idlesince = time;
645         }
646         buttons_prev = self.buttons_old;
647         self.buttons_old = buttons;
648         self.movement_old = self.movement;
649         self.v_angle_old = self.v_angle;
650
651         if(time < self.nickspamtime)
652         if(self.nickspamcount >= cvar("g_nick_flood_penalty_yellow"))
653         {
654                 // slight annoyance for nick change scripts
655                 self.movement = -1 * self.movement;
656                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
657
658                 if(self.nickspamcount >= cvar("g_nick_flood_penalty_red")) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
659                 {
660                         self.angles_x = random() * 360;
661                         self.angles_y = random() * 360;
662                         // at least I'm not forcing retardedview by also assigning to angles_z
663                         self.fixangle = 1;
664                 }
665         }
666
667         if (self.punchangle != '0 0 0')
668         {
669                 f = vlen(self.punchangle) - 10 * frametime;
670                 if (f > 0)
671                         self.punchangle = normalize(self.punchangle) * f;
672                 else
673                         self.punchangle = '0 0 0';
674         }
675
676         if (self.punchvector != '0 0 0')
677         {
678                 f = vlen(self.punchvector) - 30 * frametime;
679                 if (f > 0)
680                         self.punchvector = normalize(self.punchvector) * f;
681                 else
682                         self.punchvector = '0 0 0';
683         }
684
685         if (clienttype(self) == CLIENTTYPE_BOT)
686         {
687                 if(playerdemo_read())
688                         return;
689                 bot_think();
690         }
691         
692         MUTATOR_CALLHOOK(PlayerPhysics);
693
694         self.items &~= IT_USING_JETPACK;
695
696         if(self.classname == "player")
697         {
698                 if(self.race_penalty)
699                         if(time > self.race_penalty)
700                                 self.race_penalty = 0;
701
702                 not_allowed_to_move = 0;
703                 if(self.race_penalty)
704                         not_allowed_to_move = 1;
705                 if(!cvar("sv_ready_restart_after_countdown"))
706                 if(time < game_starttime)
707                         not_allowed_to_move = 1;
708
709                 if(not_allowed_to_move)
710                 {
711                         self.velocity = '0 0 0';
712                         self.movetype = MOVETYPE_NONE;
713                         self.disableclientprediction = 2;
714                 }
715                 else if(self.disableclientprediction == 2)
716                 {
717                         if(self.movetype == MOVETYPE_NONE)
718                                 self.movetype = MOVETYPE_WALK;
719                         self.disableclientprediction = 0;
720                 }
721         }
722
723         if (self.movetype == MOVETYPE_NONE)
724                 return;
725
726         maxspd_mod = 1;
727
728         if(g_runematch)
729         {
730                 if(self.runes & RUNE_SPEED)
731                 {
732                         if(self.runes & CURSE_SLOW)
733                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_combo_moverate");
734                         else
735                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_moverate");
736                 }
737                 else if(self.runes & CURSE_SLOW)
738                 {
739                         maxspd_mod = maxspd_mod * cvar("g_balance_curse_slow_moverate");
740                 }
741         }
742
743         if(g_minstagib && (self.items & IT_INVINCIBLE))
744         {
745                 maxspd_mod = cvar("g_minstagib_speed_moverate");
746         }
747
748         if(g_nexball && self.ballcarried)
749         {
750                 maxspd_mod = cvar("g_nexball_basketball_carrier_speed");
751         }
752
753         swampspd_mod = 1;
754         if(self.in_swamp) {
755                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
756         }
757
758         if(self.classname != "player")
759         {
760                 maxspd_mod = cvar("sv_spectator_speed_multiplier");
761                 if(!self.spectatorspeed)
762                         self.spectatorspeed = maxspd_mod;
763                 if(self.impulse && self.impulse <= 19)
764                 {
765                         if(self.lastclassname != "player")
766                         {
767                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
768                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
769                                 else if(self.impulse == 11)
770                                         self.spectatorspeed = maxspd_mod;
771                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
772                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
773                                 else if(self.impulse >= 1 && self.impulse <= 9)
774                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
775                         } // otherwise just clear
776                         self.impulse = 0;
777                 }
778                 maxspd_mod = self.spectatorspeed;
779         }
780
781         spd = max(sv_maxspeed, sv_maxairspeed) * maxspd_mod * swampspd_mod;
782         if(self.speed != spd)
783         {
784                 self.speed = spd;
785                 temps = ftos(spd);
786                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
787                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
788                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
789                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
790         }
791
792         maxspd_mod *= swampspd_mod; // only one common speed modder please!
793         swampspd_mod = 1;
794
795         // if dead, behave differently
796         if (self.deadflag)
797                 goto end;
798
799         if (!self.fixangle && !g_bugrigs)
800         {
801                 self.angles_x = 0;
802                 self.angles_y = self.v_angle_y;
803                 self.angles_z = 0;
804         }
805
806         if(self.flags & FL_ONGROUND)
807         if(self.wasFlying)
808         {
809                 self.wasFlying = 0;
810
811                 if(self.waterlevel < WATERLEVEL_SWIMMING)
812                 if(time >= self.ladder_time)
813                 if not(self.hook)
814                 {
815                         self.nextstep = time + 0.3 + random() * 0.1;
816                         trace_dphitq3surfaceflags = 0;
817                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
818                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
819                         {
820                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
821                                         GlobalSound(globalsound_metalfall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
822                                 else
823                                         GlobalSound(globalsound_fall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
824                         }
825                 }
826         }
827
828         if(IsFlying(self))
829                 self.wasFlying = 1;
830
831         if(self.classname == "player")
832         {
833                 if(sv_doublejump && time - self.jumppadusetime > 2 * sys_frametime)
834                 {
835                         tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
836                         self.flags &~= FL_ONGROUND;
837                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
838                                 self.flags |= FL_ONGROUND;
839                 }
840
841                 if (self.BUTTON_JUMP)
842                         PlayerJump ();
843                 else
844                         self.flags |= FL_JUMPRELEASED;
845
846                 if (self.waterlevel == WATERLEVEL_SWIMMING)
847                         CheckWaterJump ();
848         }
849
850         if (self.flags & FL_WATERJUMP )
851         {
852                 self.velocity_x = self.movedir_x;
853                 self.velocity_y = self.movedir_y;
854                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
855                 {
856                         self.flags &~= FL_WATERJUMP;
857                         self.teleport_time = 0;
858                 }
859         }
860         else if (g_bugrigs && self.classname == "player")
861         {
862                 RaceCarPhysics();
863         }
864         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
865         {
866                 // noclipping or flying
867                 self.flags &~= FL_ONGROUND;
868
869                 self.velocity = self.velocity * (1 - frametime * sv_friction);
870                 makevectors(self.v_angle);
871                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
872                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
873                 // acceleration
874                 wishdir = normalize(wishvel);
875                 wishspeed = vlen(wishvel);
876                 if (wishspeed > sv_maxspeed*maxspd_mod)
877                         wishspeed = sv_maxspeed*maxspd_mod;
878                 if (time >= self.teleport_time)
879                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
880         }
881         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
882         {
883                 // swimming
884                 self.flags &~= FL_ONGROUND;
885
886                 makevectors(self.v_angle);
887                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
888                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
889                 if (wishvel == '0 0 0')
890                         wishvel = '0 0 -60'; // drift towards bottom
891
892                 wishdir = normalize(wishvel);
893                 wishspeed = vlen(wishvel);
894                 if (wishspeed > sv_maxspeed*maxspd_mod)
895                         wishspeed = sv_maxspeed*maxspd_mod;
896                 wishspeed = wishspeed * 0.7;
897
898                 // water friction
899                 self.velocity = self.velocity * (1 - frametime * sv_friction);
900
901                 // water acceleration
902                 PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
903         }
904         else if (time < self.ladder_time)
905         {
906                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
907                 self.flags &~= FL_ONGROUND;
908
909                 self.velocity = self.velocity * (1 - frametime * sv_friction);
910                 makevectors(self.v_angle);
911                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
912                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
913                 if (self.gravity)
914                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
915                 else
916                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
917                 if (self.ladder_entity.classname == "func_water")
918                 {
919                         f = vlen(wishvel);
920                         if (f > self.ladder_entity.speed)
921                                 wishvel = wishvel * (self.ladder_entity.speed / f);
922
923                         self.watertype = self.ladder_entity.skin;
924                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
925                         if ((self.origin_z + self.view_ofs_z) < f)
926                                 self.waterlevel = WATERLEVEL_SUBMERGED;
927                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
928                                 self.waterlevel = WATERLEVEL_SWIMMING;
929                         else if ((self.origin_z + self.mins_z + 1) < f)
930                                 self.waterlevel = WATERLEVEL_WETFEET;
931                         else
932                         {
933                                 self.waterlevel = WATERLEVEL_NONE;
934                                 self.watertype = CONTENT_EMPTY;
935                         }
936                 }
937                 // acceleration
938                 wishdir = normalize(wishvel);
939                 wishspeed = vlen(wishvel);
940                 if (wishspeed > sv_maxspeed*maxspd_mod)
941                         wishspeed = sv_maxspeed*maxspd_mod;
942                 if (time >= self.teleport_time)
943                 {
944                         // water acceleration
945                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
946                 }
947         }
948         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!cvar("g_jetpack_fuel") || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
949         {
950                 //makevectors(self.v_angle_y * '0 1 0');
951                 makevectors(self.v_angle);
952                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
953                 // add remaining speed as Z component
954                 maxairspd = sv_maxairspeed*max(1, maxspd_mod);
955                 // fix speedhacks :P
956                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
957                 // add the unused velocity as up component
958                 wishvel_z = 0;
959
960                 // if(self.BUTTON_JUMP)
961                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
962
963                 // it is now normalized, so...
964                 float a_side, a_up, a_add, a_diff;
965                 a_side = cvar("g_jetpack_acceleration_side");
966                 a_up = cvar("g_jetpack_acceleration_up");
967                 a_add = cvar("g_jetpack_antigravity") * sv_gravity;
968
969                 wishvel_x *= a_side;
970                 wishvel_y *= a_side;
971                 wishvel_z *= a_up;
972                 wishvel_z += a_add;
973
974                 float best;
975                 best = 0;
976                 //////////////////////////////////////////////////////////////////////////////////////
977                 // finding the maximum over all vectors of above form
978                 // with wishvel having an absolute value of 1
979                 //////////////////////////////////////////////////////////////////////////////////////
980                 // we're finding the maximum over
981                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
982                 // for z in the range from -1 to 1
983                 //////////////////////////////////////////////////////////////////////////////////////
984                 // maximum is EITHER attained at the single extreme point:
985                 a_diff = a_side * a_side - a_up * a_up;
986                 if(a_diff != 0)
987                 {
988                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
989                         if(f > -1 && f < 1) // can it be attained?
990                         {
991                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
992                                 //print("middle\n");
993                         }
994                 }
995                 // OR attained at z = 1:
996                 f = (a_up + a_add) * (a_up + a_add);
997                 if(f > best)
998                 {
999                         best = f;
1000                         //print("top\n");
1001                 }
1002                 // OR attained at z = -1:
1003                 f = (a_up - a_add) * (a_up - a_add);
1004                 if(f > best)
1005                 {
1006                         best = f;
1007                         //print("bottom\n");
1008                 }
1009                 best = sqrt(best);
1010                 //////////////////////////////////////////////////////////////////////////////////////
1011
1012                 //print("best possible acceleration: ", ftos(best), "\n");
1013
1014                 float fxy, fz;
1015                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / cvar("g_jetpack_maxspeed_side"), 1);
1016                 if(wishvel_z - sv_gravity > 0)
1017                         fz = bound(0, 1 - self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1018                 else
1019                         fz = bound(0, 1 + self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1020
1021                 float fvel;
1022                 fvel = vlen(wishvel);
1023                 wishvel_x *= fxy;
1024                 wishvel_y *= fxy;
1025                 wishvel_z = (wishvel_z - sv_gravity) * fz + sv_gravity;
1026
1027                 fvel = min(1, vlen(wishvel) / best);
1028                 if(cvar("g_jetpack_fuel") && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1029                         f = min(1, self.ammo_fuel / (cvar("g_jetpack_fuel") * frametime * fvel));
1030                 else
1031                         f = 1;
1032
1033                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1034
1035                 if (f > 0 && wishvel != '0 0 0')
1036                 {
1037                         self.velocity = self.velocity + wishvel * f * frametime;
1038                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1039                                 self.ammo_fuel -= cvar("g_jetpack_fuel") * frametime * fvel * f;
1040                         self.flags &~= FL_ONGROUND;
1041                         self.items |= IT_USING_JETPACK;
1042
1043                         // jetpack also inhibits health regeneration, but only for 1 second
1044                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
1045                 }
1046         }
1047         else if (self.flags & FL_ONGROUND)
1048         {
1049                 // we get here if we ran out of ammo
1050                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1051                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1052
1053                 // walking
1054                 makevectors(self.v_angle_y * '0 1 0');
1055                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1056
1057                 if(!(self.lastflags & FL_ONGROUND))
1058                 {
1059                         if(cvar("speedmeter"))
1060                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1061                         if(self.lastground < time - 0.3)
1062                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
1063                         if(self.jumppadcount > 1)
1064                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1065                         self.jumppadcount = 0;
1066                 }
1067
1068 #ifdef LETS_TEST_FTEQCC
1069                 if(self.velocity_x || self.velocity_y)
1070                 {
1071                         // good
1072                 }
1073                 else
1074                 {
1075                         if(self.velocity_x)
1076                                 checkclient();
1077                         if(self.velocity_y)
1078                                 checkclient();
1079                 }
1080 #endif
1081
1082                 v = self.velocity;
1083                 v_z = 0;
1084                 f = vlen(v);
1085                 if(f > 0)
1086                 {
1087                         if (f < sv_stopspeed)
1088                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
1089                         else
1090                                 f = 1 - frametime * sv_friction;
1091                         if (f > 0)
1092                                 self.velocity = self.velocity * f;
1093                         else
1094                                 self.velocity = '0 0 0';
1095                 }
1096
1097                 // acceleration
1098                 wishdir = normalize(wishvel);
1099                 wishspeed = vlen(wishvel);
1100                 if (wishspeed > sv_maxspeed*maxspd_mod)
1101                         wishspeed = sv_maxspeed*maxspd_mod;
1102                 if (self.crouch)
1103                         wishspeed = wishspeed * 0.5;
1104                 if (time >= self.teleport_time)
1105                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
1106         }
1107         else
1108         {
1109                 float wishspeed0;
1110                 // we get here if we ran out of ammo
1111                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1112                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1113
1114                 if(maxspd_mod < 1)
1115                 {
1116                         maxairspd = sv_maxairspeed*maxspd_mod;
1117                         airaccel = sv_airaccelerate*maxspd_mod;
1118                 }
1119                 else
1120                 {
1121                         maxairspd = sv_maxairspeed;
1122                         airaccel = sv_airaccelerate;
1123                 }
1124                 // airborn
1125                 makevectors(self.v_angle_y * '0 1 0');
1126                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1127                 // acceleration
1128                 wishdir = normalize(wishvel);
1129                 wishspeed = wishspeed0 = vlen(wishvel);
1130                 if (wishspeed0 > sv_maxspeed*maxspd_mod)
1131                         wishspeed0 = sv_maxspeed*maxspd_mod;
1132                 if (wishspeed > maxairspd)
1133                         wishspeed = maxairspd;
1134                 if (self.crouch)
1135                         wishspeed = wishspeed * 0.5;
1136                 if (time >= self.teleport_time)
1137                 {
1138                         float accelerating;
1139                         float wishspeed2;
1140                         float airaccelqw;
1141
1142                         airaccelqw = sv_airaccel_qw;
1143                         accelerating = (self.velocity * wishdir > 0);
1144                         wishspeed2 = wishspeed;
1145
1146                         // CPM
1147                         if(sv_airstopaccelerate)
1148                                 if(self.velocity * wishdir < 0)
1149                                         airaccel = sv_airstopaccelerate*maxspd_mod;
1150                         // this doesn't play well with analog input, but can't r
1151                         // fixed like the AirControl can. So, don't set the maxa
1152                         // cvars when you want to support analog input.
1153                         if(self.movement_x == 0 && self.movement_y != 0)
1154                         {
1155                                 if(sv_maxairstrafespeed)
1156                                 {
1157                                         wishspeed = min(wishspeed, sv_maxairstrafespeed*maxspd_mod);
1158                                         if(sv_maxairstrafespeed < sv_maxairspeed)
1159                                                 airaccelqw = 1;
1160                                 }
1161                                 if(sv_airstrafeaccelerate)
1162                                 {
1163                                         airaccel = sv_airstrafeaccelerate*maxspd_mod;
1164                                         if(sv_airstrafeaccelerate > sv_airaccelerate)
1165                                                 airaccelqw = 1;
1166                                 }
1167                         }
1168                         // !CPM
1169
1170                         if(sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1171                                 PM_AirAccelerate(wishdir, wishspeed);
1172                         else
1173                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, sv_airaccel_sideways_friction / maxairspd);
1174
1175                         if(sv_aircontrol)
1176                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1177                 }
1178         }
1179
1180         if((g_cts || g_race) && self.classname != "observer") {
1181                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1182                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1183                         speedaward_holder = self.netname;
1184                         speedaward_lastupdate = time;
1185                 }
1186                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1187                         string rr;
1188                         if(g_cts)
1189                                 rr = CTS_RECORD;
1190                         else
1191                                 rr = RACE_RECORD;
1192                         race_send_speedaward(MSG_ALL);
1193                         speedaward_lastsent = speedaward_speed;
1194                         if (speedaward_speed > speedaward_alltimebest) {
1195                                 speedaward_alltimebest = speedaward_speed;
1196                                 speedaward_alltimebest_holder = speedaward_holder;
1197                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1198                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/netname"), speedaward_alltimebest_holder);
1199                                 race_send_speedaward_alltimebest(MSG_ALL);
1200                         }
1201                 }
1202         }
1203 :end
1204         if(self.flags & FL_ONGROUND)
1205                 self.lastground = time;
1206
1207         self.lastflags = self.flags;
1208         self.lastclassname = self.classname;
1209 };