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); }
// 使用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); }
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); }
// Update is called once per frame void Update() { if (SearchTest) { SearchTest = false; //if(actor.actorType == ActorType.AT_Monster) { Hexagon.PathFinder.StaticWeightScale = StaticWeightScale; Hexagon.PathFinder.DynamicWeightScale = DynamicWeightScale; Hexagon.PathFinder.StraightWeightScale = StraightWeightScale; Hexagon.PathFinder.HeuristicWeightScale = HeuristicWeightScale; Hexagon.PathFinder.MiddlePointWeight = UseMiddlePointWeight; foreach (GameObject o in lstObject) { GameObject.Destroy(o); } lstObject.Clear(); sdMainChar mc = sdGameLevel.instance.mainChar; sdGameActor actor = GetComponent <sdGameActor>(); if (actor != null) { actor.UnInject(false); } mc.UnInject(false); List <Hexagon.SearchNode> lstSearch = new List <Hexagon.SearchNode>(); BT.BinaryTree lstUnSearch = new BT.BinaryTree(); Hexagon.Manager.GetSingleton().DebugFindPath(transform.position, mc.transform.position, ref lstSearch, ref lstUnSearch); mc.Inject(false); if (actor != null) { actor.Inject(false); } Hexagon.SearchNode end = lstSearch[lstSearch.Count - 1]; for (int i = lstSearch.Count - 1; i >= 0; i--) { Hexagon.SearchNode n = lstSearch[i]; int type = 0; if (n == end) { type = 2; end = n.parent; } ushort tempHeight = Hexagon.Manager.GetSingleton().GetHeight(n.v); Add(HexagonElement.NewElement(n.v, tempHeight, n.weight(), type)); } Hexagon.SearchNode head = (Hexagon.SearchNode)lstUnSearch.Pop(); while (true) { if (head == null) { break; } ushort tempHeight = Hexagon.Manager.GetSingleton().GetHeight(head.v); Add(HexagonElement.NewElement(head.v, tempHeight, head.weight(), 1)); head = (Hexagon.SearchNode)lstUnSearch.Pop(); } } } }