Skip to content

Commit b08d187

Browse files
committed
add
add
1 parent e901616 commit b08d187

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

Classes/Astar/luaAstar

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
--lua 实现的A*算法
2+
3+
Astar = {}
4+
5+
local MAXVAL = 100000000
6+
-- function Astar.init()
7+
8+
--A*搜索
9+
--搜索从(x1,y1)到(x2,y2)点在地图map上的路径
10+
function Astar.isCanMove(line,row)
11+
local minLine = MapMng.getMovedLine() + 1
12+
local maxLine = MapMng.getMovedLine() + 10
13+
local minRow = 1
14+
local maxRow = MAX_ROW
15+
if line >=minLine and line <= maxLine and row>= minRow and row <= maxRow then
16+
local gridState = MapMng.getGridState(line, row)
17+
if gridState == GRID_STATE_VIALID then
18+
return true
19+
else
20+
return false
21+
end
22+
end
23+
return false
24+
end
25+
26+
function Astar.search(x1,y1,x2,y2)
27+
--初始化a*算法
28+
local m_vis = {}
29+
local startPoint = {}
30+
startPoint.x = x1
31+
startPoint.y = y1
32+
startPoint.G = 0
33+
startPoint.H = (12-0)*10+(9-0)*14;
34+
startPoint.F = startPoint.G + startPoint.H
35+
local m_OpenList = {}
36+
local m_CloseList = {}
37+
38+
table.insert(m_OpenList,startPoint)
39+
--queue.pushBack(m_OpenList,startPoint)
40+
while table.getn(m_OpenList) > 0 do
41+
--1、寻找开席列表中F值最低的格子,我们称它为当前格
42+
local tempPoint = {}
43+
local tempKey = 1
44+
tempPoint.F = MAXVAL
45+
for k , v in pairs(m_OpenList) do
46+
if tempPoint.F > v.F then
47+
tempPoint = v
48+
tempKey = k
49+
end
50+
end
51+
52+
--2、把当前f值最低的格子切换到"关闭列表"
53+
table.insert(m_CloseList,tempPoint)
54+
if m_vis[ tempPoint.x ] == nil then
55+
m_vis[ tempPoint.x ] = {}
56+
end
57+
m_vis[ tempPoint.x ] [ tempPoint.y ]=true
58+
--3、从"开启列表"中删除当前f值最低的格子
59+
table.remove(m_OpenList,tempKey)
60+
61+
print('xxxxxxxxxxxx2',tempPoint.x,tempPoint.y)
62+
63+
--搜索方向
64+
local dx={0,0,-1,1}
65+
local dy={1,-1,0,0}
66+
67+
--检查它所有相邻并且可以到达 (障碍物和 "关闭列表" 的方格都不考虑) 的方格.
68+
--如果这些方格还不在 "开启列表" 里的话, 将它们加入 "开启列表",
69+
--计算这些方格的 G, H 和 F 值各是多少, 并设置它们的 "父方格" 为 C.
70+
local nowPoint = tempPoint
71+
for i=1 ,4 do
72+
local nextPoint = clone(nowPoint)
73+
nextPoint.x = nowPoint.x + dx[i]
74+
nextPoint.y = nowPoint.y + dy[i]
75+
--print('xxxxxxxxxxxxxxxx33',nextPoint.x,nextPoint.y,dx[i],dy[i])
76+
nextPoint.px = nowPoint.x
77+
nextPoint.py = nowPoint.y
78+
--如果是上下左右,G值为10,其他斜方向G值设为14
79+
nextPoint.G = nowPoint.G + 10
80+
nextPoint.H = (12-nextPoint.x)*10+(9-nextPoint.y)*14
81+
nextPoint.F = nextPoint.G + nextPoint.H
82+
83+
if m_vis[nextPoint.x] == nil then m_vis[nextPoint.x]= {} end
84+
--判断(是否越界)和(是否可以行走)和是(否在关闭列表中)
85+
if Astar.isCanMove(nextPoint.y,nextPoint.x) and (not m_vis[nextPoint.x][nextPoint.y]) then
86+
--如果当前搜索得到当节点a已经在开启列表中,说明之前的搜索已经产生了一条到达a点的路径。
87+
--那么我们需要看当前得到a点路径的g值 是否小于之前得到的路径的g值.
88+
--如果小的话,需要更改到达a点的路径(即将之前的路径从“开启列表”中移除,添加当前搜索得到的路径)
89+
local isInOpenList = false
90+
local tmpKey = nil
91+
local tmpValue = nil
92+
for k,v in pairs(m_OpenList) do
93+
if v.x == nextPoint.x and v.y ==nextPoint.y then
94+
isInOpenList = true
95+
tmpKey = k
96+
tmpValue = v
97+
break
98+
end
99+
end
100+
101+
if isInOpenList then
102+
if tmpValue.G > nextPoint.G then
103+
table.remove(m_OpenList,tmpKey)
104+
table.insert(m_OpenList,nextPoint)
105+
--print('xxxxxxxxxxxx3',nextPoint.x,nextPoint.y,x2,y2)
106+
end
107+
else
108+
--print('xxxxxxxxxxxx4',nextPoint.x,nextPoint.y,x2,y2,MapMng.getGridState(x, y))
109+
table.insert(m_OpenList,nextPoint)
110+
end
111+
112+
end
113+
------------------------------------------------------------------------
114+
end
115+
--判断终点是否已经在“开启列表”中了。
116+
local isFound = false
117+
for k,v in pairs(m_OpenList) do
118+
if v.x == x2 and v.y ==y2 then
119+
isFound = true
120+
print('路径已找到')
121+
end
122+
end
123+
124+
if isFound then break end
125+
end
126+
end
127+

0 commit comments

Comments
 (0)