From a86eb14b35e112ff2b94d15e757f7c40b442a307 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Mon, 9 Oct 2023 03:01:41 +0100 Subject: [PATCH] Update Enemy AI for Hearing Perception --- .../AI/Enemy/Basic/BB_BasicEnemy.uasset | 4 +- .../AI/Enemy/Basic/BT_BasicEnemy.uasset | 4 +- .../Blueprints/BP_FirstPersonCharacter.uasset | 4 +- .../EndlessVendetta/AI/AI_EnemyController.cpp | 50 ++++++++++++------- .../EndlessVendetta/AI/AI_EnemyController.h | 1 + .../AI/Tasks/BTTask_StopInvestigating.cpp | 27 ++++++++++ .../AI/Tasks/BTTask_StopInvestigating.h | 20 ++++++++ 7 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.h diff --git a/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset b/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset index 10435623..86c597c3 100644 --- a/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset +++ b/EndlessVendetta/Content/AI/Enemy/Basic/BB_BasicEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d0a3db5cfec43d17f9da7040dd28af3f31ff5cb2d00756125f57bda2281fb3b6 -size 5092 +oid sha256:4fb06286211ec1dc9e6245e12bb63c2b2cdc6afa6bb179de76eb1352d50a3358 +size 5694 diff --git a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset index fb2bdba6..e3fc9299 100644 --- a/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset +++ b/EndlessVendetta/Content/AI/Enemy/Basic/BT_BasicEnemy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c34f4b7820dd26e2577649567ec2d734d0d8b0df38e081f45a21b3fbe7ed1e5a -size 22715 +oid sha256:c9173e7ac91148e35778b0d3cd1bbc818b793426a87c14a27ab1b205bacddfc2 +size 30952 diff --git a/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset index c68329b2..6f9ddff6 100644 --- a/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset +++ b/EndlessVendetta/Content/FirstPerson/Blueprints/BP_FirstPersonCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dfda56ad26d2c6bcc0fec5a9baf228eeba7cf6c4728566886d051ea933c3e3f2 -size 26540 +oid sha256:228cb8be1c293a528c0116b78bac09d6dd2a180ca322029dbe245df30e35c126 +size 49329 diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.cpp index f29f3d4a..864586e4 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.cpp @@ -7,6 +7,7 @@ #include "BehaviorTree/BlackboardComponent.h" #include "EndlessVendetta/EndlessVendettaCharacter.h" #include "Perception/AIPerceptionComponent.h" +#include "Perception/AISenseConfig_Hearing.h" #include "Perception/AISenseConfig_Sight.h" @@ -53,38 +54,53 @@ void AAI_EnemyController::Tick(float DeltaTime) void AAI_EnemyController::SetupPerceptionSystem() { SightConfig = CreateDefaultSubobject(TEXT("Sight Config")); - if (IsValid(SightConfig)) - { - SetPerceptionComponent(*CreateDefaultSubobject(TEXT("Perception Component"))); - SightConfig->SightRadius = 2000.0f; - SightConfig->LoseSightRadius = 2100.0f; - SightConfig->PeripheralVisionAngleDegrees = 70.0f; - SightConfig->SetMaxAge(20.0f); - SightConfig->AutoSuccessRangeFromLastSeenLocation = 520.0f; - SightConfig->DetectionByAffiliation.bDetectEnemies = true; - SightConfig->DetectionByAffiliation.bDetectFriendlies = true; - SightConfig->DetectionByAffiliation.bDetectNeutrals = true; - GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation()); - GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_EnemyController::OnTargetPerceptionUpdated); - GetPerceptionComponent()->ConfigureSense(*SightConfig); - } + SightConfig->SightRadius = 2000.0f; + SightConfig->LoseSightRadius = 2100.0f; + SightConfig->PeripheralVisionAngleDegrees = 70.0f; + SightConfig->SetMaxAge(20.0f); + SightConfig->AutoSuccessRangeFromLastSeenLocation = 520.0f; + SightConfig->DetectionByAffiliation.bDetectEnemies = true; + SightConfig->DetectionByAffiliation.bDetectFriendlies = true; + SightConfig->DetectionByAffiliation.bDetectNeutrals = true; + + HearingConfig = CreateDefaultSubobject(TEXT("Hearing Config")); + HearingConfig->HearingRange = 2000.0f; + HearingConfig->DetectionByAffiliation.bDetectEnemies = true; + HearingConfig->DetectionByAffiliation.bDetectFriendlies = true; + HearingConfig->DetectionByAffiliation.bDetectNeutrals = true; + + if (!IsValid(SightConfig)) return; + if (!IsValid(HearingConfig)) return; + + SetPerceptionComponent(*CreateDefaultSubobject(TEXT("Perception Component"))); + GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation()); + GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_EnemyController::OnTargetPerceptionUpdated); + GetPerceptionComponent()->ConfigureSense(*SightConfig); + GetPerceptionComponent()->ConfigureSense(*HearingConfig); } void AAI_EnemyController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus) { if (AEndlessVendettaCharacter* const PlayerCharacter = Cast(Actor)) { - if (Stimulus.WasSuccessfullySensed()) + if (Stimulus.WasSuccessfullySensed() && Stimulus.Type == SightConfig->GetSenseID()) { GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor); GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation); GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true); } - else + else if (!Stimulus.WasSuccessfullySensed() && Stimulus.Type == SightConfig->GetSenseID()) { GetBlackboardComponent()->ClearValue("TargetActor"); GetBlackboardComponent()->ClearValue("TargetLocation"); GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false); } + + if (Stimulus.WasSuccessfullySensed() && Stimulus.Type == HearingConfig->GetSenseID()) + { + GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor); + GetBlackboardComponent()->SetValueAsVector("InvestigationLocation", Stimulus.StimulusLocation); + GetBlackboardComponent()->SetValueAsBool("IsInvestigating", true); + } } } diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.h b/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.h index 786bb0c3..c17469a2 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AI_EnemyController.h @@ -27,6 +27,7 @@ public: private: class UAISenseConfig_Sight* SightConfig; + class UAISenseConfig_Hearing* HearingConfig; void SetupPerceptionSystem(); UFUNCTION() diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.cpp new file mode 100644 index 00000000..8e5cd367 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.cpp @@ -0,0 +1,27 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "BTTask_StopInvestigating.h" + +#include "BehaviorTree/BlackboardComponent.h" +#include "EndlessVendetta/AI/AI_EnemyController.h" +#include "EndlessVendetta/AI/EnemyCharacter.h" + +UBTTask_StopInvestigating::UBTTask_StopInvestigating() +{ + NodeName = TEXT("Stop Investigating"); +} + +EBTNodeResult::Type UBTTask_StopInvestigating::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) +{ + if (AAI_EnemyController* const EnemyController = Cast(OwnerComp.GetOwner())) + { + if (UBlackboardComponent* const BlackboardComponent = OwnerComp.GetBlackboardComponent()) + { + BlackboardComponent->ClearValue(GetSelectedBlackboardKey()); + FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded); + return EBTNodeResult::Succeeded; + } + } + return EBTNodeResult::Failed; +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.h b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.h new file mode 100644 index 00000000..ff6f37d6 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/AI/Tasks/BTTask_StopInvestigating.h @@ -0,0 +1,20 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "BehaviorTree/Tasks/BTTask_BlackboardBase.h" +#include "BTTask_StopInvestigating.generated.h" + +/** + * + */ +UCLASS() +class ENDLESSVENDETTA_API UBTTask_StopInvestigating : public UBTTask_BlackboardBase +{ + GENERATED_BODY() + +public: + UBTTask_StopInvestigating(); + virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override; +};