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