heroes-of-nerevelon/lib/pathfinder.lua

35 lines
818 B
Lua

local deque = require "lib.utils.deque"
--- @param cur Vec3
--- @param to Vec3
--- @param acc Deque
local function greedy_trace_step(cur, to, acc)
local lengthTable = {}
for x = -1, 1 do
for y = -1, 1 do
local point = Vec3 { cur.x + x, cur.y + y }
table.insert(lengthTable, { point, (point - to):length() })
end
end
local min = lengthTable[1]
for i = 2, #lengthTable do
if lengthTable[i][2] < min[2] then min = lengthTable[i] end
end
local next = min[1]
acc = acc:push_back(cur)
if cur == to then
return acc
end
return greedy_trace_step(next, to, acc)
end
--- @param from Vec3
--- @param to Vec3
--- @return Deque
local function trace(from, to)
return greedy_trace_step(from, to, deque.new())
end
return trace