public MapCellAStar Pop() { MapCellAStar r = (MapCellAStar)_list[0]; _list.RemoveAt(0); return(r); }
public int CompareTo(object obj) { MapCellAStar n = (MapCellAStar)obj; int cFactor = this.TotalCost - n.TotalCost; return(cFactor); }
public MapCellAStar(MapCellAStar parentcell, MapCellAStar goalcell, short x, short y, short MapId) { this.parentcell = parentcell; this._goalcell = goalcell; this.X = x; this.Y = y; this.MapId = MapId; InitCell(); }
public bool IsMatch(MapCellAStar n) { if (n != null) { return(X == n.X && Y == n.Y); } else { return(false); } }
public int IndexOf(MapCellAStar n) { for (int i = 0; i < _list.Count; i++) { MapCellAStar cellInTheList = (MapCellAStar)_list[i]; if (cellInTheList.IsMatch(n)) { return(i); } } return(-1); }
public int Push(MapCellAStar n) { int k = _list.BinarySearch(n, _cellComparer); if (k == -1) { _list.Insert(0, n); } else if (k < 0) { k = ~k; _list.Insert(k, n); } else if (k >= 0) { _list.Insert(k, n); } return(k); }
public List <MapCellAStar> GetSuccessors() { List <MapCellAStar> successors = new List <MapCellAStar>(); for (short xd = -1; xd <= 1; xd++) { for (short yd = -1; yd <= 1; yd++) { if (!ServerManager.GetMap(MapId).IsBlockedZone(X + xd, Y + yd)) { MapCellAStar n = new MapCellAStar(this, this._goalcell, (short)(X + xd), (short)(Y + yd), MapId); if (!n.IsMatch(this.parentcell) && !n.IsMatch(this)) { successors.Add(n); } } } } return(successors); }
public List <MapCell> AStar(MapCell cell1, MapCell cell2) { List <MapCell> SolutionPathList = new List <MapCell>(); SortedCostMapCellList OPEN = new SortedCostMapCellList(); SortedCostMapCellList CLOSED = new SortedCostMapCellList(); MapCellAStar cell_start = new MapCellAStar(null, null, cell1.X, cell1.Y, cell1.MapId); MapCellAStar cell_goal = new MapCellAStar(null, null, cell2.X, cell2.Y, cell2.MapId); OPEN.Push(cell_start); while (OPEN.Count > 0) { MapCellAStar cell_current = OPEN.Pop(); if (cell_current.IsMatch(cell_goal)) { cell_goal.parentcell = cell_current.parentcell; break; } List <MapCellAStar> successors = cell_current.GetSuccessors(); foreach (MapCellAStar cell_successor in successors) { int oFound = OPEN.IndexOf(cell_successor); if (oFound > 0) { MapCellAStar existing_cell = OPEN.CellAt(oFound); if (existing_cell.CompareTo(cell_current) <= 0) { continue; } } int cFound = CLOSED.IndexOf(cell_successor); if (cFound > 0) { MapCellAStar existing_cell = CLOSED.CellAt(cFound); if (existing_cell.CompareTo(cell_current) <= 0) { continue; } } if (oFound != -1) { OPEN.RemoveAt(oFound); } if (cFound != -1) { CLOSED.RemoveAt(cFound); } OPEN.Push(cell_successor); } CLOSED.Push(cell_current); } MapCellAStar p = cell_goal; while (p != null) { SolutionPathList.Insert(0, p); p = p.parentcell; } return(SolutionPathList); }