AMurphysLawAIController::AMurphysLawAIController() // Enable navigation within crowds //: Super(FObjectInitializer::Get().SetDefaultSubobjectClass<UCrowdFollowingComponent>(TEXT("PathFollowingComponent"))) { bWantsPlayerState = true; // bAllowStrafe = true; // ?? Run and shoot // The action logic of the AI (assigned from blueprint) BehaviorTreeAsset = nullptr; // No pawn is yet controlled bPossessPawn = false; // Create and configure sight sense UAISenseConfig_Sight* SightSenseConfig = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight sense"); SightSenseConfig->PeripheralVisionAngleDegrees = 90.f; SightSenseConfig->DetectionByAffiliation.bDetectEnemies = true; SightSenseConfig->DetectionByAffiliation.bDetectNeutrals = true; SightSenseConfig->DetectionByAffiliation.bDetectFriendlies = true; SightSenseConfig->SightRadius = 100.f * 20.f; SightSenseConfig->LoseSightRadius = SightSenseConfig->SightRadius * 1.2f; SightSenseConfig->AutoSuccessRangeFromLastSeenLocation = SightSenseConfig->SightRadius * 1.1f; UAISenseConfig_Hearing* HearingSenseConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>("Hearing sense"); HearingSenseConfig->HearingRange = 100.f * 50.f; HearingSenseConfig->LoSHearingRange = 100.f * 150.f; HearingSenseConfig->bUseLoSHearing = true; HearingSenseConfig->DetectionByAffiliation.bDetectEnemies = true; HearingSenseConfig->DetectionByAffiliation.bDetectNeutrals = true; HearingSenseConfig->DetectionByAffiliation.bDetectFriendlies = true; // Create perceptions system UAIPerceptionComponent* Perceptions = CreateDefaultSubobject<UAIPerceptionComponent>("PerceptionComp"); Perceptions->ConfigureSense(*SightSenseConfig); Perceptions->ConfigureSense(*HearingSenseConfig); Perceptions->SetDominantSense(SightSenseConfig->GetSenseImplementation()); Perceptions->Deactivate(); SetPerceptionComponent(*Perceptions); }
float UAISense_Touch::Update() { AIPerception::FListenerMap& ListenersMap = *GetListeners(); for (int32 EventIndex = 0; EventIndex < RegisteredEvents.Num(); ++EventIndex) { const FAITouchEvent& Event = RegisteredEvents[EventIndex]; if (Event.TouchReceiver != NULL && Event.OtherActor != NULL) { IAIPerceptionListenerInterface* PerceptionListener = Cast<IAIPerceptionListenerInterface>(Event.TouchReceiver); if (PerceptionListener != NULL) { UAIPerceptionComponent* PerceptionComponent = PerceptionListener->GetPerceptionComponent(); if (PerceptionComponent != NULL && ListenersMap.Contains(PerceptionComponent->GetListenerId())) { // this has to succeed, will assert a failure FPerceptionListener& Listener = ListenersMap[PerceptionComponent->GetListenerId()]; Listener.RegisterStimulus(Event.OtherActor, FAIStimulus(GetSenseIndex(), 1.f, Event.Location, Event.Location)); } } } } RegisteredEvents.Reset(); // return decides when next tick is going to happen return SuspendNextUpdate; }
float UAISense_Prediction::Update() { AIPerception::FListenerMap& ListenersMap = *GetListeners(); for (int32 EventIndex = 0; EventIndex < RegisteredEvents.Num(); ++EventIndex) { const FAIPredictionEvent& Event = RegisteredEvents[EventIndex]; if (Event.Requestor != NULL && Event.PredictedActor != NULL) { IAIPerceptionListenerInterface* PerceptionListener = Cast<IAIPerceptionListenerInterface>(Event.Requestor); if (PerceptionListener != NULL) { UAIPerceptionComponent* PerceptionComponent = PerceptionListener->GetPerceptionComponent(); if (PerceptionComponent != NULL && ListenersMap.Contains(PerceptionComponent->GetListenerId())) { // this has to succeed, will assert a failure FPerceptionListener& Listener = ListenersMap[PerceptionComponent->GetListenerId()]; // calculate the prediction here: const FVector PredictedLocation = Event.PredictedActor->GetActorLocation() + Event.PredictedActor->GetVelocity() * Event.TimeToPredict; Listener.RegisterStimulus(Event.PredictedActor, FAIStimulus(*this, 1.f, PredictedLocation, Listener.CachedLocation)); } } } } RegisteredEvents.Reset(); // return decides when next tick is going to happen return SuspendNextUpdate; }
float UAISense_Damage::Update() { AIPerception::FListenerMap& ListenersMap = *GetListeners(); for (int32 EventIndex = 0; EventIndex < RegisteredEvents.Num(); ++EventIndex) { const FAIDamageEvent& Event = RegisteredEvents[EventIndex]; IAIPerceptionListenerInterface* PerceptionListener = Cast<IAIPerceptionListenerInterface>(Event.DamagedActor); if (PerceptionListener != NULL) { UAIPerceptionComponent* PerceptionComponent = PerceptionListener->GetPerceptionComponent(); if (PerceptionComponent != NULL) { // this has to succeed, will assert a failure FPerceptionListener& Listener = ListenersMap[PerceptionComponent->GetListenerId()]; Listener.RegisterStimulus(Event.Instigator, FAIStimulus(GetSenseIndex(), Event.Amount, Event.Location, Event.HitLocation)); } } } RegisteredEvents.Reset(); // return decides when next tick is going to happen return SuspendNextUpdate; }
void FPerceptionListener::UpdateListenerProperties(UAIPerceptionComponent& InListener) { verify(&InListener == Listener.Get()); // using InListener rather then Listener to avoid slight overhead of TWeakObjectPtr TeamIdentifier = InListener.GetTeamIdentifier(); Filter = InListener.GetPerceptionFilter(); }
void UAISense_Blueprint::OnListenerUpdateImpl(const FPerceptionListener& UpdatedListener) { UAIPerceptionComponent* PercComp = UpdatedListener.Listener.Get(); if (PercComp) { OnListenerUpdated(PercComp->GetOwner(), PercComp); } }
void UAISense_Blueprint::OnListenerRemovedImpl(const FPerceptionListener& UpdatedListener) { UAIPerceptionComponent* PercComp = UpdatedListener.Listener.Get(); if (PercComp) { ListenerContainer.RemoveSingleSwap(PercComp); OnListenerUnregistered(PercComp->GetOwner(), PercComp); } }
void UAISense_Blueprint::OnNewListenerImpl(const FPerceptionListener& NewListener) { UAIPerceptionComponent* PercComp = NewListener.Listener.Get(); if (PercComp) { ListenerContainer.AddUnique(PercComp); OnListenerRegistered(PercComp->GetOwner(), PercComp); RequestImmediateUpdate(); } }
FPerceptionListener::FPerceptionListener(UAIPerceptionComponent& InListener) : Listener(&InListener) , ListenerID(FPerceptionListenerID::InvalidID()) { UpdateListenerProperties(InListener); ListenerID = InListener.GetListenerId(); }
void UPerceptionGameplayDebuggerObject::CollectDataToReplicate(APlayerController* MyPC, AActor *SelectedActor) { Super::CollectDataToReplicate(MyPC, SelectedActor); #if ENABLED_GAMEPLAY_DEBUGGER if (GetWorld()->TimeSeconds - LastDataCaptureTime < 2) { return; } APawn* MyPawn = Cast<APawn>(SelectedActor); if (MyPawn) { AAIController* BTAI = Cast<AAIController>(MyPawn->GetController()); if (BTAI) { UAIPerceptionComponent* PerceptionComponent = BTAI->GetAIPerceptionComponent(); if (PerceptionComponent == nullptr) { PerceptionComponent = MyPawn->FindComponentByClass<UAIPerceptionComponent>(); } if (PerceptionComponent) { TArray<FString> PerceptionTexts; GenericShapeElements.Reset(); PerceptionComponent->GrabGameplayDebuggerData(PerceptionTexts, GenericShapeElements); DistanceFromPlayer = DistanceFromSensor = -1; if (MyPC && MyPC->GetPawn()) { DistanceFromPlayer = (MyPawn->GetActorLocation() - MyPC->GetPawn()->GetActorLocation()).Size(); DistanceFromSensor = SensingComponentLocation != FVector::ZeroVector ? (SensingComponentLocation - MyPC->GetPawn()->GetActorLocation()).Size() : -1; } } UAIPerceptionSystem* PerceptionSys = UAIPerceptionSystem::GetCurrent(MyPawn->GetWorld()); if (PerceptionSys) { PerceptionLegend = PerceptionSys->GetPerceptionDebugLegend(); } } } #endif }
void UAISenseConfig_Sight::DrawDebugInfo(UCanvas& Canvas, UAIPerceptionComponent& PerceptionComponent) const { /*PeripheralVisionAngleDegrees*/ const AActor* BodyActor = PerceptionComponent.GetBodyActor(); if (BodyActor != nullptr) { UWorld* World = BodyActor->GetWorld(); FVector BodyLocation, BodyFacing; PerceptionComponent.GetLocationAndDirection(BodyLocation, BodyFacing); DrawDebugCylinder(World, BodyLocation, BodyLocation + FVector(0, 0, -50), SightRadius, 32, UAISense_Sight::GetDebugSightRangeColor()); DrawDebugCylinder(World, BodyLocation, BodyLocation + FVector(0, 0, -50), LoseSightRadius, 32, UAISense_Sight::GetDebugLoseSightColor()); const float SightPieLength = FMath::Max(LoseSightRadius, SightRadius); DrawDebugLine(World, BodyLocation, BodyLocation + (BodyFacing * SightPieLength), UAISense_Sight::GetDebugSightRangeColor()); DrawDebugLine(World, BodyLocation, BodyLocation + (BodyFacing.RotateAngleAxis(PeripheralVisionAngleDegrees, FVector::UpVector) * SightPieLength), UAISense_Sight::GetDebugSightRangeColor()); DrawDebugLine(World, BodyLocation, BodyLocation + (BodyFacing.RotateAngleAxis(-PeripheralVisionAngleDegrees, FVector::UpVector) * SightPieLength), UAISense_Sight::GetDebugSightRangeColor()); } }
void UAISenseConfig_Hearing::DrawDebugInfo(UCanvas& Canvas, UAIPerceptionComponent& PerceptionComponent) const { const AActor* BodyActor = PerceptionComponent.GetBodyActor(); if (BodyActor != nullptr) { UWorld* World = BodyActor->GetWorld(); FVector OwnerLocation = BodyActor->GetActorLocation(); DrawDebugCylinder(World, OwnerLocation, OwnerLocation + FVector(0, 0, -50), HearingRange, 32, UAISense_Hearing::GetDebugHearingRangeColor()); if (bUseLoSHearing) { DrawDebugCylinder(World, OwnerLocation, OwnerLocation + FVector(0, 0, -50), LoSHearingRange, 32, UAISense_Hearing::GetDebugLoSHearingRangeeColor()); } } }