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