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