--[[
A*寻路算法,目前只适用于 01图, 0可通行, 1不可通行
--]]
-- 行走的4个方向
local four_dir = {
{1, 0},
{0, 1},
{0, -1},
{-1, 0},
}
-- 行走的8个方向
local eight_dir = {
{1, 1},
{1, 0},
{1, -1},
{0, 1},
{0, -1},
{-1, 1},
{-1, 0},
{-1, -1}
}
local AStar = {}
-- 地图、起始点、终点
AStar.init = function(self, map, startPoint, endPoint, four_dir)
self.startPoint = startPoint
self.endPoint = endPoint
self.map = map
self.cost = 10 -- 单位花费
self.diag = 1.4 -- 对角线长, 根号2 一位小数
self.open_list = {}
self.close_list = {}
self.mapRows = #map
self.mapCols = #map[1]
self.four_dir = four_dir -- 使用4方向的寻路还是八方向的寻路
end
-- 搜索路径 ,核心代码
AStar.searchPath = function(self)
-- 验证终点是否为阻挡,如果为阻挡,则直接返回空路径
if self.map[self.endPoint.row][self.endPoint.col] ~= 0 then
Logger.info("(", self.endPoint.row, ",", self.endPoint.col, ") 是阻挡!!!无法寻路")
return nil
end
-- 把第一节点加入 open_list中
local startNode = {}
startNo