]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_physics.qc
record more antilag frames
[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 >= 2)
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(g_runematch)
57         {
58                 if(self.runes & RUNE_SPEED)
59                 {
60                         if(self.runes & CURSE_SLOW)
61                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_combo_jumpheight");
62                         else
63                                 mjumpheight = mjumpheight * cvar("g_balance_rune_speed_jumpheight");
64                 }
65                 else if(self.runes & CURSE_SLOW)
66                 {
67                         mjumpheight = mjumpheight * cvar("g_balance_curse_slow_jumpheight");
68                 }
69         }
70
71         if(g_minstagib && (self.items & IT_INVINCIBLE))
72         {
73                 mjumpheight = mjumpheight * cvar("g_minstagib_speed_jumpheight");
74         }
75
76         self.velocity_z = self.velocity_z + mjumpheight;
77         self.oldvelocity_z = self.velocity_z;
78
79         self.flags = self.flags - FL_ONGROUND;
80         self.flags = self.flags - FL_JUMPRELEASED;
81
82         if (self.crouch)
83                 player_setanim(self.anim_duckjump, FALSE, TRUE, TRUE);
84         else
85                 player_setanim(self.anim_jump, FALSE, TRUE, TRUE);
86
87         if(g_jump_grunt)
88                 PlayerSound(playersound_jump, CHAN_PLAYER, 0);
89 }
90
91 void CheckWaterJump()
92 {
93         local vector start, end;
94
95 // check for a jump-out-of-water
96         makevectors (self.angles);
97         start = self.origin;
98         start_z = start_z + 8;
99         v_forward_z = 0;
100         normalize(v_forward);
101         end = start + v_forward*24;
102         traceline (start, end, TRUE, self);
103         if (trace_fraction < 1)
104         {       // solid at waist
105                 start_z = start_z + self.maxs_z - 8;
106                 end = start + v_forward*24;
107                 self.movedir = trace_plane_normal * -50;
108                 traceline (start, end, TRUE, self);
109                 if (trace_fraction == 1)
110                 {       // open at eye level
111                         self.flags = self.flags | FL_WATERJUMP;
112                         self.velocity_z = 225;
113                         self.flags = self.flags - (self.flags & FL_JUMPRELEASED);
114                         self.teleport_time = time + 2;  // safety net
115                         return;
116                 }
117         }
118 };
119
120 .vector movement_old;
121 .float buttons_old;
122 .vector v_angle_old;
123
124 void Nixnex_GiveCurrentWeapon();
125 void SV_PlayerPhysics()
126 {
127         local vector wishvel, wishdir, v;
128         local float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, shtest_score, buttons;
129         string temps;
130
131         antilag_record(self);
132
133         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;
134         if(!sv_maxidle_spectatorsareidle || self.movetype == MOVETYPE_WALK)
135         {
136                 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
137                         self.parm_idlesince = time;
138         }
139         self.buttons_old = buttons;
140         self.movement_old = self.movement;
141         self.v_angle_old = self.v_angle;
142
143         if(time > self.shtest_next)
144         {
145                 if(self.shtest_next > 0)
146                 {
147                         // self.shtest_accumulator:
148                         //   started at time - SHTEST_DELTA
149                         //   should be at SHTEST_DELTA
150                         shtest_score = self.shtest_accumulator / (SHTEST_DELTA + time - self.shtest_next);
151                         if(shtest_score > SHTEST_THRESHOLD)
152                                 print("TIME PARADOX: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
153                         else if(cvar("developer_shtest"))
154                                 dprint("okay: shtest for ", self.netname, " said ", ftos(shtest_score), "\n");
155                 }
156                 self.shtest_next = time + SHTEST_DELTA;
157                 self.shtest_accumulator = 0;
158         }
159         self.shtest_accumulator += frametime;
160
161         if (clienttype(self) == CLIENTTYPE_BOT)
162                 bot_think();
163
164         if (self.movetype == MOVETYPE_NONE)
165                 return;
166
167         if (self.punchangle != '0 0 0')
168         {
169                 f = vlen(self.punchangle) - 10 * frametime;
170                 if (f > 0)
171                         self.punchangle = normalize(self.punchangle) * f;
172                 else
173                         self.punchangle = '0 0 0';
174         }
175
176         if (self.punchvector != '0 0 0')
177         {
178                 f = vlen(self.punchvector) - 30 * frametime;
179                 if (f > 0)
180                         self.punchvector = normalize(self.punchvector) * f;
181                 else
182                         self.punchvector = '0 0 0';
183         }
184
185         maxspd_mod = 1;
186
187         if(g_runematch)
188         {
189                 if(self.runes & RUNE_SPEED)
190                 {
191                         if(self.runes & CURSE_SLOW)
192                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_combo_moverate");
193                         else
194                                 maxspd_mod = maxspd_mod * cvar("g_balance_rune_speed_moverate");
195                 }
196                 else if(self.runes & CURSE_SLOW)
197                 {
198                         maxspd_mod = maxspd_mod * cvar("g_balance_curse_slow_moverate");
199                 }
200         }
201
202         if(g_minstagib && (self.items & IT_INVINCIBLE))
203         {
204                 maxspd_mod = cvar("g_minstagib_speed_moverate");
205         }
206
207         swampspd_mod = 1;
208         if(self.in_swamp) {
209                 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
210         }
211
212         if(self.flags & FL_NOTARGET)
213         {
214                 maxspd_mod = cvar("sv_spectator_speed_multiplier");
215                 if(!self.spectatorspeed)
216                         self.spectatorspeed = maxspd_mod;
217                 if(self.impulse && self.impulse <= 19)
218                 {
219                         if(self.lastflags & FL_NOTARGET)
220                         {
221                                 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18)
222                                         self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
223                                 else if(self.impulse == 11)
224                                         self.spectatorspeed = maxspd_mod;
225                                 else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19)
226                                         self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
227                                 else if(self.impulse >= 1 && self.impulse <= 9)
228                                         self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
229                         } // otherwise just clear
230                         self.impulse = 0;
231                 }
232                 maxspd_mod = self.spectatorspeed;
233         }
234
235         spd = sv_maxspeed * maxspd_mod * swampspd_mod;
236
237         if(self.speed != spd)
238         {
239                 self.speed = spd;
240                 temps = ftos(spd);
241                 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
242                 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
243                 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
244                 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
245
246                 temps = ftos(sv_accelerate * maxspd_mod);
247                 stuffcmd(self, strcat("cl_movement_accelerate ", temps, "\n"));
248         }
249
250         // if dead, behave differently
251         if (self.deadflag)
252                 return;
253
254         if (!self.fixangle)
255         {
256                 self.angles_x = 0;
257                 self.angles_y = self.v_angle_y;
258                 self.angles_z = 0;
259         }
260
261         if(self.flags & FL_ONGROUND)
262         if(self.wasFlying)
263         {
264                 self.wasFlying = 0;
265
266                 if(self.waterlevel < 2)
267                 if(time >= self.ladder_time)
268                 if not(self.hook)
269                 {
270                         self.nextstep = time + 0.3 + random() * 0.1;
271                         trace_dphitq3surfaceflags = 0;
272                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
273                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
274                         {
275                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
276                                         GlobalSound(globalsound_metalfall, CHAN_PLAYER, 0);
277                                 else
278                                         GlobalSound(globalsound_fall, CHAN_PLAYER, 0);
279                         }
280                 }
281         }
282
283         if(IsFlying(self))
284                 self.wasFlying = 1;
285
286         if(self.classname == "player")
287         {
288                 if(sv_doublejump)
289                 {
290                         self.flags = self.flags - (self.flags & FL_ONGROUND);
291                         tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 2', MOVE_NORMAL, self);
292                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
293                                 self.flags = self.flags | FL_ONGROUND;
294                 }
295
296                 if (self.BUTTON_JUMP)
297                         PlayerJump ();
298                 else
299                         self.flags = self.flags | FL_JUMPRELEASED;
300
301                 if (self.waterlevel == 2)
302                         CheckWaterJump ();
303         }
304
305         if (self.flags & FL_WATERJUMP )
306         {
307                 self.velocity_x = self.movedir_x;
308                 self.velocity_y = self.movedir_y;
309                 if (time > self.teleport_time || self.waterlevel == 0)
310                 {
311                         self.flags = self.flags - (self.flags & FL_WATERJUMP);
312                         self.teleport_time = 0;
313                 }
314         }
315         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
316         {
317                 // noclipping or flying
318                 self.flags = self.flags - (self.flags & FL_ONGROUND);
319
320                 self.velocity = self.velocity * (1 - frametime * sv_friction);
321                 makevectors(self.v_angle);
322                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
323                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
324                 // acceleration
325                 wishdir = normalize(wishvel);
326                 wishspeed = vlen(wishvel);
327                 if (wishspeed > sv_maxspeed*maxspd_mod)
328                         wishspeed = sv_maxspeed*maxspd_mod;
329                 if (time >= self.teleport_time)
330                 {
331                         f = wishspeed - (self.velocity * wishdir);
332                         if (f > 0)
333                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
334                 }
335         }
336         else if (self.waterlevel >= 2)
337         {
338                 // swimming
339                 self.flags = self.flags - (self.flags & FL_ONGROUND);
340
341                 makevectors(self.v_angle);
342                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
343                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
344                 if (wishvel == '0 0 0')
345                         wishvel = '0 0 -60'; // drift towards bottom
346
347                 wishdir = normalize(wishvel);
348                 wishspeed = vlen(wishvel);
349                 if (wishspeed > sv_maxspeed*maxspd_mod)
350                         wishspeed = sv_maxspeed*maxspd_mod;
351                 wishspeed = wishspeed * 0.7;
352
353                 // water friction
354                 self.velocity = self.velocity * (1 - frametime * sv_friction);
355
356                 // water acceleration
357                 f = wishspeed - (self.velocity * wishdir);
358                 if (f > 0)
359                         self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
360         }
361         else if (time < self.ladder_time)
362         {
363                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
364                 self.flags = self.flags - (self.flags & FL_ONGROUND);
365
366                 self.velocity = self.velocity * (1 - frametime * sv_friction);
367                 makevectors(self.v_angle);
368                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
369                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
370                 if (self.gravity)
371                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
372                 else
373                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
374                 if (self.ladder_entity.classname == "func_water")
375                 {
376                         f = vlen(wishvel);
377                         if (f > self.ladder_entity.speed)
378                                 wishvel = wishvel * (self.ladder_entity.speed / f);
379
380                         self.watertype = self.ladder_entity.skin;
381                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
382                         if ((self.origin_z + self.view_ofs_z) < f)
383                                 self.waterlevel = 3;
384                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
385                                 self.waterlevel = 2;
386                         else if ((self.origin_z + self.mins_z + 1) < f)
387                                 self.waterlevel = 1;
388                         else
389                         {
390                                 self.waterlevel = 0;
391                                 self.watertype = CONTENT_EMPTY;
392                         }
393                 }
394                 // acceleration
395                 wishdir = normalize(wishvel);
396                 wishspeed = vlen(wishvel);
397                 if (wishspeed > sv_maxspeed)
398                         wishspeed = sv_maxspeed;
399                 if (time >= self.teleport_time)
400                 {
401                         f = wishspeed - (self.velocity * wishdir);
402                         if (f > 0)
403                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
404                 }
405         }
406         else if (self.flags & FL_ONGROUND)
407         {
408                 // walking
409                 makevectors(self.v_angle_y * '0 1 0');
410                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
411
412                 if(!(self.lastflags & FL_ONGROUND))
413                 {
414                         if(cvar("speedmeter"))
415                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
416                         if(self.lastground < time - 0.3)
417                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
418                         if(self.jumppadcount > 1)
419                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
420                         self.jumppadcount = 0;
421                 }
422
423                 if (self.velocity_x || self.velocity_y)
424                 if (!(self.flags & FL_JUMPRELEASED) || !self.BUTTON_JUMP)
425                 {
426                         v = self.velocity;
427                         v_z = 0;
428                         f = vlen(v);
429                         if (f < sv_stopspeed)
430                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
431                         else
432                                 f = 1 - frametime * sv_friction;
433                         if (f > 0)
434                                 self.velocity = self.velocity * f;
435                         else
436                                 self.velocity = '0 0 0';
437                 }
438                 // acceleration
439                 wishdir = normalize(wishvel);
440                 wishspeed = vlen(wishvel);
441                 if (wishspeed > sv_maxspeed*maxspd_mod)
442                         wishspeed = sv_maxspeed*maxspd_mod;
443                 if (self.crouch)
444                         wishspeed = wishspeed * 0.5;
445                 if (time >= self.teleport_time)
446                 {
447                         f = wishspeed - (self.velocity * wishdir);
448                         if (f > 0)
449                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
450                 }
451         }
452         else
453         {
454                 if(maxspd_mod < 1)
455                 {
456                         maxairspd = sv_maxairspeed*maxspd_mod;
457                         airaccel = sv_airaccelerate*maxspd_mod;
458                 }
459                 else
460                 {
461                         maxairspd = sv_maxairspeed;
462                         airaccel = sv_airaccelerate;
463                 }
464                 // airborn
465                 makevectors(self.v_angle_y * '0 1 0');
466                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
467                 // acceleration
468                 wishdir = normalize(wishvel);
469                 wishspeed = vlen(wishvel);
470                 if (wishspeed > maxairspd)
471                         wishspeed = maxairspd;
472                 if (self.crouch)
473                         wishspeed = wishspeed * 0.5;
474                 if (time >= self.teleport_time)
475                 {
476                         // NOTE: this does the same as the commented out old code if:
477                         //   sv_airaccel_qw 0
478                         //   sv_airaccel_sideways_friction 0
479                         
480                         float vel_straight;
481                         float vel_z;
482                         vector vel_perpend;
483                         vel_straight = self.velocity * wishdir;
484                         vel_z = self.velocity_z;
485                         vel_perpend = self.velocity - vel_straight * wishdir - vel_z * '0 0 1';
486
487                         f = wishspeed - vel_straight;
488                         if(f > 0)
489                                 vel_straight = vel_straight + min(f, airaccel * frametime * wishspeed) * sv_airaccel_qw;
490                         if(wishspeed > 0)
491                                 vel_straight = vel_straight + min(wishspeed, airaccel * frametime * wishspeed) * (1 - sv_airaccel_qw);
492
493                         // anti-sideways friction to fix QW-style bunnyhopping
494                         vel_perpend = vel_perpend * (1 - frametime * (wishspeed / maxairspd) * sv_airaccel_sideways_friction);
495
496                         self.velocity = vel_straight * wishdir + vel_z * '0 0 1' + vel_perpend;
497
498                         /*
499                         f = wishspeed;// - (self.velocity * wishdir);
500                         if (f > 0)
501                                 self.velocity = self.velocity + wishdir * min(f, airaccel * frametime * wishspeed);
502                         */
503                 }
504         }
505
506         if(self.flags & FL_ONGROUND)
507                 self.lastground = time;
508
509         self.lastflags = self.flags;
510 };