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