用lua 实现的A*star 寻路算法

local AStar={}
function AStar:create(s_node,e_node,map_list)
local openlist={}
local closelist={}

local maplist=map_list

local startnode={x=s_node.x,y=s_node.y,g=0,h=0,f=0,prenode=nil}
local endnode={x=e_node.x,y=e_node.y}

if startnode.x==endnode.x and startnode.y==endnode.y then
  return {}
end
if maplist[endnode.x][endnode.y] > 0 then
  return {}
end



local isendnode=nil
local function setH(node)
  node['h']=math.abs(node['x']-endnode['x'])+math.abs(node['y']-endnode['y'])
end

local function setG(node)
  node['g']=node['prenode']['g']+1
end
local function setF(node)
  node['f']=node['g']+node['h']
end

local function GetNodeByPoint(node,x,y)

--这里超出的范围--可以根据自己的要求来改
  if x<1 or y<1 or x>7 or y>7 then
    return
  end

  if maplist[x][y]>0 then
    return
  end


  for key, var in ipairs(closelist) do
    if var.x==x and var.y==y then
      return
    end
  end

  local snode={}
  snode['x']=x
  snode['y']=y
  snode['prenode']=node
  setH(snode)
  setG(snode)
  setF(snode)

  for key, var in ipairs(openlist) do
    if var.x==x and var.y==y then
      if snode['f']>var['f'] then
        return
      end
    var['prenode']=node
    --setH(var)
    setG(var)
    setF(var)
    return
    end
  end

  openlist[#openlist+1]=snode
  if (snode['x'] == endnode['x']) and (snode['y']==endnode['y']) then
    isendnode=snode
  end

end

local function addfengopen(node)
  GetNodeByPoint(node, node['x'], node['y']+1)
  GetNodeByPoint(node, node['x'], node['y']-1)
  GetNodeByPoint(node, node['x']+1, node['y'])
  GetNodeByPoint(node, node['x']-1, node['y'])
end

local function findmin()
  local fmin=9999
  local node=nil
  local index=nil
  for key, var in ipairs(openlist) do
    if fmin>var['f'] then
      fmin=var['f']
      node=var
      index=key
    end
  end

  if node then
    table.remove(openlist,index)
    return node
  end
end

local function search()
  local currentnode=startnode
  currentnode['g']=1

  setH(currentnode)
  setF(currentnode)

  closelist[#closelist+1]=currentnode

  local xunluan=0
  while true do
    addfengopen(currentnode)

    currentnode=findmin()
    if not currentnode then
      return {}
  end
  closelist[#closelist+1]=currentnode

-- print('open list ...')
-- showtable1:printtable(openlist)
-- print('close list ...')
-- showtable1:printtable(closelist)

  if isendnode then
    print('tui chu',isendnode,xunluan)
    break
  end
  xunluan=xunluan+1
end

--找到了路径
  local apath={}
  while isendnode['prenode'] do
    apath[#apath+1]={x=isendnode['x'],y=isendnode['y']}
    isendnode=isendnode['prenode']
  end

  return apath
end

return search()
end

return AStar

转载于:https://www.cnblogs.com/wpcf/p/4049866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值