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