]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_physics.qc
hehe, make it look better lined up
[divverent/nexuiz.git] / data / qcsrc / server / cl_physics.qc
1 .float race_penalty;
2
3 float sv_accelerate;
4 float sv_friction;
5 float sv_maxspeed;
6 float sv_airaccelerate;
7 float sv_maxairspeed;
8 float sv_stopspeed;
9 float sv_gravity;
10 float sv_airaccel_sideways_friction;
11 float sv_airaccel_qw;
12 float sv_airstopaccelerate;
13 float sv_airstrafeaccelerate;
14 float sv_maxairstrafespeed;
15 float sv_aircontrol;
16 float sv_warsowbunny_airforwardaccel;
17 float sv_warsowbunny_accel;
18 float sv_warsowbunny_topspeed;
19 float sv_warsowbunny_turnaccel;
20 float sv_warsowbunny_backtosideratio;
21
22 .float ladder_time;
23 .entity ladder_entity;
24 .float gravity;
25 .float swamp_slowdown;
26 .float lastflags;
27 .float lastground;
28 .float wasFlying;
29 .float spectatorspeed;
30
31 #define SHTEST_DELTA 10
32 #define SHTEST_THRESHOLD 1.1
33 .float shtest_next;
34 .float shtest_accumulator;
35 .float doublejump_nextjumptime;
36
37 /*
38 =============
39 PlayerJump
40
41 When you press the jump key
42 =============
43 */
44 void PlayerJump (void)
45 {
46         float mjumpheight;
47
48         mjumpheight = cvar("sv_jumpvelocity");
49         if (self.waterlevel >= WATERLEVEL_SWIMMING)
50         {
51                 if (self.watertype == CONTENT_WATER)
52                         self.velocity_z = 200;
53                 else if (self.watertype == CONTENT_SLIME)
54                         self.velocity_z = 80;
55                 else
56                         self.velocity_z = 50;
57
58                 return;
59         }
60
61         if (!(self.flags & FL_ONGROUND))
62                 return;
63
64         if(!sv_pogostick)
65                 if (!(self.flags & FL_JUMPRELEASED))
66                         return;
67
68         if(self.health <= g_bloodloss)
69                 return;
70
71         if(sv_doublejump)
72                 if(time < self.doublejump_nextjumptime)
73                         return;
74
75         if(g_runematch)
76         {
77                 if(self.runes & RUNE_SPEED)
78                 {
79                         if(self.runes & CURSE_SLOW)
80                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_combo_jumpheight");
81                         else
82                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_jumpheight");
83                 }
84                 else if(self.runes & CURSE_SLOW)
85                 {
86                         mjumpheight = mjumpheight * cvar("g_balance_curse_slow_jumpheight");
87                 }
88         }
89
90         if(g_minstagib && (self.items & IT_INVINCIBLE))
91         {
92                 mjumpheight = mjumpheight * cvar("g_minstagib_speed_jumpheight");
93         }
94
95         if(!(self.lastflags & FL_ONGROUND))
96         {
97                 if(cvar("speedmeter"))
98                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
99                 if(self.lastground < time - 0.3)
100                         self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
101                 if(self.jumppadcount > 1)
102                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
103                 self.jumppadcount = 0;
104         }
105
106         self.velocity_z = self.velocity_z + mjumpheight;
107         self.oldvelocity_z = self.velocity_z;
108
109         self.flags &~= FL_ONGROUND;
110         self.flags &~= FL_JUMPRELEASED;
111
112         if (self.crouch)
113                 setanim(self, self.anim_duckjump, FALSE, TRUE, TRUE);
114         else
115                 setanim(self, self.anim_jump, FALSE, TRUE, TRUE);
116
117         if(g_jump_grunt)
118                 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
119
120         if(sv_doublejump)
121         {
122                 // we're moving upwards at self.velocity_z
123                 // only allow jumping after we got 3 units upwards
124                 // so we for sure leave the FL_ONGROUND check
125                 //
126                 // but as this sucks because of factoring in gravity, we'll just do it
127                 // for 4 units, and constant velocity
128                 self.doublejump_nextjumptime = time + 4 / max(40, self.velocity_z); // max 0.1s blocking of jumps
129         }
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 accel, float accelqw, float sidefric)
411 {
412         float vel_straight;
413         float vel_z;
414         vector vel_perpend;
415         float addspeed;
416
417         vel_straight = self.velocity * wishdir;
418         vel_z = self.velocity_z;
419         vel_perpend = self.velocity - vel_straight * wishdir - vel_z * '0 0 1';
420
421         addspeed = wishspeed - vel_straight;
422         if(addspeed > 0)
423                 vel_straight = vel_straight + min(addspeed, accel * frametime * wishspeed) * accelqw;
424         if(wishspeed > 0)
425                 vel_straight = vel_straight + min(wishspeed, accel * frametime * wishspeed) * (1 - accelqw);
426
427         vel_perpend = vel_perpend * (1 - frametime * wishspeed * sidefric);
428
429         self.velocity = vel_straight * wishdir + vel_z * '0 0 1' + vel_perpend;
430 }
431
432 void PM_AirAccelerate(vector wishdir, float wishspeed)
433 {
434         vector curvel, wishvel, acceldir, curdir;
435         float addspeed, accelspeed, curspeed, f;
436         float dot;
437
438         if(wishspeed == 0)
439                 return;
440
441         curvel = self.velocity;
442         curvel_z = 0;
443         curspeed = vlen(curvel);
444
445         if(wishspeed > curspeed * 1.01)
446         {
447                 wishspeed = min(wishspeed, curspeed + sv_warsowbunny_airforwardaccel * sv_maxspeed * frametime);
448         }
449         else
450         {
451                 f = max(0, (sv_warsowbunny_topspeed - curspeed) / (sv_warsowbunny_topspeed - sv_maxspeed));
452                 wishspeed = max(curspeed, sv_maxspeed) + sv_warsowbunny_accel * f * sv_maxspeed * frametime;
453         }
454         wishvel = wishdir * wishspeed;
455         acceldir = wishvel - curvel;
456         addspeed = vlen(acceldir);
457         acceldir = normalize(acceldir);
458
459         accelspeed = min(addspeed, sv_warsowbunny_turnaccel * sv_maxspeed * frametime);
460
461         if(sv_warsowbunny_backtosideratio < 1)
462         {
463                 curdir = normalize(curvel);
464                 dot = acceldir * curdir;
465                 if(dot < 0)
466                         acceldir = acceldir - (1 - sv_warsowbunny_backtosideratio) * dot * curdir;
467         }
468
469         self.velocity += accelspeed * acceldir;
470 }
471
472 .vector movement_old;
473 .float buttons_old;
474 .vector v_angle_old;
475 .string lastclassname;
476
477 void Nixnex_GiveCurrentWeapon();
478 .float() PlayerPhysplug;
479 void SV_PlayerPhysics()
480 {
481         local vector wishvel, wishdir, v;
482         local float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, shtest_score, buttons;
483         string temps;
484         float buttons_prev;
485         float not_allowed_to_move;
486
487     if(self.PlayerPhysplug)
488         if(self.PlayerPhysplug())
489             return;
490
491         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;
492         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
493         {
494                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
495                         self.parm_idlesince = time;
496         }
497         buttons_prev = self.buttons_old;
498         self.buttons_old = buttons;
499         self.movement_old = self.movement;
500         self.v_angle_old = self.v_angle;
501
502         if(time < self.nickspamtime)
503         if(self.nickspamcount >= cvar("g_nick_flood_penalty_yellow"))
504         {
505                 // slight annoyance for nick change scripts
506                 self.movement = -1 * self.movement;
507                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
508
509                 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!
510                 {
511                         self.angles_x = random() * 360;
512                         self.angles_y = random() * 360;
513                         // at least I'm not forcing retardedview by also assigning to angles_z
514                         self.fixangle = 1;
515                 }
516         }
517
518         if(time > self.shtest_next)
519         {
520                 if(self.shtest_next > 0)
521                 {
522                         // self.shtest_accumulator:
523                         //   started at time - SHTEST_DELTA
524                         //   should be at SHTEST_DELTA
525                         shtest_score = self.shtest_accumulator / (SHTEST_DELTA + time - self.shtest_next);
526                         if(shtest_score > SHTEST_THRESHOLD)
527                                 print("TIME PARADOX: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
528                         else if(cvar("developer_shtest"))
529                                 dprint("okay: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
530                 }
531                 self.shtest_next = time + SHTEST_DELTA;
532                 self.shtest_accumulator = 0;
533         }
534         self.shtest_accumulator += frametime;
535
536         if (clienttype(self) == CLIENTTYPE_BOT)
537                 bot_think();
538
539         self.items &~= IT_USING_JETPACK;
540
541         if(self.classname == "player")
542         {
543                 if(self.race_penalty)
544                         if(time > self.race_penalty)
545                                 self.race_penalty = 0;
546
547                 not_allowed_to_move = 0;
548                 if(self.race_penalty)
549                         not_allowed_to_move = 1;
550                 if(!cvar("sv_ready_restart_after_countdown"))
551                 if(time < game_starttime)
552                         not_allowed_to_move = 1;
553
554                 if(not_allowed_to_move)
555                 {
556                         self.velocity = '0 0 0';
557                         self.movetype = MOVETYPE_NONE;
558                         self.disableclientprediction = 2;
559                 }
560                 else if(self.disableclientprediction == 2)
561                 {
562                         if(self.movetype == MOVETYPE_NONE)
563                                 self.movetype = MOVETYPE_WALK;
564                         self.disableclientprediction = 0;
565                 }
566         }
567
568         if (self.movetype == MOVETYPE_NONE)
569                 return;
570
571         if (self.punchangle != '0 0 0')
572         {
573                 f = vlen(self.punchangle) - 10 * frametime;
574                 if (f > 0)
575                         self.punchangle = normalize(self.punchangle) * f;
576                 else
577                         self.punchangle = '0 0 0';
578         }
579
580         if (self.punchvector != '0 0 0')
581         {
582                 f = vlen(self.punchvector) - 30 * frametime;
583                 if (f > 0)
584                         self.punchvector = normalize(self.punchvector) * f;
585                 else
586                         self.punchvector = '0 0 0';
587         }
588
589         maxspd_mod = 1;
590
591         if(g_runematch)
592         {
593                 if(self.runes & RUNE_SPEED)
594                 {
595                         if(self.runes & CURSE_SLOW)
596                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_combo_moverate");
597                         else
598                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_moverate");
599                 }
600                 else if(self.runes & CURSE_SLOW)
601                 {
602                         maxspd_mod = maxspd_mod * cvar("g_balance_curse_slow_moverate");
603                 }
604         }
605
606         if(g_minstagib && (self.items & IT_INVINCIBLE))
607         {
608                 maxspd_mod = cvar("g_minstagib_speed_moverate");
609         }
610
611         if(g_nexball && self.ballcarried)
612         {
613                 maxspd_mod = cvar("g_nexball_basketball_carrier_speed");
614         }
615
616         swampspd_mod = 1;
617         if(self.in_swamp) {
618                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
619         }
620
621         if(self.classname != "player")
622         {
623                 maxspd_mod = cvar("sv_spectator_speed_multiplier");
624                 if(!self.spectatorspeed)
625                         self.spectatorspeed = maxspd_mod;
626                 if(self.impulse && self.impulse <= 19)
627                 {
628                         if(self.lastclassname != "player")
629                         {
630                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
631                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
632                                 else if(self.impulse == 11)
633                                         self.spectatorspeed = maxspd_mod;
634                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
635                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
636                                 else if(self.impulse >= 1 && self.impulse <= 9)
637                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
638                         } // otherwise just clear
639                         self.impulse = 0;
640                 }
641                 maxspd_mod = self.spectatorspeed;
642         }
643
644         spd = max(sv_maxspeed, sv_maxairspeed) * maxspd_mod * swampspd_mod;
645         if(self.speed != spd)
646         {
647                 self.speed = spd;
648                 temps = ftos(spd);
649                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
650                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
651                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
652                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
653         }
654
655         maxspd_mod *= swampspd_mod; // only one common speed modder please!
656         swampspd_mod = 1;
657
658         // if dead, behave differently
659         if (self.deadflag)
660                 goto end;
661
662         if (!self.fixangle && !g_bugrigs)
663         {
664                 self.angles_x = 0;
665                 self.angles_y = self.v_angle_y;
666                 self.angles_z = 0;
667         }
668
669         if(self.flags & FL_ONGROUND)
670         if(self.wasFlying)
671         {
672                 self.wasFlying = 0;
673
674                 if(self.waterlevel < WATERLEVEL_SWIMMING)
675                 if(time >= self.ladder_time)
676                 if not(self.hook)
677                 {
678                         self.nextstep = time + 0.3 + random() * 0.1;
679                         trace_dphitq3surfaceflags = 0;
680                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
681                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
682                         {
683                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
684                                         GlobalSound(globalsound_metalfall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
685                                 else
686                                         GlobalSound(globalsound_fall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
687                         }
688                 }
689         }
690
691         if(IsFlying(self))
692                 self.wasFlying = 1;
693
694         if(self.classname == "player")
695         {
696                 if(sv_doublejump)
697                 {
698                         self.flags &~= FL_ONGROUND;
699                         tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 1', MOVE_NORMAL, self);
700                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
701                                 self.flags |= FL_ONGROUND;
702                 }
703
704                 if (self.BUTTON_JUMP)
705                         PlayerJump ();
706                 else
707                         self.flags |= FL_JUMPRELEASED;
708
709                 if (self.waterlevel == WATERLEVEL_SWIMMING)
710                         CheckWaterJump ();
711         }
712
713         if (self.flags & FL_WATERJUMP )
714         {
715                 self.velocity_x = self.movedir_x;
716                 self.velocity_y = self.movedir_y;
717                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
718                 {
719                         self.flags &~= FL_WATERJUMP;
720                         self.teleport_time = 0;
721                 }
722         }
723         else if (g_bugrigs && self.classname == "player")
724         {
725                 RaceCarPhysics();
726         }
727         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
728         {
729                 // noclipping or flying
730                 self.flags &~= FL_ONGROUND;
731
732                 self.velocity = self.velocity * (1 - frametime * sv_friction);
733                 makevectors(self.v_angle);
734                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
735                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
736                 // acceleration
737                 wishdir = normalize(wishvel);
738                 wishspeed = vlen(wishvel);
739                 if (wishspeed > sv_maxspeed*maxspd_mod)
740                         wishspeed = sv_maxspeed*maxspd_mod;
741                 if (time >= self.teleport_time)
742                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
743         }
744         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
745         {
746                 // swimming
747                 self.flags &~= FL_ONGROUND;
748
749                 makevectors(self.v_angle);
750                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
751                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
752                 if (wishvel == '0 0 0')
753                         wishvel = '0 0 -60'; // drift towards bottom
754
755                 wishdir = normalize(wishvel);
756                 wishspeed = vlen(wishvel);
757                 if (wishspeed > sv_maxspeed*maxspd_mod)
758                         wishspeed = sv_maxspeed*maxspd_mod;
759                 wishspeed = wishspeed * 0.7;
760
761                 // water friction
762                 self.velocity = self.velocity * (1 - frametime * sv_friction);
763
764                 // water acceleration
765                 PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
766         }
767         else if (time < self.ladder_time)
768         {
769                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
770                 self.flags &~= FL_ONGROUND;
771
772                 self.velocity = self.velocity * (1 - frametime * sv_friction);
773                 makevectors(self.v_angle);
774                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
775                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
776                 if (self.gravity)
777                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
778                 else
779                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
780                 if (self.ladder_entity.classname == "func_water")
781                 {
782                         f = vlen(wishvel);
783                         if (f > self.ladder_entity.speed)
784                                 wishvel = wishvel * (self.ladder_entity.speed / f);
785
786                         self.watertype = self.ladder_entity.skin;
787                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
788                         if ((self.origin_z + self.view_ofs_z) < f)
789                                 self.waterlevel = WATERLEVEL_SUBMERGED;
790                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
791                                 self.waterlevel = WATERLEVEL_SWIMMING;
792                         else if ((self.origin_z + self.mins_z + 1) < f)
793                                 self.waterlevel = WATERLEVEL_WETFEET;
794                         else
795                         {
796                                 self.waterlevel = WATERLEVEL_NONE;
797                                 self.watertype = CONTENT_EMPTY;
798                         }
799                 }
800                 // acceleration
801                 wishdir = normalize(wishvel);
802                 wishspeed = vlen(wishvel);
803                 if (wishspeed > sv_maxspeed*maxspd_mod)
804                         wishspeed = sv_maxspeed*maxspd_mod;
805                 if (time >= self.teleport_time)
806                 {
807                         // water acceleration
808                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
809                 }
810         }
811         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!cvar("g_jetpack_fuel") || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
812         {
813                 //makevectors(self.v_angle_y * '0 1 0');
814                 makevectors(self.v_angle);
815                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
816                 // add remaining speed as Z component
817                 maxairspd = sv_maxairspeed*max(1, maxspd_mod);
818                 // fix speedhacks :P
819                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
820                 // add the unused velocity as up component
821                 wishvel_z = 0;
822
823                 // if(self.BUTTON_JUMP)
824                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
825
826                 // it is now normalized, so...
827                 float a_side, a_up, a_add, a_diff;
828                 a_side = cvar("g_jetpack_acceleration_side");
829                 a_up = cvar("g_jetpack_acceleration_up");
830                 a_add = cvar("g_jetpack_antigravity") * sv_gravity;
831
832                 wishvel_x *= a_side;
833                 wishvel_y *= a_side;
834                 wishvel_z *= a_up;
835                 wishvel_z += a_add;
836
837                 float best;
838                 best = 0;
839                 //////////////////////////////////////////////////////////////////////////////////////
840                 // finding the maximum over all vectors of above form
841                 // with wishvel having an absolute value of 1
842                 //////////////////////////////////////////////////////////////////////////////////////
843                 // we're finding the maximum over
844                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
845                 // for z in the range from -1 to 1
846                 //////////////////////////////////////////////////////////////////////////////////////
847                 // maximum is EITHER attained at the single extreme point:
848                 a_diff = a_side * a_side - a_up * a_up;
849                 if(a_diff != 0)
850                 {
851                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
852                         if(f > -1 && f < 1) // can it be attained?
853                         {
854                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
855                                 //print("middle\n");
856                         }
857                 }
858                 // OR attained at z = 1:
859                 f = (a_up + a_add) * (a_up + a_add);
860                 if(f > best)
861                 {
862                         best = f;
863                         //print("top\n");
864                 }
865                 // OR attained at z = -1:
866                 f = (a_up - a_add) * (a_up - a_add);
867                 if(f > best)
868                 {
869                         best = f;
870                         //print("bottom\n");
871                 }
872                 best = sqrt(best);
873                 //////////////////////////////////////////////////////////////////////////////////////
874
875                 //print("best possible acceleration: ", ftos(best), "\n");
876
877                 float fxy, fz;
878                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / cvar("g_jetpack_maxspeed_side"), 1);
879                 if(wishvel_z - sv_gravity > 0)
880                         fz = bound(0, 1 - self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
881                 else
882                         fz = bound(0, 1 + self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
883
884                 float fvel;
885                 fvel = vlen(wishvel);
886                 wishvel_x *= fxy;
887                 wishvel_y *= fxy;
888                 wishvel_z = (wishvel_z - sv_gravity) * fz + sv_gravity;
889
890                 fvel = min(1, vlen(wishvel) / best);
891                 if(cvar("g_jetpack_fuel") && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
892                         f = min(1, self.ammo_fuel / (cvar("g_jetpack_fuel") * frametime * fvel));
893                 else
894                         f = 1;
895
896                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
897
898                 if (f > 0 && wishvel != '0 0 0')
899                 {
900                         self.velocity = self.velocity + wishvel * f * frametime;
901                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
902                                 self.ammo_fuel -= cvar("g_jetpack_fuel") * frametime * fvel * f;
903                         self.flags &~= FL_ONGROUND;
904                         self.items |= IT_USING_JETPACK;
905
906                         // jetpack also inhibits health regeneration, but only for 1 second
907                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
908                 }
909         }
910         else if (self.flags & FL_ONGROUND)
911         {
912                 // we get here if we ran out of ammo
913                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
914                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
915
916                 // walking
917                 makevectors(self.v_angle_y * '0 1 0');
918                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
919
920                 if(!(self.lastflags & FL_ONGROUND))
921                 {
922                         if(cvar("speedmeter"))
923                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
924                         if(self.lastground < time - 0.3)
925                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
926                         if(self.jumppadcount > 1)
927                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
928                         self.jumppadcount = 0;
929                 }
930
931                 if (self.velocity_x || self.velocity_y)
932                 if (!(self.flags & FL_JUMPRELEASED) || !self.BUTTON_JUMP)
933                 {
934                         v = self.velocity;
935                         v_z = 0;
936                         f = vlen(v);
937                         if (f < sv_stopspeed)
938                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
939                         else
940                                 f = 1 - frametime * sv_friction;
941                         if (f > 0)
942                                 self.velocity = self.velocity * f;
943                         else
944                                 self.velocity = '0 0 0';
945                 }
946                 // acceleration
947                 wishdir = normalize(wishvel);
948                 wishspeed = vlen(wishvel);
949                 if (wishspeed > sv_maxspeed*maxspd_mod)
950                         wishspeed = sv_maxspeed*maxspd_mod;
951                 if (self.crouch)
952                         wishspeed = wishspeed * 0.5;
953                 if (time >= self.teleport_time)
954                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
955         }
956         else
957         {
958                 // we get here if we ran out of ammo
959                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
960                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
961
962                 if(maxspd_mod < 1)
963                 {
964                         maxairspd = sv_maxairspeed*maxspd_mod;
965                         airaccel = sv_airaccelerate*maxspd_mod;
966                 }
967                 else
968                 {
969                         maxairspd = sv_maxairspeed;
970                         airaccel = sv_airaccelerate;
971                 }
972                 // airborn
973                 makevectors(self.v_angle_y * '0 1 0');
974                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
975                 // acceleration
976                 wishdir = normalize(wishvel);
977                 wishspeed = vlen(wishvel);
978                 if (wishspeed > maxairspd)
979                         wishspeed = maxairspd;
980                 if (self.crouch)
981                         wishspeed = wishspeed * 0.5;
982                 if (time >= self.teleport_time)
983                 {
984                         float accelerating;
985                         float wishspeed2;
986                         float airaccelqw;
987
988                         airaccelqw = sv_airaccel_qw;
989                         accelerating = (self.velocity * wishdir > 0);
990                         wishspeed2 = wishspeed;
991
992                         // CPM
993                         if(sv_airstopaccelerate)
994                                 if(self.velocity * wishdir < 0)
995                                         airaccel = sv_airstopaccelerate*maxspd_mod;
996                         if(self.movement_x == 0 && self.movement_y != 0)
997                         {
998                                 if(sv_maxairstrafespeed)
999                                 {
1000                                         wishspeed = min(wishspeed, sv_maxairstrafespeed*maxspd_mod);
1001                                         if(sv_maxairstrafespeed < sv_maxairspeed)
1002                                                 airaccelqw = 1;
1003                                 }
1004                                 if(sv_airstrafeaccelerate)
1005                                 {
1006                                         airaccel = sv_airstrafeaccelerate*maxspd_mod;
1007                                         if(sv_airstrafeaccelerate > sv_airaccelerate)
1008                                                 airaccelqw = 1;
1009                                 }
1010                         }
1011                         // !CPM
1012
1013                         if(sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1014                                 PM_AirAccelerate(wishdir, wishspeed);
1015                         else
1016                                 PM_Accelerate(wishdir, wishspeed, airaccel, airaccelqw, sv_airaccel_sideways_friction / maxairspd);
1017
1018                         if(sv_aircontrol)
1019                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1020                 }
1021         }
1022
1023 :end
1024         if(self.flags & FL_ONGROUND)
1025                 self.lastground = time;
1026
1027         self.lastflags = self.flags;
1028         self.lastclassname = self.classname;
1029 };