-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuafinding.lua
222 lines (184 loc) · 6.46 KB
/
Luafinding.lua
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
-- // https://github.com/GlorifiedPig/Luafinding
local Heap = {}
Heap.__index = Heap
local function findLowest( a, b )
return a < b
end
local function newHeap( template, compare )
return setmetatable( {
Data = {},
Compare = compare or findLowest,
Size = 0
}, template )
end
local function sortUp( heap, index )
if index <= 1 then return end
local pIndex = index % 2 == 0 and index / 2 or ( index - 1 ) / 2
if not heap.Compare( heap.Data[pIndex], heap.Data[index] ) then
heap.Data[pIndex], heap.Data[index] = heap.Data[index], heap.Data[pIndex]
sortUp( heap, pIndex )
end
end
local function sortDown( heap, index )
local leftIndex, rightIndex, minIndex
leftIndex = index * 2
rightIndex = leftIndex + 1
if rightIndex > heap.Size then
if leftIndex > heap.Size then return
else minIndex = leftIndex end
else
if heap.Compare( heap.Data[leftIndex], heap.Data[rightIndex] ) then minIndex = leftIndex
else minIndex = rightIndex end
end
if not heap.Compare( heap.Data[index], heap.Data[minIndex] ) then
heap.Data[index], heap.Data[minIndex] = heap.Data[minIndex], heap.Data[index]
sortDown( heap, minIndex )
end
end
function Heap:Empty()
return self.Size == 0
end
function Heap:Clear()
self.Data, self.Size, self.Compare = {}, 0, self.Compare or findLowest
return self
end
function Heap:Push( item )
if item then
self.Size = self.Size + 1
self.Data[self.Size] = item
sortUp( self, self.Size )
end
return self
end
function Heap:Pop()
local root
if self.Size > 0 then
root = self.Data[1]
self.Data[1] = self.Data[self.Size]
self.Data[self.Size] = nil
self.Size = self.Size - 1
if self.Size > 1 then
sortDown( self, 1 )
end
end
return root
end
local Heap = setmetatable( Heap, { __call = function( self, ... ) return newHeap( self, ... ) end } )
local Vector = Vector2.new
local Luafinding = {}
Luafinding.__index = Luafinding
-- This instantiates a new Luafinding class for usage later.
-- "start" and "finish" should both be 2 dimensional vectors, or just a table with "x" and "y" keys. See the note at the top of this file.
-- positionOpenCheck can be a function or a table.
-- If it's a function it must have a return value of true or false depending on whether or not the position is open.
-- If it's a table it should simply be a table of values such as "pos[x][y] = true".
function Luafinding:Initialize( start, finish, positionOpenCheck )
local newPath = setmetatable( { Start = start, Finish = finish, PositionOpenCheck = positionOpenCheck }, Luafinding )
newPath:CalculatePath()
return newPath
end
local function distance( start, finish )
local dx = start.x - finish.x
local dy = start.y - finish.y
return dx * dx + dy * dy
end
local positionIsOpen
local function positionIsOpenTable( pos, check ) return check[pos.x] and check[pos.x][pos.y] end
local function positionIsOpenCustom( pos, check ) return check( pos ) end
local adjacentPositions = {
Vector( 0, -1 ),
Vector( -1, 0 ),
Vector( 0, 1 ),
Vector( 1, 0 ),
Vector( -1, -1 ),
Vector( 1, -1 ),
Vector( -1, 1 ),
Vector( 1, 1 )
}
local function fetchOpenAdjacentNodes( pos, positionOpenCheck )
local result = {}
for i = 1, #adjacentPositions do
local adjacentPos = pos + adjacentPositions[i]
if positionIsOpen( adjacentPos, positionOpenCheck ) then
table.insert( result, adjacentPos )
end
end
return result
end
-- This is the function used to actually calculate the path.
-- It returns the calcated path table, or nil if it cannot find a path.
function Luafinding:CalculatePath()
local start, finish, positionOpenCheck = self.Start, self.Finish, self.PositionOpenCheck
if not positionOpenCheck then return end
positionIsOpen = type( positionOpenCheck ) == "table" and positionIsOpenTable or positionIsOpenCustom
if not positionIsOpen( finish, positionOpenCheck ) then return end
local open, closed = Heap(), {}
start.gScore = 0
start.hScore = distance( start, finish )
start.fScore = start.hScore
open.Compare = function( a, b )
return a.fScore < b.fScore
end
open:Push( start )
while not open:Empty() do
local current = open:Pop()
local currentId = current:ID()
if not closed[currentId] then
if current == finish then
local path = {}
while true do
if current.previous then
table.insert( path, 1, current )
current = current.previous
else
table.insert( path, 1, start )
self.Path = path
return path
end
end
end
closed[currentId] = true
local adjacents = fetchOpenAdjacentNodes( current, positionOpenCheck )
for i = 1, #adjacents do
local adjacent = adjacents[i]
if not closed[adjacent:ID()] then
local added_gScore = current.gScore + distance( current, adjacent )
if not adjacent.gScore or added_gScore < adjacent.gScore then
adjacent.gScore = added_gScore
if not adjacent.hScore then
adjacent.hScore = distance( adjacent, finish )
end
adjacent.fScore = added_gScore + adjacent.hScore
open:Push( adjacent )
adjacent.previous = current
end
end
end
end
end
end
function Luafinding:GetPath()
return self.Path
end
function Luafinding:GetDistance()
local path = self.Path
if not path then return end
return distance( path[1], path[#path] )
end
function Luafinding:GetTiles()
local path = self.Path
if not path then return end
return #path
end
function Luafinding:__tostring()
local path = self.Path
local string = ""
if path then
for k, v in ipairs( path ) do
local formatted = ( k .. ": " .. v )
string = k == 1 and formatted or string .. "\n" .. formatted
end
end
return string
end
return setmetatable( Luafinding, { __call = function( self, ... ) return self:Initialize( ... ) end } )