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