]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_physics.qc
factor out Q3's PM_Accelerate function
[divverent/nexuiz.git] / data / qcsrc / server / cl_physics.qc
1 float sv_accelerate;
2 float sv_friction;
3 float sv_maxspeed;
4 float sv_airaccelerate;
5 float sv_maxairspeed;
6 float sv_stopspeed;
7 float sv_gravity;
8 float sv_airaccel_sideways_friction;
9 float sv_airaccel_qw;
10 float sv_airstopaccelerate;
11 float sv_airstrafeaccelerate;
12 float sv_maxairstrafespeed;
13 float sv_aircontrol;
14 .float ladder_time;
15 .entity ladder_entity;
16 .float gravity;
17 .float swamp_slowdown;
18 .float lastflags;
19 .float lastground;
20 .float wasFlying;
21 .float spectatorspeed;
22
23 #define SHTEST_DELTA 10
24 #define SHTEST_THRESHOLD 1.1
25 .float shtest_next;
26 .float shtest_accumulator;
27
28 /*
29 =============
30 PlayerJump
31
32 When you press the jump key
33 =============
34 */
35 void PlayerJump (void)
36 {
37         float mjumpheight;
38
39         mjumpheight = cvar("sv_jumpvelocity");
40         if (self.waterlevel >= WATERLEVEL_SWIMMING)
41         {
42                 if (self.watertype == CONTENT_WATER)
43                         self.velocity_z = 200;
44                 else if (self.watertype == CONTENT_SLIME)
45                         self.velocity_z = 80;
46                 else
47                         self.velocity_z = 50;
48
49                 return;
50         }
51
52
53         if (!(self.flags & FL_ONGROUND))
54                 return;
55
56         if(!sv_pogostick)
57                 if (!(self.flags & FL_JUMPRELEASED))
58                         return;
59                         
60         if(self.health <= g_bloodloss)
61                 return;
62
63         if(g_runematch)
64         {
65                 if(self.runes & RUNE_SPEED)
66                 {
67                         if(self.runes & CURSE_SLOW)
68                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_combo_jumpheight");
69                         else
70                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_jumpheight");
71                 }
72                 else if(self.runes & CURSE_SLOW)
73                 {
74                         mjumpheight = mjumpheight * cvar("g_balance_curse_slow_jumpheight");
75                 }
76         }
77
78         if(g_minstagib && (self.items & IT_INVINCIBLE))
79         {
80                 mjumpheight = mjumpheight * cvar("g_minstagib_speed_jumpheight");
81         }
82
83         self.velocity_z = self.velocity_z + mjumpheight;
84         self.oldvelocity_z = self.velocity_z;
85
86         self.flags &~= FL_ONGROUND;
87         self.flags &~= FL_JUMPRELEASED;
88
89         if (self.crouch)
90                 setanim(self, self.anim_duckjump, FALSE, TRUE, TRUE);
91         else
92                 setanim(self, self.anim_jump, FALSE, TRUE, TRUE);
93
94         if(g_jump_grunt)
95                 PlayerSound(playersound_jump, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
96 }
97
98 void CheckWaterJump()
99 {
100         local vector start, end;
101
102 // check for a jump-out-of-water
103         makevectors (self.angles);
104         start = self.origin;
105         start_z = start_z + 8;
106         v_forward_z = 0;
107         normalize(v_forward);
108         end = start + v_forward*24;
109         traceline (start, end, TRUE, self);
110         if (trace_fraction < 1)
111         {       // solid at waist
112                 start_z = start_z + self.maxs_z - 8;
113                 end = start + v_forward*24;
114                 self.movedir = trace_plane_normal * -50;
115                 traceline (start, end, TRUE, self);
116                 if (trace_fraction == 1)
117                 {       // open at eye level
118                         self.flags |= FL_WATERJUMP;
119                         self.velocity_z = 225;
120                         self.flags &~= FL_JUMPRELEASED;
121                         self.teleport_time = time + 2;  // safety net
122                         return;
123                 }
124         }
125 };
126
127 float racecar_angle(float forward, float down)
128 {
129         float ret, angle_mult;
130
131         if(forward < 0)
132         {
133                 forward = -forward;
134                 down = -down;
135         }
136
137         ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
138
139         angle_mult = forward / (800 + forward);
140
141         if(ret > 180)
142                 return ret * angle_mult + 360 * (1 - angle_mult);
143         else
144                 return ret * angle_mult;
145 }
146
147 void RaceCarPhysics()
148 {
149         // using this move type for "big rigs"
150         // the engine does not push the entity!
151
152         float accel, steer, f;
153         vector angles_save, rigvel;
154
155         angles_save = self.angles;
156         accel = bound(-1, self.movement_x / sv_maxspeed, 1);
157         steer = bound(-1, self.movement_y / sv_maxspeed, 1);
158
159         if(g_bugrigs_reverse_speeding)
160         {
161                 if(accel < 0)
162                 {
163                         // back accel is DIGITAL
164                         // to prevent speedhack
165                         if(accel < -0.5)
166                                 accel = -1;
167                         else
168                                 accel = 0;
169                 }
170         }
171
172         self.angles_x = 0;
173         self.angles_z = 0;
174         makevectors(self.angles); // new forward direction!
175
176         if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
177         {
178                 float myspeed, upspeed, steerfactor, accelfactor;
179
180                 myspeed = self.velocity * v_forward;
181                 upspeed = self.velocity * v_up;
182
183                 // responsiveness factor for steering and acceleration
184                 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
185                 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
186
187                 if(myspeed < 0 && g_bugrigs_reverse_spinning)
188                         steerfactor = -myspeed * g_bugrigs_steer;
189                 else
190                         steerfactor = -myspeed * f * g_bugrigs_steer;
191
192                 if(myspeed < 0 && g_bugrigs_reverse_speeding)
193                         accelfactor = g_bugrigs_accel;
194                 else
195                         accelfactor = f * g_bugrigs_accel;
196                 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
197
198                 if(accel < 0)
199                 {
200                         if(myspeed > 0)
201                         {
202                                 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
203                         }
204                         else
205                         {
206                                 if(!g_bugrigs_reverse_speeding)
207                                         myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
208                         }
209                 }
210                 else
211                 {
212                         if(myspeed >= 0)
213                         {
214                                 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
215                         }
216                         else
217                         {
218                                 if(g_bugrigs_reverse_stopping)
219                                         myspeed = 0;
220                                 else
221                                         myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
222                         }
223                 }
224                 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
225                 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
226
227                 self.angles_y += steer * frametime * steerfactor; // apply steering
228                 makevectors(self.angles); // new forward direction!
229
230                 myspeed += accel * accelfactor * frametime;
231
232                 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
233         }
234         else
235         {
236                 myspeed = vlen(self.velocity);
237
238                 // responsiveness factor for steering and acceleration
239                 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
240                 steerfactor = -myspeed * f;
241                 self.angles_y += steer * frametime * steerfactor; // apply steering
242
243                 rigvel = self.velocity;
244                 makevectors(self.angles); // new forward direction!
245         }
246
247         rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
248         //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
249         //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
250         //MAXIMA: solve(total_acceleration(v) = 0, v);
251
252         if(g_bugrigs_planar_movement)
253         {
254                 vector rigvel_xy, neworigin, up;
255                 float mt;
256
257                 rigvel_z -= frametime * sv_gravity; // 4x gravity plays better
258                 rigvel_xy = rigvel;
259                 rigvel_xy_z = 0;
260
261                 if(g_bugrigs_planar_movement_car_jumping && !g_touchexplode) // touchexplode is a better way to handle collisions
262                         mt = MOVE_NORMAL;
263                 else
264                         mt = MOVE_NOMONSTERS;
265
266                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
267                 up = trace_endpos - self.origin;
268
269                 // BUG RIGS: align the move to the surface instead of doing collision testing
270                 // can we move?
271                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
272
273                 // align to surface
274                 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
275
276                 if(trace_fraction < 0.5)
277                 {
278                         trace_fraction = 1;
279                         neworigin = self.origin;
280                 }
281                 else
282                         neworigin = trace_endpos;
283
284                 if(trace_fraction < 1)
285                 {
286                         // now set angles_x so that the car points parallel to the surface
287                         self.angles = vectoangles(
288                                         '1 0 0' * v_forward_x * trace_plane_normal_z
289                                         +
290                                         '0 1 0' * v_forward_y * trace_plane_normal_z
291                                         +
292                                         '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
293                                         );
294                         self.flags |= FL_ONGROUND;
295                 }
296                 else
297                 {
298                         // now set angles_x so that the car points forward, but is tilted in velocity direction
299                         self.flags &~= FL_ONGROUND;
300                 }
301
302                 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
303                 self.movetype = MOVETYPE_NOCLIP;
304         }
305         else
306         {
307                 rigvel_z -= frametime * sv_gravity; // 4x gravity plays better
308                 self.velocity = rigvel;
309                 self.movetype = MOVETYPE_FLY;
310         }
311
312         trace_fraction = 1;
313         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
314         if(trace_fraction != 1)
315         {
316                 self.angles = vectoangles2(
317                                 '1 0 0' * v_forward_x * trace_plane_normal_z
318                                 +
319                                 '0 1 0' * v_forward_y * trace_plane_normal_z
320                                 +
321                                 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
322                                 trace_plane_normal
323                                 );
324         }
325         else
326         {
327                 vector vel_local;
328
329                 vel_local_x = v_forward * self.velocity;
330                 vel_local_y = v_right * self.velocity;
331                 vel_local_z = v_up * self.velocity;
332
333                 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
334                 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
335         }
336
337         // smooth the angles
338         vector vf1, vu1, smoothangles;
339         makevectors(self.angles);
340         f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
341         if(f == 0)
342                 f = 1;
343         vf1 = v_forward * f;
344         vu1 = v_up * f;
345         makevectors(angles_save);
346         vf1 = vf1 + v_forward * (1 - f);
347         vu1 = vu1 + v_up * (1 - f);
348         smoothangles = vectoangles2(vf1, vu1);
349         self.angles_x = -smoothangles_x;
350         self.angles_z =  smoothangles_z;
351 }
352
353 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
354 {
355         float zspeed, xyspeed, dot, k;
356
357         if(self.movement_x == 0 || self.movement_y != 0)
358                 return; // can't control movement if not moving forward or backward
359         
360         zspeed = self.velocity_z;
361         self.velocity_z = 0;
362         xyspeed = vlen(self.velocity);
363         self.velocity = normalize(self.velocity);
364
365         dot = self.velocity * wishdir;
366         k = 32;
367         k *= sv_aircontrol*dot*dot*frametime;
368
369         if(dot > 0) // we can't change direction while slowing down
370                 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
371
372         self.velocity = self.velocity * xyspeed;
373         self.velocity_z = zspeed;
374 }
375
376 void PM_Accelerate(vector wishdir, float wishspeed, float accel, float accelqw, float sidefric)
377 {
378         float vel_straight;
379         float vel_z;
380         vector vel_perpend;
381         float addspeed;
382
383         vel_straight = self.velocity * wishdir;
384         vel_z = self.velocity_z;
385         vel_perpend = self.velocity - vel_straight * wishdir - vel_z * '0 0 1';
386
387         addspeed = wishspeed - vel_straight;
388         if(addspeed > 0)
389                 vel_straight = vel_straight + min(addspeed, accel * frametime * wishspeed) * accelqw;
390         if(wishspeed > 0)
391                 vel_straight = vel_straight + min(wishspeed, accel * frametime * wishspeed) * (1 - accelqw);
392
393         vel_perpend = vel_perpend * (1 - frametime * wishspeed * sidefric);
394
395         self.velocity = vel_straight * wishdir + vel_z * '0 0 1' + vel_perpend;
396 }
397
398 .vector movement_old;
399 .float buttons_old;
400 .vector v_angle_old;
401 .string lastclassname;
402
403 void Nixnex_GiveCurrentWeapon();
404 void SV_PlayerPhysics()
405 {
406         local vector wishvel, wishdir, v;
407         local float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, shtest_score, buttons;
408         string temps;
409         float buttons_prev;
410
411         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;
412         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
413         {
414                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
415                         self.parm_idlesince = time;
416         }
417         buttons_prev = self.buttons_old;
418         self.buttons_old = buttons;
419         self.movement_old = self.movement;
420         self.v_angle_old = self.v_angle;
421
422         if(time < self.nickspamtime)
423         if(self.nickspamcount >= cvar("g_nick_flood_penalty_yellow"))
424         {
425                 // slight annoyance for nick change scripts
426                 self.movement = -1 * self.movement;
427                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
428
429                 if(self.nickspamcount >= cvar("g_nick_flood_penalty_red")) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
430                 {
431                         self.angles_x = random() * 360;
432                         self.angles_y = random() * 360;
433                         // at least I'm not forcing retardedview by also assigning to angles_z
434                         self.fixangle = 1;
435                 }
436         }
437
438         if(time > self.shtest_next)
439         {
440                 if(self.shtest_next > 0)
441                 {
442                         // self.shtest_accumulator:
443                         //   started at time - SHTEST_DELTA
444                         //   should be at SHTEST_DELTA
445                         shtest_score = self.shtest_accumulator / (SHTEST_DELTA + time - self.shtest_next);
446                         if(shtest_score > SHTEST_THRESHOLD)
447                                 print("TIME PARADOX: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
448                         else if(cvar("developer_shtest"))
449                                 dprint("okay: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
450                 }
451                 self.shtest_next = time + SHTEST_DELTA;
452                 self.shtest_accumulator = 0;
453         }
454         self.shtest_accumulator += frametime;
455
456         if (clienttype(self) == CLIENTTYPE_BOT)
457                 bot_think();
458
459         self.items &~= IT_USING_JETPACK;
460
461         if (self.movetype == MOVETYPE_NONE && self.disableclientprediction != 2)
462                 return;
463
464         if (self.punchangle != '0 0 0')
465         {
466                 f = vlen(self.punchangle) - 10 * frametime;
467                 if (f > 0)
468                         self.punchangle = normalize(self.punchangle) * f;
469                 else
470                         self.punchangle = '0 0 0';
471         }
472
473         if (self.punchvector != '0 0 0')
474         {
475                 f = vlen(self.punchvector) - 30 * frametime;
476                 if (f > 0)
477                         self.punchvector = normalize(self.punchvector) * f;
478                 else
479                         self.punchvector = '0 0 0';
480         }
481
482         maxspd_mod = 1;
483
484         if(g_runematch)
485         {
486                 if(self.runes & RUNE_SPEED)
487                 {
488                         if(self.runes & CURSE_SLOW)
489                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_combo_moverate");
490                         else
491                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_moverate");
492                 }
493                 else if(self.runes & CURSE_SLOW)
494                 {
495                         maxspd_mod = maxspd_mod * cvar("g_balance_curse_slow_moverate");
496                 }
497         }
498
499         if(g_minstagib && (self.items & IT_INVINCIBLE))
500         {
501                 maxspd_mod = cvar("g_minstagib_speed_moverate");
502         }
503
504         if(g_nexball && self.ballcarried)
505         {
506                 maxspd_mod = cvar("g_nexball_basketball_carrier_speed");
507         }
508
509         swampspd_mod = 1;
510         if(self.in_swamp) {
511                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
512         }
513
514         if(self.classname != "player")
515         {
516                 maxspd_mod = cvar("sv_spectator_speed_multiplier");
517                 if(!self.spectatorspeed)
518                         self.spectatorspeed = maxspd_mod;
519                 if(self.impulse && self.impulse <= 19)
520                 {
521                         if(self.lastclassname != "player")
522                         {
523                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
524                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
525                                 else if(self.impulse == 11)
526                                         self.spectatorspeed = maxspd_mod;
527                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
528                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
529                                 else if(self.impulse >= 1 && self.impulse <= 9)
530                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
531                         } // otherwise just clear
532                         self.impulse = 0;
533                 }
534                 maxspd_mod = self.spectatorspeed;
535         }
536
537         spd = max(sv_maxspeed, sv_maxairspeed) * maxspd_mod * swampspd_mod;
538         if(self.speed != spd)
539         {
540                 self.speed = spd;
541                 temps = ftos(spd);
542                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
543                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
544                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
545                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
546         }
547
548         maxspd_mod *= swampspd_mod; // only one common speed modder please!
549         swampspd_mod = 1;
550
551         // if dead, behave differently
552         if (self.deadflag)
553                 goto end;
554
555         if (!self.fixangle && !g_bugrigs)
556         {
557                 self.angles_x = 0;
558                 self.angles_y = self.v_angle_y;
559                 self.angles_z = 0;
560         }
561
562         if(self.flags & FL_ONGROUND)
563         if(self.wasFlying)
564         {
565                 self.wasFlying = 0;
566
567                 if(self.waterlevel < WATERLEVEL_SWIMMING)
568                 if(time >= self.ladder_time)
569                 if not(self.hook)
570                 {
571                         self.nextstep = time + 0.3 + random() * 0.1;
572                         trace_dphitq3surfaceflags = 0;
573                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
574                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
575                         {
576                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
577                                         GlobalSound(globalsound_metalfall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
578                                 else
579                                         GlobalSound(globalsound_fall, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
580                         }
581                 }
582         }
583
584         if(IsFlying(self))
585                 self.wasFlying = 1;
586
587         if(self.classname == "player")
588         {
589                 if(sv_doublejump)
590                 {
591                         self.flags &~= FL_ONGROUND;
592                         tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 2', MOVE_NORMAL, self);
593                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
594                                 self.flags |= FL_ONGROUND;
595                 }
596
597                 if (self.BUTTON_JUMP)
598                         PlayerJump ();
599                 else
600                         self.flags |= FL_JUMPRELEASED;
601
602                 if (self.waterlevel == WATERLEVEL_SWIMMING)
603                         CheckWaterJump ();
604         }
605
606         if (self.flags & FL_WATERJUMP )
607         {
608                 self.velocity_x = self.movedir_x;
609                 self.velocity_y = self.movedir_y;
610                 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
611                 {
612                         self.flags &~= FL_WATERJUMP;
613                         self.teleport_time = 0;
614                 }
615         }
616         else if (g_bugrigs && self.classname == "player")
617         {
618                 RaceCarPhysics();
619         }
620         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
621         {
622                 // noclipping or flying
623                 self.flags &~= FL_ONGROUND;
624
625                 self.velocity = self.velocity * (1 - frametime * sv_friction);
626                 makevectors(self.v_angle);
627                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
628                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
629                 // acceleration
630                 wishdir = normalize(wishvel);
631                 wishspeed = vlen(wishvel);
632                 if (wishspeed > sv_maxspeed*maxspd_mod)
633                         wishspeed = sv_maxspeed*maxspd_mod;
634                 if (time >= self.teleport_time)
635                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
636         }
637         else if (self.waterlevel >= WATERLEVEL_SWIMMING)
638         {
639                 // swimming
640                 self.flags &~= FL_ONGROUND;
641
642                 makevectors(self.v_angle);
643                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
644                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
645                 if (wishvel == '0 0 0')
646                         wishvel = '0 0 -60'; // drift towards bottom
647
648                 wishdir = normalize(wishvel);
649                 wishspeed = vlen(wishvel);
650                 if (wishspeed > sv_maxspeed*maxspd_mod)
651                         wishspeed = sv_maxspeed*maxspd_mod;
652                 wishspeed = wishspeed * 0.7;
653
654                 // water friction
655                 self.velocity = self.velocity * (1 - frametime * sv_friction);
656
657                 // water acceleration
658                 PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
659         }
660         else if (time < self.ladder_time)
661         {
662                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
663                 self.flags &~= FL_ONGROUND;
664
665                 self.velocity = self.velocity * (1 - frametime * sv_friction);
666                 makevectors(self.v_angle);
667                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
668                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
669                 if (self.gravity)
670                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
671                 else
672                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
673                 if (self.ladder_entity.classname == "func_water")
674                 {
675                         f = vlen(wishvel);
676                         if (f > self.ladder_entity.speed)
677                                 wishvel = wishvel * (self.ladder_entity.speed / f);
678
679                         self.watertype = self.ladder_entity.skin;
680                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
681                         if ((self.origin_z + self.view_ofs_z) < f)
682                                 self.waterlevel = WATERLEVEL_SUBMERGED;
683                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
684                                 self.waterlevel = WATERLEVEL_SWIMMING;
685                         else if ((self.origin_z + self.mins_z + 1) < f)
686                                 self.waterlevel = WATERLEVEL_WETFEET;
687                         else
688                         {
689                                 self.waterlevel = WATERLEVEL_NONE;
690                                 self.watertype = CONTENT_EMPTY;
691                         }
692                 }
693                 // acceleration
694                 wishdir = normalize(wishvel);
695                 wishspeed = vlen(wishvel);
696                 if (wishspeed > sv_maxspeed*maxspd_mod)
697                         wishspeed = sv_maxspeed*maxspd_mod;
698                 if (time >= self.teleport_time)
699                 {
700                         // water acceleration
701                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
702                 }
703         }
704         else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!cvar("g_jetpack_fuel") || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO))
705         {
706                 //makevectors(self.v_angle_y * '0 1 0');
707                 makevectors(self.v_angle);
708                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
709                 // add remaining speed as Z component
710                 maxairspd = sv_maxairspeed*max(1, maxspd_mod);
711                 // fix speedhacks :P
712                 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
713                 // add the unused velocity as up component
714                 wishvel_z = 0;
715
716                 // if(self.BUTTON_JUMP)
717                         wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
718
719                 // it is now normalized, so...
720                 float a_side, a_up, a_add, a_diff;
721                 a_side = cvar("g_jetpack_acceleration_side");
722                 a_up = cvar("g_jetpack_acceleration_up");
723                 a_add = cvar("g_jetpack_antigravity") * sv_gravity;
724
725                 wishvel_x *= a_side;
726                 wishvel_y *= a_side;
727                 wishvel_z *= a_up;
728                 wishvel_z += a_add;
729
730                 float best;
731                 best = 0;
732                 //////////////////////////////////////////////////////////////////////////////////////
733                 // finding the maximum over all vectors of above form
734                 // with wishvel having an absolute value of 1
735                 //////////////////////////////////////////////////////////////////////////////////////
736                 // we're finding the maximum over
737                 //   f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
738                 // for z in the range from -1 to 1
739                 //////////////////////////////////////////////////////////////////////////////////////
740                 // maximum is EITHER attained at the single extreme point:
741                 a_diff = a_side * a_side - a_up * a_up;
742                 if(a_diff != 0)
743                 {
744                         f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
745                         if(f > -1 && f < 1) // can it be attained?
746                         {
747                                 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
748                                 //print("middle\n");
749                         }
750                 }
751                 // OR attained at z = 1:
752                 f = (a_up + a_add) * (a_up + a_add);
753                 if(f > best)
754                 {
755                         best = f;
756                         //print("top\n");
757                 }
758                 // OR attained at z = -1:
759                 f = (a_up - a_add) * (a_up - a_add);
760                 if(f > best)
761                 {
762                         best = f;
763                         //print("bottom\n");
764                 }
765                 best = sqrt(best);
766                 //////////////////////////////////////////////////////////////////////////////////////
767
768                 //print("best possible acceleration: ", ftos(best), "\n");
769
770                 float fxy, fz;
771                 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / cvar("g_jetpack_maxspeed_side"), 1);
772                 if(wishvel_z - sv_gravity > 0)
773                         fz = bound(0, 1 - self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
774                 else
775                         fz = bound(0, 1 + self.velocity_z / cvar("g_jetpack_maxspeed_up"), 1);
776
777                 float fvel;
778                 fvel = vlen(wishvel);
779                 wishvel_x *= fxy;
780                 wishvel_y *= fxy;
781                 wishvel_z = (wishvel_z - sv_gravity) * fz + sv_gravity;
782
783                 fvel = min(1, vlen(wishvel) / best);
784                 if(cvar("g_jetpack_fuel") && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
785                         f = min(1, self.ammo_fuel / (cvar("g_jetpack_fuel") * frametime * fvel));
786                 else
787                         f = 1;
788
789                 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
790
791                 if (f > 0 && wishvel != '0 0 0')
792                 {
793                         self.velocity = self.velocity + wishvel * f * frametime;
794                         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
795                                 self.ammo_fuel -= cvar("g_jetpack_fuel") * frametime * fvel * f;
796                         self.flags &~= FL_ONGROUND;
797                         self.items |= IT_USING_JETPACK;
798
799                         // jetpack also inhibits health regeneration, but only for 1 second
800                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_fuel_regen"));
801                 }
802         }
803         else if (self.flags & FL_ONGROUND)
804         {
805                 // we get here if we ran out of ammo
806                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
807                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
808
809                 // walking
810                 makevectors(self.v_angle_y * '0 1 0');
811                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
812
813                 if(!(self.lastflags & FL_ONGROUND))
814                 {
815                         if(cvar("speedmeter"))
816                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
817                         if(self.lastground < time - 0.3)
818                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
819                         if(self.jumppadcount > 1)
820                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
821                         self.jumppadcount = 0;
822                 }
823
824                 if (self.velocity_x || self.velocity_y)
825                 if (!(self.flags & FL_JUMPRELEASED) || !self.BUTTON_JUMP)
826                 {
827                         v = self.velocity;
828                         v_z = 0;
829                         f = vlen(v);
830                         if (f < sv_stopspeed)
831                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
832                         else
833                                 f = 1 - frametime * sv_friction;
834                         if (f > 0)
835                                 self.velocity = self.velocity * f;
836                         else
837                                 self.velocity = '0 0 0';
838                 }
839                 // acceleration
840                 wishdir = normalize(wishvel);
841                 wishspeed = vlen(wishvel);
842                 if (wishspeed > sv_maxspeed*maxspd_mod)
843                         wishspeed = sv_maxspeed*maxspd_mod;
844                 if (self.crouch)
845                         wishspeed = wishspeed * 0.5;
846                 if (time >= self.teleport_time)
847                         PM_Accelerate(wishdir, wishspeed, sv_accelerate*maxspd_mod, 1, 0);
848         }
849         else
850         {
851                 // we get here if we ran out of ammo
852                 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32))
853                         sprint(self, "You don't have any fuel for the ^2Jetpack\n");
854
855                 if(maxspd_mod < 1)
856                 {
857                         maxairspd = sv_maxairspeed*maxspd_mod;
858                         airaccel = sv_airaccelerate*maxspd_mod;
859                 }
860                 else
861                 {
862                         maxairspd = sv_maxairspeed;
863                         airaccel = sv_airaccelerate;
864                 }
865                 // airborn
866                 makevectors(self.v_angle_y * '0 1 0');
867                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
868                 // acceleration
869                 wishdir = normalize(wishvel);
870                 wishspeed = vlen(wishvel);
871                 if (wishspeed > maxairspd)
872                         wishspeed = maxairspd;
873                 if (self.crouch)
874                         wishspeed = wishspeed * 0.5;
875                 if (time >= self.teleport_time)
876                 {
877                         // CPM: air control
878                         float wishspeed2;
879                         wishspeed2 = wishspeed;
880                         if(sv_airstopaccelerate)
881                                 if(self.velocity * wishdir < 0)
882                                         airaccel = sv_airstopaccelerate;
883                         if(self.movement_x == 0 && self.movement_y != 0)
884                         {
885                                 if(sv_maxairstrafespeed)
886                                         wishspeed = min(wishspeed, sv_maxairstrafespeed);
887                                 if(sv_airstrafeaccelerate)
888                                         airaccel = sv_airstrafeaccelerate;
889                         }
890                         // !CPM
891                         
892                         PM_Accelerate(wishdir, wishspeed, airaccel, sv_airaccel_qw, sv_airaccel_sideways_friction / maxairspd);
893                         if(sv_aircontrol)
894                                 CPM_PM_Aircontrol(wishdir, wishspeed2);
895
896                         /*
897                         f = wishspeed;// - (self.velocity * wishdir);
898                         if (f > 0)
899                                 self.velocity = self.velocity + wishdir * min(f, airaccel * frametime * wishspeed);
900                         */
901                 }
902         }
903
904 :end
905         if(self.flags & FL_ONGROUND)
906                 self.lastground = time;
907
908         self.lastflags = self.flags;
909         self.lastclassname = self.classname;
910 };