1 #include "movetypes.qh"
4 #include "../common/stats.qh"
5 #include "../common/util.qh"
6 #include "../csqcmodellib/common.qh"
7 #include "../dpdefs/csprogsdefs.qh"
8 #include "../server/t_items.qh"
11 const int MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE = 4;
12 #define GRAVITY_UNAFFECTED_BY_TICRATE (getstati(STAT_MOVEFLAGS) & MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE)
14 .entity move_groundentity; // FIXME add move_groundnetworkentity?
15 .float move_suspendedinair;
16 .float move_didgravity;
18 void _Movetype_CheckVelocity() // SV_CheckVelocity
22 float _Movetype_CheckWater(entity ent) // SV_CheckWater
24 vector point = ent.move_origin;
25 point.z += (ent.mins.z + 1);
27 int nativecontents = pointcontents(point);
29 if(ent.move_watertype)
30 if(ent.move_watertype != nativecontents)
32 //print(sprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents));
33 if(ent.contentstransition)
34 ent.contentstransition(ent.move_watertype, nativecontents);
37 ent.move_waterlevel = 0;
38 ent.move_watertype = CONTENT_EMPTY;
40 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
41 if(supercontents & DPCONTENTS_LIQUIDSMASK)
43 ent.move_watertype = nativecontents;
44 ent.move_waterlevel = 1;
45 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
46 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
48 ent.move_waterlevel = 2;
49 point.y = ent.origin.y + ent.view_ofs.y;
50 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
51 ent.move_waterlevel = 3;
55 return (ent.move_waterlevel > 1);
58 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
60 float contents = pointcontents(ent.move_origin);
62 if(!ent.move_watertype)
65 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
67 ent.move_watertype = contents;
68 ent.move_waterlevel = 1;
72 else if(ent.move_watertype != contents)
74 //print(sprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.move_origin), pointcontents(ent.move_origin), ent.move_watertype, contents));
75 if(ent.contentstransition)
76 ent.contentstransition(ent.move_watertype, contents);
79 if(contents <= CONTENT_WATER)
81 ent.move_watertype = contents;
82 ent.move_waterlevel = 1;
86 ent.move_watertype = CONTENT_EMPTY;
87 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
91 void _Movetype_Impact(entity oth) // SV_Impact
93 entity oldother, oldself;
119 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
121 entity e, oldself, oldother;
126 for(e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
129 if(boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
134 trace_allsolid = false;
135 trace_startsolid = false;
137 trace_inwater = false;
139 trace_endpos = e.origin;
140 trace_plane_normal = '0 0 1';
141 trace_plane_dist = 0;
152 void _Movetype_LinkEdict(float touch_triggers) // SV_LinkEdict
155 if(self.solid == SOLID_BSP)
157 // TODO set the absolute bbox
166 mi = mi + self.origin;
167 ma = ma + self.origin;
169 if(self.move_flags & FL_ITEM)
192 _Movetype_LinkEdict_TouchAreaGrid();
195 float _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
198 org = self.move_origin + ofs;
200 int cont = self.dphitcontentsmask;
201 self.dphitcontentsmask = DPCONTENTS_SOLID;
202 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
203 self.dphitcontentsmask = cont;
208 if(vlen(trace_endpos - self.move_origin) > 0.0001)
209 self.move_origin = trace_endpos;
213 float _Movetype_UnstickEntity() // SV_UnstickEntity
215 if(!_Movetype_TestEntityPosition('0 0 0'))
217 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
218 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
219 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
220 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
221 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
222 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
223 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
224 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
226 for(i = 1; i <= 17; ++i)
228 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
229 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
231 dprintf("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
234 dprintf("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
235 _Movetype_LinkEdict(true);
239 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
241 vel = vel - ((vel * norm) * norm) * f;
243 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
244 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
245 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
250 void _Movetype_PushEntityTrace(vector push)
255 end = self.move_origin + push;
257 if(self.move_nomonsters)
258 type = max(0, self.move_nomonsters);
259 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
261 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
262 type = MOVE_NOMONSTERS;
266 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
269 float _Movetype_PushEntity(vector push, float failonstartsolid) // SV_PushEntity
271 _Movetype_PushEntityTrace(push);
273 if(trace_startsolid && failonstartsolid)
274 return trace_fraction;
276 self.move_origin = trace_endpos;
278 if(trace_fraction < 1)
279 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
280 _Movetype_Impact(trace_ent);
282 return trace_fraction;
285 const float MAX_CLIP_PLANES = 5;
286 void _Movetype_Physics_Toss(float dt) // SV_Physics_Toss
288 if(self.move_flags & FL_ONGROUND)
290 if(self.move_velocity.z >= 1/32)
291 self.move_flags &= ~FL_ONGROUND;
292 else if(!self.move_groundentity)
294 else if(self.move_suspendedinair && wasfreed(self.move_groundentity))
296 self.move_groundentity = world;
301 self.move_suspendedinair = false;
303 _Movetype_CheckVelocity();
305 if(self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS)
307 self.move_didgravity = 1;
308 if(GRAVITY_UNAFFECTED_BY_TICRATE)
311 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
313 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
318 self.move_velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
320 self.move_velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
324 self.move_angles = self.move_angles + self.move_avelocity * dt;
326 float movetime, bump;
328 for(bump = 0; bump < MAX_CLIP_PLANES && movetime > 0; ++bump)
331 move = self.move_velocity * movetime;
332 _Movetype_PushEntity(move, true);
338 _Movetype_UnstickEntity();
339 _Movetype_PushEntity(move, false);
344 if(trace_fraction == 1)
347 movetime *= 1 - min(1, trace_fraction);
349 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
351 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 2.0);
352 self.move_flags &= ~FL_ONGROUND;
354 else if(self.move_movetype == MOVETYPE_BOUNCE)
356 float d, bouncefac, bouncestop;
358 bouncefac = self.move_bounce_factor; if(!bouncefac) bouncefac = 0.5;
359 bouncestop = self.move_bounce_stopspeed; if(!bouncestop) bouncestop = 60 / 800;
361 bouncestop *= self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
363 bouncestop *= getstatf(STAT_MOVEVARS_GRAVITY);
365 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1 + bouncefac);
367 d = trace_plane_normal * self.move_velocity;
368 if(trace_plane_normal.z > 0.7 && d < bouncestop && d > -bouncestop)
370 self.move_flags |= FL_ONGROUND;
371 self.move_groundentity = trace_ent;
372 self.move_velocity = '0 0 0';
373 self.move_avelocity = '0 0 0';
376 self.move_flags &= ~FL_ONGROUND;
380 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1.0);
381 if(trace_plane_normal.z > 0.7)
383 self.move_flags |= FL_ONGROUND;
384 self.move_groundentity = trace_ent;
385 if(trace_ent.solid == SOLID_BSP)
386 self.move_suspendedinair = true;
387 self.move_velocity = '0 0 0';
388 self.move_avelocity = '0 0 0';
391 self.move_flags &= ~FL_ONGROUND;
394 // DP revision 8905 (just, WHY...)
395 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
398 // DP revision 8918 (WHY...)
399 if(self.move_flags & FL_ONGROUND)
403 if(GRAVITY_UNAFFECTED_BY_TICRATE)
404 if(self.move_didgravity > 0)
405 if(!(self.move_flags & FL_ONGROUND))
408 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
410 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
413 _Movetype_CheckWaterTransition(self);
416 void _Movetype_Physics_Frame(float movedt)
418 self.move_didgravity = -1;
419 switch(self.move_movetype)
422 case MOVETYPE_FAKEPUSH:
423 error("SV_Physics_Pusher not implemented");
427 case MOVETYPE_FOLLOW:
428 error("SV_Physics_Follow not implemented");
430 case MOVETYPE_NOCLIP:
431 _Movetype_CheckWater(self);
432 self.move_origin = self.move_origin + ticrate * self.move_velocity;
433 self.move_angles = self.move_angles + ticrate * self.move_avelocity;
434 _Movetype_LinkEdict(false);
437 error("SV_Physics_Step not implemented");
440 error("SV_Physics_Walk not implemented");
443 case MOVETYPE_BOUNCE:
444 case MOVETYPE_BOUNCEMISSILE:
445 case MOVETYPE_FLYMISSILE:
447 _Movetype_Physics_Toss(movedt);
452 void Movetype_Physics_NoMatchServer() // optimized
456 movedt = time - self.move_time;
457 self.move_time = time;
459 _Movetype_Physics_Frame(movedt);
463 self.avelocity = self.move_avelocity;
464 self.velocity = self.move_velocity;
465 self.angles = self.move_angles;
466 setorigin(self, self.move_origin);
469 void Movetype_Physics_MatchServer(bool sloppy)
471 Movetype_Physics_MatchTicrate(ticrate, sloppy);
474 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
476 float n, i, dt, movedt;
480 Movetype_Physics_NoMatchServer();
484 dt = time - self.move_time;
487 n = max(0, floor(dt / tr));
489 self.move_time += n * tr;
491 if(!self.move_didgravity)
492 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
494 for(i = 0; i < n; ++i)
496 _Movetype_Physics_Frame(movedt);
501 self.avelocity = self.move_avelocity;
503 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
505 // now continue the move from move_time to time
506 self.velocity = self.move_velocity;
508 if(self.move_didgravity > 0)
510 if(GRAVITY_UNAFFECTED_BY_TICRATE)
513 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
515 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
520 self.velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
522 self.velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
526 self.angles = self.move_angles + dt * self.avelocity;
528 if(sloppy || self.movetype == MOVETYPE_NOCLIP)
530 setorigin(self, self.move_origin + dt * self.velocity);
534 _Movetype_PushEntityTrace(dt * self.velocity);
535 if(!trace_startsolid)
536 setorigin(self, trace_endpos);
539 if(self.move_didgravity > 0)
541 if(GRAVITY_UNAFFECTED_BY_TICRATE)
544 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
546 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
552 self.velocity = self.move_velocity;
553 self.angles = self.move_angles;
554 setorigin(self, self.move_origin);