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