This commit is contained in:
Bram verhulst
2025-06-02 21:29:29 +02:00
parent 1104b12ba8
commit f9a11385fe
17 changed files with 3338 additions and 274 deletions

View File

@@ -55,44 +55,46 @@ namespace BT_Action
SteeringPlugin_Output steering{};
std::chrono::steady_clock::time_point timer{};
float maxTime{};
float maxTime{ 5.0f }; // Default value if not set
bool doOnce{};
blackboardPtr->GetData("Interface", interfacePtr);
blackboardPtr->GetData("Target", target);
blackboardPtr->GetData("Steering", steering);
blackboardPtr->GetData("FailSafe", timer);
blackboardPtr->GetData("MaxFailSafe", maxTime);
blackboardPtr->GetData("MaxFailSafe", maxTime); // Make sure this is set before calling
blackboardPtr->GetData("FailSafeDoOnce", doOnce);
// Initialize timer if not done yet
if (!doOnce) {
blackboardPtr->ChangeData("FailSafe", std::chrono::steady_clock::now());
timer = std::chrono::steady_clock::now();
blackboardPtr->ChangeData("FailSafe", timer);
blackboardPtr->ChangeData("FailSafeDoOnce", true);
}
const auto agentInfo = interfacePtr->Agent_GetInfo();
const auto nextTargetPos = interfacePtr->NavMesh_GetClosestPathPoint(target);
steering.LinearVelocity = nextTargetPos - agentInfo.Position;
steering.LinearVelocity.Normalize();
steering.LinearVelocity *= agentInfo.MaxLinearSpeed;
// Check success conditions first
const std::chrono::steady_clock::time_point currentTime{ std::chrono::steady_clock::now() };
const std::chrono::duration<float> elapsedSec{ currentTime - timer };
// Check if we've reached the target
if (Distance(target, agentInfo.Position) < 3.f) {
blackboardPtr->ChangeData("FailSafeDoOnce", false);
return BT::State::Success;
}
// Check if failsafe timeout has been reached
if (elapsedSec.count() > maxTime) {
blackboardPtr->ChangeData("FailSafeDoOnce", false);
return BT::State::Success;
}
if (Distance(target, agentInfo.Position) < 3.f) {
blackboardPtr->ChangeData("FailSafeDoOnce", false);
return BT::State::Success;
}
// Calculate steering if we're still running
steering.LinearVelocity = nextTargetPos - agentInfo.Position;
steering.LinearVelocity.Normalize();
steering.LinearVelocity *= agentInfo.MaxLinearSpeed;
blackboardPtr->ChangeData("Steering", steering);
return BT::State::Running;
@@ -715,4 +717,90 @@ namespace BT_Condition
return thinkerPtr->HouseToReExplore();
}
bool CheckMovement(Blackboard* blackboardPtr, float timeout) {
IExamInterface* interfacePtr{};
blackboardPtr->GetData("Interface", interfacePtr);
// Get current position
const Elite::Vector2 currentPosition = interfacePtr->Agent_GetInfo().Position;
// Get or initialize last position and timer from blackboard
Elite::Vector2 lastPosition;
std::chrono::steady_clock::time_point lastMovementTime;
// Try to get last position from blackboard, or initialize if not present
if (!blackboardPtr->GetData("LastPosition", lastPosition)) {
lastPosition = currentPosition;
blackboardPtr->AddData("LastPosition", lastPosition);
}
// Try to get last movement time from blackboard, or initialize if not present
if (!blackboardPtr->GetData("LastMovementTime", lastMovementTime)) {
lastMovementTime = std::chrono::steady_clock::now();
blackboardPtr->AddData("LastMovementTime", lastMovementTime);
}
// Check if we've moved significantly (more than a small threshold)
const float movementThreshold = 0.5f; // Adjust this based on your game scale
const float distanceMoved = currentPosition.DistanceSquared(lastPosition);
if (distanceMoved > movementThreshold * movementThreshold) {
// We've moved significantly, update last position and time in blackboard
blackboardPtr->ChangeData("LastPosition", currentPosition);
blackboardPtr->ChangeData("LastMovementTime", std::chrono::steady_clock::now());
return false;
}
// Check how long we've been stuck
const auto currentTime = std::chrono::steady_clock::now();
const auto elapsedTime = std::chrono::duration_cast<std::chrono::seconds>(currentTime - lastMovementTime).count();
// Return true if we've been stuck for longer than the timeout
return elapsedTime >= timeout;
}
}
namespace BT_Grid {
BT::State UpdateExplorationGrid(Blackboard* blackboardPtr) {
PRINT_FUNCTION_NAME();
IExamInterface* interfacePtr{};
Thinker* thinkerPtr{};
if (!blackboardPtr->GetData("Interface", interfacePtr) || !blackboardPtr->GetData("Brain", thinkerPtr)) {
return BT::State::Failure;
}
const AgentInfo agentInfo = interfacePtr->Agent_GetInfo();
//Visible houses, enemies, items, and purge zones
const std::vector<EnemyInfo>& enemiesInFOV = interfacePtr->GetEnemiesInFOV();
const std::vector<ItemInfo>& itemsInFOV = interfacePtr->GetItemsInFOV();
const std::vector<PurgeZoneInfo>& purgeZonesInFOV = interfacePtr->GetPurgeZonesInFOV();
const std::vector<HouseInfo>& housesInFOV = interfacePtr->GetHousesInFOV();
thinkerPtr->m_ExplorationGrid.Update(agentInfo, housesInFOV);
return BT::State::Success;
}
BT::State FindUnexploredCell(Blackboard* blackboardPtr) {
PRINT_FUNCTION_NAME();
Thinker* thinkerPtr{};
blackboardPtr->GetData("Brain", thinkerPtr);
IExamInterface* interfacePtr{};
if (!blackboardPtr->GetData("Interface", interfacePtr)) {
return BT::State::Failure;
}
Elite::Vector2 playerPos = interfacePtr->Agent_GetInfo().Position;
const Elite::Vector2 target{ thinkerPtr->m_ExplorationGrid.FindNearestUnexplored(playerPos)};
if (target == Elite::Vector2{})
return BT::State::Failure;
blackboardPtr->ChangeData("Target", target);
return BT::State::Success;
}
}