This repository was archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathNavGrid.cpp
More file actions
executable file
·467 lines (404 loc) · 12.9 KB
/
Copy pathNavGrid.cpp
File metadata and controls
executable file
·467 lines (404 loc) · 12.9 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Fill out your copyright notice in the Description page of Project Settings.
#include "NavGrid.h"
#include "AssetRegistryModule.h"
#include <limits>
DEFINE_LOG_CATEGORY(NavGrid);
TEnumAsByte<ECollisionChannel> ANavGrid::ECC_NavGridWalkable = ECollisionChannel::ECC_GameTraceChannel1;
FName ANavGrid::DisableVirtualTilesTag = "NavGrid:DisableVirtualTiles";
// Sets default values
ANavGrid::ANavGrid()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
TileClass = UNavTileComponent::StaticClass();
SceneComponent = CreateDefaultSubobject<USceneComponent>("RootComponent");
RootComponent = SceneComponent;
Cursor = CreateDefaultSubobject<UStaticMeshComponent>(FName("Cursor"));
Cursor->SetupAttachment(GetRootComponent());
Cursor->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
Cursor->ToggleVisibility(false);
auto HCRef = TEXT("StaticMesh'/NavGrid/SMesh/NavGrid_Cursor.NavGrid_Cursor'");
auto HCFinder = ConstructorHelpers::FObjectFinder<UStaticMesh>(HCRef);
if (HCFinder.Succeeded())
{
Cursor->SetStaticMesh(HCFinder.Object);
}
else
{
UE_LOG(NavGrid, Error, TEXT("Error loading %s"), HCRef);
}
AddHighlightType("Movable", TEXT("Material'/NavGrid/Materials/Movable_Mat.Movable_Mat'"));
AddHighlightType("Dangerous", TEXT("Material'/NavGrid/Materials/Dangerous_Mat.Dangerous_Mat'"));
AddHighlightType("Special", TEXT("Material'/NavGrid/Materials/Special_Mat.Special_Mat'"));
CurrentPawn = NULL;
CurrentTile = NULL;
}
void ANavGrid::SetTileHighlight(UNavTileComponent & Tile, FName Type)
{
Tile.SetHighlight(Type);
}
void ANavGrid::ClearTileHighlights()
{
for (auto &H : TileHighlights)
{
H.Value->ClearInstances();
}
}
void ANavGrid::AddHighlightType(const FName &Type, const TCHAR *FileName)
{
TileHighLightPaths.Add(Type, FileName);
}
UInstancedStaticMeshComponent * ANavGrid::GetHighlightComponent(FName Type)
{
/* build the instanced mesh component if we have not already done so */
if (!TileHighlights.Contains(Type) && TileHighLightPaths.Contains(Type))
{
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
UStaticMesh *Mesh = LoadObject<UStaticMesh>(this, TEXT("StaticMesh'/NavGrid/SMesh/NavGrid_TileHighlight.NavGrid_TileHighlight'"));
check(Mesh);
UMaterial *Material = LoadObject<UMaterial>(this, TileHighLightPaths[Type]);
check(Material);
auto *Comp = NewObject<UInstancedStaticMeshComponent>(this);
Comp->SetupAttachment(GetRootComponent());
Comp->SetStaticMesh(Mesh);
Comp->SetMaterial(0, Material);
Comp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Comp->RegisterComponent();
Comp->bOnlyOwnerSee = true;
TileHighlights.Add(Type, Comp);
}
/* we *should* now have the object we need*/
if (TileHighlights.Contains(Type))
{
return TileHighlights[Type];
}
else
{
return NULL;
}
}
ANavGrid *ANavGrid::GetNavGrid(AActor *ActorInWorld)
{
return GetNavGrid(ActorInWorld->GetWorld());
}
ANavGrid * ANavGrid::GetNavGrid(UWorld *World)
{
ANavGridGameState* GameState = World->GetGameState<ANavGridGameState>();
if (IsValid(GameState))
{
return GameState->GetNavGrid();
}
else
{
return nullptr;
}
}
UNavTileComponent *ANavGrid::GetTile(const FVector &WorldLocation, bool FindFloor/*= true*/, float UpwardTraceLength/* = 100*/, float DownwardTraceLength/* = 100*/)
{
return LineTraceTile(WorldLocation, FindFloor, UpwardTraceLength, DownwardTraceLength);
}
UNavTileComponent * ANavGrid::LineTraceTile(const FVector & WorldLocation, bool FindFloor, float UpwardTraceLength, float DownwardTraceLength)
{
UNavTileComponent *Result = nullptr;
if (FindFloor)
{
Result = LineTraceTile(WorldLocation + FVector(0, 0, UpwardTraceLength), WorldLocation - FVector(0, 0, DownwardTraceLength));
}
else
{
/* Do a bunch of horizontal line traces and pick the closest tile component*/
UNavTileComponent *Closest = nullptr;
static FVector EndPoints[8] = {
FVector(0, 200, 0),
FVector(200, 200, 0),
FVector(200, 0, 0),
FVector(200, -200, 0),
FVector(0, -200, 0),
FVector(-200, -200, 0),
FVector(-200, 0, 0),
FVector(-200, 200, 0)
};
for (FVector EndPoint : EndPoints)
{
UNavTileComponent *Candidate = LineTraceTile(WorldLocation - EndPoint, WorldLocation + EndPoint);
if (Candidate)
{
if (!Closest)
{
Closest = Candidate;
}
else if (FVector::Dist(Candidate->GetComponentLocation(), WorldLocation) < FVector::Dist(Closest->GetComponentLocation(), WorldLocation))
{
Closest = Candidate;
}
}
}
Result = Closest;
}
return Result;
}
UNavTileComponent *ANavGrid::LineTraceTile(const FVector &Start, const FVector &End)
{
TArray<FHitResult> HitResults;
FCollisionQueryParams CQP;
CQP.TraceTag = "NavGridTile";
GetWorld()->LineTraceMultiByChannel(HitResults, Start, End, ECC_NavGridWalkable, CQP);
if (HitResults.Num())
{
UPrimitiveComponent *Comp = HitResults[0].GetComponent();
return Cast<UNavTileComponent>(Comp);
}
else
{
return nullptr;
}
}
void ANavGrid::TileClicked(const UNavTileComponent *Tile)
{
OnTileClicked.Broadcast(Tile);
}
void ANavGrid::TileCursorOver(const UNavTileComponent *Tile)
{
OnTileCursorOver.Broadcast(Tile);
}
void ANavGrid::EndTileCursorOver(const UNavTileComponent *Tile)
{
OnEndTileCursorOver.Broadcast(Tile);
}
void ANavGrid::CalculateTilesInRange(AGridPawn *Pawn)
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_ANavGrid_CalculateTilesInRange);
ClearTiles();
if (EnableVirtualTiles)
{
GenerateVirtualTiles(Pawn);
}
UNavTileComponent *Current = Pawn->GetTile();
/* if we're not on the grid, the number of tiles in range is zero */
if (!Current)
{
return;
}
Current->Distance = 0;
TArray<UNavTileComponent *> NeighbouringTiles;
Current->GetUnobstructedNeighbours(*Pawn->MovementCollisionCapsule, NeighbouringTiles);
TArray<UNavTileComponent *> TentativeSet(NeighbouringTiles);
while (Current)
{
Current->GetUnobstructedNeighbours(*Pawn->MovementCollisionCapsule, NeighbouringTiles);
for (UNavTileComponent *N : NeighbouringTiles)
{
if (!N->Traversable(Pawn->MovementComponent->AvailableMovementModes))
{
continue;
}
if (!N->Visited)
{
float TentativeDistance = N->Cost + Current->Distance;
if (TentativeDistance <= N->Distance)
{
// Prioritize straight paths by using the world distance as a tiebreaker
// when TentativeDistance is equal N->Dinstance
float OldDistance = std::numeric_limits<float>::infinity();
float NewDistance = 0;
if (TentativeDistance == N->Distance)
{
NewDistance = (Current->GetComponentLocation() - N->GetComponentLocation()).Size();
if (N->Backpointer)
{
OldDistance = (N->Backpointer->GetComponentLocation() - N->GetComponentLocation()).Size();
}
}
if (NewDistance < OldDistance) // Always true if TentativeDistance < N->Distance
{
N->Distance = TentativeDistance;
N->Backpointer = Current;
if (TentativeDistance <= Pawn->MovementComponent->MovementRange)
{
TentativeSet.AddUnique(N);
}
}
}
}
}
Current->Visited = true;
TentativeSet.Remove(Current);
if (Current != Pawn->GetTile()) { TilesInRange.Add(Current); } // dont include the starting tile
if (TentativeSet.Num())
{
Current = TentativeSet[0];
}
else
{
Current = NULL;
}
}
}
void ANavGrid::GetTilesInRange(AGridPawn *Pawn, TArray<UNavTileComponent*>& OutTiles)
{
if (Pawn != CurrentPawn || Pawn->GetTile() != CurrentTile)
{
CalculateTilesInRange(Pawn);
CurrentPawn = Pawn;
CurrentTile = Pawn->GetTile();
}
OutTiles = TilesInRange;
}
void ANavGrid::ClearTiles()
{
TilesInRange.Empty();
TArray<UNavTileComponent *> AllTiles;
GetEveryTile(AllTiles, GetWorld());
for (auto *T : AllTiles)
{
T->Reset();
}
ClearTileHighlights();
NumPersistentTiles = AllTiles.Num() - VirtualTiles.Num();
}
bool ANavGrid::TraceTileLocation(const FVector & TraceStart, const FVector & TraceEnd, FVector & OutTilePos)
{
FCollisionQueryParams CQP;
CQP.bFindInitialOverlaps = true;
CQP.TraceTag = "NavGridTilePlacement";
FHitResult HitResult;
GetWorld()->LineTraceSingleByChannel(HitResult, TraceStart, TraceEnd, ECollisionChannel::ECC_Pawn, CQP);
bool bHasDisableTileTag = false;
AActor* HitActor = nullptr;
if (HitResult.IsValidBlockingHit())
{
HitActor = HitResult.GetActor();
}
if (HitActor)
{
bHasDisableTileTag = HitActor->ActorHasTag(DisableVirtualTilesTag);
}
OutTilePos = HitResult.ImpactPoint;
// return true if we hit the 'outside' of something that does not have the disabletile-tag
return HitResult.bBlockingHit && !HitResult.bStartPenetrating && !bHasDisableTileTag;
}
UNavTileComponent * ANavGrid::PlaceTile(const FVector & Location, AActor * TileOwner)
{
if (!TileOwner)
{
TileOwner = this;
}
UNavTileComponent *TileComp = NewObject<UNavTileComponent>(TileOwner, TileClass);
TileComp->SetupAttachment(TileOwner->GetRootComponent());
TileComp->SetWorldTransform(FTransform::Identity);
TileComp->SetWorldLocation(Location);
TileComp->SetBoxExtent(FVector(TileSize / 2, TileSize / 2, 5));
TileComp->RegisterComponentWithWorld(TileOwner->GetWorld());
TileComp->SetGrid(this);
return TileComp;
}
UNavTileComponent * ANavGrid::ConsiderPlaceTile(const FVector &TraceStart, const FVector &TraceEnd, AActor * TileOwner /*= NULL*/)
{
if (!TileOwner)
{
TileOwner = this;
}
FVector TileLocation;
bool FoundGoodLocation = TraceTileLocation(TraceStart, TraceEnd, TileLocation);
if (FoundGoodLocation)
{
// check if we a new tile will overlap any existing tiles
// use a mutlisweep as tiles returs overlap responses to this channel
TArray<FHitResult> HitResults;
FCollisionShape TileShape = FCollisionShape::MakeBox(FVector(TileSize / 3, TileSize / 3, 25));
GetWorld()->SweepMultiByChannel(HitResults, TileLocation, TileLocation - FVector(0, 0, 1), FQuat::Identity, ECC_NavGridWalkable, TileShape);
UNavTileComponent* ExistingTile = nullptr;
for (FHitResult& HitResult : HitResults)
{
if (IsValid(ExistingTile = Cast<UNavTileComponent>(HitResult.Component.Get())))
{
break;
}
}
if (!IsValid(ExistingTile))
{
return PlaceTile(TileLocation, TileOwner);
}
}
return nullptr;
}
FVector ANavGrid::AdjustToTileLocation(const FVector &Location)
{
UNavTileComponent *SnapTile = LineTraceTile(Location, true, 100, 100);
if (SnapTile)
{
return SnapTile->GetComponentLocation();
}
// try to position the pawn so that it matches a regular grid
// we do not change the vertical location
FVector Offset = Location - GetActorLocation();
int32 XRest = (int32)Offset.X % (int32)TileSize;
int32 YRest = (int32)Offset.Y % (int32)TileSize;
FVector AdjustedLocation = Location;
AdjustedLocation.X += (TileSize / 2) - XRest;
AdjustedLocation.Y += (TileSize / 2) - YRest;
return AdjustedLocation;
}
void ANavGrid::GenerateVirtualTiles(const AGridPawn *Pawn)
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_ANavGrid_GenerateVirtualTiles);
// only keep a reasonable number
if (VirtualTiles.Num() > MaxVirtualTiles)
{
UE_LOG(NavGrid, Log, TEXT("Limit reached (%i), removing all virtual tiles"), MaxVirtualTiles);
DestroyVirtualTiles();
}
GenerateVirtualTile(Pawn);
FVector Center = AdjustToTileLocation(Pawn->GetActorLocation());
FVector Min = Center - FVector(Pawn->MovementComponent->MovementRange * TileSize);
FVector Max = Center + FVector(Pawn->MovementComponent->MovementRange * TileSize);
for (float X = Min.X; X <= Max.X; X += TileSize)
{
for (float Y = Min.Y; Y <= Max.Y; Y += TileSize)
{
for (float Z = Max.Z; Z >= Min.Z; Z -= TileSize)
{
UNavTileComponent *TileComp = ConsiderPlaceTile(FVector(X, Y, Z + TileSize), FVector(X, Y, Z - 0.1));
if (TileComp)
{
VirtualTiles.Add(TileComp);
}
}
}
}
}
void ANavGrid::GenerateVirtualTile(const AGridPawn * Pawn)
{
FVector Location = AdjustToTileLocation(Pawn->GetActorLocation());
UNavTileComponent *TileComp = ConsiderPlaceTile(Location + FVector(0, 0, TileSize), Location - FVector(0, 0, 0.1));
if (TileComp)
{
VirtualTiles.Add(TileComp);
}
}
void ANavGrid::DestroyVirtualTiles()
{
for (UNavTileComponent *T : VirtualTiles)
{
if (IsValid(T))
{
T->DestroyComponent();
}
}
VirtualTiles.Empty();
}
void ANavGrid::Destroyed()
{
Super::Destroyed();
DestroyVirtualTiles();
}
void ANavGrid::GetEveryTile(TArray<UNavTileComponent *> &OutTiles, UWorld * World)
{
for (TObjectIterator<UNavTileComponent> Itr; Itr; ++Itr)
{
if (Itr->GetWorld() == World)
{
OutTiles.Add(*Itr);
}
}
}