-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
288 lines (256 loc) · 7.2 KB
/
main.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
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
function reset()
nextNode = startNode
openList = {}
closedList = {}
table.insert(closedList, startNode)
startAdj = checkWalkable(getAdjacent(startNode.x, startNode.y, startNode))
addToOpenList(startAdj)
time = 0
complete = false
status = "Seaching"
end
function setStartPoint(newX, newY)
startNode = { x = newX, y = newY, g = 0}
reset()
end
function setEndPoint(newX, newY)
endNode = { x = newX, y = newY, g = 0}
reset()
end
function drawMap()
for i, row in ipairs(map) do
for j, col in ipairs(row) do
if col == 1 then
local x = 32 * j
local y = 32 * i
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", x, y, 32, 32)
end
if col == 0 then
local x = 32 * j
local y = 32 * i
love.graphics.setColor(0, 255, 0, 125)
love.graphics.rectangle("line", x, y, 32, 32)
end
end
end
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", startNode.x * 32, startNode.y * 32, 32, 32)
love.graphics.setColor(255, 127, 0)
love.graphics.rectangle("fill", endNode.x * 32, endNode.y * 32, 32, 32)
end
function getAdjacent(x, y, parent)
local adj = {}
if map[y-1][x] == 0 then
if map[y][x-1] == 0 then
adj.topLeft = { x = x - 1, y = y - 1, g = 14 + parent.g, parent = parent}
end
if map[y][x+1] == 0 then
adj.topRight = { x = x + 1, y = y - 1, g = 14 + parent.g, parent = parent}
end
end
if map[y+1][x] == 0 then
if map[y][x-1] == 0 then
adj.bottomLeft = { x = x - 1, y = y + 1, g = 14 + parent.g, parent = parent}
end
if map[y][x+1] == 0 then
adj.bottomRight = { x = x + 1, y = y + 1, g = 14 + parent.g, parent = parent}
end
end
adj.up = { x = x, y = y - 1, g = 10 + parent.g, parent = parent}
adj.left = { x = x - 1, y = y, g = 10 + parent.g, parent = parent}
adj.right = { x = x + 1, y = y, g = 10 + parent.g, parent = parent}
adj.down = { x = x, y = y + 1, g = 10 + parent.g, parent = parent}
return adj
end
function getAdjacentInOpen(currentNode)
local adj = getAdjacent(currentNode.x, currentNode.y, currentNode)
local adjInOpen = {}
for _, adjacent in pairs(adj) do
for _, node in ipairs(openList) do
if adjacent.x == node.x and adjacent.y == node.y then
table.insert(adjInOpen, node)
end
end
end
return adjInOpen
end
function checkIfShorter(currentNode, adjacent)
local adj = getAdjacent(currentNode.x, currentNode.y, currentNode)
local adjInOpen = getAdjacentInOpen(currentNode)
if #adjInOpen > 0 then
for _, openAdjacent in ipairs(adjInOpen) do
for _, newAdjacent in ipairs(adj) do
if (openAdjacent.x == newAdjacent.x and openAdjacent.y == newAdjacent.y) then
local currentG = openAdjacent.g
local newG = newAdjacent.g
if newG < currentG then
adjacent.parent = currentNode
adjacent.g = newG
end
end
end
end
end
end
function checkWalkable(nodes)
local walkable = {}
for _, node in pairs(nodes) do
if map[node.y][node.x] == 0 then
table.insert(walkable, node)
end
end
return walkable
end
function addToOpenList(newNodes)
for _, newNode in pairs(newNodes) do
alreadyInList = false
for _, node in ipairs(openList) do
if node.x == newNode.x and node.y == newNode.y then
alreadyInList = true
end
for _, closedNode in ipairs(closedList) do
if newNode.x == closedNode.x and newNode.y == closedNode.y then
alreadyInList = true
end
end
end
if not alreadyInList then
table.insert(openList, newNode)
end
end
end
function drawOpenList()
for _, node in ipairs(openList) do
love.graphics.setColor(0, 0, 125)
love.graphics.rectangle("fill", node.x * 32, node.y * 32, 32, 32)
love.graphics.setColor(255, 255, 255)
-- love.graphics.print(node.parent.x..","..node.parent.y, (node.x * 32) + 3, (node.y * 32) + 3)
love.graphics.print(getF(node)..","..node.g, (node.x * 32) + 3, (node.y * 32) + 13)
-- love.graphics.print(node.x..","..node.y, (node.x * 32) + 3, (node.y * 32) + 3)
end
love.graphics.setColor(255, 255, 255)
end
function drawClosedList()
for _, node in ipairs(closedList) do
love.graphics.setColor(125, 0, 125)
love.graphics.rectangle("fill", node.x * 32, node.y * 32, 32, 32)
love.graphics.setColor(255, 255, 255)
if node.parent then
love.graphics.print(node.parent.x..","..node.parent.y, (node.x * 32) + 3, (node.y * 32) + 3)
love.graphics.print(getF(node)..","..node.g, (node.x * 32) + 3, (node.y * 32) + 13)
end
end
love.graphics.setColor(255, 255, 255)
end
function getH(node)
local xDifference = 10 * ( math.abs(node.x - endNode.x))
local yDifference = 10 * ( math.abs(node.y - endNode.y))
return xDifference + yDifference
end
function getF(node)
return node.g + getH(node)
end
function findNextNode()
local lowestF = 999999999
local nextNode = nil
local openIndex = nil
for i, node in ipairs(openList) do
if getF(node) <= lowestF then
lowestF = getF(node)
nextNode = node
openIndex = i
end
end
return nextNode, openIndex
end
function drawPath()
love.graphics.setColor(255, 0, 255)
-- love.graphics.rectangle("fill", nextNode.x * 32, nextNode.y * 32, 32, 32)
local nodeParent = nextNode.parent
while not (nodeParent.x == startNode.x and nodeParent.y == startNode.y) do
love.graphics.rectangle("fill", nodeParent.x * 32, nodeParent.y * 32, 32, 32)
nodeParent = nodeParent.parent
end
end
function love.update(dt)
-- time = time + dt
-- if time > 0.000005 then
-- time = time - 1
if not complete then
if not (nextNode.x == endNode.x and nextNode.y == endNode.y) then
nextNode, openIndex = findNextNode()
table.insert(closedList, nextNode)
table.remove(openList, openIndex)
if #openList == 0 then
status = "Not found"
complete = true
end
newAdj = checkWalkable(getAdjacent(nextNode.x, nextNode.y, nextNode))
addToOpenList(newAdj)
for _, openNode in ipairs(openList) do
checkIfShorter(nextNode)
end
else
status = "Found"
complete = true
end
end
-- end
end
function love.load()
font = love.graphics.newFont(8)
love.graphics.setFont(font)
love.window.setMode(384, 352)
love.window.setTitle("Pathfinding")
map = require("map")
startNode = { x = 4, y = 5, g = 0}
endNode = { x = 8, y = 5 }
newX, newY = 0, 0
mouseX = 0
reset()
end
function love.keypressed(key)
if key == "r" then
reset()
end
end
function love.mousepressed(x, y, button)
if button == "l" then
mouseX = x
newX = math.floor(x / 32)
newY = math.floor(y / 32)
if newY <= #map and newX <= #map[1] and newX > 0 and newY > 0 and not (newX == endNode.x and newY == endNode.y) then
if map[newY][newX] == 0 then
setStartPoint(newX, newY)
reset()
end
end
end
if button == "r" then
mouseX = x
newX = math.floor(x / 32)
newY = math.floor(y / 32)
if newY <= #map and newX <= #map[1] and newX > 0 and newY > 0 and not (newX == startNode.x and newY == startNode.y) then
if map[newY][newX] == 0 then
setEndPoint(newX, newY)
reset()
end
end
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
drawMap()
love.graphics.setColor(125, 125, 125)
love.graphics.rectangle("fill", nextNode.x * 32, nextNode.y * 32, 32, 32)
drawOpenList()
drawClosedList()
love.graphics.print(mouseX, 10, 10)
-- love.graphics.print(status, 10, 10)
if complete then
-- drawOpenList()
-- drawClosedList()
drawPath()
end
end