Exemplo n.º 1
0
 public AStar(Game game, Grid grid, AStarHeuristic heuristic)
     : base(game)
 {
     this.grid        = grid;
     CurrentHeuristic = heuristic;
     IsActive         = false;
     PathFound        = false;
 }
Exemplo n.º 2
0
    // Perform A Star algorithm
    public void RunAStar(int startX, int startY, int endX, int endY, AStarHeuristic heuristic, float heuristicWeight)
    {
        int startIndex = startX + startY * _width;
        int endIndex   = endX + endY * _width;

        int index = 0;

        for (int y = 0; y < _height; ++y)
        {
            for (int x = 0; x < _width; ++x, ++index)
            {
                switch (heuristic)
                {
                case AStarHeuristic.None:
                    _nodes[index].heuristic = 0.0f;
                    break;

                case AStarHeuristic.Manhattan:
                    _nodes[index].heuristic = Mathf.Abs(endX - x) + Mathf.Abs(endY - y);
                    break;

                case AStarHeuristic.Euclidean:
                    _nodes[index].heuristic = Mathf.Sqrt(Mathf.Pow(endX - x, 2) + Mathf.Pow(endY - y, 2));
                    break;

                default:
                    break;
                }
            }
        }
        _nodes[startIndex].distance = 0.0f;

        if (startIndex == endIndex)
        {
            return;
        }

        PriorityQueue <int> queue = new PriorityQueue <int>(new TileAStarComparer(this, heuristicWeight));

        queue.Enqueue(startIndex);

        while (queue.Count > 0 && _nodes[endIndex].parent == -1)
        {
            index = queue.Dequeue();
            int curX = index % _width;
            int curY = index / _width;

            AStarHelper(curX, curY, curX + 1, curY, queue);
            AStarHelper(curX, curY, curX, curY + 1, queue);
            AStarHelper(curX, curY, curX - 1, curY, queue);
            AStarHelper(curX, curY, curX, curY - 1, queue);
        }
    }
Exemplo n.º 3
0
	public IEnumerator AStarSolve(){
		AStarHeuristic aStar = new AStarHeuristic(state_);


		List<Move> moveList = aStar.GetMoveList(aStar.Solve());

		foreach(Move move in moveList){
			
			SwapPiece(move);
			
			//delay the moves to visualise the solving
			yield return new WaitForSeconds(0.2f);
			//yield return null;
		}


		//yield return null;

	}
Exemplo n.º 4
0
    public void RunAStar(int startX, int startY, int endX, int endY, AStarHeuristic heuristic, float heuristicWeight)
    {
        int startIndex = FindContainingNode(startX, startY);
        int endIndex   = FindContainingNode(endX, endY);

        for (int i = 0; i < NumNodes; ++i)
        {
            if (i == startIndex)
            {
                _nodes[i].distance = 0.0f;
            }
            else
            {
                _nodes[i].distance = INFINITY;
            }

            switch (heuristic)
            {
            case AStarHeuristic.None:
                _nodes[i].heuristic = 0.0f;
                break;

            case AStarHeuristic.Manhattan:
                _nodes[i].heuristic =
                    Mathf.Abs((_nodes[i].x + _nodes[i].size / 2.0f) - (_nodes[endIndex].x + _nodes[endIndex].size / 2.0f)) +
                    Mathf.Abs((_nodes[i].y + _nodes[i].size / 2.0f) - (_nodes[endIndex].y + _nodes[endIndex].size / 2.0f));
                break;

            case AStarHeuristic.Euclidean:
                _nodes[i].heuristic = Mathf.Sqrt(
                    Mathf.Pow((_nodes[i].x + _nodes[i].size / 2.0f) - (_nodes[endIndex].x + _nodes[endIndex].size / 2.0f), 2.0f) +
                    Mathf.Pow((_nodes[i].y + _nodes[i].size / 2.0f) - (_nodes[endIndex].y + _nodes[endIndex].size / 2.0f), 2.0f)
                    );
                break;

            default:
                break;
            }

            _nodes[i].parent = -1;
        }

        if (endIndex == -1 || startIndex == endIndex)
        {
            return;
        }

        PriorityQueue <int> queue = new PriorityQueue <int>(new WaypointAStarComparer(this, heuristicWeight));

        queue.Enqueue(startIndex);

        while (queue.Count > 0 && _nodes[endIndex].parent == -1)
        {
            int index = queue.Dequeue();

            for (int i = 0; i < _nodes[index].neighbors.Count; ++i)
            {
                int   nIndex   = _nodes[index].neighbors[i];
                float distance = Mathf.Sqrt(
                    Mathf.Pow((_nodes[index].x + _nodes[index].size / 2.0f) - (_nodes[nIndex].x + _nodes[nIndex].size / 2.0f), 2.0f) +
                    Mathf.Pow((_nodes[index].y + _nodes[index].size / 2.0f) - (_nodes[nIndex].y + _nodes[nIndex].size / 2.0f), 2.0f)
                    );
                if (_nodes[nIndex].distance > _nodes[index].distance + distance)
                {
                    _nodes[nIndex].distance = _nodes[index].distance + distance;
                    _nodes[nIndex].parent   = index;
                    queue.Enqueue(nIndex);
                }
            }
        }
    }
Exemplo n.º 5
0
 public AStar(AStarMap map, AStarHeuristic heuristic)
 {
     this.map       = map;
     this.heuristic = heuristic;
 }
Exemplo n.º 6
0
    public List <Vector2> Pathfind(Vector2 source, Vector2 goal, AStarHeuristic Heuristic)
    {
        // Simple implementation of A*

        List <Vector2> openSet = new List <Vector2> {
            source
        };
        List <Vector2> closedSet = new List <Vector2>();

        Dictionary <Vector2, Vector2> cameFrom = new Dictionary <Vector2, Vector2>();

        Dictionary <Vector2, float> gScores = new Dictionary <Vector2, float>();
        Dictionary <Vector2, float> fScores = new Dictionary <Vector2, float>();

        foreach (RoadNode level in graph.Values)
        {
            gScores[level.transform.position] = Mathf.Infinity;
            fScores[level.transform.position] = Mathf.Infinity;
        }
        gScores[source] = 0;
        fScores[source] = Heuristic(source, goal);

        while (openSet.Count != 0)
        {
            // Find level from the open set which has lowest fScore
            Vector2 currentNode = openSet[0];
            for (int i = 0; i < openSet.Count; i++)
            {
                if (fScores[currentNode] > fScores[openSet[i]])
                {
                    currentNode = openSet[i];
                }
            }

            if (currentNode == goal)
            {
                return(ReconstructPath(cameFrom, currentNode));
            }

            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            // Get levels connected to this level
            List <Vector2> neighbors = new List <Vector2>();
            foreach (RoadConnection connection in graph[currentNode].connections.Values)
            {
                if (connection != null)
                {
                    neighbors.Add(connection.to.transform.position);
                }
            }

            foreach (Vector2 neighbor in neighbors)
            {
                if (closedSet.Contains(neighbor))
                {
                    continue;
                }

                float tentativeGScore = gScores[currentNode] + 1;
                if (!openSet.Contains(neighbor))
                {
                    openSet.Add(neighbor);
                }
                else if (tentativeGScore >= gScores[neighbor])
                {
                    continue;
                }

                cameFrom[neighbor] = currentNode;
                gScores[neighbor]  = tentativeGScore;
                fScores[neighbor]  = gScores[neighbor] + Heuristic(neighbor, goal);
            }
        }
        return(null);
    }
Exemplo n.º 7
0
 public AStar(AStarMap map, AStarHeuristic heuristic)
 {
     this.map = map;
     this.heuristic = heuristic;
 }