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