namespace Holo.Virtual.Rooms.Pathfinding { using System; using System.Collections; public sealed class AStar { private Heap FClosedList = new Heap(); private AStarNode FGoalNode; private Heap FOpenList = new Heap(); private ArrayList FSolution = new ArrayList(); private AStarNode FStartNode; private ArrayList FSuccessors = new ArrayList(); public void FindPath(AStarNode AStartNode, AStarNode AGoalNode) { this.FStartNode = AStartNode; this.FGoalNode = AGoalNode; this.FOpenList.Add(this.FStartNode); while (this.FOpenList.Count > 0) { AStarNode NodeCurrent = (AStarNode) this.FOpenList.Pop(); if (NodeCurrent.IsGoal()) { while (NodeCurrent != null) { this.FSolution.Insert(0, NodeCurrent); NodeCurrent = NodeCurrent.Parent; } break; } NodeCurrent.GetSuccessors(this.FSuccessors); foreach (AStarNode NodeSuccessor in this.FSuccessors) { AStarNode NodeOpen = null; if (this.FOpenList.Contains(NodeSuccessor)) { NodeOpen = (AStarNode) this.FOpenList[this.FOpenList.IndexOf(NodeSuccessor)]; } if ((NodeOpen == null) || (NodeSuccessor.TotalCost <= NodeOpen.TotalCost)) { AStarNode NodeClosed = null; if (this.FClosedList.Contains(NodeSuccessor)) { NodeClosed = (AStarNode) this.FClosedList[this.FClosedList.IndexOf(NodeSuccessor)]; } if ((NodeClosed == null) || (NodeSuccessor.TotalCost <= NodeClosed.TotalCost)) { this.FOpenList.Remove(NodeOpen); this.FClosedList.Remove(NodeClosed); this.FOpenList.Push(NodeSuccessor); } } } this.FClosedList.Add(NodeCurrent); } } private void PrintNodeList(object ANodeList) { Console.WriteLine("Node list:"); foreach (AStarNode n in ANodeList as IEnumerable) { n.PrintNodeInfo(); } Console.WriteLine("====="); } public ArrayList Solution { get { return this.FSolution; } } } }