]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_physics.qc
sv_doublejump: trace only 0.01 units down instead of 1, this fixes the
[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                         self.flags &~= FL_ONGROUND;
826                         tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
827                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
828                                 self.flags |= FL_ONGROUND;
829                 }
830
831                 if (self.BUTTON_JUMP)
832                         PlayerJump ();
833                 else
834                         self.flags |= FL_JUMPRELEASED;
835
836                 if (self.waterlevel == WATERLEVEL_SWIMMING)
837                         CheckWaterJump ();
838         }
839
840         if (self.flags & FL_WATERJUMP )
841         {
842                 self.velocity_x = self.movedir_x;
843                 self.velocity_y = self.movedir_y;
844                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
845                 {
846                         self.flags &~= FL_WATERJUMP;
847                         self.teleport_time = 0;
848                 }
849         }
850         else if (g_bugrigs && self.classname == "player")
851         {
852                 RaceCarPhysics();
853         }
854         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
855         {
856                 // noclipping or flying
857                 self.flags &~= FL_ONGROUND;
858
859                 self.velocity = self.velocity * (1 - frametime * sv_friction);
860                 makevectors(self.v_angle);
861                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
862                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
863                 // acceleration
864                 wishdir = normalize(wishvel);
865                 wishspeed = vlen(wishvel);
866                 if (wishspeed > sv_maxspeed*maxspd_mod)
867                         wishspeed = sv_maxspeed*maxspd_mod;
868                 if (time >= self.teleport_time)
869                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
870         }
871         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
872         {
873                 // swimming
874                 self.flags &~= FL_ONGROUND;
875
876                 makevectors(self.v_angle);
877                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
878                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
879                 if (wishvel == '0 0 0')
880                         wishvel = '0 0 -60'; // drift towards bottom
881
882                 wishdir = normalize(wishvel);
883                 wishspeed = vlen(wishvel);
884                 if (wishspeed > sv_maxspeed*maxspd_mod)
885                         wishspeed = sv_maxspeed*maxspd_mod;
886                 wishspeed = wishspeed * 0.7;
887
888                 // water friction
889                 self.velocity = self.velocity * (1 - frametime * sv_friction);
890
891                 // water acceleration
892                 PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
893         }
894         else if (time < self.ladder_time)
895         {
896                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
897                 self.flags &~= FL_ONGROUND;
898
899                 self.velocity = self.velocity * (1 - frametime * sv_friction);
900                 makevectors(self.v_angle);
901                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
902                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
903                 if (self.gravity)
904                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
905                 else
906                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
907                 if (self.ladder_entity.classname == "func_water")
908                 {
909                         f = vlen(wishvel);
910                         if (f > self.ladder_entity.speed)
911                                 wishvel = wishvel * (self.ladder_entity.speed / f);
912
913                         self.watertype = self.ladder_entity.skin;
914                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
915                         if ((self.origin_z + self.view_ofs_z) < f)
916                                 self.waterlevel = WATERLEVEL_SUBMERGED;
917                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
918                                 self.waterlevel = WATERLEVEL_SWIMMING;
919                         else if ((self.origin_z + self.mins_z + 1) < f)
920                                 self.waterlevel = WATERLEVEL_WETFEET;
921                         else
922                         {
923                                 self.waterlevel = WATERLEVEL_NONE;
924                                 self.watertype = CONTENT_EMPTY;
925                         }
926                 }
927                 // acceleration
928                 wishdir = normalize(wishvel);
929                 wishspeed = vlen(wishvel);
930                 if (wishspeed > sv_maxspeed*maxspd_mod)
931                         wishspeed = sv_maxspeed*maxspd_mod;
932                 if (time >= self.teleport_time)
933                 {
934                         // water acceleration
935                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
936                 }
937         }
938         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!cvar("g_jetpack_fuel") || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
939         {
940                 //makevectors(self.v_angle_y * '0 1 0');
941                 makevectors(self.v_angle);
942                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
943                 // add remaining speed as Z component
944                 maxairspd = sv_maxairspeed*max(1, maxspd_mod);
945                 // fix speedhacks :P
946                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
947                 // add the unused velocity as up component
948                 wishvel_z = 0;
949
950                 // if(self.BUTTON_JUMP)
951                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
952
953                 // it is now normalized, so...
954                 float a_side, a_up, a_add, a_diff;
955                 a_side = cvar("g_jetpack_acceleration_side");
956                 a_up = cvar("g_jetpack_acceleration_up");
957                 a_add = cvar("g_jetpack_antigravity") * sv_gravity;
958
959                 wishvel_x *= a_side;
960                 wishvel_y *= a_side;
961                 wishvel_z *= a_up;
962                 wishvel_z += a_add;
963
964                 float best;
965                 best = 0;
966                 //////////////////////////////////////////////////////////////////////////////////////
967                 // finding the maximum over all vectors of above form
968                 // with wishvel having an absolute value of 1
969                 //////////////////////////////////////////////////////////////////////////////////////
970                 // we're finding the maximum over
971                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
972                 // for z in the range from -1 to 1
973                 //////////////////////////////////////////////////////////////////////////////////////
974                 // maximum is EITHER attained at the single extreme point:
975                 a_diff = a_side * a_side - a_up * a_up;
976                 if(a_diff != 0)
977                 {
978                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
979                         if(f > -1 && f < 1) // can it be attained?
980                         {
981                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
982                                 //print("middle\n");
983                         }
984                 }
985                 // OR attained at z = 1:
986                 f = (a_up + a_add) * (a_up + a_add);
987                 if(f > best)
988                 {
989                         best = f;
990                         //print("top\n");
991                 }
992                 // OR attained at z = -1:
993                 f = (a_up - a_add) * (a_up - a_add);
994                 if(f > best)
995                 {
996                         best = f;
997                         //print("bottom\n");
998                 }
999                 best = sqrt(best);
1000                 //////////////////////////////////////////////////////////////////////////////////////
1001
1002                 //print("best possible acceleration: ", ftos(best), "\n");
1003
1004                 float fxy, fz;
1005                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / cvar("g_jetpack_maxspeed_side"), 1);
1006                 if(wishvel_z - sv_gravity > 0)
1007                         fz = bound(0, 1 - self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1008                 else
1009                         fz = bound(0, 1 + self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
1010
1011                 float fvel;
1012                 fvel = vlen(wishvel);
1013                 wishvel_x *= fxy;
1014                 wishvel_y *= fxy;
1015                 wishvel_z = (wishvel_z - sv_gravity) * fz + sv_gravity;
1016
1017                 fvel = min(1, vlen(wishvel) / best);
1018                 if(cvar("g_jetpack_fuel") && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1019                         f = min(1, self.ammo_fuel / (cvar("g_jetpack_fuel") * frametime * fvel));
1020                 else
1021                         f = 1;
1022
1023                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1024
1025                 if (f > 0 && wishvel != '0 0 0')
1026                 {
1027                         self.velocity = self.velocity + wishvel * f * frametime;
1028                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1029                                 self.ammo_fuel -= cvar("g_jetpack_fuel") * frametime * fvel * f;
1030                         self.flags &~= FL_ONGROUND;
1031                         self.items |= IT_USING_JETPACK;
1032
1033                         // jetpack also inhibits health regeneration, but only for 1 second
1034                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
1035                 }
1036         }
1037         else if (self.flags & FL_ONGROUND)
1038         {
1039                 // we get here if we ran out of ammo
1040                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1041                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1042
1043                 // walking
1044                 makevectors(self.v_angle_y * '0 1 0');
1045                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1046
1047                 if(!(self.lastflags & FL_ONGROUND))
1048                 {
1049                         if(cvar("speedmeter"))
1050                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1051                         if(self.lastground < time - 0.3)
1052                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
1053                         if(self.jumppadcount > 1)
1054                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1055                         self.jumppadcount = 0;
1056                 }
1057
1058 #ifdef LETS_TEST_FTEQCC
1059                 if(self.velocity_x || self.velocity_y)
1060                 {
1061                         // good
1062                 }
1063                 else
1064                 {
1065                         if(self.velocity_x)
1066                                 checkclient();
1067                         if(self.velocity_y)
1068                                 checkclient();
1069                 }
1070 #endif
1071
1072                 v = self.velocity;
1073                 v_z = 0;
1074                 f = vlen(v);
1075                 if(f > 0)
1076                 {
1077                         if (f < sv_stopspeed)
1078                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
1079                         else
1080                                 f = 1 - frametime * sv_friction;
1081                         if (f > 0)
1082                                 self.velocity = self.velocity * f;
1083                         else
1084                                 self.velocity = '0 0 0';
1085                 }
1086
1087                 // acceleration
1088                 wishdir = normalize(wishvel);
1089                 wishspeed = vlen(wishvel);
1090                 if (wishspeed > sv_maxspeed*maxspd_mod)
1091                         wishspeed = sv_maxspeed*maxspd_mod;
1092                 if (self.crouch)
1093                         wishspeed = wishspeed * 0.5;
1094                 if (time >= self.teleport_time)
1095                         PM_Accelerate(wishdir, wishspeed, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
1096         }
1097         else
1098         {
1099                 float wishspeed0;
1100                 // we get here if we ran out of ammo
1101                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1102                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1103
1104                 if(maxspd_mod < 1)
1105                 {
1106                         maxairspd = sv_maxairspeed*maxspd_mod;
1107                         airaccel = sv_airaccelerate*maxspd_mod;
1108                 }
1109                 else
1110                 {
1111                         maxairspd = sv_maxairspeed;
1112                         airaccel = sv_airaccelerate;
1113                 }
1114                 // airborn
1115                 makevectors(self.v_angle_y * '0 1 0');
1116                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1117                 // acceleration
1118                 wishdir = normalize(wishvel);
1119                 wishspeed = wishspeed0 = vlen(wishvel);
1120                 if (wishspeed0 > sv_maxspeed*maxspd_mod)
1121                         wishspeed0 = sv_maxspeed*maxspd_mod;
1122                 if (wishspeed > maxairspd)
1123                         wishspeed = maxairspd;
1124                 if (self.crouch)
1125                         wishspeed = wishspeed * 0.5;
1126                 if (time >= self.teleport_time)
1127                 {
1128                         float accelerating;
1129                         float wishspeed2;
1130                         float airaccelqw;
1131
1132                         airaccelqw = sv_airaccel_qw;
1133                         accelerating = (self.velocity * wishdir > 0);
1134                         wishspeed2 = wishspeed;
1135
1136                         // CPM
1137                         if(sv_airstopaccelerate)
1138                                 if(self.velocity * wishdir < 0)
1139                                         airaccel = sv_airstopaccelerate*maxspd_mod;
1140                         // this doesn't play well with analog input, but can't r
1141                         // fixed like the AirControl can. So, don't set the maxa
1142                         // cvars when you want to support analog input.
1143                         if(self.movement_x == 0 && self.movement_y != 0)
1144                         {
1145                                 if(sv_maxairstrafespeed)
1146                                 {
1147                                         wishspeed = min(wishspeed, sv_maxairstrafespeed*maxspd_mod);
1148                                         if(sv_maxairstrafespeed < sv_maxairspeed)
1149                                                 airaccelqw = 1;
1150                                 }
1151                                 if(sv_airstrafeaccelerate)
1152                                 {
1153                                         airaccel = sv_airstrafeaccelerate*maxspd_mod;
1154                                         if(sv_airstrafeaccelerate > sv_airaccelerate)
1155                                                 airaccelqw = 1;
1156                                 }
1157                         }
1158                         // !CPM
1159
1160                         if(sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1161                                 PM_AirAccelerate(wishdir, wishspeed);
1162                         else
1163                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, sv_airaccel_sideways_friction / maxairspd);
1164
1165                         if(sv_aircontrol)
1166                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1167                 }
1168         }
1169
1170         if((g_cts || g_race) && self.classname != "observer") {
1171                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1172                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1173                         speedaward_holder = self.netname;
1174                         speedaward_lastupdate = time;
1175                 }
1176                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1177                         string rr;
1178                         if(g_cts)
1179                                 rr = CTS_RECORD;
1180                         else
1181                                 rr = RACE_RECORD;
1182                         race_send_speedaward(MSG_ALL);
1183                         speedaward_lastsent = speedaward_speed;
1184                         if (speedaward_speed > speedaward_alltimebest) {
1185                                 speedaward_alltimebest = speedaward_speed;
1186                                 speedaward_alltimebest_holder = speedaward_holder;
1187                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1188                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/netname"), speedaward_alltimebest_holder);
1189                                 race_send_speedaward_alltimebest(MSG_ALL);
1190                         }
1191                 }
1192         }
1193 :end
1194         if(self.flags & FL_ONGROUND)
1195                 self.lastground = time;
1196
1197         self.lastflags = self.flags;
1198         self.lastclassname = self.classname;
1199 };