Ejemplo 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);
    }