]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_physics.qc
more consistent water/lava leaving physics for jump vs moveup (removes need for weird...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_physics.qc
1 .float race_penalty;
2 .float restart_jump;
3
4 .float ladder_time;
5 .entity ladder_entity;
6 .float gravity;
7 .float swamp_slowdown;
8 .float lastflags;
9 .float lastground;
10 .float wasFlying;
11 .float spectatorspeed;
12
13 .float multijump_count;
14 .float multijump_ready;
15 .float prevjumpbutton;
16
17 /*
18 =============
19 PlayerJump
20
21 When you press the jump key
22 =============
23 */
24 void PlayerJump (void)
25 {
26         if(self.freezetag_frozen)
27                 return; // no jumping in freezetag when frozen
28
29         float mjumpheight;
30         float doublejump;
31
32         doublejump = FALSE;
33         if (autocvar_sv_doublejump)
34         {
35                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
36                 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
37                 {
38                         doublejump = TRUE;
39
40                         // we MUST clip velocity here!
41                         float f;
42                         f = self.velocity * trace_plane_normal;
43                         if(f < 0)
44                                 self.velocity -= f * trace_plane_normal;
45                 }
46         }
47
48         mjumpheight = autocvar_sv_jumpvelocity;
49         if (self.waterlevel >= WATERLEVEL_SWIMMING)
50         {
51                 self.velocity_z = self.stat_sv_maxspeed * 0.7;
52                 return;
53         }
54
55         if (autocvar_g_multijump)
56         {
57                 if (self.prevjumpbutton == FALSE && !(self.flags & FL_ONGROUND)) // jump button pressed this frame and we are in midair
58                         self.multijump_ready = TRUE;  // this is necessary to check that we released the jump button and pressed it again
59                 else
60                         self.multijump_ready = FALSE;
61         }
62
63         if(!doublejump && self.multijump_ready && self.multijump_count < autocvar_g_multijump && self.velocity_z > autocvar_g_multijump_speed)
64         {
65                 // doublejump = FALSE; // checked above in the if
66                 if (autocvar_g_multijump)
67                 {
68                         if (autocvar_g_multijump_add == 0) // in this case we make the z velocity == jumpvelocity
69                         {
70                                 if (self.velocity_z < mjumpheight)
71                                 {
72                                         doublejump = TRUE;
73                                         self.velocity_z = 0;
74                                 }
75                         }
76                         else
77                                 doublejump = TRUE;
78
79                         if(doublejump)
80                         {
81                                 if(self.movement_x != 0 || self.movement_y != 0) // don't remove all speed if player isnt pressing any movement keys
82                                 {
83                                         float curspeed;
84                                         vector wishvel, wishdir;
85
86                                         curspeed = max(
87                                                 vlen(vec2(self.velocity)), // current xy speed
88                                                 vlen(vec2(antilag_takebackavgvelocity(self, max(self.lastteleporttime + sys_frametime, time - 0.25), time))) // average xy topspeed over the last 0.25 secs
89                                         );
90                                         makevectors(self.v_angle_y * '0 1 0');
91                                         wishvel = v_forward * self.movement_x + v_right * self.movement_y;
92                                         wishdir = normalize(wishvel);
93
94                                         self.velocity_x = wishdir_x * curspeed; // allow "dodging" at a multijump
95                                         self.velocity_y = wishdir_y * curspeed;
96                                         // keep velocity_z unchanged!
97                                 }
98                                 if (autocvar_g_multijump > 0)
99                                         self.multijump_count += 1;
100                         }
101                 }
102                 self.multijump_ready = FALSE; // require releasing and pressing the jump button again for the next jump
103         }
104
105         if (!doublejump)
106                 if (!(self.flags & FL_ONGROUND))
107                         return;
108
109         if(self.cvar_cl_movement_track_canjump)
110                 if (!(self.flags & FL_JUMPRELEASED))
111                         return;
112
113         if(self.health <= g_bloodloss)
114                 return;
115
116         // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
117         // velocity bounds.  Final velocity is bound between (jumpheight *
118         // min + jumpheight) and (jumpheight * max + jumpheight);
119
120         if(autocvar_sv_jumpspeedcap_min != "")
121         {
122                 float minjumpspeed;
123
124                 minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
125
126                 if (self.velocity_z < minjumpspeed)
127                         mjumpheight += minjumpspeed - self.velocity_z;
128         }
129
130         if(autocvar_sv_jumpspeedcap_max != "")
131         {
132                 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
133                 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
134
135                 if(!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
136                 {
137                         float maxjumpspeed;
138
139                         maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
140
141                         if (self.velocity_z > maxjumpspeed)
142                                 mjumpheight -= self.velocity_z - maxjumpspeed;
143                 }
144         }
145
146         if(!(self.lastflags & FL_ONGROUND))
147         {
148                 if(autocvar_speedmeter)
149                         dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
150                 if(self.lastground < time - 0.3)
151                 {
152                         self.velocity_x *= (1 - autocvar_sv_friction_on_land);
153                         self.velocity_y *= (1 - autocvar_sv_friction_on_land);
154                 }
155                 if(self.jumppadcount > 1)
156                         dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
157                 self.jumppadcount = 0;
158         }
159
160         self.velocity_z = self.velocity_z + mjumpheight;
161         self.oldvelocity_z = self.velocity_z;
162
163         self.flags &~= FL_ONGROUND;
164         self.flags &~= FL_JUMPRELEASED;
165
166         if (self.crouch)
167                 setanim(self, self.anim_duckjump, FALSE, TRUE, TRUE);
168         else if (self.animstate_startframe != self.anim_melee_x || (self.animstate_startframe == self.anim_melee_x && time - self.animstate_starttime >= 21/20)) // jump animation shouldn't override melee until we have animation blending (or until the anim finished, 21/20 = numframes/fps)
169                 setanim(self, self.anim_jump, FALSE, TRUE, TRUE);
170
171         if(g_jump_grunt)
172                 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
173
174         self.restart_jump = -1; // restart jump anim next time
175         // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
176 }
177 void CheckWaterJump()
178 {
179         vector start, end;
180
181 // check for a jump-out-of-water
182         makevectors (self.angles);
183         start = self.origin;
184         start_z = start_z + 8;
185         v_forward_z = 0;
186         normalize(v_forward);
187         end = start + v_forward*24;
188         traceline (start, end, TRUE, self);
189         if (trace_fraction < 1)
190         {       // solid at waist
191                 start_z = start_z + self.maxs_z - 8;
192                 end = start + v_forward*24;
193                 self.movedir = trace_plane_normal * -50;
194                 traceline (start, end, TRUE, self);
195                 if (trace_fraction == 1)
196                 {       // open at eye level
197                         self.flags |= FL_WATERJUMP;
198                         self.velocity_z = 225;
199                         self.flags &~= FL_JUMPRELEASED;
200                         self.teleport_time = time + 2;  // safety net
201                         return;
202                 }
203         }
204 }
205 void CheckPlayerJump()
206 {
207         if(self.flags & FL_ONGROUND)
208         {
209                 if (autocvar_g_multijump > 0)
210                         self.multijump_count = 0;
211                 else
212                         self.multijump_count = -2; // the cvar value for infinite jumps is -1, so this needs to be smaller
213         }
214
215         if (self.BUTTON_JUMP)
216                 PlayerJump ();
217         else
218                 self.flags |= FL_JUMPRELEASED;
219
220         if (self.waterlevel == WATERLEVEL_SWIMMING)
221                 CheckWaterJump ();
222         self.prevjumpbutton = self.BUTTON_JUMP;
223 }
224
225 float racecar_angle(float forward, float down)
226 {
227         float ret, angle_mult;
228
229         if(forward < 0)
230         {
231                 forward = -forward;
232                 down = -down;
233         }
234
235         ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
236
237         angle_mult = forward / (800 + forward);
238
239         if(ret > 180)
240                 return ret * angle_mult + 360 * (1 - angle_mult);
241         else
242                 return ret * angle_mult;
243 }
244
245 void RaceCarPhysics()
246 {
247         // using this move type for "big rigs"
248         // the engine does not push the entity!
249
250         float accel, steer, f, myspeed, steerfactor;
251         vector angles_save, rigvel;
252
253         angles_save = self.angles;
254         accel = bound(-1, self.movement_x / self.stat_sv_maxspeed, 1);
255         steer = bound(-1, self.movement_y / self.stat_sv_maxspeed, 1);
256
257         if(g_bugrigs_reverse_speeding)
258         {
259                 if(accel < 0)
260                 {
261                         // back accel is DIGITAL
262                         // to prevent speedhack
263                         if(accel < -0.5)
264                                 accel = -1;
265                         else
266                                 accel = 0;
267                 }
268         }
269
270         self.angles_x = 0;
271         self.angles_z = 0;
272         makevectors(self.angles); // new forward direction!
273
274         if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
275         {
276                 float upspeed, accelfactor;
277
278                 myspeed = self.velocity * v_forward;
279                 upspeed = self.velocity * v_up;
280
281                 // responsiveness factor for steering and acceleration
282                 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
283                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
284
285                 if(myspeed < 0 && g_bugrigs_reverse_spinning)
286                         steerfactor = -myspeed * g_bugrigs_steer;
287                 else
288                         steerfactor = -myspeed * f * g_bugrigs_steer;
289
290                 if(myspeed < 0 && g_bugrigs_reverse_speeding)
291                         accelfactor = g_bugrigs_accel;
292                 else
293                         accelfactor = f * g_bugrigs_accel;
294                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
295
296                 if(accel < 0)
297                 {
298                         if(myspeed > 0)
299                         {
300                                 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
301                         }
302                         else
303                         {
304                                 if(!g_bugrigs_reverse_speeding)
305                                         myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
306                         }
307                 }
308                 else
309                 {
310                         if(myspeed >= 0)
311                         {
312                                 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
313                         }
314                         else
315                         {
316                                 if(g_bugrigs_reverse_stopping)
317                                         myspeed = 0;
318                                 else
319                                         myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
320                         }
321                 }
322                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
323                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
324
325                 self.angles_y += steer * frametime * steerfactor; // apply steering
326                 makevectors(self.angles); // new forward direction!
327
328                 myspeed += accel * accelfactor * frametime;
329
330                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
331         }
332         else
333         {
334                 myspeed = vlen(self.velocity);
335
336                 // responsiveness factor for steering and acceleration
337                 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
338                 steerfactor = -myspeed * f;
339                 self.angles_y += steer * frametime * steerfactor; // apply steering
340
341                 rigvel = self.velocity;
342                 makevectors(self.angles); // new forward direction!
343         }
344
345         rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
346         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
347         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
348         //MAXIMA: solve(total_acceleration(v) = 0, v);
349
350         if(g_bugrigs_planar_movement)
351         {
352                 vector rigvel_xy, neworigin, up;
353                 float mt;
354
355                 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
356                 rigvel_xy = vec2(rigvel);
357
358                 if(g_bugrigs_planar_movement_car_jumping && !g_touchexplode) // touchexplode is a better way to handle collisions
359                         mt = MOVE_NORMAL;
360                 else
361                         mt = MOVE_NOMONSTERS;
362
363                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
364                 up = trace_endpos - self.origin;
365
366                 // BUG RIGS: align the move to the surface instead of doing collision testing
367                 // can we move?
368                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
369
370                 // align to surface
371                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
372
373                 if(trace_fraction < 0.5)
374                 {
375                         trace_fraction = 1;
376                         neworigin = self.origin;
377                 }
378                 else
379                         neworigin = trace_endpos;
380
381                 if(trace_fraction < 1)
382                 {
383                         // now set angles_x so that the car points parallel to the surface
384                         self.angles = vectoangles(
385                                         '1 0 0' * v_forward_x * trace_plane_normal_z
386                                         +
387                                         '0 1 0' * v_forward_y * trace_plane_normal_z
388                                         +
389                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
390                                         );
391                         self.flags |= FL_ONGROUND;
392                 }
393                 else
394                 {
395                         // now set angles_x so that the car points forward, but is tilted in velocity direction
396                         self.flags &~= FL_ONGROUND;
397                 }
398
399                 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
400                 self.movetype = MOVETYPE_NOCLIP;
401         }
402         else
403         {
404                 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
405                 self.velocity = rigvel;
406                 self.movetype = MOVETYPE_FLY;
407         }
408
409         trace_fraction = 1;
410         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
411         if(trace_fraction != 1)
412         {
413                 self.angles = vectoangles2(
414                                 '1 0 0' * v_forward_x * trace_plane_normal_z
415                                 +
416                                 '0 1 0' * v_forward_y * trace_plane_normal_z
417                                 +
418                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
419                                 trace_plane_normal
420                                 );
421         }
422         else
423         {
424                 vector vel_local;
425
426                 vel_local_x = v_forward * self.velocity;
427                 vel_local_y = v_right * self.velocity;
428                 vel_local_z = v_up * self.velocity;
429
430                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
431                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
432         }
433
434         // smooth the angles
435         vector vf1, vu1, smoothangles;
436         makevectors(self.angles);
437         f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
438         if(f == 0)
439                 f = 1;
440         vf1 = v_forward * f;
441         vu1 = v_up * f;
442         makevectors(angles_save);
443         vf1 = vf1 + v_forward * (1 - f);
444         vu1 = vu1 + v_up * (1 - f);
445         smoothangles = vectoangles2(vf1, vu1);
446         self.angles_x = -smoothangles_x;
447         self.angles_z =  smoothangles_z;
448 }
449
450 float IsMoveInDirection(vector mv, float angle) // key mix factor
451 {
452         if(mv_x == 0 && mv_y == 0)
453                 return 0; // avoid division by zero
454         angle -= RAD2DEG * atan2(mv_y, mv_x);
455         angle = remainder(angle, 360) / 45;
456         if(angle >  1)
457                 return 0;
458         if(angle < -1)
459                 return 0;
460         return 1 - fabs(angle);
461 }
462
463 float GeomLerp(float a, float lerp, float b)
464 {
465         if(a == 0)
466         {
467                 if(lerp < 1)
468                         return 0;
469                 else
470                         return b;
471         }
472         if(b == 0)
473         {
474                 if(lerp > 0)
475                         return 0;
476                 else
477                         return a;
478         }
479         return a * pow(fabs(b / a), lerp);
480 }
481
482 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
483 {
484         float zspeed, xyspeed, dot, k;
485
486 #if 0
487         // this doesn't play well with analog input
488         if(self.movement_x == 0 || self.movement_y != 0)
489                 return; // can't control movement if not moving forward or backward
490         k = 32;
491 #else
492         k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
493         if(k <= 0)
494                 return;
495 #endif
496
497         k *= bound(0, wishspeed / autocvar_sv_maxairspeed, 1);
498
499         zspeed = self.velocity_z;
500         self.velocity_z = 0;
501         xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
502
503         dot = self.velocity * wishdir;
504
505         if(dot > 0) // we can't change direction while slowing down
506         {
507                 k *= pow(dot, autocvar_sv_aircontrol_power)*frametime;
508                 xyspeed = max(0, xyspeed - autocvar_sv_aircontrol_penalty * sqrt(max(0, 1 - dot*dot)) * k/32);
509                 k *= autocvar_sv_aircontrol;
510                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
511         }
512
513         self.velocity = self.velocity * xyspeed;
514         self.velocity_z = zspeed;
515 }
516
517 float AdjustAirAccelQW(float accelqw, float factor)
518 {
519         return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
520 }
521
522 // example config for alternate speed clamping:
523 //   sv_airaccel_qw 0.8
524 //   sv_airaccel_sideways_friction 0
525 //   prvm_globalset server speedclamp_mode 1
526 //     (or 2)
527 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
528 {
529         float vel_straight;
530         float vel_z;
531         vector vel_perpend;
532         float step;
533
534         vector vel_xy;
535         float vel_xy_current;
536         float vel_xy_backward, vel_xy_forward;
537         float speedclamp;
538
539         if(stretchfactor > 0)
540                 speedclamp = stretchfactor;
541         else if(accelqw < 0)
542                 speedclamp = 1; // full clamping, no stretch
543         else
544                 speedclamp = -1; // no clamping
545
546         if(accelqw < 0)
547                 accelqw = -accelqw;
548
549         if(autocvar_sv_gameplayfix_q2airaccelerate)
550                 wishspeed0 = wishspeed;
551
552         vel_straight = self.velocity * wishdir;
553         vel_z = self.velocity_z;
554         vel_xy = vec2(self.velocity);
555         vel_perpend = vel_xy - vel_straight * wishdir;
556
557         step = accel * frametime * wishspeed0;
558
559         vel_xy_current  = vlen(vel_xy);
560         if(speedlimit)
561                 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
562         vel_xy_forward  = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
563         vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
564         if(vel_xy_backward < 0)
565                 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
566
567         vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
568
569         if(sidefric < 0 && (vel_perpend*vel_perpend))
570                 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
571         {
572                 float f, fminimum;
573                 f = max(0, 1 + frametime * wishspeed * sidefric);
574                 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
575                 // this cannot be > 1
576                 if(fminimum <= 0)
577                         vel_perpend = vel_perpend * max(0, f);
578                 else
579                 {
580                         fminimum = sqrt(fminimum);
581                         vel_perpend = vel_perpend * max(fminimum, f);
582                 }
583         }
584         else
585                 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
586         
587         vel_xy = vel_straight * wishdir + vel_perpend;
588         
589         if(speedclamp >= 0)
590         {
591                 float vel_xy_preclamp;
592                 vel_xy_preclamp = vlen(vel_xy);
593                 if(vel_xy_preclamp > 0) // prevent division by zero
594                 {
595                         vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
596                         if(vel_xy_current < vel_xy_preclamp)
597                                 vel_xy = vel_xy * (vel_xy_current / vel_xy_preclamp);
598                 }
599         }
600
601         self.velocity = vel_xy + vel_z * '0 0 1';
602 }
603
604 void PM_AirAccelerate(vector wishdir, float wishspeed)
605 {
606         vector curvel, wishvel, acceldir, curdir;
607         float addspeed, accelspeed, curspeed, f;
608         float dot;
609
610         if(wishspeed == 0)
611                 return;
612
613         curvel = self.velocity;
614         curvel_z = 0;
615         curspeed = vlen(curvel);
616
617         if(wishspeed > curspeed * 1.01)
618         {
619                 wishspeed = min(wishspeed, curspeed + autocvar_sv_warsowbunny_airforwardaccel * self.stat_sv_maxspeed * frametime);
620         }
621         else
622         {
623                 f = max(0, (autocvar_sv_warsowbunny_topspeed - curspeed) / (autocvar_sv_warsowbunny_topspeed - self.stat_sv_maxspeed));
624                 wishspeed = max(curspeed, self.stat_sv_maxspeed) + autocvar_sv_warsowbunny_accel * f * self.stat_sv_maxspeed * frametime;
625         }
626         wishvel = wishdir * wishspeed;
627         acceldir = wishvel - curvel;
628         addspeed = vlen(acceldir);
629         acceldir = normalize(acceldir);
630
631         accelspeed = min(addspeed, autocvar_sv_warsowbunny_turnaccel * self.stat_sv_maxspeed * frametime);
632
633         if(autocvar_sv_warsowbunny_backtosideratio < 1)
634         {
635                 curdir = normalize(curvel);
636                 dot = acceldir * curdir;
637                 if(dot < 0)
638                         acceldir = acceldir - (1 - autocvar_sv_warsowbunny_backtosideratio) * dot * curdir;
639         }
640
641         self.velocity += accelspeed * acceldir;
642 }
643
644 .vector movement_old;
645 .float buttons_old;
646 .vector v_angle_old;
647 .string lastclassname;
648
649 .float() PlayerPhysplug;
650
651 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
652 .float specialcommand_pos;
653 void SpecialCommand()
654 {
655 #ifdef TETRIS
656         TetrisImpulse();
657 #else
658         if(!CheatImpulse(99))
659                 print("A hollow voice says \"Plugh\".\n");
660 #endif
661 }
662
663 float speedaward_speed;
664 string speedaward_holder;
665 string speedaward_uid;
666 void race_send_speedaward(float msg)
667 {
668         // send the best speed of the round
669         WriteByte(msg, SVC_TEMPENTITY);
670         WriteByte(msg, TE_CSQC_RACE);
671         WriteByte(msg, RACE_NET_SPEED_AWARD);
672         WriteInt24_t(msg, floor(speedaward_speed+0.5));
673         WriteString(msg, speedaward_holder);
674 }
675
676 float speedaward_alltimebest;
677 string speedaward_alltimebest_holder;
678 string speedaward_alltimebest_uid;
679 void race_send_speedaward_alltimebest(float msg)
680 {
681         // send the best speed
682         WriteByte(msg, SVC_TEMPENTITY);
683         WriteByte(msg, TE_CSQC_RACE);
684         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
685         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
686         WriteString(msg, speedaward_alltimebest_holder);
687 }
688
689 string GetMapname(void);
690 float speedaward_lastupdate;
691 float speedaward_lastsent;
692 void SV_PlayerPhysics()
693 {
694         vector wishvel, wishdir, v;
695         float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
696         string temps;
697         float buttons_prev;
698         float not_allowed_to_move;
699         string c;
700
701         WarpZone_PlayerPhysics_FixVAngle();
702         
703         maxspd_mod = 1;
704         if(g_minstagib && (self.items & IT_INVINCIBLE))
705                 maxspd_mod *= autocvar_g_minstagib_speed_highspeed;
706         if(self.ballcarried)
707                 if(g_nexball)
708                         maxspd_mod *= autocvar_g_nexball_basketball_carrier_highspeed;
709                 else if(g_keepaway)
710                         maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
711
712         if(g_runematch)
713         {
714                 if(self.runes & RUNE_SPEED)
715                 {
716                         if(self.runes & CURSE_SLOW)
717                                 maxspd_mod *= autocvar_g_balance_rune_speed_combo_highspeed;
718                         else
719                                 maxspd_mod *= autocvar_g_balance_rune_speed_highspeed;
720                 }
721                 else if(self.runes & CURSE_SLOW)
722                 {
723                         maxspd_mod *= autocvar_g_balance_curse_slow_highspeed;
724                 }
725         }
726         maxspd_mod *= autocvar_g_movement_highspeed;
727
728         // fix physics stats for g_movement_highspeed
729         // TODO maybe rather use maxairspeed? needs testing
730         self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
731         if(autocvar_sv_airstrafeaccel_qw)
732                 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
733         else
734                 self.stat_sv_airstrafeaccel_qw = 0;
735         self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
736         self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
737
738     if(self.PlayerPhysplug)
739         if(self.PlayerPhysplug())
740             return;
741
742         self.race_movetime_frac += frametime;
743         f = floor(self.race_movetime_frac);
744         self.race_movetime_frac -= f;
745         self.race_movetime_count += f;
746         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
747
748         anticheat_physics();
749
750         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);
751
752         if(!buttons)
753                 c = "x";
754         else if(buttons == 1)
755                 c = "1";
756         else if(buttons == 2)
757                 c = " ";
758         else if(buttons == 128)
759                 c = "s";
760         else if(buttons == 256)
761                 c = "w";
762         else if(buttons == 512)
763                 c = "a";
764         else if(buttons == 1024)
765                 c = "d";
766         else
767                 c = "?";
768
769         if(c == substring(specialcommand, self.specialcommand_pos, 1))
770         {
771                 self.specialcommand_pos += 1;
772                 if(self.specialcommand_pos >= strlen(specialcommand))
773                 {
774                         self.specialcommand_pos = 0;
775                         SpecialCommand();
776                         return;
777                 }
778         }
779         else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
780                 self.specialcommand_pos = 0;
781
782         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
783         {
784                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
785                         self.parm_idlesince = time;
786         }
787         buttons_prev = self.buttons_old;
788         self.buttons_old = buttons;
789         self.movement_old = self.movement;
790         self.v_angle_old = self.v_angle;
791
792         if(time < self.nickspamtime)
793         if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
794         {
795                 // slight annoyance for nick change scripts
796                 self.movement = -1 * self.movement;
797                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
798
799                 if(self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
800                 {
801                         self.angles_x = random() * 360;
802                         self.angles_y = random() * 360;
803                         // at least I'm not forcing retardedview by also assigning to angles_z
804                         self.fixangle = TRUE;
805                 }
806         }
807
808         if (self.punchangle != '0 0 0')
809         {
810                 f = vlen(self.punchangle) - 10 * frametime;
811                 if (f > 0)
812                         self.punchangle = normalize(self.punchangle) * f;
813                 else
814                         self.punchangle = '0 0 0';
815         }
816
817         if (self.punchvector != '0 0 0')
818         {
819                 f = vlen(self.punchvector) - 30 * frametime;
820                 if (f > 0)
821                         self.punchvector = normalize(self.punchvector) * f;
822                 else
823                         self.punchvector = '0 0 0';
824         }
825
826         if (clienttype(self) == CLIENTTYPE_BOT)
827         {
828                 if(playerdemo_read())
829                         return;
830                 bot_think();
831         }
832         
833         self.items &~= IT_USING_JETPACK;
834
835         if(self.classname == "player")
836         {
837                 if(self.race_penalty)
838                         if(time > self.race_penalty)
839                                 self.race_penalty = 0;
840
841                 not_allowed_to_move = 0;
842                 if(self.race_penalty)
843                         not_allowed_to_move = 1;
844                 if(!autocvar_sv_ready_restart_after_countdown)
845                 if(time < game_starttime)
846                         not_allowed_to_move = 1;
847
848                 if(not_allowed_to_move)
849                 {
850                         self.velocity = '0 0 0';
851                         self.movetype = MOVETYPE_NONE;
852                         self.disableclientprediction = 2;
853                 }
854                 else if(self.disableclientprediction == 2)
855                 {
856                         if(self.movetype == MOVETYPE_NONE)
857                                 self.movetype = MOVETYPE_WALK;
858                         self.disableclientprediction = 0;
859                 }
860         }
861
862         if (self.movetype == MOVETYPE_NONE)
863                 return;
864
865         // when we get here, disableclientprediction cannot be 2
866         self.disableclientprediction = 0;
867         if(time < self.ladder_time)
868                 self.disableclientprediction = 1;
869
870         MUTATOR_CALLHOOK(PlayerPhysics);
871
872         if(self.player_blocked)
873         {
874                 self.movement = '0 0 0';
875                 self.disableclientprediction = 1;
876         }
877
878         maxspd_mod = 1;
879
880         swampspd_mod = 1;
881         if(self.in_swamp) {
882                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
883         }
884
885         // conveyors: first fix velocity
886         if(self.conveyor.state)
887                 self.velocity -= self.conveyor.movedir;
888
889         if(self.classname != "player")
890         {
891                 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
892                 if(!self.spectatorspeed)
893                         self.spectatorspeed = maxspd_mod;
894                 if(self.impulse && self.impulse <= 19)
895                 {
896                         if(self.lastclassname != "player")
897                         {
898                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
899                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
900                                 else if(self.impulse == 11)
901                                         self.spectatorspeed = maxspd_mod;
902                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
903                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
904                                 else if(self.impulse >= 1 && self.impulse <= 9)
905                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
906                         } // otherwise just clear
907                         self.impulse = 0;
908                 }
909                 maxspd_mod = self.spectatorspeed;
910         }
911
912         spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
913         if(self.speed != spd)
914         {
915                 self.speed = spd;
916                 temps = ftos(spd);
917                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
918                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
919                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
920                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
921         }
922
923         maxspd_mod *= swampspd_mod; // only one common speed modder please!
924         swampspd_mod = 1;
925
926         // if dead, behave differently
927         if (self.deadflag)
928                 goto end;
929
930         if (!self.fixangle && !g_bugrigs)
931         {
932                 self.angles_x = 0;
933                 self.angles_y = self.v_angle_y;
934                 self.angles_z = 0;
935         }
936
937         if(self.flags & FL_ONGROUND)
938         if(self.classname == "player") // no fall sounds for observers thank you very much
939         if(self.wasFlying)
940         {
941                 self.wasFlying = 0;
942
943                 if(self.waterlevel < WATERLEVEL_SWIMMING)
944                 if(time >= self.ladder_time)
945                 if not(self.hook)
946                 {
947                         self.nextstep = time + 0.3 + random() * 0.1;
948                         trace_dphitq3surfaceflags = 0;
949                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
950                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
951                         {
952                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
953                                         GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
954                                 else
955                                         GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
956                         }
957                 }
958         }
959
960         if(IsFlying(self))
961                 self.wasFlying = 1;
962
963         if(self.classname == "player")
964                 CheckPlayerJump();
965
966         if (self.flags & FL_WATERJUMP )
967         {
968                 self.velocity_x = self.movedir_x;
969                 self.velocity_y = self.movedir_y;
970                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
971                 {
972                         self.flags &~= FL_WATERJUMP;
973                         self.teleport_time = 0;
974                 }
975         }
976         else if (g_bugrigs && self.classname == "player")
977         {
978                 RaceCarPhysics();
979         }
980         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
981         {
982                 // noclipping or flying
983                 self.flags &~= FL_ONGROUND;
984
985                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
986                 makevectors(self.v_angle);
987                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
988                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
989                 // acceleration
990                 wishdir = normalize(wishvel);
991                 wishspeed = vlen(wishvel);
992                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
993                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
994                 if (time >= self.teleport_time)
995                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
996         }
997         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
998         {
999                 // swimming
1000                 self.flags &~= FL_ONGROUND;
1001
1002                 makevectors(self.v_angle);
1003                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1004                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
1005                 if (wishvel == '0 0 0')
1006                         wishvel = '0 0 -60'; // drift towards bottom
1007
1008                 wishdir = normalize(wishvel);
1009                 wishspeed = vlen(wishvel);
1010                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1011                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1012                 wishspeed = wishspeed * 0.7;
1013
1014                 // water friction
1015                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
1016
1017                 // water acceleration
1018                 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1019         }
1020         else if (time < self.ladder_time)
1021         {
1022                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1023                 self.flags &~= FL_ONGROUND;
1024
1025                 float g;
1026                 g = autocvar_sv_gravity * frametime;
1027                 if(self.gravity)
1028                         g *= self.gravity;
1029                 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
1030                 {
1031                         g *= 0.5;
1032                         self.velocity_z += g;
1033                 }
1034
1035                 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
1036                 makevectors(self.v_angle);
1037                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1038                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
1039                 self.velocity_z += g;
1040                 if (self.ladder_entity.classname == "func_water")
1041                 {
1042                         f = vlen(wishvel);
1043                         if (f > self.ladder_entity.speed)
1044                                 wishvel = wishvel * (self.ladder_entity.speed / f);
1045
1046                         self.watertype = self.ladder_entity.skin;
1047                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
1048                         if ((self.origin_z + self.view_ofs_z) < f)
1049                                 self.waterlevel = WATERLEVEL_SUBMERGED;
1050                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1051                                 self.waterlevel = WATERLEVEL_SWIMMING;
1052                         else if ((self.origin_z + self.mins_z + 1) < f)
1053                                 self.waterlevel = WATERLEVEL_WETFEET;
1054                         else
1055                         {
1056                                 self.waterlevel = WATERLEVEL_NONE;
1057                                 self.watertype = CONTENT_EMPTY;
1058                         }
1059                 }
1060                 // acceleration
1061                 wishdir = normalize(wishvel);
1062                 wishspeed = vlen(wishvel);
1063                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1064                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1065                 if (time >= self.teleport_time)
1066                 {
1067                         // water acceleration
1068                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1069                 }
1070         }
1071         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!autocvar_g_jetpack_fuel || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
1072         {
1073                 //makevectors(self.v_angle_y * '0 1 0');
1074                 makevectors(self.v_angle);
1075                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1076                 // add remaining speed as Z component
1077                 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1078                 // fix speedhacks :P
1079                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1080                 // add the unused velocity as up component
1081                 wishvel_z = 0;
1082
1083                 // if(self.BUTTON_JUMP)
1084                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1085
1086                 // it is now normalized, so...
1087                 float a_side, a_up, a_add, a_diff;
1088                 a_side = autocvar_g_jetpack_acceleration_side;
1089                 a_up = autocvar_g_jetpack_acceleration_up;
1090                 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1091
1092                 wishvel_x *= a_side;
1093                 wishvel_y *= a_side;
1094                 wishvel_z *= a_up;
1095                 wishvel_z += a_add;
1096
1097                 float best;
1098                 best = 0;
1099                 //////////////////////////////////////////////////////////////////////////////////////
1100                 // finding the maximum over all vectors of above form
1101                 // with wishvel having an absolute value of 1
1102                 //////////////////////////////////////////////////////////////////////////////////////
1103                 // we're finding the maximum over
1104                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1105                 // for z in the range from -1 to 1
1106                 //////////////////////////////////////////////////////////////////////////////////////
1107                 // maximum is EITHER attained at the single extreme point:
1108                 a_diff = a_side * a_side - a_up * a_up;
1109                 if(a_diff != 0)
1110                 {
1111                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1112                         if(f > -1 && f < 1) // can it be attained?
1113                         {
1114                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1115                                 //print("middle\n");
1116                         }
1117                 }
1118                 // OR attained at z = 1:
1119                 f = (a_up + a_add) * (a_up + a_add);
1120                 if(f > best)
1121                 {
1122                         best = f;
1123                         //print("top\n");
1124                 }
1125                 // OR attained at z = -1:
1126                 f = (a_up - a_add) * (a_up - a_add);
1127                 if(f > best)
1128                 {
1129                         best = f;
1130                         //print("bottom\n");
1131                 }
1132                 best = sqrt(best);
1133                 //////////////////////////////////////////////////////////////////////////////////////
1134
1135                 //print("best possible acceleration: ", ftos(best), "\n");
1136
1137                 float fxy, fz;
1138                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1139                 if(wishvel_z - autocvar_sv_gravity > 0)
1140                         fz = bound(0, 1 - self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1141                 else
1142                         fz = bound(0, 1 + self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1143
1144                 float fvel;
1145                 fvel = vlen(wishvel);
1146                 wishvel_x *= fxy;
1147                 wishvel_y *= fxy;
1148                 wishvel_z = (wishvel_z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1149
1150                 fvel = min(1, vlen(wishvel) / best);
1151                 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1152                         f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1153                 else
1154                         f = 1;
1155
1156                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1157
1158                 if (f > 0 && wishvel != '0 0 0')
1159                 {
1160                         self.velocity = self.velocity + wishvel * f * frametime;
1161                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
1162                                 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1163                         self.flags &~= FL_ONGROUND;
1164                         self.items |= IT_USING_JETPACK;
1165
1166                         // jetpack also inhibits health regeneration, but only for 1 second
1167                         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1168                 }
1169         }
1170         else if (self.flags & FL_ONGROUND)
1171         {
1172                 // we get here if we ran out of ammo
1173                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1174                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1175
1176                 // walking
1177                 makevectors(self.v_angle_y * '0 1 0');
1178                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1179
1180                 if(!(self.lastflags & FL_ONGROUND))
1181                 {
1182                         if(autocvar_speedmeter)
1183                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1184                         if(self.lastground < time - 0.3)
1185                                 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1186                         if(self.jumppadcount > 1)
1187                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1188                         self.jumppadcount = 0;
1189                 }
1190
1191 #ifdef LETS_TEST_FTEQCC
1192                 if(self.velocity_x || self.velocity_y)
1193                 {
1194                         // good
1195                 }
1196                 else
1197                 {
1198                         if(self.velocity_x)
1199                                 checkclient();
1200                         if(self.velocity_y)
1201                                 checkclient();
1202                 }
1203 #endif
1204
1205                 v = self.velocity;
1206                 v_z = 0;
1207                 f = vlen(v);
1208                 if(f > 0)
1209                 {
1210                         if (f < autocvar_sv_stopspeed)
1211                                 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1212                         else
1213                                 f = 1 - frametime * autocvar_sv_friction;
1214                         if (f > 0)
1215                                 self.velocity = self.velocity * f;
1216                         else
1217                                 self.velocity = '0 0 0';
1218                         /*
1219                            Mathematical analysis time!
1220
1221                            Our goal is to invert this mess.
1222
1223                            For the two cases we get:
1224                                 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1225                                   = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1226                                 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1227                            and
1228                                 v = v0 * (1 - frametime * autocvar_sv_friction)
1229                                 v0 = v / (1 - frametime * autocvar_sv_friction)
1230
1231                            These cases would be chosen ONLY if:
1232                                 v0 < autocvar_sv_stopspeed
1233                                 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1234                                 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1235                            and, respectively:
1236                                 v0 >= autocvar_sv_stopspeed
1237                                 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1238                                 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1239                          */
1240                 }
1241
1242                 // acceleration
1243                 wishdir = normalize(wishvel);
1244                 wishspeed = vlen(wishvel);
1245                 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1246                         wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1247                 if (self.crouch)
1248                         wishspeed = wishspeed * 0.5;
1249                 if (time >= self.teleport_time)
1250                         PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1251         }
1252         else
1253         {
1254                 float wishspeed0;
1255                 // we get here if we ran out of ammo
1256                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
1257                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1258
1259                 if(maxspd_mod < 1)
1260                 {
1261                         maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1262                         airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1263                 }
1264                 else
1265                 {
1266                         maxairspd = autocvar_sv_maxairspeed;
1267                         airaccel = autocvar_sv_airaccelerate;
1268                 }
1269                 // airborn
1270                 makevectors(self.v_angle_y * '0 1 0');
1271                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1272                 // acceleration
1273                 wishdir = normalize(wishvel);
1274                 wishspeed = wishspeed0 = vlen(wishvel);
1275                 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1276                         wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1277                 if (wishspeed > maxairspd)
1278                         wishspeed = maxairspd;
1279                 if (self.crouch)
1280                         wishspeed = wishspeed * 0.5;
1281                 if (time >= self.teleport_time)
1282                 {
1283                         float accelerating;
1284                         float wishspeed2;
1285                         float airaccelqw;
1286                         float strafity;
1287
1288                         airaccelqw = self.stat_sv_airaccel_qw;
1289                         accelerating = (self.velocity * wishdir > 0);
1290                         wishspeed2 = wishspeed;
1291
1292                         // CPM
1293                         if(autocvar_sv_airstopaccelerate)
1294                         {
1295                                 vector curdir;
1296                                 curdir = self.velocity;
1297                                 curdir_z = 0;
1298                                 curdir = normalize(curdir);
1299                                 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1300                         }
1301                         // note that for straight forward jumping:
1302                         // step = accel * frametime * wishspeed0;
1303                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1304                         // -->
1305                         // dv/dt = accel * maxspeed (when slow)
1306                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1307                         // log dv/dt = logaccel + logmaxspeed (when slow)
1308                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1309                         strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1310                         if(autocvar_sv_maxairstrafespeed)
1311                                 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1312                         if(autocvar_sv_airstrafeaccelerate)
1313                                 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1314                         if(self.stat_sv_airstrafeaccel_qw)
1315                                 airaccelqw = copysign(1-GeomLerp(1-fabs(self.stat_sv_airaccel_qw), strafity, 1-fabs(self.stat_sv_airstrafeaccel_qw)), ((strafity > 0.5) ? self.stat_sv_airstrafeaccel_qw : self.stat_sv_airaccel_qw));
1316                         // !CPM
1317
1318                         if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1319                                 PM_AirAccelerate(wishdir, wishspeed);
1320                         else
1321                                 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1322
1323                         if(autocvar_sv_aircontrol)
1324                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
1325                 }
1326         }
1327
1328         if((g_cts || g_race) && self.classname != "observer") {
1329                 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed) {
1330                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1331                         speedaward_holder = self.netname;
1332                         speedaward_uid = self.crypto_idfp;
1333                         speedaward_lastupdate = time;
1334                 }
1335                 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1) {
1336                         string rr;
1337                         if(g_cts)
1338                                 rr = CTS_RECORD;
1339                         else
1340                                 rr = RACE_RECORD;
1341                         race_send_speedaward(MSG_ALL);
1342                         speedaward_lastsent = speedaward_speed;
1343                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "") {
1344                                 speedaward_alltimebest = speedaward_speed;
1345                                 speedaward_alltimebest_holder = speedaward_holder;
1346                                 speedaward_alltimebest_uid = speedaward_uid;
1347                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1348                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1349                                 race_send_speedaward_alltimebest(MSG_ALL);
1350                         }
1351                 }
1352         }
1353
1354         float xyspeed;
1355         xyspeed = vlen('1 0 0' * self.velocity_x + '0 1 0' * self.velocity_y);
1356         if(self.weapon == WEP_NEX && autocvar_g_balance_nex_charge && autocvar_g_balance_nex_charge_velocity_rate && xyspeed > autocvar_g_balance_nex_charge_minspeed)
1357         {
1358                 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1359                 xyspeed = min(xyspeed, autocvar_g_balance_nex_charge_maxspeed);
1360                 f = (xyspeed - autocvar_g_balance_nex_charge_minspeed) / (autocvar_g_balance_nex_charge_maxspeed - autocvar_g_balance_nex_charge_minspeed);
1361                 // add the extra charge
1362                 self.nex_charge = min(1, self.nex_charge + autocvar_g_balance_nex_charge_velocity_rate * f * frametime);
1363         }
1364 :end
1365         if(self.flags & FL_ONGROUND)
1366                 self.lastground = time;
1367
1368         // conveyors: then break velocity again
1369         if(self.conveyor.state)
1370                 self.velocity += self.conveyor.movedir;
1371
1372         self.lastflags = self.flags;
1373         self.lastclassname = self.classname;
1374 }