A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VRAIPerceptionOverrides.h
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
3#pragma once
4#include "CoreMinimal.h"
5#include "UObject/ObjectMacros.h"
6#include "VRBaseCharacter.h"
7#include "AIModule/Classes/GenericTeamAgentInterface.h"
8#include "AIModule/Classes/Perception/AISense.h"
9#include "AIModule/Classes/Perception/AISenseConfig.h"
10
11#include "VRAIPerceptionOverrides.generated.h"
12
13class IAISightTargetInterface;
15
16class FGameplayDebuggerCategory;
17class UAIPerceptionComponent;
18
20
21UCLASS(meta = (DisplayName = "AI Sight VR config"))
22class VREXPANSIONPLUGIN_API UAISenseConfig_Sight_VR : public UAISenseConfig
23{
24 GENERATED_UCLASS_BODY()
25
26public:
27 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", NoClear, config)
28 TSubclassOf<UAISense_Sight_VR> Implementation;
29
31 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0.0, ClampMin = 0.0))
32 float SightRadius;
33
35 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0.0, ClampMin = 0.0))
36 float LoseSightRadius;
37
40 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0.0, ClampMin = 0.0, UIMax = 180.0, ClampMax = 180.0, DisplayName = "PeripheralVisionHalfAngleDegrees"))
41 float PeripheralVisionAngleDegrees;
42
44 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config)
45 FAISenseAffiliationFilter DetectionByAffiliation;
46
48 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config)
49 float AutoSuccessRangeFromLastSeenLocation;
50
51 virtual TSubclassOf<UAISense> GetSenseImplementation() const override;
52
53#if WITH_EDITOR
54 virtual void PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) override;
55#endif // WITH_EDITOR
56
57#if WITH_GAMEPLAY_DEBUGGER
58 virtual void DescribeSelfToGameplayDebugger(const UAIPerceptionComponent* PerceptionComponent, FGameplayDebuggerCategory* DebuggerCategory) const;
59#endif // WITH_GAMEPLAY_DEBUGGER
60
61};
62
63
65{
66 enum Type
67 {
71 };
72}
74USTRUCT()
75struct VREXPANSIONPLUGIN_API FAISightEventVR
76{
77 GENERATED_USTRUCT_BODY()
78
79 typedef UAISense_Sight_VR FSenseClass;
80
81 float Age;
83
84 UPROPERTY()
85 AActor* SeenActor;
86
87 UPROPERTY()
88 AActor* Observer;
89
90 FAISightEventVR() : SeenActor(nullptr), Observer(nullptr) {}
91
92 FAISightEventVR(AActor* InSeenActor, AActor* InObserver, ESightPerceptionEventNameVR::Type InEventType)
93 : Age(0.f), EventType(InEventType), SeenActor(InSeenActor), Observer(InObserver)
94 {
95 }
96};
100 typedef uint32 FTargetId;
103 TWeakObjectPtr<AActor> Target;
104 IAISightTargetInterface* SightTargetInterface;
105 FGenericTeamId TeamId;
107
108 FAISightTargetVR(AActor* InTarget = NULL, FGenericTeamId InTeamId = FGenericTeamId::NoTeam);
109
110 FORCEINLINE FVector GetLocationSimple() const
111 {
112 // Changed this up to support my VR Characters
113 const AVRBaseCharacter * VRChar = Cast<const AVRBaseCharacter>(Target);
114 return Target.IsValid() ? (VRChar != nullptr ? VRChar->GetVRLocation_Inline() : Target->GetActorLocation()) : FVector::ZeroVector;
115 }
117 FORCEINLINE const AActor* GetTargetActor() const { return Target.Get(); }
118};
119
120struct FAISightQueryVR
121{
122 FPerceptionListenerID ObserverId;
124
125 float Score;
126 float Importance;
128 FVector LastSeenLocation;
131 mutable int32 UserData;
132
133 uint64 bLastResult : 1;
134 uint64 LastProcessedFrameNumber : 63;
135
136 FAISightQueryVR(FPerceptionListenerID ListenerId = FPerceptionListenerID::InvalidID(), FAISightTargetVR::FTargetId Target = FAISightTargetVR::InvalidTargetId)
137 : ObserverId(ListenerId), TargetId(Target), Score(0), Importance(0), LastSeenLocation(FAISystem::InvalidLocation), UserData(0), bLastResult(false), LastProcessedFrameNumber(GFrameCounter)
138 {
141 float GetAge() const
143 return (float)(GFrameCounter - LastProcessedFrameNumber);
146 void RecalcScore()
147 {
148 Score = GetAge() + Importance;
150
151 void OnProcessed()
152 {
153 LastProcessedFrameNumber = GFrameCounter;
154 }
155
157 {
158 LastSeenLocation = FAISystem::InvalidLocation;
159 bLastResult = false;
160 }
163 {
164 public:
166 {}
168 bool operator()(const FAISightQueryVR& A, const FAISightQueryVR& B) const
169 {
170 return A.Score > B.Score;
171 }
172 };
174
175UCLASS(ClassGroup = AI, config = Game)
176class VREXPANSIONPLUGIN_API UAISense_Sight_VR : public UAISense
177{
178 GENERATED_UCLASS_BODY()
179
180public:
181 struct FDigestedSightProperties
182 {
183 float PeripheralVisionAngleCos;
184 float SightRadiusSq;
185 float AutoSuccessRangeSqFromLastSeenLocation;
186 float LoseSightRadiusSq;
187 uint8 AffiliationFlags;
188
189 FDigestedSightProperties();
190 FDigestedSightProperties(const UAISenseConfig_Sight_VR& SenseConfig);
191 };
192
193 typedef TMap<FAISightTargetVR::FTargetId, FAISightTargetVR> FTargetsContainer;
194 FTargetsContainer ObservedTargets;
195 TMap<FPerceptionListenerID, FDigestedSightProperties> DigestedProperties;
196
200 int32 NextOutOfRangeIndex = 0;
201 bool bSightQueriesOutOfRangeDirty = true;
202 TArray<FAISightQueryVR> SightQueriesOutOfRange;
203 TArray<FAISightQueryVR> SightQueriesInRange;
205protected:
206 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
207 int32 MaxTracesPerTick;
208
209 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
210 int32 MinQueriesPerTimeSliceCheck;
211
212 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
213 double MaxTimeSlicePerTick;
214
215 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
216 float HighImportanceQueryDistanceThreshold;
218 float HighImportanceDistanceSquare;
220 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
221 float MaxQueryImportance;
223 UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
224 float SightLimitQueryImportance;
226 ECollisionChannel DefaultSightCollisionChannel;
228public:
229
230 virtual void PostInitProperties() override;
231
232 void RegisterEvent(const FAISightEventVR& Event);
233
234 virtual void RegisterSource(AActor& SourceActors) override;
235 virtual void UnregisterSource(AActor& SourceActor) override;
237 virtual void OnListenerForgetsActor(const FPerceptionListener& Listener, AActor& ActorToForget) override;
238 virtual void OnListenerForgetsAll(const FPerceptionListener& Listener) override;
239
240protected:
241 virtual float Update() override;
243 virtual bool ShouldAutomaticallySeeTarget(const FDigestedSightProperties& PropDigest, FAISightQueryVR* SightQuery, FPerceptionListener& Listener, AActor* TargetActor, float& OutStimulusStrength) const;
245 void OnNewListenerImpl(const FPerceptionListener& NewListener);
246 void OnListenerUpdateImpl(const FPerceptionListener& UpdatedListener);
247 void OnListenerRemovedImpl(const FPerceptionListener& RemovedListener);
248 virtual void OnListenerConfigUpdated(const FPerceptionListener& UpdatedListener) override;
249
250 void GenerateQueriesForListener(const FPerceptionListener& Listener, const FDigestedSightProperties& PropertyDigest, const TFunction<void(FAISightQueryVR&)>& OnAddedFunc = nullptr);
251
253 void RemoveAllQueriesByListener(const FPerceptionListener& Listener, const TFunction<void(const FAISightQueryVR&)>& OnRemoveFunc = nullptr);
254 void RemoveAllQueriesToTarget(const FAISightTargetVR::FTargetId& TargetId, const TFunction<void(const FAISightQueryVR&)>& OnRemoveFunc = nullptr);
255
257 bool RegisterTarget(AActor& TargetActor, const TFunction<void(FAISightQueryVR&)>& OnAddedFunc = nullptr);
258
259 float CalcQueryImportance(const FPerceptionListener& Listener, const FVector& TargetLocation, const float SightRadiusSq) const;
260
261 // Deprecated methods
262public:
263 UE_DEPRECATED(4.25, "Not needed anymore done automatically at the beginning of each update.")
264 FORCEINLINE void SortQueries() {}
265
267 {
268 DontSort,
269 Sort
270 };
271 UE_DEPRECATED(4.25, "Use RemoveAllQueriesByListener without unneeded PostProcess parameter.")
272 void RemoveAllQueriesByListener(const FPerceptionListener& Listener, FQueriesOperationPostProcess PostProcess) { RemoveAllQueriesByListener(Listener); }
273 UE_DEPRECATED(4.25, "Use RemoveAllQueriesByListener without unneeded PostProcess parameter.")
274 void RemoveAllQueriesByListener(const FPerceptionListener& Listener, FQueriesOperationPostProcess PostProcess, TFunctionRef<void(const FAISightQueryVR&)> OnRemoveFunc) { RemoveAllQueriesByListener(Listener, [&](const FAISightQueryVR& query) { OnRemoveFunc(query); }); }
275 UE_DEPRECATED(4.25, "Use RemoveAllQueriesToTarget without unneeded PostProcess parameter.")
276 void RemoveAllQueriesToTarget(const FAISightTargetVR::FTargetId& TargetId, FQueriesOperationPostProcess PostProcess) { RemoveAllQueriesToTarget(TargetId); }
277 UE_DEPRECATED(4.25, "Use RemoveAllQueriesToTarget without unneeded PostProcess parameter.")
278 void RemoveAllQueriesToTarget(const FAISightTargetVR::FTargetId& TargetId, FQueriesOperationPostProcess PostProcess, TFunctionRef<void(const FAISightQueryVR&)> OnRemoveFunc) { RemoveAllQueriesToTarget(TargetId, [&](const FAISightQueryVR& query) { OnRemoveFunc(query); }); }
279
280
281 UE_DEPRECATED(4.25, "Use RegisterTarget without unneeded PostProcess parameter.")
282 bool RegisterTarget(AActor& TargetActor, FQueriesOperationPostProcess PostProcess) { return RegisterTarget(TargetActor); }
283 UE_DEPRECATED(4.25, "Use RegisterTarget without unneeded PostProcess parameter.")
284 bool RegisterTarget(AActor& TargetActor, FQueriesOperationPostProcess PostProcess, TFunctionRef<void(FAISightQueryVR&)> OnAddedFunc) { return RegisterTarget(TargetActor, [&](FAISightQueryVR& query) { OnAddedFunc(query); }); }
285
286};
DECLARE_LOG_CATEGORY_EXTERN(LogAIPerceptionVR, Warning, All)
FVector GetVRLocation_Inline() const
bool operator()(const FAISightQueryVR &A, const FAISightQueryVR &B) const
UCLASS(ClassGroup = AI, config = Game)
void RemoveAllQueriesByListener(const FPerceptionListener &Listener, FQueriesOperationPostProcess PostProcess, TFunctionRef< void(const FAISightQueryVR &)> OnRemoveFunc)
TArray< FAISightQueryVR > SightQueriesInRange
float HighImportanceQueryDistanceThreshold
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
int32 MaxTracesPerTick
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
void RemoveAllQueriesToTarget(const FAISightTargetVR::FTargetId &TargetId, FQueriesOperationPostProcess PostProcess, TFunctionRef< void(const FAISightQueryVR &)> OnRemoveFunc)
TMap< FPerceptionListenerID, FDigestedSightProperties > DigestedProperties
bool RegisterTarget(AActor &TargetActor, FQueriesOperationPostProcess PostProcess)
void RemoveAllQueriesToTarget(const FAISightTargetVR::FTargetId &TargetId, FQueriesOperationPostProcess PostProcess)
bool RegisterTarget(AActor &TargetActor, FQueriesOperationPostProcess PostProcess, TFunctionRef< void(FAISightQueryVR &)> OnAddedFunc)
float MaxQueryImportance
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
void RemoveAllQueriesByListener(const FPerceptionListener &Listener, FQueriesOperationPostProcess PostProcess)
TArray< FAISightQueryVR > SightQueriesOutOfRange
float SightLimitQueryImportance
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
double MaxTimeSlicePerTick
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
TMap< FAISightTargetVR::FTargetId, FAISightTargetVR > FTargetsContainer
ECollisionChannel DefaultSightCollisionChannel
FTargetsContainer ObservedTargets
int32 MinQueriesPerTimeSliceCheck
UPROPERTY(EditDefaultsOnly, Category = "AI Perception", config)
UCLASS(meta = (DisplayName = "AI Sight VR config"))
float PeripheralVisionAngleDegrees
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0....
FAISenseAffiliationFilter DetectionByAffiliation
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config)
float LoseSightRadius
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0....
TSubclassOf< UAISense_Sight_VR > Implementation
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", NoClear, config)
float AutoSuccessRangeFromLastSeenLocation
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config)
float SightRadius
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Sense", config, meta = (UIMin = 0....
UAISense_Sight_VR FSenseClass
FAISightEventVR(AActor *InSeenActor, AActor *InObserver, ESightPerceptionEventNameVR::Type InEventType)
ESightPerceptionEventNameVR::Type EventType
AActor * Observer
UPROPERTY()
AActor * SeenActor
UPROPERTY()
FAISightTargetVR::FTargetId TargetId
FPerceptionListenerID ObserverId
FAISightQueryVR(FPerceptionListenerID ListenerId=FPerceptionListenerID::InvalidID(), FAISightTargetVR::FTargetId Target=FAISightTargetVR::InvalidTargetId)
static const FTargetId InvalidTargetId
IAISightTargetInterface * SightTargetInterface
FORCEINLINE FVector GetLocationSimple() const
TWeakObjectPtr< AActor > Target
FORCEINLINE const AActor * GetTargetActor() const
FAISightTargetVR(AActor *InTarget=NULL, FGenericTeamId InTeamId=FGenericTeamId::NoTeam)