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