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