added spawning

Tile test tool now spawns classes and data
Test avoidance added to aicontroller and ainavigation
This commit is contained in:
marauder2k7 2025-07-27 12:43:19 +01:00
parent a5e969a8fd
commit f730d0bf1c
8 changed files with 286 additions and 301 deletions

View file

@ -168,8 +168,19 @@ bool AIController::getAIMove(Move* movePtr)
{
obj = getAIInfo()->mObj;
}
bool adjusted = false;
if (getNav()->avoidObstacles())
{
adjusted = true;
}
else if (mRandI(0, 100) < mControllerData->mFlocking.mChance && getNav()->flock())
{
adjusted = true;
}
// Only repath if not already adjusted and on risky ground
RayInfo info;
if (obj->getContainer()->castRay(obj->getPosition(), obj->getPosition() - Point3F(0, 0, mControllerData->mHeightTolerance), StaticShapeObjectType, &info))
if (!adjusted && obj->getContainer()->castRay(obj->getPosition(), obj->getPosition() - Point3F(0, 0, mControllerData->mHeightTolerance), StaticShapeObjectType, &info))
{
getNav()->repath();
}

View file

@ -343,6 +343,10 @@ void AINavigation::repath()
{
mPathData.path->mTo = mMoveDestination;
}
else if (avoidObstacles())
{
mPathData.path->mTo = mMoveDestination;
}
else
{
// If we're following, get their position.
@ -380,6 +384,60 @@ void AINavigation::clearPath()
mPathData = PathData();
}
bool AINavigation::avoidObstacles()
{
SimObjectPtr<SceneObject> obj = getCtrl()->getAIInfo()->mObj;
obj->disableCollision();
Point3F pos = obj->getBoxCenter();
VectorF forward = obj->getTransform().getForwardVector();
forward.normalizeSafe();
// Generate forward-left and forward-right by rotating forward vector
VectorF right = mCross(forward, Point3F(0, 0, 1));
VectorF leftDir = forward + right * -0.5f; // front-left
VectorF rightDir = forward + right * 0.5f; // front-right
leftDir.normalizeSafe();
rightDir.normalizeSafe();
F32 rayLength = getCtrl()->mMovement.getMoveSpeed();
Point3F directions[3] = {
forward,
leftDir,
rightDir
};
bool hit[3] = { false, false, false };
RayInfo info;
for (int i = 0; i < 3; ++i)
{
Point3F end = pos + directions[i] * rayLength;
if (obj->getContainer()->castRay(pos, end, sAILoSMask, &info))
{
hit[i] = true;
}
}
Point3F avoidance = Point3F::Zero;
if (hit[0]) avoidance += right * 1.0f;
if (hit[1]) avoidance += right * 1.5f;
if (hit[2]) avoidance -= right * 1.5f;
if (!avoidance.isZero())
{
avoidance.normalizeSafe();
F32 clearance = getCtrl()->getAIInfo()->mRadius * 1.5f;
Point3F newDest = info.point + avoidance * rayLength;
mMoveDestination = newDest;
obj->enableCollision();
return true;
}
obj->enableCollision();
return false;
}
bool AINavigation::flock()
{
AIControllerData::Flocking flockingData = getCtrl()->mControllerData->mFlocking;

View file

@ -98,6 +98,7 @@ struct AINavigation
/// Move to the specified node in the current path.
void moveToNode(S32 node);
bool avoidObstacles();
bool flock();
#endif
};

View file

@ -668,6 +668,7 @@ bool NavMesh::build(bool background, bool saveIntermediates)
}
mBuilding = true;
m_geo = NULL;
ctx->startTimer(RC_TIMER_TOTAL);
@ -1083,7 +1084,7 @@ unsigned char *NavMesh::buildTileData(const Tile &tile, U32 &dataSize)
}
else if (mWaterMethod == Impassable)
{
m_triareas[t] = RC_NULL_AREA;
m_triareas[t] = NullArea;
}
}
}

View file

@ -51,7 +51,6 @@ void NavMeshTestTool::spawnPlayer(const Point3F& position)
}
obj->setPosition(position);
obj->registerObject();
SimObject* cleanup = Sim::findObject("MissionCleanup");
if (cleanup)
{
@ -59,8 +58,29 @@ void NavMeshTestTool::spawnPlayer(const Point3F& position)
missionCleanup->addObject(obj);
}
mPlayer = obj;
Con::executef(this, "onPlayerSpawned", obj->getIdString());
#ifdef TORQUE_NAVIGATION_ENABLED
AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
if (asAIPlayer)
{
Con::executef(this, "onPlayerSelected");
}
else
{
ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
if (sbo && sbo->getAIController() && sbo->getAIController()->mControllerData)
{
Con::executef(this, "onPlayerSelected");
}
else
{
Con::executef(this, "onPlayerSelected");
}
}
#else
Con::executef(this, "onPlayerSelected");
#endif
}
void NavMeshTestTool::drawAgent(duDebugDrawTorque& dd, const F32* pos, F32 r, F32 h, F32 c, const U32 col)
@ -94,6 +114,12 @@ NavMeshTestTool::NavMeshTestTool()
mPathStart = Point3F::Max;
mPathEnd = Point3F::Max;
mTestPath = NULL;
mLinkTypes = LinkData(AllFlags);
mFilter.setIncludeFlags(mLinkTypes.getFlags());
mFilter.setExcludeFlags(0);
}
void NavMeshTestTool::onActivated(const Gui3DMouseEvent& evt)
@ -103,6 +129,17 @@ void NavMeshTestTool::onActivated(const Gui3DMouseEvent& evt)
void NavMeshTestTool::onDeactivated()
{
if (mTestPath != NULL) {
mTestPath->deleteObject();
mTestPath = NULL;
}
if (mPlayer != NULL)
{
mPlayer = NULL;
Con::executef(this, "onPlayerDeselected");
}
Con::executef(this, "onDeactivated");
}
@ -145,14 +182,14 @@ void NavMeshTestTool::on3DMouseDown(const Gui3DMouseEvent& evt)
AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
if (asAIPlayer)
{
Con::executef(this, "onPlayerSelected", Con::getIntArg(asAIPlayer->mLinkTypes.getFlags()));
Con::executef(this, "onPlayerSelected");
}
else
{
ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
if (sbo && sbo->getAIController() && sbo->getAIController()->mControllerData)
{
Con::executef(this, "onPlayerSelected", Con::getIntArg(sbo->getAIController()->mControllerData->mLinkTypes.getFlags()));
Con::executef(this, "onPlayerSelected");
}
else
{
@ -176,10 +213,30 @@ void NavMeshTestTool::on3DMouseDown(const Gui3DMouseEvent& evt)
if (mPathStart != Point3F::Max)
{
mPathEnd = ri.point;
mTestPath = new NavPath();
mTestPath->mMesh = mNavMesh;
mTestPath->mFrom = mPathStart;
mTestPath->mTo = mPathEnd;
mTestPath->mFromSet = mTestPath->mToSet = true;
mTestPath->mAlwaysRender = true;
mTestPath->mLinkTypes = mLinkTypes;
mTestPath->mFilter = mFilter;
mTestPath->mXray = true;
// Paths plan automatically upon being registered.
if (!mTestPath->registerObject())
{
delete mTestPath;
return;
}
}
else
{
mPathStart = ri.point;
if (mTestPath != NULL) {
mTestPath->deleteObject();
mTestPath = NULL;
}
}
}
else
@ -300,3 +357,4 @@ DefineEngineMethod(NavMeshTestTool, setSpawnDatablock, void, (String dbName), ,
{
object->setSpawnDatablock(dbName);
}

View file

@ -16,6 +16,9 @@ protected:
SimObjectPtr<SceneObject> mCurPlayer;
Point3F mPathStart;
Point3F mPathEnd;
NavPath* mTestPath;
LinkData mLinkTypes;
dtQueryFilter mFilter;
public:
DECLARE_CONOBJECT(NavMeshTestTool);