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