diff --git a/Engine/source/T3D/AI/AIController.cpp b/Engine/source/T3D/AI/AIController.cpp index acd7063f6..e54b173bb 100644 --- a/Engine/source/T3D/AI/AIController.cpp +++ b/Engine/source/T3D/AI/AIController.cpp @@ -201,14 +201,16 @@ bool AIController::getAIMove(Move* movePtr) } } #else - if (getGoal()->getDist() > mControllerData->mMoveTolerance) + if (getGoal()->getDist() > mControllerData->mFollowTolerance) { - if (getGoal()->mPosSet) + if (getGoal()->mObj.isValid()) + getNav()->followObject(getGoal()->mObj, mControllerData->mFollowTolerance); + else if (getGoal()->mPosSet) getNav()->setPathDestination(getGoal()->getPosition(true)); getGoal()->mInRange = false; } - if (getGoal()->getDist() < mControllerData->mMoveTolerance) + if (getGoal()->getDist() < mControllerData->mFollowTolerance) { mMovement.mMoveState = ModeStop; @@ -519,9 +521,9 @@ AIControllerData::AIControllerData() mMoveStuckTolerance = 0.01f; mMoveStuckTestDelay = 30; mHeightTolerance = 0.001f; + mFollowTolerance = 1.0f; #ifdef TORQUE_NAVIGATION_ENABLED - mFollowTolerance = 1.0f; mLinkTypes = LinkData(AllFlags); mNavSize = AINavigation::Regular; mFlocking.mChance = 90; @@ -544,9 +546,9 @@ AIControllerData::AIControllerData(const AIControllerData& other, bool temp_clon mMoveStuckTolerance = other.mMoveStuckTolerance; mMoveStuckTestDelay = other.mMoveStuckTestDelay; mHeightTolerance = other.mHeightTolerance; + mFollowTolerance = other.mFollowTolerance; #ifdef TORQUE_NAVIGATION_ENABLED - mFollowTolerance = other.mFollowTolerance; mLinkTypes = other.mLinkTypes; mNavSize = other.mNavSize; mFlocking.mChance = other.mFlocking.mChance; @@ -575,6 +577,13 @@ void AIControllerData::initPersistFields() "it helps the AIController controlled object from never reaching its destination due to minor obstacles, " "rounding errors on its position calculation, etc. By default it is set to 0.25.\n"); + addFieldV("followTolerance", TypeRangedF32, Offset(mFollowTolerance, AIControllerData), &CommonValidators::PositiveFloat, + "@brief Distance from destination before stopping.\n\n" + "When the AIController controlled object is moving to a given destination it will move to within " + "this distance of the destination and then stop. By providing this tolerance " + "it helps the AIController controlled object from never reaching its destination due to minor obstacles, " + "rounding errors on its position calculation, etc. By default it is set to 0.25.\n"); + addFieldV("AttackRadius", TypeRangedF32, Offset(mAttackRadius, AIControllerData), &CommonValidators::PositiveFloat, "@brief Distance considered in firing range for callback purposes."); @@ -601,12 +610,6 @@ void AIControllerData::initPersistFields() #ifdef TORQUE_NAVIGATION_ENABLED addGroup("Pathfinding"); - addFieldV("followTolerance", TypeRangedF32, Offset(mFollowTolerance, AIControllerData), &CommonValidators::PositiveFloat, - "@brief Distance from destination before stopping.\n\n" - "When the AIController controlled object is moving to a given destination it will move to within " - "this distance of the destination and then stop. By providing this tolerance " - "it helps the AIController controlled object from never reaching its destination due to minor obstacles, " - "rounding errors on its position calculation, etc. By default it is set to 0.25.\n"); addFieldV("FlockChance", TypeRangedS32, Offset(mFlocking.mChance, AIControllerData), &CommonValidators::S32Percent, "@brief chance of flocking."); addFieldV("FlockMin", TypeRangedF32, Offset(mFlocking.mMin, AIControllerData), &CommonValidators::PositiveFloat, @@ -645,8 +648,9 @@ void AIControllerData::packData(BitStream* stream) stream->write(mMoveStuckTolerance); stream->write(mMoveStuckTestDelay); stream->write(mHeightTolerance); -#ifdef TORQUE_NAVIGATION_ENABLED stream->write(mFollowTolerance); + +#ifdef TORQUE_NAVIGATION_ENABLED //enums stream->write(mLinkTypes.getFlags()); stream->write((U32)mNavSize); @@ -667,9 +671,9 @@ void AIControllerData::unpackData(BitStream* stream) stream->read(&mMoveStuckTolerance); stream->read(&mMoveStuckTestDelay); stream->read(&mHeightTolerance); + stream->read(&mFollowTolerance); #ifdef TORQUE_NAVIGATION_ENABLED - stream->read(&mFollowTolerance); //enums U16 linkFlags; stream->read(&linkFlags); diff --git a/Engine/source/T3D/AI/AIController.h b/Engine/source/T3D/AI/AIController.h index 3180c4213..18d95e210 100644 --- a/Engine/source/T3D/AI/AIController.h +++ b/Engine/source/T3D/AI/AIController.h @@ -153,12 +153,12 @@ public: DECLARE_CONOBJECT(AIControllerData); F32 mMoveTolerance; // Distance from destination point before we stop + F32 mFollowTolerance; // Distance from destination object before we stop F32 mAttackRadius; // Distance to trigger weaponry calcs S32 mMoveStuckTestDelay; // The number of ticks to wait before checking if the AI is stuck F32 mMoveStuckTolerance; // Distance tolerance on stuck check F32 mHeightTolerance; // how high above the navmesh are we before we stop trying to repath #ifdef TORQUE_NAVIGATION_ENABLED - F32 mFollowTolerance; // Distance from destination object before we stop struct Flocking { U32 mChance; // chance of flocking F32 mMin; // min flocking separation distance diff --git a/Engine/source/T3D/AI/AINavigation.cpp b/Engine/source/T3D/AI/AINavigation.cpp index ceb9d668e..258939f06 100644 --- a/Engine/source/T3D/AI/AINavigation.cpp +++ b/Engine/source/T3D/AI/AINavigation.cpp @@ -156,6 +156,30 @@ void AINavigation::onReachDestination() } } +void AINavigation::followObject() +{ + if (getCtrl()->getGoal()->getDist() < getCtrl()->mControllerData->mMoveTolerance) + return; + + if (setPathDestination(getCtrl()->getGoal()->getPosition(true))) + { +#ifdef TORQUE_NAVIGATION_ENABLED + getCtrl()->clearCover(); +#endif + } +} + +void AINavigation::followObject(SceneObject* obj, F32 radius) +{ + getCtrl()->setGoal(obj, radius); + followObject(); +} + +void AINavigation::clearFollow() +{ + getCtrl()->clearGoal(); +} + DefineEngineMethod(AIController, setMoveDestination, void, (Point3F goal, bool slowDown), (true), "@brief Tells the AI to move to the location provided\n\n" @@ -210,6 +234,23 @@ DefineEngineMethod(AIController, getPathDestination, Point3F, (), , return object->getNav()->getPathDestination(); } +DefineEngineMethod(AIController, followObject, void, (SimObjectId obj, F32 radius), , + "@brief Tell the AIPlayer to follow another object.\n\n" + + "@param obj ID of the object to follow.\n" + "@param radius Maximum distance we let the target escape to.") +{ + SceneObject* follow; +#ifdef TORQUE_NAVIGATION_ENABLED + object->getNav()->clearPath(); + object->clearCover(); +#endif + object->getNav()->clearFollow(); + + if (Sim::findObject(obj, follow)) + object->getNav()->followObject(follow, radius); +} + #ifdef TORQUE_NAVIGATION_ENABLED NavMesh* AINavigation::findNavMesh() const { @@ -315,27 +356,6 @@ void AINavigation::repath() moveToNode(1); } -void AINavigation::followObject() -{ - if (getCtrl()->getGoal()->getDist() < getCtrl()->mControllerData->mMoveTolerance) - return; - - if (setPathDestination(getCtrl()->getGoal()->getPosition(true))) - { - getCtrl()->clearCover(); - } -} - -void AINavigation::followObject(SceneObject* obj, F32 radius) -{ - getCtrl()->setGoal(obj, radius); - followObject(); -} - -void AINavigation::clearFollow() -{ - getCtrl()->clearGoal(); -} void AINavigation::followNavPath(NavPath* path) { @@ -497,21 +517,6 @@ DefineEngineMethod(AIController, followNavPath, void, (SimObjectId obj), , object->getNav()->followNavPath(path); } -DefineEngineMethod(AIController, followObject, void, (SimObjectId obj, F32 radius), , - "@brief Tell the AIPlayer to follow another object.\n\n" - - "@param obj ID of the object to follow.\n" - "@param radius Maximum distance we let the target escape to.") -{ - SceneObject* follow; - object->getNav()->clearPath(); - object->clearCover(); - object->getNav()->clearFollow(); - - if (Sim::findObject(obj, follow)) - object->getNav()->followObject(follow, radius); -} - DefineEngineMethod(AIController, repath, void, (), , "@brief Tells the AI to re-plan its path. Does nothing if the character " diff --git a/Engine/source/T3D/AI/AINavigation.h b/Engine/source/T3D/AI/AINavigation.h index e93871691..4d4f1b966 100644 --- a/Engine/source/T3D/AI/AINavigation.h +++ b/Engine/source/T3D/AI/AINavigation.h @@ -44,6 +44,10 @@ struct AINavigation void onReachDestination(); + void followObject(); + void followObject(SceneObject* obj, F32 radius); + void clearFollow(); + #ifdef TORQUE_NAVIGATION_ENABLED /// Stores information about a path. struct PathData { @@ -92,9 +96,6 @@ struct AINavigation SimObjectPtr getPath() { return mPathData.path; }; void followNavPath(NavPath* path); - void followObject(); - void followObject(SceneObject* obj, F32 radius); - void clearFollow(); /// Move to the specified node in the current path. void moveToNode(S32 node); bool flock();