]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/systems/physics.qc
40f7eea24eecd276d92862030223d4c4dad0cd93
[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         int buttons_prev = this.buttons_old;
16         this.buttons_old = PHYS_INPUT_BUTTON_MASK(this);
17         this.movement_old = this.movement;
18         this.v_angle_old = this.v_angle;
19
20         sys_phys_ai(this);
21
22         sys_phys_pregame_hold(this);
23
24         if (IS_SVQC) {
25                 if (PHYS_MOVETYPE(this) == MOVETYPE_NONE) { return; }
26                 // when we get here, disableclientprediction cannot be 2
27                 this.disableclientprediction = 0;
28         }
29
30         viewloc_PlayerPhysics(this);
31
32         PM_check_frozen(this);
33
34         PM_check_blocked(this);
35
36         float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown;  // cvar("g_balance_swamp_moverate");
37
38 // conveyors: first fix velocity
39         if (this.conveyor.state) { this.velocity -= this.conveyor.movedir; }
40         MUTATOR_CALLHOOK(PlayerPhysics, this);
41
42         if (!IS_PLAYER(this)) {
43                 sys_phys_spectator_control(this);
44                 maxspeed_mod = this.spectatorspeed;
45         }
46         sys_phys_fixspeed(this, maxspeed_mod);
47
48         if (IS_DEAD(this)) {
49                 // handle water here
50                 vector midpoint = ((this.absmin + this.absmax) * 0.5);
51                 if (pointcontents(midpoint) == CONTENT_WATER) {
52                         this.velocity = this.velocity * 0.5;
53
54                         // do we want this?
55                         // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
56                         // { this.velocity_z = 70; }
57                 }
58                 goto end;
59         }
60
61         if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
62         if (IS_PLAYER(this)) {
63                 if (IS_ONGROUND(this)) {
64                         PM_check_hitground(this);
65                         PM_Footsteps(this);
66                 } else if (IsFlying(this)) {
67                         this.wasFlying = true;
68                 }
69                 CheckPlayerJump(this);
70         }
71
72         if (this.flags & FL_WATERJUMP) {
73                 this.velocity_x = this.movedir.x;
74                 this.velocity_y = this.movedir.y;
75                 if (time > PHYS_TELEPORT_TIME(this)
76                     || this.waterlevel == WATERLEVEL_NONE
77                     || PHYS_WATERJUMP_TIME(this) <= 0
78                    ) {
79                         this.flags &= ~FL_WATERJUMP;
80                         PHYS_TELEPORT_TIME(this) = 0;
81                         PHYS_WATERJUMP_TIME(this) = 0;
82                 }
83         } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod)) {
84                 // handled
85         } else if (PHYS_MOVETYPE(this) == MOVETYPE_NOCLIP
86             || PHYS_MOVETYPE(this) == MOVETYPE_FLY
87             || PHYS_MOVETYPE(this) == MOVETYPE_FLY_WORLDONLY
88             || MUTATOR_CALLHOOK(IsFlying, this)) {
89                 this.com_phys_friction = PHYS_FRICTION(this);
90                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
91                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
92                 this.com_phys_friction_air = true;
93                 sys_phys_simulate(this, dt);
94                 this.com_phys_friction_air = false;
95         } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
96                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
97                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
98                 this.com_phys_water = true;
99                 sys_phys_simulate(this, dt);
100                 this.com_phys_water = false;
101         } else if (time < this.ladder_time) {
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_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
106                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
107                 this.com_phys_ladder = true;
108                 this.com_phys_friction_air = true;
109                 sys_phys_simulate(this, dt);
110                 this.com_phys_friction_air = false;
111                 this.com_phys_ladder = false;
112                 this.com_phys_gravity = '0 0 0';
113         } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
114                 PM_jetpack(this, maxspeed_mod);
115         } else if (IS_ONGROUND(this)) {
116                 if (!WAS_ONGROUND(this)) {
117                         emit(phys_land, this);
118                         if (this.lastground < time - 0.3) {
119                                 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
120                         }
121                 }
122                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
123                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
124                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
125                 this.com_phys_ground = true;
126                 this.com_phys_vel_2d = true;
127                 sys_phys_simulate(this, dt);
128                 this.com_phys_vel_2d = false;
129                 this.com_phys_ground = false;
130                 this.com_phys_gravity = '0 0 0';
131         } else {
132                 PM_air(this, buttons_prev, maxspeed_mod);
133         }
134
135         LABEL(end)
136         if (IS_ONGROUND(this)) { this.lastground = time; }
137 // conveyors: then break velocity again
138         if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
139         this.lastflags = this.flags;
140
141         this.lastclassname = this.classname;
142 }
143
144 void sys_phys_simulate(entity this, float dt)
145 {
146         const vector g = -this.com_phys_gravity;
147         const bool jump = this.com_in_jump;
148
149         if (!this.com_phys_ground) {
150                 // noclipping
151                 // flying
152                 // on a spawnfunc_func_ladder
153                 // swimming in spawnfunc_func_water
154                 // swimming
155                 UNSET_ONGROUND(this);
156
157                 if (this.com_phys_friction_air) {
158                         this.velocity_z += g.z / 2;
159                         this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
160                         this.velocity_z += g.z / 2;
161                 }
162         }
163
164         if (this.com_phys_water) {
165                 // water jump only in certain situations
166                 // this mimics quakeworld code
167                 if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc) {
168                         vector yawangles = '0 1 0' * this.v_angle.y;
169                         makevectors(yawangles);
170                         vector forward = v_forward;
171                         vector spot = this.origin + 24 * forward;
172                         spot_z += 8;
173                         traceline(spot, spot, MOVE_NOMONSTERS, this);
174                         if (trace_startsolid) {
175                                 spot_z += 24;
176                                 traceline(spot, spot, MOVE_NOMONSTERS, this);
177                                 if (!trace_startsolid) {
178                                         this.velocity = forward * 50;
179                                         this.velocity_z = 310;
180                                         if (IS_CSQC) { PHYS_WATERJUMP_TIME(this) = 2; }
181                                         UNSET_ONGROUND(this);
182                                         SET_JUMP_HELD(this);
183                                 }
184                         }
185                 }
186         }
187         makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
188         // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
189         vector wishvel = v_forward * this.movement.x
190             + v_right * this.movement.y
191             + '0 0 1' * this.movement.z * (this.com_phys_vel_2d ? 0 : 1);
192         if (this.com_phys_water) {
193                 if (this.viewloc) {
194                         wishvel.z = -160;    // drift anyway
195                 } else if (wishvel == '0 0 0') {
196                         wishvel = '0 0 -60'; // drift towards bottom
197                 }
198         }
199         if (this.com_phys_ladder) {
200                 if (this.viewloc) {
201                         wishvel.z = this.oldmovement.x;
202                 }
203                 if (this.ladder_entity.classname == "func_water") {
204                         float f = vlen(wishvel);
205                         if (f > this.ladder_entity.speed) {
206                                 wishvel *= (this.ladder_entity.speed / f);
207                         }
208
209                         this.watertype = this.ladder_entity.skin;
210                         f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
211                         if ((this.origin_z + this.view_ofs_z) < f) {
212                                 this.waterlevel = WATERLEVEL_SUBMERGED;
213                         } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
214                                 this.waterlevel = WATERLEVEL_SWIMMING;
215                         } else if ((this.origin_z + this.mins_z + 1) < f) {
216                                 this.waterlevel = WATERLEVEL_WETFEET;
217                         } else {
218                                 this.waterlevel = WATERLEVEL_NONE;
219                                 this.watertype = CONTENT_EMPTY;
220                         }
221                 }
222         }
223         // acceleration
224         const vector wishdir = normalize(wishvel);
225         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
226         if (this.com_phys_ground || this.com_phys_water) {
227                 if (IS_DUCKED(this)) { wishspeed *= 0.5; }
228         }
229         if (this.com_phys_water) {
230                 wishspeed *= 0.7;
231
232                 //      if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
233                 {
234                         // water friction
235                         float f = 1 - dt * PHYS_FRICTION(this);
236                         f = min(max(0, f), 1);
237                         this.velocity *= f;
238
239                         f = wishspeed - this.velocity * wishdir;
240                         if (f > 0) {
241                                 float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
242                                 this.velocity += accelspeed * wishdir;
243                         }
244
245                         // holding jump button swims upward slowly
246                         if (jump && !this.viewloc) {
247                                 // was:
248                                 // lava: 50
249                                 // slime: 80
250                                 // water: 100
251                                 // idea: double those
252                                 this.velocity_z = 200;
253                         }
254                 }
255                 if (this.viewloc) {
256                         const float addspeed = wishspeed - this.velocity * wishdir;
257                         if (addspeed > 0) {
258                                 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
259                                 this.velocity += accelspeed * wishdir;
260                         }
261                 } else {
262                         // water acceleration
263                         PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
264                         PM_ClientMovement_Move(this);
265                 }
266                 return;
267         }
268         if (this.com_phys_ground) {
269                 // apply edge friction
270                 const float f2 = vlen2(vec2(this.velocity));
271                 if (f2 > 0) {
272                         trace_dphitq3surfaceflags = 0;
273                         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
274                         // TODO: apply edge friction
275                         // apply ground friction
276                         const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
277                             ? PHYS_FRICTION_SLICK(this)
278                                 : PHYS_FRICTION(this);
279
280                         float f = sqrt(f2);
281                         f = 1 - dt * realfriction
282                             * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
283                         f = max(0, f);
284                         this.velocity *= f;
285                         /*
286                            Mathematical analysis time!
287
288                            Our goal is to invert this mess.
289
290                            For the two cases we get:
291                             v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
292                               = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
293                             v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
294                            and
295                             v = v0 * (1 - dt * PHYS_FRICTION(this))
296                             v0 = v / (1 - dt * PHYS_FRICTION(this))
297
298                            These cases would be chosen ONLY if:
299                             v0 < PHYS_STOPSPEED(this)
300                             v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
301                             v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
302                            and, respectively:
303                             v0 >= PHYS_STOPSPEED(this)
304                             v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
305                             v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
306                          */
307                 }
308                 const float addspeed = wishspeed - this.velocity * wishdir;
309                 if (addspeed > 0) {
310                         const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
311                         this.velocity += accelspeed * wishdir;
312                 }
313                 if (IS_CSQC && vdist(this.velocity, >, 0)) {
314                         PM_ClientMovement_Move(this);
315                 }
316                 return;
317         }
318
319         if (IS_CSQC || time >= PHYS_TELEPORT_TIME(this)) {
320                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
321         }
322         PM_ClientMovement_Move(this);
323 }