2 #include "../dpdefs/csprogsdefs.qh"
4 #include "../common/stats.qh"
5 #include "../common/util.qh"
6 #include "movetypes.qh"
7 #include "../csqcmodellib/common.qh"
8 #include "../server/t_items.qh"
14 #define GRAVITY_UNAFFECTED_BY_TICRATE (getstati(STAT_MOVEFLAGS) & MOVEFLAG_GRAVITYUNAFFECTEDBYTICRATE)
16 .entity move_groundentity; // FIXME add move_groundnetworkentity?
17 .float move_suspendedinair;
18 .float move_didgravity;
20 void _Movetype_CheckVelocity() // SV_CheckVelocity
24 float _Movetype_CheckWater(entity ent) // SV_CheckWater
26 vector point = ent.move_origin;
27 point.z += (ent.mins.z + 1);
29 int nativecontents = pointcontents(point);
31 if(ent.move_watertype)
32 if(ent.move_watertype != nativecontents)
34 //print(sprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", ent.move_watertype, nativecontents));
35 if(ent.contentstransition)
36 ent.contentstransition(ent.move_watertype, nativecontents);
39 ent.move_waterlevel = 0;
40 ent.move_watertype = CONTENT_EMPTY;
42 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
43 if(supercontents & DPCONTENTS_LIQUIDSMASK)
45 ent.move_watertype = nativecontents;
46 ent.move_waterlevel = 1;
47 point.y = (ent.origin.y + ((ent.mins.z + ent.maxs.y) * 0.5));
48 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
50 ent.move_waterlevel = 2;
51 point.y = ent.origin.y + ent.view_ofs.y;
52 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
53 ent.move_waterlevel = 3;
57 return (ent.move_waterlevel > 1);
60 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
62 float contents = pointcontents(ent.move_origin);
64 if(!ent.move_watertype)
67 if(!autocvar_cl_gameplayfix_fixedcheckwatertransition)
69 ent.move_watertype = contents;
70 ent.move_waterlevel = 1;
74 else if(ent.move_watertype != contents)
76 //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));
77 if(ent.contentstransition)
78 ent.contentstransition(ent.move_watertype, contents);
81 if(contents <= CONTENT_WATER)
83 ent.move_watertype = contents;
84 ent.move_waterlevel = 1;
88 ent.move_watertype = CONTENT_EMPTY;
89 ent.move_waterlevel = (autocvar_cl_gameplayfix_fixedcheckwatertransition ? 0 : contents);
93 void _Movetype_Impact(entity oth) // SV_Impact
95 entity oldother, oldself;
121 void _Movetype_LinkEdict_TouchAreaGrid() // SV_LinkEdict_TouchAreaGrid
123 entity e, oldself, oldother;
128 for(e = findradius(0.5 * (self.absmin + self.absmax), 0.5 * vlen(self.absmax - self.absmin)); e; e = e.chain)
131 if(boxesoverlap(e.absmin, e.absmax, oldself.absmin, oldself.absmax))
136 trace_allsolid = false;
137 trace_startsolid = false;
139 trace_inwater = false;
141 trace_endpos = e.origin;
142 trace_plane_normal = '0 0 1';
143 trace_plane_dist = 0;
154 void _Movetype_LinkEdict(float touch_triggers) // SV_LinkEdict
157 if(self.solid == SOLID_BSP)
159 // TODO set the absolute bbox
168 mi = mi + self.origin;
169 ma = ma + self.origin;
171 if(self.move_flags & FL_ITEM)
194 _Movetype_LinkEdict_TouchAreaGrid();
197 float _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
200 org = self.move_origin + ofs;
202 int cont = self.dphitcontentsmask;
203 self.dphitcontentsmask = DPCONTENTS_SOLID;
204 tracebox(self.move_origin, self.mins, self.maxs, self.move_origin, MOVE_NOMONSTERS, self);
205 self.dphitcontentsmask = cont;
210 if(vlen(trace_endpos - self.move_origin) > 0.0001)
211 self.move_origin = trace_endpos;
215 float _Movetype_UnstickEntity() // SV_UnstickEntity
217 if(!_Movetype_TestEntityPosition('0 0 0'))
219 if(!_Movetype_TestEntityPosition('-1 0 0')) goto success;
220 if(!_Movetype_TestEntityPosition('1 0 0')) goto success;
221 if(!_Movetype_TestEntityPosition('0 -1 0')) goto success;
222 if(!_Movetype_TestEntityPosition('0 1 0')) goto success;
223 if(!_Movetype_TestEntityPosition('-1 -1 0')) goto success;
224 if(!_Movetype_TestEntityPosition('1 -1 0')) goto success;
225 if(!_Movetype_TestEntityPosition('-1 1 0')) goto success;
226 if(!_Movetype_TestEntityPosition('1 1 0')) goto success;
228 for(i = 1; i <= 17; ++i)
230 if(!_Movetype_TestEntityPosition('0 0 -1' * i)) goto success;
231 if(!_Movetype_TestEntityPosition('0 0 1' * i)) goto success;
233 dprintf("Can't unstick an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
236 dprintf("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.move_origin));
237 _Movetype_LinkEdict(true);
241 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
243 vel = vel - ((vel * norm) * norm) * f;
245 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
246 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
247 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
252 void _Movetype_PushEntityTrace(vector push)
257 end = self.move_origin + push;
259 if(self.move_nomonsters)
260 type = max(0, self.move_nomonsters);
261 else if(self.move_movetype == MOVETYPE_FLYMISSILE)
263 else if(self.solid == SOLID_TRIGGER || self.solid == SOLID_NOT)
264 type = MOVE_NOMONSTERS;
268 tracebox(self.move_origin, self.mins, self.maxs, end, type, self);
271 float _Movetype_PushEntity(vector push, float failonstartsolid) // SV_PushEntity
273 _Movetype_PushEntityTrace(push);
275 if(trace_startsolid && failonstartsolid)
276 return trace_fraction;
278 self.move_origin = trace_endpos;
280 if(trace_fraction < 1)
281 if(self.solid >= SOLID_TRIGGER && (!(self.move_flags & FL_ONGROUND) || (self.move_groundentity != trace_ent)))
282 _Movetype_Impact(trace_ent);
284 return trace_fraction;
287 const float MAX_CLIP_PLANES = 5;
288 void _Movetype_Physics_Toss(float dt) // SV_Physics_Toss
290 if(self.move_flags & FL_ONGROUND)
292 if(self.move_velocity.z >= 1/32)
293 self.move_flags &= ~FL_ONGROUND;
294 else if(!self.move_groundentity)
296 else if(self.move_suspendedinair && wasfreed(self.move_groundentity))
298 self.move_groundentity = world;
303 self.move_suspendedinair = false;
305 _Movetype_CheckVelocity();
307 if(self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS)
309 self.move_didgravity = 1;
310 if(GRAVITY_UNAFFECTED_BY_TICRATE)
313 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
315 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
320 self.move_velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
322 self.move_velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
326 self.move_angles = self.move_angles + self.move_avelocity * dt;
328 float movetime, bump;
330 for(bump = 0; bump < MAX_CLIP_PLANES && movetime > 0; ++bump)
333 move = self.move_velocity * movetime;
334 _Movetype_PushEntity(move, true);
340 _Movetype_UnstickEntity();
341 _Movetype_PushEntity(move, false);
346 if(trace_fraction == 1)
349 movetime *= 1 - min(1, trace_fraction);
351 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
353 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 2.0);
354 self.move_flags &= ~FL_ONGROUND;
356 else if(self.move_movetype == MOVETYPE_BOUNCE)
358 float d, bouncefac, bouncestop;
360 bouncefac = self.move_bounce_factor; if(!bouncefac) bouncefac = 0.5;
361 bouncestop = self.move_bounce_stopspeed; if(!bouncestop) bouncestop = 60 / 800;
363 bouncestop *= self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
365 bouncestop *= getstatf(STAT_MOVEVARS_GRAVITY);
367 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1 + bouncefac);
369 d = trace_plane_normal * self.move_velocity;
370 if(trace_plane_normal.z > 0.7 && d < bouncestop && d > -bouncestop)
372 self.move_flags |= FL_ONGROUND;
373 self.move_groundentity = trace_ent;
374 self.move_velocity = '0 0 0';
375 self.move_avelocity = '0 0 0';
378 self.move_flags &= ~FL_ONGROUND;
382 self.move_velocity = _Movetype_ClipVelocity(self.move_velocity, trace_plane_normal, 1.0);
383 if(trace_plane_normal.z > 0.7)
385 self.move_flags |= FL_ONGROUND;
386 self.move_groundentity = trace_ent;
387 if(trace_ent.solid == SOLID_BSP)
388 self.move_suspendedinair = true;
389 self.move_velocity = '0 0 0';
390 self.move_avelocity = '0 0 0';
393 self.move_flags &= ~FL_ONGROUND;
396 // DP revision 8905 (just, WHY...)
397 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
400 // DP revision 8918 (WHY...)
401 if(self.move_flags & FL_ONGROUND)
405 if(GRAVITY_UNAFFECTED_BY_TICRATE)
406 if(self.move_didgravity > 0)
407 if(!(self.move_flags & FL_ONGROUND))
410 self.move_velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
412 self.move_velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
415 _Movetype_CheckWaterTransition(self);
418 void _Movetype_Physics_Frame(float movedt)
420 self.move_didgravity = -1;
421 switch(self.move_movetype)
424 case MOVETYPE_FAKEPUSH:
425 error("SV_Physics_Pusher not implemented");
429 case MOVETYPE_FOLLOW:
430 error("SV_Physics_Follow not implemented");
432 case MOVETYPE_NOCLIP:
433 _Movetype_CheckWater(self);
434 self.move_origin = self.move_origin + ticrate * self.move_velocity;
435 self.move_angles = self.move_angles + ticrate * self.move_avelocity;
436 _Movetype_LinkEdict(false);
439 error("SV_Physics_Step not implemented");
442 error("SV_Physics_Walk not implemented");
445 case MOVETYPE_BOUNCE:
446 case MOVETYPE_BOUNCEMISSILE:
447 case MOVETYPE_FLYMISSILE:
449 _Movetype_Physics_Toss(movedt);
454 void Movetype_Physics_NoMatchServer() // optimized
458 movedt = time - self.move_time;
459 self.move_time = time;
461 _Movetype_Physics_Frame(movedt);
465 self.avelocity = self.move_avelocity;
466 self.velocity = self.move_velocity;
467 self.angles = self.move_angles;
468 setorigin(self, self.move_origin);
471 void Movetype_Physics_MatchServer(bool sloppy)
473 Movetype_Physics_MatchTicrate(ticrate, sloppy);
476 void Movetype_Physics_MatchTicrate(float tr, bool sloppy) // SV_Physics_Entity
478 float n, i, dt, movedt;
482 Movetype_Physics_NoMatchServer();
486 dt = time - self.move_time;
489 n = max(0, floor(dt / tr));
491 self.move_time += n * tr;
493 if(!self.move_didgravity)
494 self.move_didgravity = ((self.move_movetype == MOVETYPE_BOUNCE || self.move_movetype == MOVETYPE_TOSS) && !(self.move_flags & FL_ONGROUND));
496 for(i = 0; i < n; ++i)
498 _Movetype_Physics_Frame(movedt);
503 self.avelocity = self.move_avelocity;
505 if(dt > 0 && self.move_movetype != MOVETYPE_NONE && !(self.move_flags & FL_ONGROUND))
507 // now continue the move from move_time to time
508 self.velocity = self.move_velocity;
510 if(self.move_didgravity > 0)
512 if(GRAVITY_UNAFFECTED_BY_TICRATE)
515 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
517 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
522 self.velocity_z -= dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
524 self.velocity_z -= dt * getstatf(STAT_MOVEVARS_GRAVITY);
528 self.angles = self.move_angles + dt * self.avelocity;
530 if(sloppy || self.movetype == MOVETYPE_NOCLIP)
532 setorigin(self, self.move_origin + dt * self.velocity);
536 _Movetype_PushEntityTrace(dt * self.velocity);
537 if(!trace_startsolid)
538 setorigin(self, trace_endpos);
541 if(self.move_didgravity > 0)
543 if(GRAVITY_UNAFFECTED_BY_TICRATE)
546 self.velocity_z -= 0.5 * dt * self.gravity * getstatf(STAT_MOVEVARS_GRAVITY);
548 self.velocity_z -= 0.5 * dt * getstatf(STAT_MOVEVARS_GRAVITY);
554 self.velocity = self.move_velocity;
555 self.angles = self.move_angles;
556 setorigin(self, self.move_origin);