NSEntity: new method GetNearbySpot() which will return a free position close to the entity.

This commit is contained in:
Marco Cawthorne 2023-10-11 00:48:29 -07:00
parent 9602d5eac8
commit 7d9de3a2cc
Signed by: eukara
GPG key ID: CE2032F0A2882A22
2 changed files with 69 additions and 0 deletions

View file

@ -371,4 +371,7 @@ public:
/** Relink the entity against the world. Updates PVS info etc. */
nonvirtual void Relink(void);
/** Finds a free spot of an entity near itself of same size. Extra padding as argument. */
nonvirtual vector GetNearbySpot(void);
};

View file

@ -823,6 +823,7 @@ void NSEntity::SpawnKey( string strKey, string strValue ) {
case "health":
health = ReadFloat( strValue );
break;
case "movewith":
case "parentname":
SetParent( ReadString(strValue) );
break;
@ -990,4 +991,69 @@ void
NSEntity::Relink(void)
{
setorigin(this, origin);
}
vector
NSEntity::GetNearbySpot(void)
{
vector testPos;
float minDist = maxs[0];
makevectors([0,0,0]);
/* space in front? */
testPos = GetOrigin() + v_forward * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* behind? */
testPos = GetOrigin() - v_forward * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* left? */
testPos = GetOrigin() - v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* right? */
testPos = GetOrigin() + v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* front left? */
testPos = GetOrigin() + v_forward * minDist - v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* front right? */
testPos = GetOrigin() + v_forward * minDist + v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* back left? */
testPos = GetOrigin() - v_forward * minDist - v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
/* back right? */
testPos = GetOrigin() - v_forward * minDist + v_right * minDist;
tracebox(testPos, mins, maxs, testPos, MOVE_NORMAL, this);
if (trace_fraction == 1.0f)
return testPos;
return g_vec_null;
}