-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinder.cpp
More file actions
180 lines (113 loc) · 4.43 KB
/
Copy pathPathfinder.cpp
File metadata and controls
180 lines (113 loc) · 4.43 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
// Fill out your copyright notice in the Description page of Project Settings.
#include "Pathfinder.h"
#include "APlatform.h"
#include <queue>
#include <unordered_map>
// Sets default values for this component's properties
UPathfinder::UPathfinder()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UPathfinder::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UPathfinder::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
void UPathfinder::UpdatePlatformMap(TArray<APlatform*> _RoomPlatforms)
{
// RoomPlatforms = _RoomPlatforms;
PlatformDistanceToPlatform.clear();
RoomPlatforms.clear();
// UE_LOG(LogTemp, Error, TEXT("RoomPlatforms %s"), *GetOwner()->GetName());
// UE_LOG(LogTemp, Error, TEXT("RoomPlatforms %d"), _RoomPlatforms.Num());
//for (int i = 0; i < RoomPlatforms.Num(); i++) {
// PlatformDistanceToPlatform.AddZeroed();
//
//}
if (_RoomPlatforms.Num() > 1) {
PlatformDistanceToPlatform.resize(_RoomPlatforms.Num());
for (int i = 0; i < _RoomPlatforms.Num(); i++) {
RoomPlatforms[_RoomPlatforms[i]->PlatformID] = _RoomPlatforms[i];
PlatformDistanceToPlatform[i].resize(_RoomPlatforms.Num());
}
for (std::pair<int, APlatform*> Platform : RoomPlatforms) {
for (FNeighbor Neighbor : Platform.second->Neighbors) {
//Distance from platforms
PlatformDistanceToPlatform[Platform.second->PlatformID][Neighbor.PlatformID] = FVector::Distance(RoomPlatforms[Neighbor.PlatformID]->GetActorLocation(), Platform.second->GetActorLocation());
}
}
}
}
TArray<int> UPathfinder::FindPath(int From, int To) {
TArray<int> Path;
if (From == To) {
return Path;
}
//list to search
TArray<APlatform*> OpenList;
//from platform to platform
std::unordered_map<int, int> CameFrom;
//platform, distance to goal
TArray<float> PlatformTotalDistance;
//platform distance to goal
TArray<float> PlatformDistanceToGoal;
for (int i = 0; i < RoomPlatforms.size(); i++) {
PlatformTotalDistance.Add(-1.0f);
PlatformDistanceToGoal.Add(-1.0f);
OpenList.Add(NULL);
}
for (std::pair<int, APlatform*> Platform : RoomPlatforms) {
OpenList[Platform.second->PlatformID] = Platform.second;
PlatformDistanceToGoal[Platform.second->PlatformID] = FVector::Distance(RoomPlatforms[Platform.second->PlatformID]->GetActorLocation(), RoomPlatforms[To]->GetActorLocation());
}
//searched list
std::vector<bool> ClosedList;
ClosedList.resize(RoomPlatforms.size(), false);
//the smaller distance travelled
float SmallestDistanceTraveled = 999999.0f;
int CurrentPlatform = From;
if (RoomPlatforms.size() > 0) {
PlatformTotalDistance[From] = PlatformDistanceToGoal[From];
// Distance - item
std::priority_queue<std::pair<float, int>, std::vector<std::pair<float, int>>, std::greater<std::pair<float, int>>> PathQueue;
PathQueue.push(std::make_pair(PlatformDistanceToGoal[From], From));
while (!PathQueue.empty()) {
CurrentPlatform = PathQueue.top().second;
UE_LOG(LogTemp, Error, TEXT("RoomPlatforms %d"), PathQueue.top().second);
PathQueue.pop();
ClosedList[CurrentPlatform] = true;
if (CurrentPlatform == To) {
break;
}
for (FNeighbor Neighbor : RoomPlatforms[CurrentPlatform]->Neighbors) {
float NewDistance = PlatformTotalDistance[CurrentPlatform] + PlatformDistanceToPlatform[CurrentPlatform][Neighbor.PlatformID] + PlatformDistanceToGoal[Neighbor.PlatformID];
if (NewDistance < PlatformTotalDistance[Neighbor.PlatformID] || PlatformTotalDistance[Neighbor.PlatformID] == -1.0f) {
CameFrom[Neighbor.PlatformID] = CurrentPlatform;
PlatformTotalDistance[Neighbor.PlatformID] = NewDistance;
}
if (!ClosedList[Neighbor.PlatformID]) {
PathQueue.push(std::make_pair(PlatformTotalDistance[Neighbor.PlatformID], Neighbor.PlatformID));
}
}
}
int iterator = 1;
Path.Add(To);
int i = CameFrom[To];
Path.Insert(i, 0);
while (i != From) {
i = CameFrom[i];
Path.Insert(i, 0);
}
}
return Path;
}