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