]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_physics.qc
add to the playersound system a globalsound system; make the footsteps and fall sound...
[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                 {
239                         self.nextstep = time + 0.3 + random() * 0.1;
240                         trace_dphitq3surfaceflags = 0;
241                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
242                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
243                         {
244                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
245                                         GlobalSound(globalsound_metalfall, CHAN_AUTO, 0);
246                                 else
247                                         GlobalSound(globalsound_fall, CHAN_AUTO, 0);
248                         }
249                 }
250
251         if(self.classname == "player")
252         {
253                 if(sv_doublejump)
254                 {
255                         self.flags = self.flags - (self.flags & FL_ONGROUND);
256                         tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 2', MOVE_NORMAL, self);
257                         if(trace_fraction < 1 && trace_plane_normal_z > 0.7)
258                                 self.flags = self.flags | FL_ONGROUND;
259                 }
260
261                 if (self.BUTTON_JUMP)
262                         PlayerJump ();
263                 else
264                         self.flags = self.flags | FL_JUMPRELEASED;
265
266                 if (self.waterlevel == 2)
267                         CheckWaterJump ();
268         }
269
270         if (self.flags & FL_WATERJUMP )
271         {
272                 self.velocity_x = self.movedir_x;
273                 self.velocity_y = self.movedir_y;
274                 if (time > self.teleport_time || self.waterlevel == 0)
275                 {
276                         self.flags = self.flags - (self.flags & FL_WATERJUMP);
277                         self.teleport_time = 0;
278                 }
279         }
280         else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
281         {
282                 // noclipping or flying
283                 self.flags = self.flags - (self.flags & FL_ONGROUND);
284
285                 self.velocity = self.velocity * (1 - frametime * sv_friction);
286                 makevectors(self.v_angle);
287                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
288                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
289                 // acceleration
290                 wishdir = normalize(wishvel);
291                 wishspeed = vlen(wishvel);
292                 if (wishspeed > sv_maxspeed*maxspd_mod)
293                         wishspeed = sv_maxspeed*maxspd_mod;
294                 if (time >= self.teleport_time)
295                 {
296                         f = wishspeed - (self.velocity * wishdir);
297                         if (f > 0)
298                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
299                 }
300         }
301         else if (self.waterlevel >= 2)
302         {
303                 // swimming
304                 self.flags = self.flags - (self.flags & FL_ONGROUND);
305
306                 makevectors(self.v_angle);
307                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
308                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
309                 if (wishvel == '0 0 0')
310                         wishvel = '0 0 -60'; // drift towards bottom
311
312                 wishdir = normalize(wishvel);
313                 wishspeed = vlen(wishvel);
314                 if (wishspeed > sv_maxspeed*maxspd_mod)
315                         wishspeed = sv_maxspeed*maxspd_mod;
316                 wishspeed = wishspeed * 0.7;
317
318                 // water friction
319                 self.velocity = self.velocity * (1 - frametime * sv_friction);
320
321                 // water acceleration
322                 f = wishspeed - (self.velocity * wishdir);
323                 if (f > 0)
324                         self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
325         }
326         else if (time < self.ladder_time)
327         {
328                 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
329                 self.flags = self.flags - (self.flags & FL_ONGROUND);
330
331                 self.velocity = self.velocity * (1 - frametime * sv_friction);
332                 makevectors(self.v_angle);
333                 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
334                 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
335                 if (self.gravity)
336                         self.velocity_z = self.velocity_z + self.gravity * sv_gravity * frametime;
337                 else
338                         self.velocity_z = self.velocity_z + sv_gravity * frametime;
339                 if (self.ladder_entity.classname == "func_water")
340                 {
341                         f = vlen(wishvel);
342                         if (f > self.ladder_entity.speed)
343                                 wishvel = wishvel * (self.ladder_entity.speed / f);
344
345                         self.watertype = self.ladder_entity.skin;
346                         f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
347                         if ((self.origin_z + self.view_ofs_z) < f)
348                                 self.waterlevel = 3;
349                         else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
350                                 self.waterlevel = 2;
351                         else if ((self.origin_z + self.mins_z + 1) < f)
352                                 self.waterlevel = 1;
353                         else
354                         {
355                                 self.waterlevel = 0;
356                                 self.watertype = CONTENT_EMPTY;
357                         }
358                 }
359                 // acceleration
360                 wishdir = normalize(wishvel);
361                 wishspeed = vlen(wishvel);
362                 if (wishspeed > sv_maxspeed)
363                         wishspeed = sv_maxspeed;
364                 if (time >= self.teleport_time)
365                 {
366                         f = wishspeed - (self.velocity * wishdir);
367                         if (f > 0)
368                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
369                 }
370         }
371         else if (self.flags & FL_ONGROUND)
372         {
373                 // walking
374                 makevectors(self.v_angle_y * '0 1 0');
375                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
376
377                 if(!(self.lastflags & FL_ONGROUND))
378                 {
379                         if(cvar("speedmeter"))
380                                 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
381                         if(self.lastground < time - 0.3)
382                                 self.velocity = self.velocity * (1 - cvar("sv_friction_on_land"));
383                         if(self.jumppadcount > 1)
384                                 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
385                         self.jumppadcount = 0;
386                 }
387
388                 if (self.velocity_x || self.velocity_y)
389                 if (!(self.flags & FL_JUMPRELEASED) || !self.BUTTON_JUMP)
390                 {
391                         v = self.velocity;
392                         v_z = 0;
393                         f = vlen(v);
394                         if (f < sv_stopspeed)
395                                 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
396                         else
397                                 f = 1 - frametime * sv_friction;
398                         if (f > 0)
399                                 self.velocity = self.velocity * f;
400                         else
401                                 self.velocity = '0 0 0';
402                 }
403                 // acceleration
404                 wishdir = normalize(wishvel);
405                 wishspeed = vlen(wishvel);
406                 if (wishspeed > sv_maxspeed*maxspd_mod)
407                         wishspeed = sv_maxspeed*maxspd_mod;
408                 if (self.crouch)
409                         wishspeed = wishspeed * 0.5;
410                 if (time >= self.teleport_time)
411                 {
412                         f = wishspeed - (self.velocity * wishdir);
413                         if (f > 0)
414                                 self.velocity = self.velocity + wishdir * min(f, sv_accelerate*maxspd_mod * frametime * wishspeed);
415                 }
416         }
417         else
418         {
419                 if(maxspd_mod < 1)
420                 {
421                         maxairspd = sv_maxairspeed*maxspd_mod;
422                         airaccel = sv_airaccelerate*maxspd_mod;
423                 }
424                 else
425                 {
426                         maxairspd = sv_maxairspeed;
427                         airaccel = sv_airaccelerate;
428                 }
429                 // airborn
430                 makevectors(self.v_angle_y * '0 1 0');
431                 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
432                 // acceleration
433                 wishdir = normalize(wishvel);
434                 wishspeed = vlen(wishvel);
435                 if (wishspeed > maxairspd)
436                         wishspeed = maxairspd;
437                 if (self.crouch)
438                         wishspeed = wishspeed * 0.5;
439                 if (time >= self.teleport_time)
440                 {
441                         // NOTE: this does the same as the commented out old code if:
442                         //   sv_airaccel_qw 0
443                         //   sv_airaccel_sideways_friction 0
444                         
445                         float vel_straight;
446                         float vel_z;
447                         vector vel_perpend;
448                         vel_straight = self.velocity * wishdir;
449                         vel_z = self.velocity_z;
450                         vel_perpend = self.velocity - vel_straight * wishdir - vel_z * '0 0 1';
451
452                         f = wishspeed - vel_straight;
453                         if(f > 0)
454                                 vel_straight = vel_straight + min(f, airaccel * frametime * wishspeed) * sv_airaccel_qw;
455                         if(wishspeed > 0)
456                                 vel_straight = vel_straight + min(wishspeed, airaccel * frametime * wishspeed) * (1 - sv_airaccel_qw);
457
458                         // anti-sideways friction to fix QW-style bunnyhopping
459                         vel_perpend = vel_perpend * (1 - frametime * (wishspeed / maxairspd) * sv_airaccel_sideways_friction);
460
461                         self.velocity = vel_straight * wishdir + vel_z * '0 0 1' + vel_perpend;
462
463                         /*
464                         f = wishspeed;// - (self.velocity * wishdir);
465                         if (f > 0)
466                                 self.velocity = self.velocity + wishdir * min(f, airaccel * frametime * wishspeed);
467                         */
468                 }
469         }
470
471         if(self.flags & FL_ONGROUND)
472                 self.lastground = time;
473
474         self.lastflags = self.flags;
475 };