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