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