-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBCG_EnemyAttackPredictManager.cpp
More file actions
350 lines (272 loc) · 11.7 KB
/
Copy pathMBCG_EnemyAttackPredictManager.cpp
File metadata and controls
350 lines (272 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright DevRespawn.com (MBCG). All Rights Reserved.
#include "MBCG/EnemyAttackPrediction/MBCG_EnemyAttackPredictManager.h"
#include "Logging/StructuredLog.h" // for UE_LOGFMT
#include "Weapons/LyraRangedWeaponInstance.h"
#include "Physics/LyraCollisionChannels.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "DrawDebugHelpers.h"
DEFINE_LOG_CATEGORY_STATIC(LogAMBCG_EnemyAttackPredictManager, All, All);
// Example of use:
// UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Display, "Enemy removed: {0}", InEnemy->GetName());
AMBCG_EnemyAttackPredictManager::AMBCG_EnemyAttackPredictManager()
{
PrimaryActorTick.bCanEverTick = true;
}
void AMBCG_EnemyAttackPredictManager::BeginPlay()
{
Super::BeginPlay();
// by default
LastRegisteredAttackThreatTimeSeconds = GetWorld()->GetTimeSeconds();
}
void AMBCG_EnemyAttackPredictManager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
TArray<APawn*> AMBCG_EnemyAttackPredictManager::GetEnemies() const
{
return Enemies;
}
void AMBCG_EnemyAttackPredictManager::AddUniqueEnemy(APawn* InEnemy)
{
if (!InEnemy) return;
if (InEnemy && !Enemies.Contains(InEnemy))
{
Enemies.AddUnique(InEnemy);
}
}
void AMBCG_EnemyAttackPredictManager::RemoveEnemy(APawn* InEnemy)
{
if (!InEnemy) return;
if (InEnemy && Enemies.Contains(InEnemy))
{
Enemies.Remove(InEnemy);
}
}
void AMBCG_EnemyAttackPredictManager::RemoveAllEnemies()
{
Enemies.Empty();
}
int32 AMBCG_EnemyAttackPredictManager::GetNumberOfEnemies() const
{
return Enemies.Num();
}
APawn* AMBCG_EnemyAttackPredictManager::GetHero() const
{
return Hero;
}
void AMBCG_EnemyAttackPredictManager::SetHero(APawn* InNewHero)
{
Hero = InNewHero;
}
void AMBCG_EnemyAttackPredictManager::AddTraceIgnoreActor(/*out*/ FCollisionQueryParams& TraceParams, const AActor* ActorToIgnore) const
{
if (!ActorToIgnore)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function AddTraceIgnoreActor(): ActorToIgnore argument is unexpectedly nullptr. The function will exit now.");
return;
}
// Ignore ActorToIgnore and any actors attached to it
TraceParams.AddIgnoredActor(ActorToIgnore);
TArray<AActor*> AttachedActors;
ActorToIgnore->GetAttachedActors(/*out*/ AttachedActors);
TraceParams.AddIgnoredActors(AttachedActors);
}
void AMBCG_EnemyAttackPredictManager::AddTraceIgnore(/*out*/ FCollisionQueryParams& TraceParams, const TArray<AActor*>& ActorsToIgnore) const
{
for (const AActor* Actor : ActorsToIgnore)
{
if (Actor)
{
AddTraceIgnoreActor(TraceParams, Actor);
}
}
}
bool AMBCG_EnemyAttackPredictManager::IsLineOfFireOpen( //
const FVector& StartTrace, //
const FVector& EndTrace, //
const AActor* SourceActorToIgnore, // to avoid false alert
const AActor* TargetActorToIgnore, // to avoid false alert
const TArray<AActor*>& OtherActorsToIgnore) const
{
// FYI: it's modified UMBCG_LyraGA_RangedWeapon::WeaponTrace
/*
if (!SourceActorToIgnore)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function IsLineOfFireOpen(): SourceActorToIgnore argument is unexpectedly nullptr.");
}
if (!TargetActorToIgnore)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function IsLineOfFireOpen(): TargetActorToIgnore argument is unexpectedly nullptr.");
}
*/
// Ignore source and target actors when line tracing
TArray<AActor*> ActorsToIgnore = OtherActorsToIgnore;
if (SourceActorToIgnore)
{
ActorsToIgnore.Add(const_cast<AActor*>(SourceActorToIgnore));
}
if (TargetActorToIgnore)
{
ActorsToIgnore.Add(const_cast<AActor*>(TargetActorToIgnore));
}
FHitResult HitResult;
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(IsLineOfFireOpen), /*bTraceComplex=*/false);
TraceParams.bReturnPhysicalMaterial = false;
AddTraceIgnore(TraceParams, ActorsToIgnore);
const ECollisionChannel TraceChannel = Lyra_TraceChannel_Weapon;
if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, TraceChannel, TraceParams))
{
// line of the fire is obstructed
return false;
}
else
{
// DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Red, false, 1.0f, 0, 1.0f);
return true;
}
}
bool AMBCG_EnemyAttackPredictManager::IsLineOfFireOpenNoOtherIgnoredActors( //
const FVector& StartTrace, //
const FVector& EndTrace, //
const AActor* SourceActorToIgnore, // to avoid false alert
const AActor* TargetActorToIgnore // to avoid false alert
) const
{
TArray<AActor*> EmptyArray;
return IsLineOfFireOpen(StartTrace, EndTrace, SourceActorToIgnore, TargetActorToIgnore, EmptyArray);
}
bool AMBCG_EnemyAttackPredictManager::CanActorBeSeenFromLocationNoOtherIgnoredActors(
const FVector& SourceLocation, const AActor* SourceActor, const AActor* TargetActor, const TArray<FVector>& TargetCachedSocketsRelativeLocations) const
{
if (!TargetActor) return false;
// Testing against sockets
bool ShouldCheckActorLocation = true;
for (const FVector& SocketRelativeLocation : TargetCachedSocketsRelativeLocations)
{
const FVector EndTrace = TargetActor->GetActorLocation() + SocketRelativeLocation;
if (SocketRelativeLocation.IsZero())
{
// Zero SocketRelativeLocation value means we are testing against just ActorLocation right now, and we will not need to test it again
ShouldCheckActorLocation = false;
}
if (IsLineOfFireOpenNoOtherIgnoredActors(SourceLocation, EndTrace, SourceActor, TargetActor)) return true;
}
// Test against target's ActorLocation
if (ShouldCheckActorLocation && (IsLineOfFireOpenNoOtherIgnoredActors(SourceLocation, TargetActor->GetActorLocation(), SourceActor, TargetActor))) return true;
return false;
}
bool AMBCG_EnemyAttackPredictManager::CanPlayerSeeActor(const APlayerController* PlayerController, const AActor* TargetActor, const TArray<FVector>& TargetCachedSocketsRelativeLocations) const
{
if (!PlayerController || !TargetActor) return false;
FVector PlayerViewLocation;
FRotator PlayerViewRotation;
PlayerController->GetPlayerViewPoint(PlayerViewLocation, PlayerViewRotation);
FVector2D ScreenLocation;
bool bCouldBeProjectedOnScreen = PlayerController->ProjectWorldLocationToScreen(TargetActor->GetActorLocation(), ScreenLocation);
if (!bCouldBeProjectedOnScreen) return false;
// bCouldBeProjectedOnScreen not alwasy return false when actor is not in the viewport
if ((ScreenLocation.X < 0) || (ScreenLocation.Y < 0)) return false;
int32 ViewportSizeX;
int32 ViewportSizeY;
PlayerController->GetViewportSize(ViewportSizeX, ViewportSizeY);
if ((ScreenLocation.X > ViewportSizeX) || (ScreenLocation.Y > ViewportSizeY)) return false;
return CanActorBeSeenFromLocationNoOtherIgnoredActors(PlayerViewLocation, nullptr /* SourceActor */, TargetActor, TargetCachedSocketsRelativeLocations);
#if 0
/* COP: FYI
// Getting dot product between player view and the vector to the enemy to get if the enemy is front of the player
if (((TargetActor->GetActorLocation() - PlayerViewLocation) | PlayerViewRotation.Vector()) > 0)
*/
#endif
}
FVector AMBCG_EnemyAttackPredictManager::GetStraightforwardPredictedPawnLocation(const APawn* Pawn, const float DeltaTimeAhead) const
{
if (!Pawn)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function GetStraightforwardPredictedPawnLocation(): Pawn argument is unexpectedly nullptr.");
return FVector::ZeroVector;
}
FVector CurrentLocation = Pawn->GetActorLocation();
const UCharacterMovementComponent* MovementComponent = Cast<UCharacterMovementComponent>(Pawn->GetMovementComponent());
if (!MovementComponent)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function GetStraightforwardPredictedPawnLocation(): Pawn argument is unexpectedly nullptr");
return CurrentLocation;
}
// Get the pawn's current velocity (Speed and Direction)
FVector Velocity = MovementComponent->Velocity;
// Calculate the predicted location
FVector PredictedLocation = CurrentLocation + (Velocity * DeltaTimeAhead);
return PredictedLocation;
}
bool AMBCG_EnemyAttackPredictManager::IsStaticObstacleOnTraceLine(const FVector& StartTrace, const FVector& EndTrace, const TArray<AActor*>& ActorsToIgnore, /*out*/ FHitResult& HitResult) const
{
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(IsStaticObstacleOnTraceLine), /*bTraceComplex=*/false);
TraceParams.bReturnPhysicalMaterial = false;
AddTraceIgnore(TraceParams, ActorsToIgnore);
const ECollisionChannel TraceChannel = ECollisionChannel::ECC_WorldStatic;
if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, TraceChannel, TraceParams))
{
// line of the fire is obstructed
return true;
}
return false;
}
FVector AMBCG_EnemyAttackPredictManager::GetPredictedPawnLocation(const APawn* Pawn, const float DeltaTimeAhead) const
{
if (!Pawn)
{
UE_LOGFMT(LogAMBCG_EnemyAttackPredictManager, Warning, "Function GetPredictedPawnLocation(): Pawn argument is unexpectedly nullptr");
return FVector::ZeroVector;
}
FVector PredictedLocation = GetStraightforwardPredictedPawnLocation(Pawn, DeltaTimeAhead);
// Set PredictedLocation as the static obstacle on the linetrace between current pawn location and its PredictedLocation, ignoring the Pawn
TArray<AActor*> AttachedActors;
Pawn->GetAttachedActors(/*out*/ AttachedActors);
AttachedActors.AddUnique(const_cast<AActor*>(static_cast<const AActor*>(Pawn)));
FHitResult HitResult;
if (IsStaticObstacleOnTraceLine(/*StartTrace*/ Pawn->GetActorLocation(), /*EndTrace*/ PredictedLocation, /*ActorsToIgnore*/ AttachedActors, /*out*/ HitResult))
{
// static obstacle's location
PredictedLocation = HitResult.Location;
}
return PredictedLocation;
}
const TMap<FName, FVector>& AMBCG_EnemyAttackPredictManager::GetCachedHeroSocketsRelativeLocations() const
{
return CachedHeroSocketsRelativeLocations;
}
bool AMBCG_EnemyAttackPredictManager::GetCachedHeroSingleSocketRelativeLocation(const FName Socket, /*out*/ FVector& OutRelativeSocketLocation) const
{
// Check if the key (Socket) exists in the map
if (const FVector* FoundLocation = CachedHeroSocketsRelativeLocations.Find(Socket))
{
OutRelativeSocketLocation = *FoundLocation;
return true;
}
// Socket not found
OutRelativeSocketLocation = FVector::ZeroVector;
return false;
}
void AMBCG_EnemyAttackPredictManager::SetCachedHeroSingleSocketRelativeLocation(const FName Socket, const FVector& RelativeSocketLocation)
{
if (FVector* FoundLocation = CachedHeroSocketsRelativeLocations.Find(Socket))
{
// overwrite existing value
*FoundLocation = RelativeSocketLocation;
}
else
{
// add key-value pair
CachedHeroSocketsRelativeLocations.Add(Socket, RelativeSocketLocation);
}
}
void AMBCG_EnemyAttackPredictManager::RegisterAttackThreat(/*out*/ bool& bOutShouldAlertPlayer)
{
bOutShouldAlertPlayer = false;
float CurrentAttackThreatTimeSeconds = GetWorld()->GetTimeSeconds();
if ((CurrentAttackThreatTimeSeconds - LastRegisteredAttackThreatTimeSeconds) > AlertThresholdSeconds)
{
bOutShouldAlertPlayer = true;
}
LastRegisteredAttackThreatTimeSeconds = CurrentAttackThreatTimeSeconds;
}