private void AddAdjacentTilesToQueue(SearchPathNode current) { if (Map.Instance.IsInBounds(current.x, current.y + 1)) { SearchPathNode up = new SearchPathNode(current.x, current.y + 1, current); checkQueue.Enqueue(up); } if (Map.Instance.IsInBounds(current.x, current.y - 1)) { SearchPathNode down = new SearchPathNode(current.x, current.y - 1, current); checkQueue.Enqueue(down); } if (Map.Instance.IsInBounds(current.x - 1, current.y)) { SearchPathNode left = new SearchPathNode(current.x - 1, current.y, current); checkQueue.Enqueue(left); } if (Map.Instance.IsInBounds(current.x + 1, current.y)) { SearchPathNode right = new SearchPathNode(current.x + 1, current.y, current); checkQueue.Enqueue(right); } }
protected void AddFirstSearchPathNode(int nSceneWayInfoID) { m_nCurrentSearchPos = 0; if (m_aSearchPathNode[m_nCurrentSearchPos] == null) { m_aSearchPathNode[m_nCurrentSearchPos] = new SearchPathNode(); } else { m_aSearchPathNode[m_nCurrentSearchPos].Clear(); } m_aSearchPathD.Clear(); m_aSearchPathNode[m_nCurrentSearchPos].MarkUse(); //m_aSearchPathD.insert(std::make_pair(m_aSearchPathNode[m_nCurrentSearchPos].nWeight, m_nCurrentSearchPos)); ++m_nCurrentSearchPos; AddSearchPathNode(0, nSceneWayInfoID); }
// 删除原始数据 [8/3/2011 ivan edit] //public bool ClearOrgData(); //算法函数 //把某个场景的所有的路径加入到列表里面 protected int AddSearchPathNode(int nSearchPathNodeID, int nSceneWayInfoID) { //在已经使用了的范围内 if (nSearchPathNodeID >= 0 && nSearchPathNodeID < m_nCurrentSearchPos && IsValidSceneID(nSceneWayInfoID)) { //拷贝一份 SearchPathNode node = new SearchPathNode(); node.CopyOf(m_aSearchPathNode[nSearchPathNodeID]); //删除并加入到Close里面 RemoveFromOpenList(nSceneWayInfoID); AddToCloseList(nSceneWayInfoID); //八个方向 for (int i = 0; i < SceneWayInfo.MAX_WAY_ONE_SCENE; i++) { if (m_pSceneWayInfo[nSceneWayInfoID].nWayID[i] != -1) { node.AddPathID(m_pSceneWayInfo[nSceneWayInfoID].nWayID[i]); node.AddWeight(); if (m_nCurrentSearchPos < MAX_PATH_NODE) { SearchPathNode newNode = new SearchPathNode(); newNode.CopyOf(node); m_aSearchPathNode[m_nCurrentSearchPos] = newNode; } //加入到OpenList里 int wayID = GetSceneWayInfoIDFromSceneTransferDataID(m_pSceneWayInfo[nSceneWayInfoID].nWayID[i]); AddToOpenList(wayID); //添加到权重列表里,目的是为了排序,找到权重最小的就是第一个 AddSearchNode(node.nWeight, m_nCurrentSearchPos++); //删除刚才添加的,然后继续使用 node.RemoveLastAddID(); node.AddWeight(-1); } else { return(i); } } } return(0); }
public void CopyOf(SearchPathNode old) { Array.Copy(old.nPathIDArray, this.nPathIDArray, old.nPathIDArray.Length); this.nWeight = old.nWeight; this.nCurrentPos = old.nCurrentPos; }
void BreadthFirstFindBin() { path.Clear(); checkedTiles.Clear(); checkQueue.Clear(); // find my position int myX, myY; Map.Instance.GetClosestCoordinate(automaton.transform.position, out myX, out myY); // setup process SearchPathNode myPositionNode = new SearchPathNode(myX, myY, null); AddAdjacentTilesToQueue(myPositionNode); checkedTiles.Add(new Vector2(myX, myY)); SearchPathNode destination = null; while (checkQueue.Count > 0) { SearchPathNode current = checkQueue.Dequeue(); // been here before if (!checkedTiles.Add(new Vector2(current.x, current.y))) { continue; } // is it non-traversable? if (Map.Instance.GetTileBounds(current.x, current.y).HasValue) { continue; } // see if this location matches our criteria bool criteriaMet = false; List <SoilBin> adjacentBins = Map.Instance.GetAdjacentBins(current.x, current.y); for (int i = 0; i < adjacentBins.Count; i++) { if (MeetCriteria(adjacentBins[i])) { // yes, we're done criteriaMet = true; break; } } // are we finished looking? if (criteriaMet) { destination = current; break; } // keep going - add adjacent tiles to queue AddAdjacentTilesToQueue(current); } if (destination != null) { //build path to desintation while (destination.previous != null) { path.Push(Map.Instance.TileToWorldCoordinate(destination.x, destination.y)); destination = destination.previous; } PopNextLocation(); } else { // walk around or something } }
public SearchPathNode(int x, int y, SearchPathNode previous) { this.x = x; this.y = y; this.previous = previous; }