I'm trying to remove some drudgery: https://merveilles.town/@akkartik/112086504775924524
FKENDSMEJXEZPAT6K5TYJC2KZBVYKMXA7LN7ZVPZMYIBODWIB64AC
function find_path(level, src, dst)
local done = initialize_array(false, lh, lw)
local cands = {}
table.insert(cands, {x=src.x, y=src.y})
while #cands > 0 do
local cand = table.remove(cands, 1)
local x,y = cand.x, cand.y
if x == dst.x and y == dst.y then return cand end
if y >= 1 and y <= lh and x >= 1 and x <= lw and not done[y][x] then
done[y][x] = true
local curr = level[y][x]
if curr ~= CELL_WALL and curr ~= CELL_CRATE and curr ~= CELL_CRATE_ON_TARGET then
table.insert(cands, {x=x-1, y=y, prev=cand})
table.insert(cands, {x=x+1, y=y, prev=cand})
table.insert(cands, {x=x, y=y-1, prev=cand})
table.insert(cands, {x=x, y=y+1, prev=cand})
end end end end
function unwind_path(c)
local result = {}
while c do
table.insert(result, {x=c.x, y=c.y})
c = c.prev
end
return result
end
function draw_path(path)
color(1,0,0)
for _,cell in ipairs(path) do
rect('line', left+(cell.x-1)*side, top+(cell.y-1)*side, side, side)
end end
function initialize_array(val, dim, ...)
local result = {}
local other_dims = {...}
if #other_dims == 0 then
for i=1,dim do
table.insert(result, val)
end
else
for i=1,dim do
table.insert(result, initialize_array(val, ...))
end
end
return result
end