public bool Search_Way() { // Big and difficult CountSteps = 0; TNodeRecord RecFirst = new TNodeRecord(StartPosition); TNode FinishNode = null; List <TNode> Storage = new List <TNode>(); List <TNode> Maintance = new List <TNode>(); Storage.Add(new TNode(RecFirst, this, null)); // First node. bool Check = false; while (!Check && (Storage.Count > 0)) { CountSteps++; Maintance.Clear(); Maintance = Storage[0].ScanAround(); Storage.RemoveAt(0); // Clearing this point for (int i = 0; i < Maintance.Count; i++) { if (Maintance[i].Position.Position == StopPosition) { FinishNode = Maintance[i]; // Memorize! Check = true; break; } else { Storage.Add(Maintance[i]); } } if (Check) { break; } } if (Check) { // We need to recreate path TNode Current = FinishNode; while (Current != null) { this.Way.Add(new TNodeRecord(Current.Position.Position)); Current = Current.ParentNode; } if (Way.Count <= 0) { throw new Exception("Way is almost empty."); } } return(Check); }
public List <TNode> ScanAround() { List <TNode> Result = new List <TNode>(); TPoint Current = this.Position.Position; TFieldSection Self = Dijkstra.Field[Current]; TPoint ForwardRaw = Current - new TPoint(1, 0); TPoint RightRaw = Current + new TPoint(0, 1); TPoint BackwardRaw = Current + new TPoint(1, 0); TPoint LeftRaw = Current - new TPoint(0, 1); int Length = Dijkstra.Field.Length; this.Dijkstra.Nodes.Add(this.Position); if (ForwardRaw.isValid(Length - 1) && !Self.isBlockedRaw(Constants.Direction_Forward)) { TPoint Copied = ForwardRaw; TNodeRecord Rec = new TNodeRecord(Copied.X, Copied.Y); if (!Dijkstra.Nodes.nodeExists(Rec)) { Result.Add(new TNode(Rec, Dijkstra, this)); } } if (RightRaw.isValid(Length - 1) && !Self.isBlockedRaw(Constants.Direction_Right)) { TPoint Copied = RightRaw; TNodeRecord Rec = new TNodeRecord(Copied.X, Copied.Y); if (!Dijkstra.Nodes.nodeExists(Rec)) { Result.Add(new TNode(Rec, Dijkstra, this)); } } if (BackwardRaw.isValid(Length - 1) && !Self.isBlockedRaw(Constants.Direction_Backward)) { TPoint Copied = BackwardRaw; TNodeRecord Rec = new TNodeRecord(Copied.X, Copied.Y); if (!Dijkstra.Nodes.nodeExists(Rec)) { Result.Add(new TNode(Rec, Dijkstra, this)); } } if (LeftRaw.isValid(Length - 1) && !Self.isBlockedRaw(Constants.Direction_Left)) { TPoint Copied = LeftRaw; TNodeRecord Rec = new TNodeRecord(Copied.X, Copied.Y); if (!Dijkstra.Nodes.nodeExists(Rec)) { Result.Add(new TNode(Rec, Dijkstra, this)); } } return(Result); }
public TNode(TNodeRecord Position, TDijkstra Dijkstra, TNode ParentNode) { this.Position = Position; this.Dijkstra = Dijkstra; this.ParentNode = ParentNode; }