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