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