A*寻路算法 lua

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值