Move resurfacing (when frozen) code to sys_phys_simulate
[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                 if(this.move_movetype == MOVETYPE_FOLLOW) // not compatible with prediction
37                         this.disableclientprediction = 1;
38                 else if(this.move_qcphysics)
39                         this.disableclientprediction = -1;
40                 else
41                         this.disableclientprediction = 0;
42         }
43
44         viewloc_PlayerPhysics(this);
45
46         PM_check_frozen(this);
47
48         PM_check_blocked(this);
49
50         float maxspeed_mod = 1;
51
52 // conveyors: first fix velocity
53         if (this.conveyor.active) { this.velocity -= this.conveyor.movedir; }
54         MUTATOR_CALLHOOK(PlayerPhysics, this, dt);
55
56         if (!IS_PLAYER(this)) {
57                 sys_phys_spectator_control(this);
58                 maxspeed_mod = STAT(SPECTATORSPEED, this);
59         }
60         sys_phys_fixspeed(this, maxspeed_mod);
61
62         if (IS_DEAD(this)) {
63                 // handle water here
64                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
65                 int cont = pointcontents(midpoint);
66                 if (cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME) {
67                         this.velocity = this.velocity * 0.5;
68
69                         // do we want this?
70                         // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
71                         // { this.velocity_z = 70; }
72                 }
73                 sys_phys_postupdate(this);
74                 return;
75         }
76
77         PM_check_slick(this);
78
79         if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
80         if (IS_PLAYER(this)) {
81                 if (IS_ONGROUND(this)) {
82                         PM_check_hitground(this);
83                         PM_Footsteps(this);
84                 } else if (IsFlying(this)) {
85                         this.wasFlying = true;
86                 }
87                 CheckPlayerJump(this);
88         }
89
90         if (this.flags & FL_WATERJUMP) {
91                 this.velocity_x = this.movedir.x;
92                 this.velocity_y = this.movedir.y;
93                 if (this.waterlevel == WATERLEVEL_NONE) {
94                         this.flags &= ~FL_WATERJUMP;
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) && (!IS_ONSLICK(this) || !PHYS_SLICK_APPLYGRAVITY(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 && !PHYS_FROZEN(this)) {
195                         vector yawangles = '0 1 0' * this.v_angle.y;
196                         vector forward, right, up;
197                         MAKE_VECTORS(yawangles, forward, right, up);
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
214         vector forward, right, up;
215         MAKE_VECTORS(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')), forward, right, up);
216         // wishvel = forward * PHYS_CS(this).movement.x + right * PHYS_CS(this).movement.y + up * PHYS_CS(this).movement.z;
217         vector wishvel = forward * PHYS_CS(this).movement.x
218             + right * PHYS_CS(this).movement.y
219             + '0 0 1' * PHYS_CS(this).movement.z * (this.com_phys_vel_2d ? 0 : 1);
220         if (this.com_phys_water) {
221                 if (PHYS_FROZEN(this))
222                 {
223                         if(this.waterlevel >= WATERLEVEL_SUBMERGED)
224                                 wishvel = '0 0 160'; // resurface
225                 }
226                 else
227                 {
228                         if (PHYS_INPUT_BUTTON_CROUCH(this)) {
229                                 wishvel.z = -PHYS_MAXSPEED(this);
230                         }
231                         if (this.viewloc) {
232                                 wishvel.z = -160;    // drift anyway
233                         } else if (wishvel == '0 0 0') {
234                                 wishvel = '0 0 -60'; // drift towards bottom
235                         }
236                 }
237         }
238         if (this.com_phys_ladder) {
239                 if (this.viewloc) {
240                         wishvel.z = PHYS_CS(this).movement_old.x;
241                 }
242                 if (this.ladder_entity.classname == "func_water") {
243                         float f = vlen(wishvel);
244                         if (f > this.ladder_entity.speed) {
245                                 wishvel *= (this.ladder_entity.speed / f);
246                         }
247
248                         this.watertype = this.ladder_entity.skin;
249                         f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
250                         if ((this.origin_z + this.view_ofs_z) < f) {
251                                 this.waterlevel = WATERLEVEL_SUBMERGED;
252                         } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
253                                 this.waterlevel = WATERLEVEL_SWIMMING;
254                         } else if ((this.origin_z + this.mins_z + 1) < f) {
255                                 this.waterlevel = WATERLEVEL_WETFEET;
256                         } else {
257                                 this.waterlevel = WATERLEVEL_NONE;
258                                 this.watertype = CONTENT_EMPTY;
259                         }
260                 }
261         }
262         // acceleration
263         const vector wishdir = normalize(wishvel);
264         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
265
266         if (this.com_phys_air) {
267                 if (!(this.flags & FL_WATERJUMP)) {
268                         // apply air speed limit
269                         float airaccelqw = PHYS_AIRACCEL_QW(this);
270                         float wishspeed0 = wishspeed;
271                         const float maxairspd = this.com_phys_vel_max;
272                         wishspeed = min(wishspeed, maxairspd);
273                         if (IS_DUCKED(this)) {
274                                 wishspeed *= 0.5;
275                         }
276                         float airaccel = this.com_phys_acc_rate_air;
277
278                         float accelerating = (this.velocity * wishdir > 0);
279                         float wishspeed2 = wishspeed;
280
281                         // CPM: air control
282                         if (PHYS_AIRSTOPACCELERATE(this)) {
283                                 vector curdir = normalize(vec2(this.velocity));
284                                 airaccel += (this.com_phys_acc_rate_air_stop - airaccel) * max(0, -(curdir * wishdir));
285                         }
286                         // note that for straight forward jumping:
287                         // step = accel * dt * wishspeed0;
288                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
289                         // -->
290                         // dv/dt = accel * maxspeed (when slow)
291                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
292                         // log dv/dt = logaccel + logmaxspeed (when slow)
293                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
294                         float strafity = IsMoveInDirection(PHYS_CS(this).movement, -90) + IsMoveInDirection(PHYS_CS(this).movement, +90);  // if one is nonzero, other is always zero
295                         if (PHYS_MAXAIRSTRAFESPEED(this)) {
296                                 wishspeed =
297                                     min(wishspeed,
298                                         GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
299                         }
300                         if (PHYS_AIRSTRAFEACCELERATE(this)) {
301                                 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
302                         }
303                         if (PHYS_AIRSTRAFEACCEL_QW(this)) {
304                                 airaccelqw =
305                                     (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
306                                     *
307                                     (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
308                         }
309                         // !CPM
310
311                         if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && PHYS_CS(this).movement.y == 0 && PHYS_CS(this).movement.x != 0) {
312                                 PM_AirAccelerate(this, dt, wishdir, wishspeed2);
313                         } else {
314                                 float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
315                                 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw,
316                                         PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
317                         }
318
319                         if (PHYS_AIRCONTROL(this)) {
320                                 CPM_PM_Aircontrol(this, dt, wishdir, wishspeed2);
321                         }
322                 }
323         } else {
324                 if (this.com_phys_ground && IS_DUCKED(this)) { wishspeed *= 0.5; }
325                 if (this.com_phys_water) {
326                         wishspeed *= 0.7;
327
328                         //      if (!(this.flags & FL_WATERJUMP)) // TODO: use
329                         {
330                                 // water friction
331                                 float f = 1 - dt * PHYS_FRICTION(this);
332                                 f = min(max(0, f), 1);
333                                 this.velocity *= f;
334
335                                 f = wishspeed - this.velocity * wishdir;
336                                 if (f > 0) {
337                                         float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
338                                         this.velocity += accelspeed * wishdir;
339                                 }
340
341                                 // holding jump button swims upward slowly
342                                 if (this.com_in_jump && !this.viewloc && !PHYS_FROZEN(this)) {
343                                         // was:
344                                         // lava: 50
345                                         // slime: 80
346                                         // water: 100
347                                         // idea: double those
348                                         this.velocity_z = 200;
349                                         if (this.waterlevel >= WATERLEVEL_SUBMERGED) {
350                                                 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
351                                         }
352                                 }
353                         }
354                         if (this.viewloc) {
355                                 const float addspeed = wishspeed - this.velocity * wishdir;
356                                 if (addspeed > 0) {
357                                         const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
358                                         this.velocity += accelspeed * wishdir;
359                                 }
360                         } else {
361                                 // water acceleration
362                                 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
363                         }
364                         return;
365                 }
366                 if (this.com_phys_ground) {
367                         // apply edge friction
368                         const float f2 = vlen2(vec2(this.velocity));
369                         if (f2 > 0) {
370                                 // TODO: apply edge friction
371                                 // apply ground friction
372                                 const int realfriction = (IS_ONSLICK(this))
373                                     ? PHYS_FRICTION_SLICK(this)
374                                         : PHYS_FRICTION(this);
375
376                                 float f = sqrt(f2);
377                                 f = 1 - dt * realfriction
378                                     * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
379                                 f = max(0, f);
380                                 this.velocity *= f;
381                                 /*
382                                    Mathematical analysis time!
383
384                                    Our goal is to invert this mess.
385
386                                    For the two cases we get:
387                                     v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
388                                       = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
389                                     v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
390                                    and
391                                     v = v0 * (1 - dt * PHYS_FRICTION(this))
392                                     v0 = v / (1 - dt * PHYS_FRICTION(this))
393
394                                    These cases would be chosen ONLY if:
395                                     v0 < PHYS_STOPSPEED(this)
396                                     v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
397                                     v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
398                                    and, respectively:
399                                     v0 >= PHYS_STOPSPEED(this)
400                                     v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
401                                     v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
402                                  */
403                         }
404                         const float addspeed = wishspeed - this.velocity * wishdir;
405                         if (addspeed > 0) {
406                                 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
407                                 this.velocity += accelspeed * wishdir;
408                         }
409                         return;
410                 }
411
412                 if (!(this.flags & FL_WATERJUMP)) {
413                         PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
414                 }
415         }
416 }
417
418 .entity groundentity;
419 /** for other entities */
420 void sys_phys_simulate_simple(entity this, float dt)
421 {
422         vector mn = this.mins;
423         vector mx = this.maxs;
424
425         vector g = '0 0 0';
426         if (this.com_phys_gravity_factor && !g) g = '0 0 -1' * PHYS_GRAVITY(NULL);
427
428         vector acc = this.com_phys_acc;
429         vector vel = this.com_phys_vel;
430         vector pos = this.com_phys_pos;
431
432         // SV_Physics_Toss
433
434         vel += g * dt;
435
436         this.angles += dt * this.avelocity;
437         float movetime = dt;
438         for (int i = 0; i < MAX_CLIP_PLANES && movetime > 0; i++) {
439                 vector push = vel * movetime;
440                 vector p0 = pos;
441                 vector p1 = p0 + push;
442                 // SV_PushEntity
443                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
444                 if (!trace_startsolid) {
445                         bool hit = trace_fraction < 1;
446                         pos = trace_endpos;
447                         entity ent = trace_ent;
448                         // SV_LinkEdict_TouchAreaGrid
449                         if (this.solid != SOLID_NOT) {
450                                 FOREACH_ENTITY_RADIUS_ORDERED(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
451                                         if (it.solid != SOLID_TRIGGER || it == this) continue;
452                                         if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax)) {
453                                             // SV_LinkEdict_TouchAreaGrid_Call
454                                             trace_allsolid = false;
455                                             trace_startsolid = false;
456                                             trace_fraction = 1;
457                                             trace_inwater = false;
458                                             trace_inopen = true;
459                                             trace_endpos = it.origin;
460                                             trace_plane_normal = '0 0 1';
461                                             trace_plane_dist = 0;
462                                             trace_ent = this;
463                                             trace_dpstartcontents = 0;
464                                             trace_dphitcontents = 0;
465                                             trace_dphitq3surfaceflags = 0;
466                                             trace_dphittexturename = string_null;
467                                             gettouch(it)(this, it);
468                                             vel = this.velocity;
469                                         }
470                                 });
471                         }
472                         if (hit && this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || this.groundentity != ent)) {
473                                 // SV_Impact (ent, trace);
474                                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
475                                 void(entity, entity) touched = gettouch(this);
476                                 if (touched && this.solid != SOLID_NOT) {
477                                         touched(ent, this);
478                                 }
479                                 void(entity, entity) touched2 = gettouch(ent);
480                                 if (this && ent && touched2 && ent.solid != SOLID_NOT) {
481                                         trace_endpos = ent.origin;
482                                         trace_plane_normal *= -1;
483                                         trace_plane_dist *= -1;
484                                         trace_ent = this;
485                                         trace_dpstartcontents = 0;
486                                         trace_dphitcontents = 0;
487                                         trace_dphitq3surfaceflags = 0;
488                                         trace_dphittexturename = string_null;
489                                         touched2(this, ent);
490                                 }
491                         }
492                 }
493                 // end SV_PushEntity
494                 if (wasfreed(this)) { return; }
495                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
496                 if (trace_fraction == 1) { break; }
497                 movetime *= 1 - min(1, trace_fraction);
498                 ClipVelocity(vel, trace_plane_normal, vel, 1);
499         }
500
501         this.com_phys_acc = acc;
502         this.com_phys_vel = vel;
503         this.com_phys_pos = pos;
504         setorigin(this, this.com_phys_pos);
505 }
506
507 void sys_phys_update_single(entity this)
508 {
509         sys_phys_simulate_simple(this, frametime);
510 }