From 681641672c49c1a4cd517a7469f71374d0093285 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Tue, 5 Sep 2023 14:59:52 +0100 Subject: [PATCH] Add Minimum Distance Check to GetLocationNearLocation Task --- Content/Enemy/Worker/BB_Worker.uasset | 4 +-- Content/Enemy/Worker/BT_Worker.uasset | 4 +-- .../Nakatomi/TaskGetLocationNearLocation.cpp | 29 ++++++++++++------- Source/Nakatomi/TaskGetLocationNearLocation.h | 4 +++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Content/Enemy/Worker/BB_Worker.uasset b/Content/Enemy/Worker/BB_Worker.uasset index 03fe184..9ee8152 100644 --- a/Content/Enemy/Worker/BB_Worker.uasset +++ b/Content/Enemy/Worker/BB_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8d108771057841c6f81e2c5a5dbecc567d374e744cf370be843c36ff52cda8e0 -size 3378 +oid sha256:0389d49e48e76a626f19b5cb173168d2eb0dff74a6ba6a1db4ed6149f75b4ca7 +size 3379 diff --git a/Content/Enemy/Worker/BT_Worker.uasset b/Content/Enemy/Worker/BT_Worker.uasset index 17db27a..8b20939 100644 --- a/Content/Enemy/Worker/BT_Worker.uasset +++ b/Content/Enemy/Worker/BT_Worker.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6f5fef16a4688a57932e3d3f12e2194e4684417892d18bf79fde2a500ac9c733 -size 28749 +oid sha256:97e7d11c6434751de91d5bb28c1343fa1f04abb7b67b7fa254483c7e5355142e +size 30892 diff --git a/Source/Nakatomi/TaskGetLocationNearLocation.cpp b/Source/Nakatomi/TaskGetLocationNearLocation.cpp index 6febcd0..69ffea5 100644 --- a/Source/Nakatomi/TaskGetLocationNearLocation.cpp +++ b/Source/Nakatomi/TaskGetLocationNearLocation.cpp @@ -3,23 +3,32 @@ #include "TaskGetLocationNearLocation.h" #include "EnemyAIController.h" -#include "EnemyCharacter.h" #include EBTNodeResult::Type UTaskGetLocationNearLocation::ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) { - auto enemyController = Cast(owner.GetAIOwner()); - auto enemyPawn = Cast(enemyController->GetPawn()); - auto blackboardComponent = owner.GetBlackboardComponent(); - auto navigationSystem = FNavigationSystem::GetCurrent(GetWorld()); - FVector sourceLocation = blackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName); + auto BlackboardComponent = owner.GetBlackboardComponent(); - if (blackboardComponent && navigationSystem && sourceLocation != FVector::ZeroVector) + if (!BlackboardComponent) { - FNavLocation navLocation; - navigationSystem->GetRandomReachablePointInRadius(sourceLocation, MaximumDistance, navLocation); + return EBTNodeResult::Failed; + } + + auto NavigationSystem = FNavigationSystem::GetCurrent(GetWorld()); + FVector SourceLocation = BlackboardComponent->GetValueAsVector(SourceLocationKey.SelectedKeyName); - blackboardComponent->SetValueAsVector(TargetLocationKey.SelectedKeyName, navLocation.Location); + if (BlackboardComponent && NavigationSystem && SourceLocation != FVector::ZeroVector) + { + double Distance = -1.0; + FNavLocation NavLocation; + + while (Distance < MinimumDistance) + { + NavigationSystem->GetRandomReachablePointInRadius(SourceLocation, MaximumDistance, NavLocation); + NavigationSystem->GetPathLength(SourceLocation, NavLocation.Location, Distance); + } + + BlackboardComponent->SetValueAsVector(TargetLocationKey.SelectedKeyName, NavLocation.Location); return EBTNodeResult::Succeeded; } diff --git a/Source/Nakatomi/TaskGetLocationNearLocation.h b/Source/Nakatomi/TaskGetLocationNearLocation.h index a0b8e95..1b95366 100644 --- a/Source/Nakatomi/TaskGetLocationNearLocation.h +++ b/Source/Nakatomi/TaskGetLocationNearLocation.h @@ -28,6 +28,10 @@ public: Meta = (AllowPrivateAccess = "true", DisplayName = "Maximum Distance")) float MaximumDistance = 500.0f; + UPROPERTY(EditAnywhere, Category = "Options", + Meta = (AllowPrivateAccess = "true", DisplayName = "Minimum Distance")) + double MinimumDistance = 0.0f; + public: virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& owner, uint8* memory) override; };