]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/systems/physics.qc
Move PM_walk landing event 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         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                 sys_phys_simulate(this, dt);
93         } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
94                 PM_swim(this, maxspeed_mod);
95         } else if (time < this.ladder_time) {
96                 this.com_phys_friction = PHYS_FRICTION(this);
97                 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
98                 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
99                 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
100                 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
101                 this.com_phys_ladder = true;
102                 sys_phys_simulate(this, dt);
103                 this.com_phys_ladder = false;
104                 this.com_phys_gravity = '0 0 0';
105         } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
106                 PM_jetpack(this, maxspeed_mod);
107         } else if (IS_ONGROUND(this)) {
108                 if (!WAS_ONGROUND(this)) {
109                         emit(phys_land, this);
110                         if (this.lastground < time - 0.3) {
111                                 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
112                         }
113                 }
114                 PM_walk(this, maxspeed_mod);
115         } else {
116                 PM_air(this, buttons_prev, maxspeed_mod);
117         }
118
119         LABEL(end)
120         if (IS_ONGROUND(this)) { this.lastground = time; }
121 // conveyors: then break velocity again
122         if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
123         this.lastflags = this.flags;
124
125         this.lastclassname = this.classname;
126 }
127
128 void sys_phys_simulate(entity this, float dt)
129 {
130         // noclipping
131         // flying
132         // on a spawnfunc_func_ladder
133         // swimming in spawnfunc_func_water
134         UNSET_ONGROUND(this);
135
136         float g = -this.com_phys_gravity.z;
137         if (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE) {
138                 g *= 0.5;
139                 this.velocity_z += g;
140         }
141         this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
142         this.velocity_z += g;
143
144         makevectors(this.v_angle);
145         // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
146         vector wishvel = v_forward * this.movement.x
147             + v_right * this.movement.y
148             + '0 0 1' * this.movement.z;
149         if (this.com_phys_ladder) {
150                 if (this.viewloc) {
151                         wishvel.z = this.oldmovement.x;
152                 }
153                 if (this.ladder_entity.classname == "func_water") {
154                         float f = vlen(wishvel);
155                         if (f > this.ladder_entity.speed) {
156                                 wishvel *= (this.ladder_entity.speed / f);
157                         }
158
159                         this.watertype = this.ladder_entity.skin;
160                         f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
161                         if ((this.origin_z + this.view_ofs_z) < f) {
162                                 this.waterlevel = WATERLEVEL_SUBMERGED;
163                         } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
164                                 this.waterlevel = WATERLEVEL_SWIMMING;
165                         } else if ((this.origin_z + this.mins_z + 1) < f) {
166                                 this.waterlevel = WATERLEVEL_WETFEET;
167                         } else {
168                                 this.waterlevel = WATERLEVEL_NONE;
169                                 this.watertype = CONTENT_EMPTY;
170                         }
171                 }
172         }
173         // acceleration
174         vector wishdir = normalize(wishvel);
175         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
176         if (IS_CSQC || time >= PHYS_TELEPORT_TIME(this)) {
177                 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
178         }
179         PM_ClientMovement_Move(this);
180 }