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