]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/systems/physics.qc
Merge branch 'master' into Mario/killsound
[xonotic/xonotic-data.pk3dir.git] / qcsrc / ecs / systems / physics.qc
1 #include "physics.qh"
2 #include "input.qh"
3
4 .int disableclientprediction;
5
6 void sys_phys_simulate(entity this, float dt);
7 void sys_phys_simulate_simple(entity this, float dt);
8
9 void sys_phys_update(entity this, float dt)
10 {
11         if (!IS_CLIENT(this)) {
12                 sys_phys_simulate_simple(this, dt);
13                 return;
14         }
15         sys_in_update(this, dt);
16
17         sys_phys_fix(this, dt);
18         if (sys_phys_override(this, dt)) { return; } sys_phys_monitor(this, dt);
19
20         this.buttons_old = PHYS_INPUT_BUTTON_MASK(this);
21         this.movement_old = this.movement;
22         this.v_angle_old = this.v_angle;
23
24         sys_phys_ai(this);
25
26         sys_phys_pregame_hold(this);
27
28         if (IS_SVQC) {
29                 if (this.move_movetype == MOVETYPE_NONE) { return; }
30                 // when we get here, disableclientprediction cannot be 2
31                 this.disableclientprediction = (this.move_qcphysics) ? -1 : 0;
32         }
33
34         viewloc_PlayerPhysics(this);
35
36         PM_check_frozen(this);
37
38         PM_check_blocked(this);
39
40         float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown;  // cvar("g_balance_swamp_moverate");
41
42 // conveyors: first fix velocity
43         if (this.conveyor.state) { this.velocity -= this.conveyor.movedir; }
44         MUTATOR_CALLHOOK(PlayerPhysics, this, dt);
45
46         if (!IS_PLAYER(this)) {
47                 sys_phys_spectator_control(this);
48                 maxspeed_mod = this.spectatorspeed;
49         }
50         sys_phys_fixspeed(this, maxspeed_mod);
51
52         if (IS_DEAD(this)) {
53                 // handle water here
54                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
55                 int cont = pointcontents(midpoint);
56                 if (cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME) {
57                         this.velocity = this.velocity * 0.5;
58
59                         // do we want this?
60                         // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
61                         // { this.velocity_z = 70; }
62                 }
63                 goto end;
64         }
65
66         PM_check_slick(this);
67
68         if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
69         if (IS_PLAYER(this)) {
70                 if (IS_ONGROUND(this)) {
71                         PM_check_hitground(this);
72                         PM_Footsteps(this);
73                 } else if (IsFlying(this)) {
74                         this.wasFlying = true;
75                 }
76                 CheckPlayerJump(this);
77         }
78
79         if (this.flags & FL_WATERJUMP) {
80                 this.velocity_x = this.movedir.x;
81                 this.velocity_y = this.movedir.y;
82                 if (this.waterlevel == WATERLEVEL_NONE
83                     || time > PHYS_TELEPORT_TIME(this)
84                     || PHYS_WATERJUMP_TIME(this) <= 0
85                    ) {
86                         this.flags &= ~FL_WATERJUMP;
87                         PHYS_TELEPORT_TIME(this) = 0;
88                         PHYS_WATERJUMP_TIME(this) = 0;
89                 }
90         } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod, dt)) {
91                 // handled
92         } else if (this.move_movetype == MOVETYPE_NOCLIP
93             || this.move_movetype == MOVETYPE_FLY
94             || this.move_movetype == MOVETYPE_FLY_WORLDONLY
95             || MUTATOR_CALLHOOK(IsFlying, this)) {
96                 this.com_phys_friction = PHYS_FRICTION(this);
97                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
98                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
99                 this.com_phys_friction_air = true;
100                 sys_phys_simulate(this, dt);
101                 this.com_phys_friction_air = false;
102         } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
103                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
104                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
105                 this.com_phys_water = true;
106                 sys_phys_simulate(this, dt);
107                 this.com_phys_water = false;
108         } else if (time < this.ladder_time) {
109                 this.com_phys_friction = PHYS_FRICTION(this);
110                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
111                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
112                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
113                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
114                 this.com_phys_ladder = true;
115                 this.com_phys_friction_air = true;
116                 sys_phys_simulate(this, dt);
117                 this.com_phys_friction_air = false;
118                 this.com_phys_ladder = false;
119                 this.com_phys_gravity = '0 0 0';
120         } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
121                 PM_jetpack(this, maxspeed_mod, dt);
122         } else if (IS_ONGROUND(this)) {
123                 if (!WAS_ONGROUND(this)) {
124                         emit(phys_land, this);
125                         if (this.lastground < time - 0.3) {
126                                 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
127                         }
128                 }
129                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
130                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
131                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
132                 this.com_phys_ground = true;
133                 this.com_phys_vel_2d = true;
134                 sys_phys_simulate(this, dt);
135                 this.com_phys_vel_2d = false;
136                 this.com_phys_ground = false;
137                 this.com_phys_gravity = '0 0 0';
138         } else {
139                 this.com_phys_acc_rate_air = PHYS_AIRACCELERATE(this) * min(maxspeed_mod, 1);
140                 this.com_phys_acc_rate_air_stop = PHYS_AIRSTOPACCELERATE(this) * maxspeed_mod;
141                 this.com_phys_acc_rate_air_strafe = PHYS_AIRSTRAFEACCELERATE(this) * maxspeed_mod;
142                 this.com_phys_vel_max_air_strafe = PHYS_MAXAIRSTRAFESPEED(this) * maxspeed_mod;
143                 this.com_phys_vel_max_air = PHYS_MAXAIRSPEED(this) * maxspeed_mod;
144                 this.com_phys_vel_max = PHYS_MAXAIRSPEED(this) * min(maxspeed_mod, 1);
145                 this.com_phys_air = true;
146                 this.com_phys_vel_2d = true;
147                 sys_phys_simulate(this, dt);
148                 this.com_phys_vel_2d = false;
149                 this.com_phys_air = false;
150         }
151
152         LABEL(end)
153         if (IS_ONGROUND(this)) { this.lastground = time; }
154 // conveyors: then break velocity again
155         if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
156         this.lastflags = this.flags;
157
158         this.lastclassname = this.classname;
159 }
160
161 /** for players */
162 void sys_phys_simulate(entity this, float dt)
163 {
164         if (!this.com_phys_ground && !this.com_phys_air) {
165                 // noclipping
166                 // flying
167                 // on a spawnfunc_func_ladder
168                 // swimming in spawnfunc_func_water
169                 // swimming
170                 UNSET_ONGROUND(this);
171
172                 if (this.com_phys_friction_air) {
173                         const vector g = -this.com_phys_gravity;
174                         this.velocity_z += g.z / 2;
175                         this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
176                         this.velocity_z += g.z / 2;
177                 }
178         }
179
180         if (this.com_phys_water) {
181                 // water jump only in certain situations
182                 // this mimics quakeworld code
183                 if (this.com_in_jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc) {
184                         vector yawangles = '0 1 0' * this.v_angle.y;
185                         makevectors(yawangles);
186                         vector forward = v_forward;
187                         vector spot = this.origin + 24 * forward;
188                         spot_z += 8;
189                         traceline(spot, spot, MOVE_NOMONSTERS, this);
190                         if (trace_startsolid) {
191                                 spot_z += 24;
192                                 traceline(spot, spot, MOVE_NOMONSTERS, this);
193                                 if (!trace_startsolid) {
194                                         this.velocity = forward * 50;
195                                         this.velocity_z = 310;
196                                         UNSET_ONGROUND(this);
197                                         SET_JUMP_HELD(this);
198                                 }
199                         }
200                 }
201         }
202         makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
203         // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
204         vector wishvel = v_forward * this.movement.x
205             + v_right * this.movement.y
206             + '0 0 1' * this.movement.z * (this.com_phys_vel_2d ? 0 : 1);
207         if (this.com_phys_water) {
208                 if (PHYS_INPUT_BUTTON_CROUCH(this)) {
209                         wishvel.z = -PHYS_MAXSPEED(this);
210                 }
211                 if (this.viewloc) {
212                         wishvel.z = -160;    // drift anyway
213                 } else if (wishvel == '0 0 0') {
214                         wishvel = '0 0 -60'; // drift towards bottom
215                 }
216         }
217         if (this.com_phys_ladder) {
218                 if (this.viewloc) {
219                         wishvel.z = this.movement_old.x;
220                 }
221                 if (this.ladder_entity.classname == "func_water") {
222                         float f = vlen(wishvel);
223                         if (f > this.ladder_entity.speed) {
224                                 wishvel *= (this.ladder_entity.speed / f);
225                         }
226
227                         this.watertype = this.ladder_entity.skin;
228                         f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
229                         if ((this.origin_z + this.view_ofs_z) < f) {
230                                 this.waterlevel = WATERLEVEL_SUBMERGED;
231                         } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
232                                 this.waterlevel = WATERLEVEL_SWIMMING;
233                         } else if ((this.origin_z + this.mins_z + 1) < f) {
234                                 this.waterlevel = WATERLEVEL_WETFEET;
235                         } else {
236                                 this.waterlevel = WATERLEVEL_NONE;
237                                 this.watertype = CONTENT_EMPTY;
238                         }
239                 }
240         }
241         // acceleration
242         const vector wishdir = normalize(wishvel);
243         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
244
245         if (this.com_phys_air) {
246                 if ((IS_SVQC && time >= PHYS_TELEPORT_TIME(this))
247                     ||  (IS_CSQC && PHYS_WATERJUMP_TIME(this) <= 0)) {
248                         // apply air speed limit
249                         float airaccelqw = PHYS_AIRACCEL_QW(this);
250                         float wishspeed0 = wishspeed;
251                         const float maxairspd = this.com_phys_vel_max;
252                         wishspeed = min(wishspeed, maxairspd);
253                         if (IS_DUCKED(this)) {
254                                 wishspeed *= 0.5;
255                         }
256                         float airaccel = this.com_phys_acc_rate_air;
257
258                         float accelerating = (this.velocity * wishdir > 0);
259                         float wishspeed2 = wishspeed;
260
261                         // CPM: air control
262                         if (PHYS_AIRSTOPACCELERATE(this)) {
263                                 vector curdir = normalize(vec2(this.velocity));
264                                 airaccel += (this.com_phys_acc_rate_air_stop - airaccel) * max(0, -(curdir * wishdir));
265                         }
266                         // note that for straight forward jumping:
267                         // step = accel * dt * wishspeed0;
268                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
269                         // -->
270                         // dv/dt = accel * maxspeed (when slow)
271                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
272                         // log dv/dt = logaccel + logmaxspeed (when slow)
273                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
274                         float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90);  // if one is nonzero, other is always zero
275                         if (PHYS_MAXAIRSTRAFESPEED(this)) {
276                                 wishspeed =
277                                     min(wishspeed,
278                                         GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
279                         }
280                         if (PHYS_AIRSTRAFEACCELERATE(this)) {
281                                 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
282                         }
283                         if (PHYS_AIRSTRAFEACCEL_QW(this)) {
284                                 airaccelqw =
285                                     (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
286                                     *
287                                     (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
288                         }
289                         // !CPM
290
291                         if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0) {
292                                 PM_AirAccelerate(this, dt, wishdir, wishspeed2);
293                         } else {
294                                 float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
295                                 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw,
296                                         PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
297                         }
298
299                         if (PHYS_AIRCONTROL(this)) {
300                                 CPM_PM_Aircontrol(this, dt, wishdir, wishspeed2);
301                         }
302                 }
303         } else {
304                 if (this.com_phys_ground && IS_DUCKED(this)) { wishspeed *= 0.5; }
305                 if (this.com_phys_water) {
306                         wishspeed *= 0.7;
307
308                         //      if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
309                         {
310                                 // water friction
311                                 float f = 1 - dt * PHYS_FRICTION(this);
312                                 f = min(max(0, f), 1);
313                                 this.velocity *= f;
314
315                                 f = wishspeed - this.velocity * wishdir;
316                                 if (f > 0) {
317                                         float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
318                                         this.velocity += accelspeed * wishdir;
319                                 }
320
321                                 // holding jump button swims upward slowly
322                                 if (this.com_in_jump && !this.viewloc) {
323                                         // was:
324                                         // lava: 50
325                                         // slime: 80
326                                         // water: 100
327                                         // idea: double those
328                                         this.velocity_z = 200;
329                                         if (this.waterlevel >= WATERLEVEL_SUBMERGED) {
330                                                 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
331                                         }
332                                 }
333                         }
334                         if (this.viewloc) {
335                                 const float addspeed = wishspeed - this.velocity * wishdir;
336                                 if (addspeed > 0) {
337                                         const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
338                                         this.velocity += accelspeed * wishdir;
339                                 }
340                         } else {
341                                 // water acceleration
342                                 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
343                         }
344                         return;
345                 }
346                 if (this.com_phys_ground) {
347                         // apply edge friction
348                         const float f2 = vlen2(vec2(this.velocity));
349                         if (f2 > 0) {
350                                 trace_dphitq3surfaceflags = 0;
351                                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
352                                 // TODO: apply edge friction
353                                 // apply ground friction
354                                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
355                                     ? PHYS_FRICTION_SLICK(this)
356                                         : PHYS_FRICTION(this);
357
358                                 float f = sqrt(f2);
359                                 f = 1 - dt * realfriction
360                                     * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
361                                 f = max(0, f);
362                                 this.velocity *= f;
363                                 /*
364                                    Mathematical analysis time!
365
366                                    Our goal is to invert this mess.
367
368                                    For the two cases we get:
369                                     v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
370                                       = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
371                                     v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
372                                    and
373                                     v = v0 * (1 - dt * PHYS_FRICTION(this))
374                                     v0 = v / (1 - dt * PHYS_FRICTION(this))
375
376                                    These cases would be chosen ONLY if:
377                                     v0 < PHYS_STOPSPEED(this)
378                                     v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
379                                     v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
380                                    and, respectively:
381                                     v0 >= PHYS_STOPSPEED(this)
382                                     v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
383                                     v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
384                                  */
385                         }
386                         const float addspeed = wishspeed - this.velocity * wishdir;
387                         if (addspeed > 0) {
388                                 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
389                                 this.velocity += accelspeed * wishdir;
390                         }
391                         return;
392                 }
393
394                 if (IS_CSQC ? PHYS_WATERJUMP_TIME(this) <= 0 : time >= PHYS_TELEPORT_TIME(this)) {
395                         PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
396                 }
397         }
398 }
399
400 .entity groundentity;
401 /** for other entities */
402 void sys_phys_simulate_simple(entity this, float dt)
403 {
404         vector mn = this.mins;
405         vector mx = this.maxs;
406
407         vector g = '0 0 0';
408         if (this.com_phys_gravity_factor && !g) g = '0 0 -1' * PHYS_GRAVITY(NULL);
409
410         vector acc = this.com_phys_acc;
411         vector vel = this.com_phys_vel;
412         vector pos = this.com_phys_pos;
413
414         // SV_Physics_Toss
415
416         vel += g * dt;
417
418         this.angles += dt * this.avelocity;
419         float movetime = dt;
420         for (int i = 0; i < MAX_CLIP_PLANES && movetime > 0; i++) {
421                 vector push = vel * movetime;
422                 vector p0 = pos;
423                 vector p1 = p0 + push;
424                 // SV_PushEntity
425                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
426                 if (!trace_startsolid) {
427                         bool hit = trace_fraction < 1;
428                         pos = trace_endpos;
429                         entity ent = trace_ent;
430                         // SV_LinkEdict_TouchAreaGrid
431                         if (this.solid != SOLID_NOT) {
432                                 FOREACH_ENTITY_RADIUS_ORDERED(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
433                                         if (it.solid != SOLID_TRIGGER || it == this) continue;
434                                         if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax)) {
435                                             // SV_LinkEdict_TouchAreaGrid_Call
436                                             trace_allsolid = false;
437                                             trace_startsolid = false;
438                                             trace_fraction = 1;
439                                             trace_inwater = false;
440                                             trace_inopen = true;
441                                             trace_endpos = it.origin;
442                                             trace_plane_normal = '0 0 1';
443                                             trace_plane_dist = 0;
444                                             trace_ent = this;
445                                             trace_dpstartcontents = 0;
446                                             trace_dphitcontents = 0;
447                                             trace_dphitq3surfaceflags = 0;
448                                             trace_dphittexturename = string_null;
449                                             gettouch(it)(this, it);
450                                             vel = this.velocity;
451                                         }
452                                 });
453                         }
454                         if (hit && this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || this.groundentity != ent)) {
455                                 // SV_Impact (ent, trace);
456                                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
457                                 void(entity, entity) touched = gettouch(this);
458                                 if (touched && this.solid != SOLID_NOT) {
459                                         touched(ent, this);
460                                 }
461                                 void(entity, entity) touched2 = gettouch(ent);
462                                 if (this && ent && touched2 && ent.solid != SOLID_NOT) {
463                                         trace_endpos = ent.origin;
464                                         trace_plane_normal *= -1;
465                                         trace_plane_dist *= -1;
466                                         trace_ent = this;
467                                         trace_dpstartcontents = 0;
468                                         trace_dphitcontents = 0;
469                                         trace_dphitq3surfaceflags = 0;
470                                         trace_dphittexturename = string_null;
471                                         touched2(this, ent);
472                                 }
473                         }
474                 }
475                 // end SV_PushEntity
476                 if (wasfreed(this)) { return; }
477                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
478                 if (trace_fraction == 1) { break; }
479                 movetime *= 1 - min(1, trace_fraction);
480                 ClipVelocity(vel, trace_plane_normal, vel, 1);
481         }
482
483         this.com_phys_acc = acc;
484         this.com_phys_vel = vel;
485         this.com_phys_pos = pos;
486         setorigin(this, this.com_phys_pos);
487 }
488
489 void sys_phys_update_single(entity this)
490 {
491         sys_phys_simulate_simple(this, frametime);
492 }