1、1 寻路成功0 寻路失败 无法到达4 已经到达终点5 目标被占6 寻路过程中发生错误Public Function fPath(arrMap() As fsPoint, arrPath() As Long, lPathCount As Long, pStartPoint As fsPoint, pGotoPoint As fsPoint, lWidth As Long, lHeight As Long) As Long 寻路 Dim i As Long 循环用变量 Dim Count As Long 点的序号或者总数 Dim pCurrentPoint As fsPoint Dim pSear
2、chPoint As fsPoint Dim pSearchX As Long, pSearchY As Long Dim tmpX As Long, tmpY As Long Dim arrXofs(8) As Long, arrYofs(8) As Long XY变化常量数组 Dim arrIofs(8) As Long 序号变化常量数组 Dim tmpOfs As Long 序号变化量On Error GoTo PathErr:判断地图 If lWidth 1 Or lHeight lHeight - 1 Or pGotoPoint.X lWidth - 1 Then fPath = 2
3、 If arrMap(TCount).B Or arrMap(TCount).D Then可以通过或者是门点 fPath = 0 Else 否则退出寻路 If arrMap(TCount).lNpcId 0 Then fPath = 5 If pGotoPoint.Y = pStartPoint.Y And pGotoPoint.X = pStartPoint.X Then fPath = 4设置常数数组 该数组决定了寻路的优先顺序 arrXofs(0) = -1 arrXofs(1) = 1 arrXofs(2) = -1 arrXofs(3) = 1 arrXofs(4) = -1 arr
4、Xofs(5) = 1 arrXofs(6) = 0 arrXofs(7) = 0 arrYofs(0) = -1 arrYofs(1) = 1 arrYofs(2) = 1 arrYofs(3) = -1 arrYofs(4) = 0 arrYofs(5) = 0 arrYofs(6) = -1 arrYofs(7) = 1 tmpOfs = 0 - lWidth arrIofs(0) = tmpOfs - 1 arrIofs(1) = lWidth + 1 arrIofs(2) = lWidth - 1 arrIofs(3) = tmpOfs + 1 arrIofs(4) = -1 arr
5、Iofs(5) = 1 arrIofs(6) = tmpOfs arrIofs(7) = lWidth初始化数据 Count = lWidth * lHeight 地图总点数 ReDim arrBap(Count)清空所有需要用到的列表 ReDim arrOpen(Count)起始点进入开放列表 Count = pStartPoint.Y * lWidth + pStartPoint.X pStartPoint.Index = Count tmpX = Abs(pStartPoint.X - pGotoPoint.X) tmpY = Abs(pStartPoint.Y - pGotoPoint
6、.Y) pStartPoint.G = 0 If tmpX tmpY Then pStartPoint.H = tmpX Else pStartPoint.H = tmpY pStartPoint.F = pStartPoint.G + pStartPoint.H arrBap(Count).Parent = 0 arrBap(Count).aIndex = 1 arrOpen(1) = pStartPoint lOpenSize = 1 Do While lOpenSize 0 pCurrentPoint = pBinaryHeapDelete() Count = pCurrentPoint
7、.Index arrBap(Count).bClose = True arrBap(Count).aIndex = 0 For i = 0 To 7 pSearchPoint.X = pCurrentPoint.X + arrXofs(i) pSearchPoint.Y = pCurrentPoint.Y + arrYofs(i) Count = pCurrentPoint.Index + arrIofs(i) pSearchPoint.Index = Count If Count = TCount Then 就是终点 arrBap(Count).Parent = pCurrentPoint.
8、Index保存路径 lPathCount = -1 Do While arrBap(Count).Parent lPathCount = lPathCount + 1 arrPath(lPathCount) = Count Count = arrBap(Count).Parent Loop起始点放入最后 arrPath(lPathCount + 1) = Count fPath = 1 If arrMap(Count).B And arrMap(Count).lNpcId = 0 Then 不是障碍点 If Not arrBap(Count).bClose Then 不在关闭列表 pSearc
9、hPoint.G = pCurrentPoint.G + 1 tmpX = Abs(pSearchPoint.X - pGotoPoint.X) tmpY = Abs(pSearchPoint.Y - pGotoPoint.Y) pSearchPoint.H = tmpX pSearchPoint.H = tmpY pSearchPoint.F = pSearchPoint.G + pSearchPoint.H If arrBap(Count).aIndex If pSearchPoint.F If arrOpen(lSelectIndex).F arrOpen(1) = arrOpen(lO
10、penSize + 1) lSelectIndex = 1 arrBap(arrOpen(lSelectIndex).Index).aIndex = lSelectIndex比较子节点 lLeftChildIndex = lSelectIndex * 2 lRightChildIndex = lLeftChildIndex + 1 If lLeftChildIndex lOpenSize Then没有左子节点 则结束 If lLeftChildIndex = lOpenSize Then 只有左子节点 If arrOpen(lSelectIndex).F arrOpen(lLeftChildI
11、ndex).F Then 父节点F值大 则交换 arrOpen(lSelectIndex) = arrOpen(lLeftChildIndex) arrOpen(lLeftChildIndex) = tmpPoint arrBap(arrOpen(lSelectIndex).Index).aIndex = lLeftChildIndex arrBap(arrOpen(lLeftChildIndex).Index).aIndex = lSelectIndex lSelectIndex = lLeftChildIndex父节点比子节点F值小 则结束 If lRightChildIndex = lO
12、penSize Then If arrOpen(lLeftChildIndex).F ReDim bytChar(iLen - 1) Get #2, 1, bytChar() Close #2 iMapHeight = Val(GetValue(Index_ strMsg, Height, fMapSet) iMapWidth = Val(GetValue(Width nMap(lMapDataID).lWidth = iMapWidth nMap(lMapDataID).lHeight = iMapHeight nMap(lMapDataID).lDataSize = iMapWidth * iMapHeight If nMap(lMapDataID).lDataSize i = CLng(brrData(1) j = CLng(brrData(0) iCount = iMapWidth * i + j nMap(lMapDataID).cMapData(iCount).D = True读取死点bFault读取活点bThrough Exit Sub
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1