function InitMap()
self.AMap = {}
for i = 1,10 do
self.AMap[i] = {}
for j = 1,10 do
local map = {}
map.x = i
map.y = j
map.g = 999
map.h = 0
map.f = 0
map.node = {x=0,y=0}
map.info = 0
self.AMap[i][j] = map
end
end
self:InitMapDate()
end
function InitMapDate()
self.starPos = {x=1,y=1}
self.endPos = {x=8,y=8}
self.toolCom = {
{x=0,y=1},{x=1,y=0},{x=-1,y=0},{x=0,y=-1},
{x=1,y=-1},{x=-1,y=1},{x=1,y=1},{x=-1,y=-1}
}
local mapObstacle = {{x=1,y=2},{x=2,y=2},{x=3,y=2},{x=4,y=2},{x=5,y=2},{x=6,y=2},{x=7,y=2},{x=8,y=2},{x=9,y=2},
{x=9,y=3},{x=9,y=4},{x=9,y=5},{x=9,y=6},{x=9,y=7},{x=9,y=8},{x=9,y=9}}
-- local mapObstacle = {{x=1,y=2},{x=2,y=2},{x=3,y=2},{x=5,y=2},{x=6,y=2},{x=7,y=2},{x=8,y=2},{x=9,y=2},
-- {x=9,y=3},{x=9,y=4},{x=9,y=5},{x=9,y=6},{x=9,y=7},{x=9,y=8},{x=9,y=9}}
for i,pos in ipairs(mapObstacle) do
self.AMap[pos.x][pos.y].info = 1
end
for i = 1,10 do
for j = 1,10 do
self.AMap[i][j].h = math.abs(self.endPos.x - i) + math.abs(self.endPos.y - j)
end
end
self:AStarFind()
end
function AStarFind()
self.mapOpenList = {}
self.mapCloseList = {}
local mapNode = self.AMap[self.starPos.x][self.starPos.y]
mapNode.g = 0
mapNode.h = math.abs(self.endPos.x - self.starPos.x) + math.abs(self.endPos.y - self.starPos.y)
mapNode.f = mapNode.g + mapNode.h
mapNode.node = {x=999,y=999}
table.insert(self.mapOpenList,mapNode)
--self:ShowMapDate()
self:RefreshMap(mapNode)
--print("======================================one")
--self:ShowMapDate()
local wayNodeList = {}
local wayNode = self.AMap[self.endPos.x][self.endPos.y]
table.insert(wayNodeList,wayNode)
while wayNode.x~=self.starPos.x or wayNode.y~=self.starPos.y do
local x,y = wayNode.node.x,wayNode.node.y
wayNode = self.AMap[x][y]
table.insert(wayNodeList,wayNode)
end
for i = 0,#wayNodeList-1 do
local node = wayNodeList[#wayNodeList - i]
print("node.x="..node.x.." node.y="..node.y)
end
end
function RefreshMap(node)
for i,pos in ipairs(self.toolCom) do
local nodePos = {x=node.x-pos.x, y=node.y-pos.y}
if nodePos.x>0 and nodePos.x<=10 and nodePos.y>0 and nodePos.y<=10 and
self.AMap[nodePos.x][nodePos.y].info ~= 1 then
local mapNode = self.AMap[nodePos.x][nodePos.y]
local value = 1
if math.abs(mapNode.x - node.x) > 0 and math.abs(mapNode.y - node.y)>0 then
value = 1.41
end
local flog = 0
for i,map in ipairs(self.mapOpenList) do
if map.x == mapNode.x and map.y == mapNode.y then
if node.g + value < map.g then
map.g = node.g + value
map.f = map.g + map.h
map.node.x = node.x
map.node.y = node.y
end
flog = 1
end
end
for i,map in ipairs(self.mapCloseList) do
if map.x == mapNode.x and map.y == mapNode.y then
flog = 1
end
end
if flog == 0 then
mapNode.g = node.g + value
mapNode.f = mapNode.g + mapNode.h
mapNode.node.x = node.x
mapNode.node.y = node.y
table.insert(self.mapOpenList,mapNode)
end
end
if nodePos.x==self.endPos.x and nodePos.y==self.endPos.y then
print("==========================到达目的地")
return
end
end
for i,map in ipairs(self.mapOpenList) do
if map.x == node.x and map.y == node.y then
table.remove(self.mapOpenList,i)
table.insert(self.mapCloseList,map)
break
end
end
local mapF = 999
local chooseNode = nil
for i,map in ipairs(self.mapOpenList) do
if mapF > map.f then
chooseNode = map
mapF = map.f
end
end
if chooseNode then
self:RefreshMap(chooseNode)
end
end
function ShowMapDate()
for i=1,10 do
for j=1,10 do
print("===========================")
print(" i="..i.." j="..j)
print("map.g="..self.AMap[i][j].g.." map.h="..self.AMap[i][j].h.." map.f="..self.AMap[i][j].f)
end
end
end