]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/systems/physics.qc
Move PM_air to ecs
[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
8 void sys_phys_update(entity this, float dt)
9 {
10         sys_in_update(this, dt);
11
12         sys_phys_fix(this, dt);
13         if (sys_phys_override(this)) { return; } sys_phys_monitor(this);
14
15         this.buttons_old = PHYS_INPUT_BUTTON_MASK(this);
16         this.movement_old = this.movement;
17         this.v_angle_old = this.v_angle;
18
19         sys_phys_ai(this);
20
21         sys_phys_pregame_hold(this);
22
23         if (IS_SVQC) {
24                 if (PHYS_MOVETYPE(this) == MOVETYPE_NONE) { return; }
25                 // when we get here, disableclientprediction cannot be 2
26                 this.disableclientprediction = 0;
27         }
28
29         viewloc_PlayerPhysics(this);
30
31         PM_check_frozen(this);
32
33         PM_check_blocked(this);
34
35         float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown;  // cvar("g_balance_swamp_moverate");
36
37 // conveyors: first fix velocity
38         if (this.conveyor.state) { this.velocity -= this.conveyor.movedir; }
39         MUTATOR_CALLHOOK(PlayerPhysics, this);
40
41         if (!IS_PLAYER(this)) {
42                 sys_phys_spectator_control(this);
43                 maxspeed_mod = this.spectatorspeed;
44         }
45         sys_phys_fixspeed(this, maxspeed_mod);
46
47         if (IS_DEAD(this)) {
48                 // handle water here
49                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
50                 if (pointcontents(midpoint) == CONTENT_WATER) {
51                         this.velocity = this.velocity * 0.5;
52
53                         // do we want this?
54                         // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
55                         // { this.velocity_z = 70; }
56                 }
57                 goto end;
58         }
59
60         if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
61         if (IS_PLAYER(this)) {
62                 if (IS_ONGROUND(this)) {
63                         PM_check_hitground(this);
64                         PM_Footsteps(this);
65                 } else if (IsFlying(this)) {
66                         this.wasFlying = true;
67                 }
68                 CheckPlayerJump(this);
69         }
70
71         if (this.flags & FL_WATERJUMP) {
72                 this.velocity_x = this.movedir.x;
73                 this.velocity_y = this.movedir.y;
74                 if (time > PHYS_TELEPORT_TIME(this)
75                     || this.waterlevel == WATERLEVEL_NONE
76                     || PHYS_WATERJUMP_TIME(this) <= 0
77                    ) {
78                         this.flags &= ~FL_WATERJUMP;
79                         PHYS_TELEPORT_TIME(this) = 0;
80                         PHYS_WATERJUMP_TIME(this) = 0;
81                 }
82         } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod)) {
83                 // handled
84         } else if (PHYS_MOVETYPE(this) == MOVETYPE_NOCLIP
85             || PHYS_MOVETYPE(this) == MOVETYPE_FLY
86             || PHYS_MOVETYPE(this) == MOVETYPE_FLY_WORLDONLY
87             || MUTATOR_CALLHOOK(IsFlying, this)) {
88                 this.com_phys_friction = PHYS_FRICTION(this);
89                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
90                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
91                 this.com_phys_friction_air = true;
92                 sys_phys_simulate(this, dt);
93                 this.com_phys_friction_air = false;
94         } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
95                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
96                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
97                 this.com_phys_water = true;
98                 sys_phys_simulate(this, dt);
99                 this.com_phys_water = false;
100         } else if (time < this.ladder_time) {
101                 this.com_phys_friction = PHYS_FRICTION(this);
102                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
103                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
104                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
105                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
106                 this.com_phys_ladder = true;
107                 this.com_phys_friction_air = true;
108                 sys_phys_simulate(this, dt);
109                 this.com_phys_friction_air = false;
110                 this.com_phys_ladder = false;
111                 this.com_phys_gravity = '0 0 0';
112         } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
113                 PM_jetpack(this, maxspeed_mod);
114         } else if (IS_ONGROUND(this)) {
115                 if (!WAS_ONGROUND(this)) {
116                         emit(phys_land, this);
117                         if (this.lastground < time - 0.3) {
118                                 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
119                         }
120                 }
121                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
122                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
123                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
124                 this.com_phys_ground = true;
125                 this.com_phys_vel_2d = true;
126                 sys_phys_simulate(this, dt);
127                 this.com_phys_vel_2d = false;
128                 this.com_phys_ground = false;
129                 this.com_phys_gravity = '0 0 0';
130         } else {
131                 this.com_phys_acc_rate_air = PHYS_AIRACCELERATE(this) * min(maxspeed_mod, 1);
132                 this.com_phys_acc_rate_air_stop = PHYS_AIRSTOPACCELERATE(this) * maxspeed_mod;
133                 this.com_phys_acc_rate_air_strafe = PHYS_AIRSTRAFEACCELERATE(this) * maxspeed_mod;
134                 this.com_phys_vel_max_air_strafe = PHYS_MAXAIRSTRAFESPEED(this) * maxspeed_mod;
135                 this.com_phys_vel_max_air = PHYS_MAXAIRSPEED(this) * maxspeed_mod;
136                 this.com_phys_vel_max = PHYS_MAXAIRSPEED(this) * min(maxspeed_mod, 1);
137                 this.com_phys_air = true;
138                 this.com_phys_vel_2d = true;
139                 sys_phys_simulate(this, dt);
140                 this.com_phys_vel_2d = false;
141                 this.com_phys_air = false;
142         }
143
144         LABEL(end)
145         if (IS_ONGROUND(this)) { this.lastground = time; }
146 // conveyors: then break velocity again
147         if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
148         this.lastflags = this.flags;
149
150         this.lastclassname = this.classname;
151 }
152
153 void sys_phys_simulate(entity this, float dt)
154 {
155         const vector g = -this.com_phys_gravity;
156         const bool jump = this.com_in_jump;
157
158         if (!this.com_phys_ground && !this.com_phys_air) {
159                 // noclipping
160                 // flying
161                 // on a spawnfunc_func_ladder
162                 // swimming in spawnfunc_func_water
163                 // swimming
164                 UNSET_ONGROUND(this);
165
166                 if (this.com_phys_friction_air) {
167                         this.velocity_z += g.z / 2;
168                         this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
169                         this.velocity_z += g.z / 2;
170                 }
171         }
172
173         if (this.com_phys_water) {
174                 // water jump only in certain situations
175                 // this mimics quakeworld code
176                 if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc) {
177                         vector yawangles = '0 1 0' * this.v_angle.y;
178                         makevectors(yawangles);
179                         vector forward = v_forward;
180                         vector spot = this.origin + 24 * forward;
181                         spot_z += 8;
182                         traceline(spot, spot, MOVE_NOMONSTERS, this);
183                         if (trace_startsolid) {
184                                 spot_z += 24;
185                                 traceline(spot, spot, MOVE_NOMONSTERS, this);
186                                 if (!trace_startsolid) {
187                                         this.velocity = forward * 50;
188                                         this.velocity_z = 310;
189                                         if (IS_CSQC) { PHYS_WATERJUMP_TIME(this) = 2; }
190                                         UNSET_ONGROUND(this);
191                                         SET_JUMP_HELD(this);
192                                 }
193                         }
194                 }
195         }
196         makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
197         // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
198         vector wishvel = v_forward * this.movement.x
199             + v_right * this.movement.y
200             + '0 0 1' * this.movement.z * (this.com_phys_vel_2d ? 0 : 1);
201         if (this.com_phys_water) {
202                 if (this.viewloc) {
203                         wishvel.z = -160;    // drift anyway
204                 } else if (wishvel == '0 0 0') {
205                         wishvel = '0 0 -60'; // drift towards bottom
206                 }
207         }
208         if (this.com_phys_ladder) {
209                 if (this.viewloc) {
210                         wishvel.z = this.oldmovement.x;
211                 }
212                 if (this.ladder_entity.classname == "func_water") {
213                         float f = vlen(wishvel);
214                         if (f > this.ladder_entity.speed) {
215                                 wishvel *= (this.ladder_entity.speed / f);
216                         }
217
218                         this.watertype = this.ladder_entity.skin;
219                         f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
220                         if ((this.origin_z + this.view_ofs_z) < f) {
221                                 this.waterlevel = WATERLEVEL_SUBMERGED;
222                         } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
223                                 this.waterlevel = WATERLEVEL_SWIMMING;
224                         } else if ((this.origin_z + this.mins_z + 1) < f) {
225                                 this.waterlevel = WATERLEVEL_WETFEET;
226                         } else {
227                                 this.waterlevel = WATERLEVEL_NONE;
228                                 this.watertype = CONTENT_EMPTY;
229                         }
230                 }
231         }
232         // acceleration
233         const vector wishdir = normalize(wishvel);
234         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
235
236         if (this.com_phys_air) {
237                 if ((IS_SVQC && time >= PHYS_TELEPORT_TIME(this))
238                     ||  (IS_CSQC && PHYS_WATERJUMP_TIME(this) <= 0)) {
239                         // apply air speed limit
240                         float airaccelqw = PHYS_AIRACCEL_QW(this);
241                         float wishspeed0 = wishspeed;
242                         const float maxairspd = this.com_phys_vel_max;
243                         wishspeed = min(wishspeed, maxairspd);
244                         if (IS_DUCKED(this)) {
245                                 wishspeed *= 0.5;
246                         }
247                         float airaccel = this.com_phys_acc_rate_air;
248
249                         float accelerating = (this.velocity * wishdir > 0);
250                         float wishspeed2 = wishspeed;
251
252                         // CPM: air control
253                         if (PHYS_AIRSTOPACCELERATE(this)) {
254                                 vector curdir = normalize(vec2(this.velocity));
255                                 airaccel += (this.com_phys_acc_rate_air_stop - airaccel) * max(0, -(curdir * wishdir));
256                         }
257                         // note that for straight forward jumping:
258                         // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
259                         // accel  = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
260                         // -->
261                         // dv/dt = accel * maxspeed (when slow)
262                         // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
263                         // log dv/dt = logaccel + logmaxspeed (when slow)
264                         // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
265                         float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90);  // if one is nonzero, other is always zero
266                         if (PHYS_MAXAIRSTRAFESPEED(this)) {
267                                 wishspeed =
268                                     min(wishspeed,
269                                         GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
270                         }
271                         if (PHYS_AIRSTRAFEACCELERATE(this)) {
272                                 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
273                         }
274                         if (PHYS_AIRSTRAFEACCEL_QW(this)) {
275                                 airaccelqw =
276                                     (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
277                                     *
278                                     (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
279                         }
280                         // !CPM
281
282                         if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0) {
283                                 PM_AirAccelerate(this, wishdir, wishspeed2);
284                         } else {
285                                 float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
286                                 PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw,
287                                         PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
288                         }
289
290                         if (PHYS_AIRCONTROL(this)) {
291                                 CPM_PM_Aircontrol(this, wishdir, wishspeed2);
292                         }
293                 }
294         } else {
295                 if (this.com_phys_ground || this.com_phys_water) {
296                         if (IS_DUCKED(this)) { wishspeed *= 0.5; }
297                 }
298                 if (this.com_phys_water) {
299                         wishspeed *= 0.7;
300
301                         //      if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
302                         {
303                                 // water friction
304                                 float f = 1 - dt * PHYS_FRICTION(this);
305                                 f = min(max(0, f), 1);
306                                 this.velocity *= f;
307
308                                 f = wishspeed - this.velocity * wishdir;
309                                 if (f > 0) {
310                                         float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
311                                         this.velocity += accelspeed * wishdir;
312                                 }
313
314                                 // holding jump button swims upward slowly
315                                 if (jump && !this.viewloc) {
316                                         // was:
317                                         // lava: 50
318                                         // slime: 80
319                                         // water: 100
320                                         // idea: double those
321                                         this.velocity_z = 200;
322                                 }
323                         }
324                         if (this.viewloc) {
325                                 const float addspeed = wishspeed - this.velocity * wishdir;
326                                 if (addspeed > 0) {
327                                         const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
328                                         this.velocity += accelspeed * wishdir;
329                                 }
330                         } else {
331                                 // water acceleration
332                                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
333                                 PM_ClientMovement_Move(this);
334                         }
335                         return;
336                 }
337                 if (this.com_phys_ground) {
338                         // apply edge friction
339                         const float f2 = vlen2(vec2(this.velocity));
340                         if (f2 > 0) {
341                                 trace_dphitq3surfaceflags = 0;
342                                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
343                                 // TODO: apply edge friction
344                                 // apply ground friction
345                                 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
346                                     ? PHYS_FRICTION_SLICK(this)
347                                         : PHYS_FRICTION(this);
348
349                                 float f = sqrt(f2);
350                                 f = 1 - dt * realfriction
351                                     * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
352                                 f = max(0, f);
353                                 this.velocity *= f;
354                                 /*
355                                    Mathematical analysis time!
356
357                                    Our goal is to invert this mess.
358
359                                    For the two cases we get:
360                                     v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
361                                       = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
362                                     v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
363                                    and
364                                     v = v0 * (1 - dt * PHYS_FRICTION(this))
365                                     v0 = v / (1 - dt * PHYS_FRICTION(this))
366
367                                    These cases would be chosen ONLY if:
368                                     v0 < PHYS_STOPSPEED(this)
369                                     v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
370                                     v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
371                                    and, respectively:
372                                     v0 >= PHYS_STOPSPEED(this)
373                                     v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
374                                     v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
375                                  */
376                         }
377                         const float addspeed = wishspeed - this.velocity * wishdir;
378                         if (addspeed > 0) {
379                                 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
380                                 this.velocity += accelspeed * wishdir;
381                         }
382                         if (IS_CSQC && vdist(this.velocity, >, 0)) {
383                                 PM_ClientMovement_Move(this);
384                         }
385                         return;
386                 }
387
388                 if (IS_CSQC || time >= PHYS_TELEPORT_TIME(this)) {
389                         PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
390                 }
391         }
392         PM_ClientMovement_Move(this);
393 }