void UAISense_Sight::GenerateQueriesForListener(const FPerceptionListener& Listener, const FDigestedSightProperties& PropertyDigest) { bool bNewQueriesAdded = false; const IGenericTeamAgentInterface* ListenersTeamAgent = Listener.GetTeamAgent(); // create sight queries with all legal targets for (TMap<FName, FAISightTarget>::TConstIterator ItTarget(ObservedTargets); ItTarget; ++ItTarget) { const AActor* TargetActor = ItTarget->Value.GetTargetActor(); if (TargetActor == NULL) { continue; } // @todo this should be configurable - some AI might want to observe Neutrals and Friendlies as well if (ListenersTeamAgent == NULL || ListenersTeamAgent->GetTeamAttitudeTowards(*TargetActor) == ETeamAttitude::Hostile) { // create a sight query FAISightQuery SightQuery(Listener.GetListenerID(), ItTarget->Key); SightQuery.Importance = CalcQueryImportance(Listener, ItTarget->Value.GetLocationSimple(), PropertyDigest.SightRadiusSq); SightQueryQueue.Add(SightQuery); bNewQueriesAdded = true; } } // sort Sight Queries if (bNewQueriesAdded) { SortQueries(); RequestImmediateUpdate(); } }
void UAISense_Sight::GenerateQueriesForListener(const FPerceptionListener& Listener, const FDigestedSightProperties& PropertyDigest) { bool bNewQueriesAdded = false; const IGenericTeamAgentInterface* ListenersTeamAgent = Listener.GetTeamAgent(); const AActor* Avatar = Listener.GetBodyActor(); // create sight queries with all legal targets for (TMap<FName, FAISightTarget>::TConstIterator ItTarget(ObservedTargets); ItTarget; ++ItTarget) { const AActor* TargetActor = ItTarget->Value.GetTargetActor(); if (TargetActor == NULL || TargetActor == Avatar) { continue; } if (FAISenseAffiliationFilter::ShouldSenseTeam(ListenersTeamAgent, *TargetActor, PropertyDigest.AffiliationFlags)) { // create a sight query FAISightQuery SightQuery(Listener.GetListenerID(), ItTarget->Key); SightQuery.Importance = CalcQueryImportance(Listener, ItTarget->Value.GetLocationSimple(), PropertyDigest.SightRadiusSq); SightQueryQueue.Add(SightQuery); bNewQueriesAdded = true; } } // sort Sight Queries if (bNewQueriesAdded) { SortQueries(); RequestImmediateUpdate(); } }
float UAISense_Aquaphobia::Update() { AIPerception::FListenerMap& ListenersMap = *GetListeners(); //For each listener who has this sense we're going to perform a sweep to determine nearby aqua actors for (auto& Elem : ListenersMap) { //Get the listener FPerceptionListener Listener = Elem.Value; const AActor* ListenerBodyActor = Listener.GetBodyActor(); for (int32 DigestedPropertyIndex = 0; DigestedPropertyIndex < DigestedProperties.Num(); DigestedPropertyIndex++) { //Create the sphere for this sense and perform the sweep to determine nearby actors FCollisionShape CollisionSphere = FCollisionShape::MakeSphere(DigestedProperties[DigestedPropertyIndex].PhobiaRadius); TArray<FHitResult> HitResults; GetWorld()->SweepMultiByChannel(HitResults, ListenerBodyActor->GetActorLocation(), ListenerBodyActor->GetActorLocation() + FVector::UpVector*CollisionSphere.GetSphereRadius(), FQuat(), ECollisionChannel::ECC_WorldDynamic, CollisionSphere); //Draw debug sphere if we have activated it via the config if (DigestedProperties[DigestedPropertyIndex].bDisplayDebugSphere) { DrawDebugSphere(GetWorld(), ListenerBodyActor->GetActorLocation(), DigestedProperties[DigestedPropertyIndex].PhobiaRadius, 8, FColor::Blue, false, 30.f, 1, 2.f); } //Check hit results for aqua actors for (int32 i = 0; i < HitResults.Num(); i++) { FHitResult hit = HitResults[i]; //To simplify things, we're going to assume that "water resources" for this post are actors that have the following game tag if (hit.GetActor()->ActorHasTag(FName("AquaActor"))) { if ((hit.GetActor()->GetActorLocation() - ListenerBodyActor->GetActorLocation()).Size() <= DigestedProperties[DigestedPropertyIndex].PhobiaRadius) { Elem.Value.RegisterStimulus(hit.GetActor(), FAIStimulus(*this, 5.f, hit.GetActor()->GetActorLocation(), ListenerBodyActor->GetActorLocation())); GLog->Log("registered stimulus!"); } } } } } //Time until next update; in this case we're forcing the update to happen in each frame return 0.f; }
void UAISense_Sight::RemoveAllQueriesByListener(const FPerceptionListener& Listener, FQueriesOperationPostProcess PostProcess) { SCOPE_CYCLE_COUNTER(STAT_AI_Sense_Sight); if (SightQueryQueue.Num() == 0) { return; } const uint32 ListenerId = Listener.GetListenerID(); bool bQueriesRemoved = false; const FAISightQuery* SightQuery = &SightQueryQueue[SightQueryQueue.Num() - 1]; for (int32 QueryIndex = SightQueryQueue.Num() - 1; QueryIndex >= 0 ; --QueryIndex, --SightQuery) { if (SightQuery->ObserverId == ListenerId) { SightQueryQueue.RemoveAt(QueryIndex, 1, /*bAllowShrinking=*/false); bQueriesRemoved = true; } } if (PostProcess == Sort && bQueriesRemoved) { SortQueries(); } }
void UAISense_Sight::OnNewListenerImpl(const FPerceptionListener& NewListener) { check(NewListener.Listener.IsValid()); const UAISenseConfig_Sight* SenseConfig = Cast<const UAISenseConfig_Sight>(NewListener.Listener->GetSenseConfig(GetSenseID())); check(SenseConfig); const FDigestedSightProperties PropertyDigest(*SenseConfig); DigestedProperties.Add(NewListener.GetListenerID(), PropertyDigest); GenerateQueriesForListener(NewListener, PropertyDigest); }
void UAISense_Sight::OnListenerRemovedImpl(const FPerceptionListener& UpdatedListener) { RemoveAllQueriesByListener(UpdatedListener, DontSort); DigestedProperties.FindAndRemoveChecked(UpdatedListener.GetListenerID()); // note: there use to be code to remove all queries _to_ listener here as well // but that was wrong - the fact that a listener gets unregistered doesn't have to // mean it's being removed from the game altogether. }
void UAIPerceptionSystem::OnNewListener(const FPerceptionListener& NewListener) { UAISense** SenseInstance = Senses.GetData(); for (int32 SenseID = 0; SenseID < Senses.Num(); ++SenseID, ++SenseInstance) { // @todo filter out the ones that do not declare using this sense if (*SenseInstance != nullptr && NewListener.HasSense((*SenseInstance)->GetSenseID())) { (*SenseInstance)->OnNewListener(NewListener); } } }
void UAISense_Sight::OnListenerRemovedImpl(const FPerceptionListener& UpdatedListener) { RemoveAllQueriesByListener(UpdatedListener, DontSort); DigestedProperties.FindAndRemoveChecked(UpdatedListener.GetListenerID()); if (UpdatedListener.Listener.IsValid()) { // see if this listener is a Target as well const FAISightTarget::FTargetId AsTargetId = UpdatedListener.GetBodyActorName(); FAISightTarget* AsTarget = ObservedTargets.Find(AsTargetId); if (AsTarget != NULL) { RemoveAllQueriesToTarget(AsTargetId, Sort); } } else { //@todo quite possible there are left over sight queries with this listener as target } }
void UAISense_Sight::OnListenerUpdateImpl(const FPerceptionListener& UpdatedListener) { SCOPE_CYCLE_COUNTER(STAT_AI_Sense_Sight_ListenerUpdate); // first, naive implementation: // 1. remove all queries by this listener // 2. proceed as if it was a new listener // remove all queries RemoveAllQueriesByListener(UpdatedListener, DontSort); // see if this listener is a Target as well const FAISightTarget::FTargetId AsTargetId = UpdatedListener.GetBodyActorName(); FAISightTarget* AsTarget = ObservedTargets.Find(AsTargetId); if (AsTarget != NULL) { RemoveAllQueriesToTarget(AsTargetId, DontSort); if (AsTarget->Target.IsValid()) { RegisterTarget(*(AsTarget->Target.Get()), DontSort); } } const FPerceptionListenerID ListenerID = UpdatedListener.GetListenerID(); if (UpdatedListener.HasSense(GetSenseID())) { const UAISenseConfig_Sight* SenseConfig = Cast<const UAISenseConfig_Sight>(UpdatedListener.Listener->GetSenseConfig(GetSenseID())); check(SenseConfig); FDigestedSightProperties& PropertiesDigest = DigestedProperties.FindOrAdd(ListenerID); PropertiesDigest = FDigestedSightProperties(*SenseConfig); GenerateQueriesForListener(UpdatedListener, PropertiesDigest); } else { DigestedProperties.FindAndRemoveChecked(ListenerID); } }