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