示例#1
0
        public MapCellAStar Pop()
        {
            MapCellAStar r = (MapCellAStar)_list[0];

            _list.RemoveAt(0);
            return(r);
        }
示例#2
0
        public int CompareTo(object obj)
        {
            MapCellAStar n       = (MapCellAStar)obj;
            int          cFactor = this.TotalCost - n.TotalCost;

            return(cFactor);
        }
示例#3
0
 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();
 }
示例#4
0
 public bool IsMatch(MapCellAStar n)
 {
     if (n != null)
     {
         return(X == n.X && Y == n.Y);
     }
     else
     {
         return(false);
     }
 }
示例#5
0
 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);
 }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }