Esempio n. 1
0
    // this uses Dijkstra's Algorithm to get all the spaces a player can visit.
    public static List <PathfindingNode> Dijkstras(SpaceModel startSpace, int maxCost, bool ignoreTerrain)
    {
        List <PathfindingNode> allNodes = new List <PathfindingNode>();
        //List<SpaceModel> shortestPath = new List<SpaceModel>
        //{
        //    startSpace
        //};
        PathfindingNode currentnode = new PathfindingNode(startSpace, null, 0, 0, true, null);

        allNodes.Add(currentnode);

        bool done = false;

        while (!done)
        {
            foreach (SpaceModel adjacentSpace in currentnode.GetSpace().GetAdjacentSpaces())
            {
                if (adjacentSpace != null)
                {
                    PathfindingNode nextNode = adjacentSpace.GetNode();
                    if (nextNode != null)
                    {
                        // not null
                        if (!nextNode.BeenSeen())
                        {
                            // Next node hasn't been visited yet
                            if (ignoreTerrain)
                            {
                                nextNode.Update(currentnode.GetCost() + 1, currentnode.GetCost() + 1, currentnode);
                            }
                            else
                            {
                                int    adjacentSpaceCost            = currentnode.GetCost() + adjacentSpace.GetMovementCost();
                                double adjacentSpacePathfindingCost = adjacentSpaceCost;
                                if (adjacentSpace.IsHazardous())
                                {
                                    adjacentSpacePathfindingCost += 0.001;
                                }


                                nextNode.Update(adjacentSpaceCost, adjacentSpacePathfindingCost, currentnode);
                            }
                        }
                    }
                    else
                    {
                        // Is null, need new node
                        int    newNodeCost;
                        double pathfindingCost;

                        if (ignoreTerrain)
                        {
                            newNodeCost     = currentnode.GetCost() + 1;
                            pathfindingCost = newNodeCost;
                        }
                        else
                        {
                            newNodeCost     = currentnode.GetCost() + adjacentSpace.GetMovementCost();
                            pathfindingCost = newNodeCost;
                            if (adjacentSpace.IsHazardous())
                            {
                                pathfindingCost = newNodeCost + 0.001;
                            }
                            if (adjacentSpace.Occupied())
                            {
                                pathfindingCost += 100;
                            }
                        }

                        if (newNodeCost <= maxCost)
                        {
                            PathfindingNode newNode = new PathfindingNode(adjacentSpace, currentnode, newNodeCost, pathfindingCost, false, null);
                            allNodes.Add(newNode);
                            adjacentSpace.SetNode(newNode);
                        }
                    }
                }
            }
            PathfindingNode lowestNode = null;
            foreach (PathfindingNode node in allNodes)
            {
                if (!node.BeenSeen())
                {
                    if (lowestNode == null)
                    {
                        lowestNode = node;
                    }
                    else
                    {
                        if (node.GetPathfindingCost() < lowestNode.GetPathfindingCost())
                        {
                            lowestNode = node;
                        }
                    }
                }
            }
            if (lowestNode == null)
            {
                done = true;
            }
            else
            {
                lowestNode.Seen();
                currentnode = lowestNode;
            }
        }
        foreach (PathfindingNode node in allNodes)
        {
            node.GetSpace().SetNode(null);
        }

        return(allNodes);
    }
    public static List <SpaceModel> GetPathToDestination(SpaceModel startSpace, SpaceModel destSpace)
    {
        List <PathfindingNode> allNodes = new List <PathfindingNode>();

        PathfindingNode currentnode = new PathfindingNode(startSpace, null, 0, 0, true, destSpace);

        allNodes.Add(currentnode);
        bool done = false;

        while (!done)
        {
            foreach (SpaceModel adjacentSpace in currentnode.GetSpace().GetAdjacentSpaces())
            {
                if (adjacentSpace != null)
                {
                    if (adjacentSpace.GetNode() == null)
                    {
                        // Is null, need new node
                        int    newNodeCost        = currentnode.GetCost() + adjacentSpace.GetMovementCost();
                        double newPathfindingCost = newNodeCost;
                        if (adjacentSpace.IsHazardous())
                        {
                            newPathfindingCost += 10;
                        }
                        if (adjacentSpace == destSpace)
                        {
                            newPathfindingCost = 1;
                        }
                        PathfindingNode newNode = new PathfindingNode(adjacentSpace, currentnode, newNodeCost, newPathfindingCost, false, destSpace);
                        allNodes.Add(newNode);
                        adjacentSpace.SetNode(newNode);
                    }
                    else
                    {
                        // not null, there is a node here
                        PathfindingNode nextNode           = adjacentSpace.GetNode();
                        int             newNodeCost        = currentnode.GetCost() + adjacentSpace.GetMovementCost();
                        double          newPathfindingCost = newNodeCost;
                        if (adjacentSpace.IsHazardous())
                        {
                            newPathfindingCost += 10;
                        }
                        if (!nextNode.BeenSeen())
                        {
                            // Next node hasn't been visited yet
                            nextNode.Update(newNodeCost, newPathfindingCost, currentnode);
                        }
                    }
                }
            }
            currentnode.Seen();
            if (!currentnode.GetSpace().Equals(destSpace))
            {
                PathfindingNode lowestNode = null;
                foreach (PathfindingNode node in allNodes)
                {
                    if (!node.BeenSeen())
                    {
                        if (lowestNode == null)
                        {
                            lowestNode = node;
                        }
                        else
                        {
                            if (node.GetASCost() <= lowestNode.GetASCost())
                            {
                                if (node.GetRemainingCost() <= lowestNode.GetRemainingCost())
                                {
                                    lowestNode = node;
                                }
                            }
                        }
                    }
                }
                if (lowestNode == null)
                {
                    done = true;
                }
                else
                {
                    currentnode = lowestNode;
                }
            }
            else
            {
                done = true;
            }
        }

        List <SpaceModel> path          = new List <SpaceModel>();
        PathfindingNode   backtrackNode = currentnode;

        while (backtrackNode != null)
        {
            path.Add(backtrackNode.GetSpace());
            backtrackNode = backtrackNode.GetParent();
        }
        path.Reverse();

        foreach (PathfindingNode node in allNodes)
        {
            node.GetSpace().SetNode(null);

            //node.GetSpace().ClearHighlighted();
        }

        path.Remove(startSpace);
        return(path);
    }