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