]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/movelib.qc
Remove pre/post includes
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / movelib.qc
1 #if defined(CSQC)
2     #include "../dpdefs/csprogsdefs.qh"
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include "../dpdefs/progsdefs.qh"
6     #include "../dpdefs/dpextensions.qh"
7     #include "../csqcmodellib/sv_model.qh"
8 #endif
9
10 #ifdef SVQC
11 .vector moveto;
12
13 /**
14     Simulate drag
15     self.velocity = movelib_dragvec(self.velocity,0.02,0.5);
16 **/
17 vector movelib_dragvec(float drag, float exp_)
18 {
19     float lspeed,ldrag;
20
21     lspeed = vlen(self.velocity);
22     ldrag = lspeed * drag;
23     ldrag = ldrag * (drag * exp_);
24     ldrag = 1 - (ldrag / lspeed);
25
26     return self.velocity * ldrag;
27 }
28
29 /**
30     Simulate drag
31     self.velocity *= movelib_dragflt(somespeed,0.01,0.7);
32 **/
33 float movelib_dragflt(float fspeed,float drag,float exp_)
34 {
35     float ldrag;
36
37     ldrag = fspeed * drag;
38     ldrag = ldrag * ldrag * exp_;
39     ldrag = 1 - (ldrag / fspeed);
40
41     return ldrag;
42 }
43
44 /**
45     Do a inertia simulation based on velocity.
46     Basicaly, this allows you to simulate loss of steering with higher speed.
47     self.velocity = movelib_inertmove_byspeed(self.velocity,newvel,1000,0.1,0.9);
48 **/
49 vector movelib_inertmove_byspeed(vector vel_new, float vel_max,float newmin,float oldmax)
50 {
51     float influense;
52
53     influense = vlen(self.velocity) * (1 / vel_max);
54
55     influense = bound(newmin,influense,oldmax);
56
57     return (vel_new * (1 - influense)) + (self.velocity * influense);
58 }
59
60 vector movelib_inertmove(vector new_vel,float new_bias)
61 {
62     return new_vel * new_bias + self.velocity * (1-new_bias);
63 }
64
65 .float  movelib_lastupdate;
66 void movelib_move(vector force,float max_velocity,float drag,float theMass,float breakforce)
67 {
68     float deltatime;
69     float acceleration;
70     float mspeed;
71     vector breakvec;
72
73     deltatime = time - self.movelib_lastupdate;
74     if (deltatime > 0.15) deltatime = 0;
75     self.movelib_lastupdate = time;
76     if (!deltatime) return;
77
78     mspeed = vlen(self.velocity);
79
80     if (theMass)
81         acceleration = vlen(force) / theMass;
82     else
83         acceleration = vlen(force);
84
85     if (self.flags & FL_ONGROUND)
86     {
87         if (breakforce)
88         {
89             breakvec = (normalize(self.velocity) * (breakforce / theMass) * deltatime);
90             self.velocity = self.velocity - breakvec;
91         }
92
93         self.velocity = self.velocity + force * (acceleration * deltatime);
94     }
95
96     if (drag)
97         self.velocity = movelib_dragvec(drag, 1);
98
99     if (self.waterlevel > 1)
100     {
101         self.velocity = self.velocity + force * (acceleration * deltatime);
102         self.velocity = self.velocity + '0 0 0.05' * autocvar_sv_gravity * deltatime;
103     }
104     else
105         self.velocity = self.velocity + '0 0 -1' * autocvar_sv_gravity * deltatime;
106
107     mspeed = vlen(self.velocity);
108
109     if (max_velocity)
110         if (mspeed > max_velocity)
111             self.velocity = normalize(self.velocity) * (mspeed - 50);//* max_velocity;
112 }
113
114 /*
115 .float mass;
116 .float side_friction;
117 .float ground_friction;
118 .float air_friction;
119 .float water_friction;
120 .float buoyancy;
121 float movelib_deltatime;
122
123 void movelib_startupdate()
124 {
125     movelib_deltatime = time - self.movelib_lastupdate;
126
127     if (movelib_deltatime > 0.5)
128         movelib_deltatime = 0;
129
130     self.movelib_lastupdate = time;
131 }
132
133 void movelib_update(vector dir,float force)
134 {
135     vector acceleration;
136     float old_speed;
137     float ffriction,v_z;
138
139     vector breakvec;
140     vector old_dir;
141     vector ggravity;
142     vector old;
143
144     if(!movelib_deltatime)
145         return;
146     v_z = self.velocity_z;
147     old_speed    = vlen(self.velocity);
148     old_dir      = normalize(self.velocity);
149
150     //ggravity      =  (autocvar_sv_gravity / self.mass) * '0 0 100';
151     acceleration =  (force / self.mass) * dir;
152     //acceleration -= old_dir * (old_speed / self.mass);
153     acceleration -= ggravity;
154
155     if(self.waterlevel > 1)
156     {
157         ffriction = self.water_friction;
158         acceleration += self.buoyancy * '0 0 1';
159     }
160     else
161         if(self.flags & FL_ONGROUND)
162             ffriction = self.ground_friction;
163         else
164             ffriction = self.air_friction;
165
166     acceleration *= ffriction;
167     //self.velocity = self.velocity * (ffriction * movelib_deltatime);
168     self.velocity += acceleration * movelib_deltatime;
169     self.velocity_z = v_z;
170
171 }
172 */
173
174 /*
175 void movelib_move_simple(vector newdir,float velo,float blendrate)
176 {
177     self.velocity = self.velocity * (1 - blendrate) + (newdir * blendrate) * velo;
178 }
179 */
180 #define movelib_move_simple(newdir,velo,blendrate) \
181     self.velocity = self.velocity * (1 - blendrate) + (newdir * blendrate) * velo
182
183 #define movelib_move_simple_gravity(newdir,velo,blendrate) \
184     if(self.flags & FL_ONGROUND) movelib_move_simple(newdir,velo,blendrate)
185
186 void movelib_beak_simple(float force)
187 {
188     float mspeed;
189     vector mdir;
190     float vz;
191
192     mspeed = max(0,vlen(self.velocity) - force);
193     mdir   = normalize(self.velocity);
194     vz = self.velocity.z;
195     self.velocity = mdir * mspeed;
196     self.velocity_z = vz;
197 }
198
199 /**
200 Pitches and rolls the entity to match the gound.
201 Yed need to set v_up and v_forward (generally by calling makevectors) before calling this.
202 **/
203 #endif
204
205 void movelib_groundalign4point(float spring_length, float spring_up, float blendrate, float _max)
206 {
207     vector a, b, c, d, e, r, push_angle, ahead, side;
208
209     push_angle_y = 0;
210     r = (self.absmax + self.absmin) * 0.5 + (v_up * spring_up);
211     e = v_up * spring_length;
212
213     // Put springs slightly inside bbox
214     ahead = v_forward * (self.maxs.x * 0.8);
215     side  = v_right   * (self.maxs.y * 0.8);
216
217     a = r + ahead + side;
218     b = r + ahead - side;
219     c = r - ahead + side;
220     d = r - ahead - side;
221
222     traceline(a, a - e,MOVE_NORMAL,self);
223     a_z =  (1 - trace_fraction);
224     r = trace_endpos;
225
226     traceline(b, b - e,MOVE_NORMAL,self);
227     b_z =  (1 - trace_fraction);
228     r += trace_endpos;
229
230     traceline(c, c - e,MOVE_NORMAL,self);
231     c_z =  (1 - trace_fraction);
232     r += trace_endpos;
233
234     traceline(d, d - e,MOVE_NORMAL,self);
235     d_z =  (1 - trace_fraction);
236     r += trace_endpos;
237
238     a_x = r.z;
239     r = self.origin;
240     r_z = r.z;
241
242     push_angle_x = (a.z - c.z) * _max;
243     push_angle.x += (b.z - d.z) * _max;
244
245     push_angle_z = (b.z - a.z) * _max;
246     push_angle.z += (d.z - c.z) * _max;
247
248     //self.angles_x += push_angle_x * 0.95;
249     //self.angles_z += push_angle_z * 0.95;
250
251     self.angles_x = ((1-blendrate) *  self.angles.x)  + (push_angle.x * blendrate);
252     self.angles_z = ((1-blendrate) *  self.angles.z)  + (push_angle.z * blendrate);
253
254     //a = self.origin;
255     setorigin(self,r);
256 }
257