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