heroes-of-nerevelon/lib/pathfinder.lua
2025-08-13 01:47:19 +03:00

32 lines
749 B
Lua

--- @param cur Vec3
--- @param to Vec3
--- @param acc Vec3[]
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]
table.insert(acc, cur)
if cur == to then
return acc
end
return greedy_trace_step(next, to, acc)
end
--- @param from Vec3
--- @param to Vec3
local function trace(from, to)
return greedy_trace_step(from, to, {})
end
return trace