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