1 float lastclientthink, sv_maxspeed, sv_friction, sv_accelerate, sv_stopspeed;
2 float sv_edgefriction, cl_rollspeed, cl_divspeed;
5 // Highly optimized port of SV_ClientThink from engine code to QuakeC.
6 // No behavior changes! This code is much shorter and probably faster than
9 // note that darkplaces engine will call this function if it finds it,
10 // so modify for your own mods and enjoy...
12 // note also, this code uses some builtin functions from dpextensions.qc
13 // (included with darkplaces engine releases)
15 // P.S. if you find something weird in this code, it's just mimicing weird
16 // stuff in the original quake engine code (which was so unreadable it was
17 // hard to even identify what it was doing)
19 void() SV_PlayerPhysics =
21 local vector wishvel, wishdir, v;
22 local float wishspeed, f, limit;
24 if (self.movetype == MOVETYPE_NONE)
27 if (self.punchangle != '0 0 0')
29 f = vlen(self.punchangle) - 10 * frametime;
31 self.punchangle = normalize(self.punchangle) * f;
33 self.punchangle = '0 0 0';
36 // if dead, behave differently
40 if (time != lastclientthink)
42 lastclientthink = time;
43 sv_maxspeed = cvar("sv_maxspeed");
44 sv_friction = cvar("sv_friction");
45 sv_accelerate = cvar("sv_accelerate");
46 sv_stopspeed = cvar("sv_stopspeed");
47 sv_edgefriction = cvar("edgefriction");
48 // LordHavoc: this * 4 is an optimization
49 cl_rollangle = cvar("cl_rollangle") * 4;
50 // LordHavoc: this 1 / is an optimization
51 cl_divspeed = 1 / cvar("cl_rollspeed");
54 // show 1/3 the pitch angle and all the roll angle
55 self.angles_z = bound(-1, self.velocity * v_right * cl_divspeed, 1) * cl_rollangle;
58 self.angles_x = (self.v_angle_x + self.punchangle_x) * -0.333;
59 self.angles_y = self.v_angle_y + self.punchangle_y;
62 if (self.flags & FL_WATERJUMP )
64 self.velocity_x = self.movedir_x;
65 self.velocity_y = self.movedir_y;
66 if (time > self.teleport_time || self.waterlevel == 0)
68 self.flags = self.flags - (self.flags & FL_WATERJUMP);
69 self.teleport_time = 0;
75 if (self.waterlevel >= 2)
76 if (self.movetype != MOVETYPE_NOCLIP)
78 makevectors(self.v_angle);
79 if (self.movement == '0 0 0')
80 wishvel = '0 0 -60'; // drift towards bottom
82 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
84 wishspeed = vlen(wishvel);
85 if (wishspeed > sv_maxspeed)
86 wishspeed = sv_maxspeed * 0.7;
88 wishspeed = wishspeed * 0.7;
91 if (self.velocity != '0 0 0')
93 f = vlen(self.velocity) * (1 - frametime * sv_friction);
95 self.velocity = normalize(self.velocity) * f;
97 self.velocity = '0 0 0';
102 // water acceleration
106 limit = sv_accelerate * wishspeed * frametime;
109 self.velocity = self.velocity + normalize(wishvel) * limit;
111 self.velocity = self.velocity + normalize(wishvel) * f;
115 if (self.movetype == MOVETYPE_FLY)
116 makevectors(self.v_angle);
118 makevectors(self.v_angle_y * '0 1 0');
120 // hack to not let you back into teleporter
121 wishvel = v_right * self.movement_y;
122 if (time >= self.teleport_time || self.movement_x > 0)
123 wishvel = wishvel + v_forward * self.movement_x;
124 if (self.movetype != MOVETYPE_WALK)
125 wishvel_z = wishvel_z + self.movement_z;
127 wishdir = normalize(wishvel);
128 wishspeed = vlen(wishvel);
129 if (wishspeed > sv_maxspeed)
130 wishspeed = sv_maxspeed;
132 if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY)
134 self.velocity = wishdir * wishspeed;
138 if (self.flags & FL_ONGROUND) // walking
141 if (self.velocity_x || self.velocity_y)
147 // if the leading edge is over a dropoff, increase friction
148 v = self.origin + normalize(v) * 16 + '0 0 1' * self.mins_z;
150 traceline(v, v + '0 0 -34', TRUE, self);
153 if (trace_fraction == 1.0)
155 if (f < sv_stopspeed)
156 f = 1 - frametime * (sv_stopspeed / f) * sv_friction * sv_edgefriction;
158 f = 1 - frametime * sv_friction * sv_edgefriction;
162 if (f < sv_stopspeed)
163 f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
165 f = 1 - frametime * sv_friction;
169 self.velocity = '0 0 0';
171 self.velocity = self.velocity * f;
179 f = wishspeed - (self.velocity * wishdir);
182 limit = sv_accelerate * frametime * wishspeed;
185 self.velocity = self.velocity + wishdir * f;