Example #1
0
        public List <Point> calcShortestPath(int startX, int startY, int goalX, int goalY)
        {
            AStarCell startCell = map.getCell(startX, startY);
            AStarCell goalCell  = map.getCell(goalX, goalY);

            //Check if the start cell is also an obstacle (if it is, it is impossible to find a path)
            if (AStarCell.isObstacle(startCell))
            {
                UnityEngine.Debug.Log("start is null");
                return(null);
            }

            //Check if the goal cell is also an obstacle (if it is, it is impossible to find a path there)
            if (AStarCell.isObstacle(goalCell))
            {
                UnityEngine.Debug.Log("end is null");
                return(null);
            }

            startCell.reset();
            startCell.setDistanceFromStart(0);
            closedList.Clear();
            openList.Clear();
            openList.Add(startCell);

            //while we haven't reached the goal yet
            while (openList.Count() != 0)
            {
                //get the first Cell from non-searched Cell list, sorted by lowest distance from our goal as guessed by our heuristic
                AStarCell current = openList[0];

                // check if our current Cell location is the goal Cell. If it is, we are done.
                if (current.getX() == goalX && current.getY() == goalY)
                {
                    return(reconstructPath(current));
                }

                //move current Cell to the closed (already searched) list
                openList.Remove(current);
                closedList.Add(current);

                //go through all the current Cells neighbors and calculate if one should be our next step
                foreach (AStarCell neighbor in map.getNeighborList(current))
                {
                    bool neighborIsBetter;

                    //if we have already searched this Cell, don't bother and continue to the next one
                    if (closedList.Contains(neighbor))
                    {
                        continue;
                    }

                    // calculate how long the path is if we choose this neighbor as the next step in the path
                    float neighborDistanceFromStart = (current.getDistanceFromStart() + AStarMap.getDistanceBetween(current, neighbor));

                    //add neighbor to the open list if it is not there
                    if (!openList.Contains(neighbor))
                    {
                        neighbor.reset();
                        openList.Add(neighbor);
                        neighborIsBetter = true;
                        //if neighbor is closer to start it could also be better
                    }
                    else if (neighborDistanceFromStart < current.getDistanceFromStart())
                    {
                        neighborIsBetter = true;
                    }
                    else
                    {
                        neighborIsBetter = false;
                    }
                    // set neighbors parameters if it is better
                    if (neighborIsBetter)
                    {
                        neighbor.setPreviousCell(current);
                        neighbor.setDistanceFromStart(neighborDistanceFromStart);
                        neighbor.setHeuristicDistanceFromGoal(heuristic.getEstimatedDistanceToGoal(
                                                                  neighbor.getX(), neighbor.getY(), goalCell.getX(), goalCell.getY()));

                        // csharp List.Sort use QuickSort, which is unstable,
                        // but in java implement ArrayList.sort use MergeSort, which is stable,
                        // so here use MergeSort to generate the same result as in java implement
                        MergeSortClass.MergeSort(openList);
                    }
                }
            }

            UnityEngine.Debug.Log("path is null");
            return(null);
        }
Example #2
0
        public List <AStarCell> getNeighborList()
        {
            List <AStarCell> neighborList = new List <AStarCell>();

            if (y > 0)
            {// down
                AStarCell cell = map.getCell(x, (y - 1));
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (y > 0 && x < (map.getMapWith() - 1))
            {// down right
                AStarCell cell = map.getCell(x + 1, y - 1);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (x < (map.getMapWith() - 1))
            {// right
                AStarCell cell = map.getCell(x + 1, y);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (x < (map.getMapWith() - 1) && y < (map.getMapHeight() - 1))
            { // up right
                AStarCell cell = map.getCell(x + 1, y + 1);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (y < (map.getMapHeight() - 1))
            {// up
                AStarCell cell = map.getCell(x, y + 1);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (x > 0 && y < (map.getMapHeight() - 1))
            {// up left
                AStarCell cell = map.getCell(x - 1, y + 1);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (x > 0)
            {// left
                AStarCell cell = map.getCell(x - 1, y);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            if (x > 0 && y > 0)
            {// down left
                AStarCell cell = map.getCell(x - 1, y - 1);
                if (cell != null)
                {
                    neighborList.Add(cell);
                }
            }

            return(neighborList);
        }