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