public AStarSearch(int[,] occupiedGrid) { this.m_rctBounds = new ERectangle(0,0,occupiedGrid.GetLength(0)-1, occupiedGrid.GetLength(1)-1); _TheMap = new AStarNode[this.m_rctBounds.Width+1,this.m_rctBounds.Height+1]; for(int i=0; i<_TheMap.GetLength(0); i++) { for(int j=0; j<_TheMap.GetLength(1); j++) { AStarNode node = new AStarNode(i,j); _TheMap[i,j] = node; if (occupiedGrid[i,j]==0) node.Walkable = true; else node.Walkable = false; } } for (int i=0; i<8; i++) point[i] = new Point(); }
private bool IsInOpenList(AStarNode sender) { return m_listOpen.Contains(sender); }
private bool IsInClosedList(AStarNode sender) { return m_listClosed.Contains(sender); }
protected void UpdateCost(AStarNode box) { if (box.ParentNode!= null) { box.CostToStart = box.Cost + box.ParentNode.CostToStart; // if (box.ParentIsDiagonal) //TODO: this doesn't seem to help getting rid of diagonal loss.. // box.CostToStart+=0.4142f; } else box.CostToStart = 0; //Manhattan distance: box.CostToGoal = Math.Abs(GoalNode.Pos.X - box.Pos.X); box.CostToGoal += Math.Abs(GoalNode.Pos.Y - box.Pos.Y); box.TotalCost = box.CostToStart + box.CostToGoal + box.Cost; }
protected void Search() { while( m_listOpen.Count>0 && !this.m_bPaused) { m_nodeCurrent = PopCheapestNodeInOpenList(); //JB: if (m_nodeCurrent == GoalNode || (this._acceptableNodes!=null && this._acceptableNodes.Contains(m_nodeCurrent))) { if (this._acceptableNodes!=null && this._acceptableNodes.Contains(m_nodeCurrent)) { this.m_nodeGoal = this.m_nodeCurrent; } //FOUND THE SOLUTION! while(m_nodeCurrent != null) { m_listSolution.Insert(0,m_nodeCurrent); m_nodeCurrent = m_nodeCurrent.ParentNode; } this.m_bFoundSolution = true; //TODO: optimization: check for shortcuts - straight lines, as long as possible, between points on the path //so the final path is a number of points (that are not necessarily adjacent in the grid) if (SearchFinished!=null) SearchFinished(this, this.GoalNode.Pos); break; } GetSuccessorOfCurrentNode(); for (int i=0; i<SuccesorNode.Length; i++) { if (SuccesorNode[i]==null) continue; if (!m_listOpen.Contains(SuccesorNode[i]) && !m_listClosed.Contains(SuccesorNode[i])) { SuccesorNode[i].ParentNode = m_nodeCurrent; UpdateCost(SuccesorNode[i]); m_listOpen.Add(SuccesorNode[i]); } } if (m_nodeCurrent != StartNode && m_nodeCurrent != GoalNode) { if (SearchedCoordinate!=null) SearchedCoordinate(this, m_nodeCurrent.Pos); } m_listClosed.Add(m_nodeCurrent); } }
protected AStarNode PopCheapestNodeInOpenList() { float min=((AStarNode)m_listOpen[0]).TotalCost; m_nodeCurrent = (AStarNode)m_listOpen[0]; for (int i=1; i<m_listOpen.Count; i++) { if (((AStarNode)m_listOpen[i]).TotalCost < min) { min =((AStarNode)m_listOpen[i]).TotalCost; m_nodeCurrent = (AStarNode)m_listOpen[i]; } } m_listOpen.Remove(m_nodeCurrent); return m_nodeCurrent; }
public ArrayList BeginSearch() { this.m_bFoundSolution = false; if (this.m_nodeStart==null || this.m_nodeGoal==null) throw new Exception("Need start and goal!"); m_nodeStart.ParentNode = null; m_nodeCurrent = m_nodeStart; UpdateCost(m_nodeStart); ResetMap(); m_listOpen.Clear(); m_listClosed.Clear(); m_listSolution.Clear(); m_listOpen.Add(m_nodeStart); if (this.SearchStarted!=null) this.SearchStarted(this, this.StartPos); this.Search(); this._acceptableNodes = null; //don't reuse this for next search return this.Solution; }
private bool IsInOpenList(AStarNode sender) { return(m_listOpen.Contains(sender)); }
private bool IsInClosedList(AStarNode sender) { return(m_listClosed.Contains(sender)); }
public void Dispose() { _Parent = null; }