Something went wrong. Try again.
An implementation of a "utility AI" system, using weights to tell AI which tasks to do instead of a traditional behavior tree.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
#include "UtilityIntelligence.h"#include "AISystem.h"
// Sets default values for this component's propertiesUUtilityIntelligence::UUtilityIntelligence(){ PrimaryComponentTick.bCanEverTick = false; SkillTick = 0.05f; DecisionTick = 0.125f;
ContextTarget = NULL;
AcceptableRadius = GET_AI_CONFIG_VAR(AcceptanceRadius); bReachTestIncludesGoalRadius = bReachTestIncludesAgentRadius = GET_AI_CONFIG_VAR(bFinishMoveOnGoalOverlap); bAllowStrafe = GET_AI_CONFIG_VAR(bAllowStrafing); bAllowPartialPath = GET_AI_CONFIG_VAR(bAcceptPartialPaths); bTrackMovingGoal = true; bProjectGoalLocation = true; bUsePathfinding = true;}
void UUtilityIntelligence::TickSkills(){ // This ticks anything which should be happening WHILE another action happens // For example, this will handle shooting while we run away FDecisionContext startContext = GetDecisionContext(); float bestScore = -1.0f; FDecisionContext bestContext = startContext; for (const USkillSet* skillSet : SkillSets) { skillSet->FindBestSkill(startContext, bestContext, bestScore); }
if (bestScore <= 0.0f) { // Score too low to make a decision return; }
// Make the actual decision MakeDecision(bestContext);}
void UUtilityIntelligence::TickDecisions(){ // This handles strategic planning and long-term decisions // For example, this plans out a good spot to run away to and starts running there FDecisionContext startContext = GetDecisionContext(); float bestScore = -1.0f; FDecisionContext bestContext = startContext; for (const UDecisionMaker* decsionMaker : Decisions) { // Iterate over every DecisionMaker // These can be added or removed at will, so if the AI enters a tavern // a "tavern" DecisionMaker can be added to the list, allowing for special // tavern behaviors decsionMaker->FindBestDecision(startContext, bestContext, bestScore); }
if (bestScore <= 0.0f) { // Score too low to make a decision return; }
// Make the actual decision MakeDecision(bestContext);}
FDecisionContext UUtilityIntelligence::GetDecisionContext(){ FDecisionContext context; context.OurIntelligence = this; context.Decision = NULL; context.OurTarget = ContextTarget; context.AIBlackboard = AIBlackboard; return context;}
void UUtilityIntelligence::EndPlay(EEndPlayReason::Type EndPlayReason){ Super::EndPlay(EndPlayReason); StopAI();}
TArray<FMadeDecision> UUtilityIntelligence::GetRecentDecisions_Implementation() const{ return RecentDecisions;}
void UUtilityIntelligence::MakeDecision(const FDecisionContext& Context){ if (Context.Decision == NULL) { return; }
UE_LOG(LogTemp, Log, TEXT("Running decision %s."), *Context.Decision->GetName()); RecentDecisions.Add(Context.Decision->RunDecision(Context));}
void UUtilityIntelligence::StartAI(AAIController* Controller, UBlackboardComponent* Blackboard){ OurController = Controller; AIBlackboard = Blackboard;
FTimerManager& worldManager = GetWorld()->GetTimerManager(); worldManager.SetTimer(DecisionTickTimer, this, &UUtilityIntelligence::TickDecisions, DecisionTick, true, 0.0f); worldManager.SetTimer(SkillTickTimer, this, &UUtilityIntelligence::TickSkills, SkillTick, true, 0.0f);
}
void UUtilityIntelligence::StopAI(){ GetWorld()->GetTimerManager().ClearAllTimersForObject(this);}
void UUtilityIntelligence::UpdateContextTarget(AActor* NewTarget){ ContextTarget = NewTarget;}
void UUtilityIntelligence::AddNewPossibleTarget(APawn* NewTarget){ ValidTargets.Add(NewTarget);}
void UUtilityIntelligence::RemovePossibleTarget(APawn* Target){ ValidTargets.Remove(Target);}
TSet<APawn*> UUtilityIntelligence::GetPossibleTargets() const{ return ValidTargets;}
void UUtilityIntelligence::AddNewAlly(APawn* NewAlly){ ValidAllies.Add(NewAlly);}
void UUtilityIntelligence::RemoveAlly(APawn* Ally){ ValidAllies.Remove(Ally);}
TSet<APawn*> UUtilityIntelligence::GetAllies() const{ return ValidAllies;}
void UUtilityIntelligence::AddAreaOfInterest(const FVector& InterestingPoint){ AreasOfInterest.Add(InterestingPoint);}
void UUtilityIntelligence::RemoveAreaOfInterest(const FVector& InterestingPoint){ AreasOfInterest.Remove(InterestingPoint);}
AAIController* UUtilityIntelligence::GetController() const{ return OurController;}