public bool DebugAStar(Vector3 begin, Vector3 end, ref List <SearchNode> lstTempSearch, ref BT.BinaryTree lstUnSearch) { if (mgr == null) { return(false); } Coord vbegin = new Coord(); ushort beginheight = 0; Coord vend = new Coord(); ushort endheight = 0; if (!mgr.Position_Coord(begin, ref vbegin, ref beginheight) || !mgr.Position_Coord(end, ref vend, ref endheight)) { return(false); } SearchNode newNode = NewSearchNode(vbegin, end); lstUnSearch.Push(newNode.weight(), newNode); mgr.AddSearchMark(newNode.v); if (!WalkHoneycomb(vend, end, lstTempSearch, lstUnSearch)) { ClearSearchMaskOnly(lstTempSearch, lstUnSearch); return(false); } ClearSearchMaskOnly(lstTempSearch, lstUnSearch); return(true); }
bool WalkHoneycomb(Coord vend, Vector3 endPos, List <SearchNode> search, BT.BinaryTree unsearch) { int count = 0; while (true) { SearchNode node = (SearchNode)unsearch.Pop(); if (node == null) { return(false); } search.Add(node); if (node.v.Equal(vend)) { return(true); } ushort currentData = mgr.GetHeight(node.v); int current = currentData & Tile.HeightMask; Coord[] varray = node.v.Neighbour(); int newchild = 0; for (int i = 0; i < varray.Length; i++) { Coord c = varray[i]; if (!mgr.IsCoordValid(c)) { continue; } int layerCount = mgr.GetTileLayerCount(c); for (int layer = 0; layer < layerCount; layer++) { Coord newC = new Coord(); newC.x = c.x; newC.y = layer; newC.z = c.z; //c.y = layer; ushort by = mgr.GetHeight(newC); if ((by & Tile.Walkable) == 0) { continue; } if ((by & Tile.HasSearch) != 0) { continue; } //if (!newC.Equal(vend)) //{ // if ((by & Tile.HasPlayer) != 0) // { // continue; // } //} int height = by & Tile.HeightMask; float fHeightOffset = (height - current) * mgr.GetLayerHeight(); if (fHeightOffset > 0.5f || fHeightOffset < -0.5f) { continue; } SearchNode n = NewSearchNode(newC, endPos); n.parent = node; n.SetStepWeight(CalcWeight(n, endPos, currentData, by)); count++; newchild++; unsearch.Push(n.weight(), n); mgr.AddSearchMark(n.v); } } } return(false); }
// 使用A*算法计算最短路径aa public bool AStar(Vector3 begin, Vector3 end, out List <Vector3> path, bool OptimizePath) { path = null; if (mgr == null) { return(false); } Coord vbegin = new Coord(); ushort beginheight = 0; if (!mgr.Position_Coord_Radius(begin, ref vbegin, ref beginheight)) { return(false); } Coord vend = new Coord(); ushort endheight = 0; if (!mgr.Position_Coord_Radius(end, ref vend, ref endheight)) { return(false); } //List<SearchNode> lstUnSearch = new List<SearchNode>(); BT.BinaryTree lstUnSearch = new BT.BinaryTree(); SearchNode newNode = NewSearchNode(vbegin, end); lstUnSearch.Push(newNode.weight(), newNode); mgr.AddSearchMark(newNode.v); if (!WalkHoneycomb(vend, end, lstSearch, lstUnSearch)) { ClearSearchMask(lstSearch, lstUnSearch); return(false); } SearchNode node = lstSearch[lstSearch.Count - 1]; path = new List <Vector3>(); path.Insert(0, node.p); while (true) { if (node.parent == null) { break; } else { if (OptimizePath) { if (node.parent != null) { path.Insert(0, (node.p + node.parent.p) * 0.5f); } else { //path.Insert(0, node.p); } } else { path.Insert(0, node.p); } node = node.parent; } } ClearSearchMask(lstSearch, lstUnSearch); return(true); }