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