Example #1
0
 public virtual void FollowPath()
 {
     if (((Vector2)gameObject.transform.position - goalPosition).magnitude < 0.03f)
     {
         hasFinishedGoingToLastStep = true;
         if (requestedTargetPositionInGrid != null)
         {
             isFollowingPath = false;
             SetNewPositionOnMapSettingWorldPosition(MapGridded.WorldToMapPosition(gameObject.transform.position));
             StartFollowingPath(ASTARPathfinder.Instance.FindPath(positionInGrid, requestedTargetPositionInGrid));
             requestedTargetPositionInGrid = null;
             return;
         }
         if (followedPath.Count == 0 || nextNodeToFollow == followedPath[followedPath.Count - 1])
         {
             isFollowingPath = false;
         }
         else
         {
             ++indexOfFollowedPathNode;
             nextNodeToFollow = followedPath[indexOfFollowedPathNode];
             if (CheckIfCanGoTo(new IntVector2(nextNodeToFollow.X, nextNodeToFollow.Y)))
             {
                 positionInGridSyncVar = new Vector2(nextNodeToFollow.X, nextNodeToFollow.Y);
                 RpcMoveFromTo(gameObject.transform.position, new Vector2(nextNodeToFollow.X, nextNodeToFollow.Y));
             }
             else
             {
                 isFollowingPath = false;
                 RequestGoTo(new IntVector2(followedPath[followedPath.Count - 1].X, followedPath[followedPath.Count - 1].Y));
             }
         }
     }
 }
Example #2
0
 public int GetCostToGetHereFrom(MapGridElement pathNode)
 {
     if (pathNode == this)
     {
         return(0);
     }
     else
     {
         return(this.X != pathNode.X && this.Y != pathNode.Y ? 14 : 10);
     }
 }
Example #3
0
    public MapGridElement GetPathNodeWithMinimumFunctionValue(List <MapGridElement> pathNodes)
    {
        MapGridElement pathNodeWithMinimumF = pathNodes[0];

        foreach (MapGridElement actualPathNode in pathNodes)
        {
            if (actualPathNode.PathNode.CostFunctionValue < pathNodeWithMinimumF.PathNode.CostFunctionValue)
            {
                pathNodeWithMinimumF = actualPathNode;
            }
        }
        return(pathNodeWithMinimumF);
    }
Example #4
0
    public List <MapGridElement> ReconstructPath(MapGridElement start, MapGridElement goal)
    {
        List <MapGridElement> path       = new List <MapGridElement>();
        MapGridElement        actualNode = goal;

        while (actualNode.PathNode != start.PathNode)
        {
            path.Add(actualNode);
            actualNode = actualNode.PathNode.parentPathNode;
        }
        path.Reverse();
        return(path);
    }
Example #5
0
    public List <MapGridElement> FindPathForLumber(IntVector2 startNodePosition, out LumberInGame lumberToCut)
    {
        MapGridElement        startNode   = MapGridded.Instance.MapGrid[startNodePosition.Y, startNodePosition.X];
        List <MapGridElement> closedNodes = new List <MapGridElement>();
        List <MapGridElement> openNodes   = new List <MapGridElement>();

        openNodes.Add(startNode);
        startNode.PathNode.CostFromStart       = 0;
        startNode.PathNode.HeuristicCostToGoal = 0;
        startNode.PathNode.CostFunctionValue   = startNode.PathNode.CostFromStart + startNode.PathNode.HeuristicCostToGoal;
        while (openNodes.Count > 0)
        {
            MapGridElement actualOpenNode = GetPathNodeWithMinimumFunctionValue(openNodes);
            if (MapGridded.Instance.GetAdjacentGridElements(new IntVector2(actualOpenNode.X, actualOpenNode.Y)).Find(item => item.Lumber != null && !item.Lumber.IsDepleted && !item.Lumber.IsBeingCut) != null)
            {
                lumberToCut = MapGridded.Instance.GetAdjacentGridElements(new IntVector2(actualOpenNode.X, actualOpenNode.Y)).Find(item => item.Lumber != null && !item.Lumber.IsDepleted && !item.Lumber.IsBeingCut).Lumber;
                return(ReconstructPath(startNode, actualOpenNode));
            }
            openNodes.Remove(actualOpenNode);
            closedNodes.Add(actualOpenNode);
            foreach (MapGridElement nextNode in GetAdjacentNodesForPath(actualOpenNode, startNodePosition))
            {
                if (closedNodes.Contains(nextNode))
                {
                    continue;
                }
                int  testedCostFromStart           = actualOpenNode.PathNode.CostFromStart + nextNode.GetCostToGetHereFrom(actualOpenNode);
                bool isTestedCostFromStartIsBetter = false;
                if (!openNodes.Contains(nextNode))
                {
                    openNodes.Add(nextNode);
                    nextNode.PathNode.HeuristicCostToGoal = 0;
                    isTestedCostFromStartIsBetter         = true;
                }
                else if (testedCostFromStart < nextNode.PathNode.CostFromStart)
                {
                    isTestedCostFromStartIsBetter = true;
                }
                if (isTestedCostFromStartIsBetter)
                {
                    nextNode.PathNode.parentPathNode    = actualOpenNode;
                    nextNode.PathNode.CostFromStart     = testedCostFromStart;
                    nextNode.PathNode.CostFunctionValue = nextNode.PathNode.CostFromStart + nextNode.PathNode.HeuristicCostToGoal;
                }
            }
        }
        lumberToCut = null;
        return(null);
    }
Example #6
0
    public List <MapGridElement> FindPathForNearestCastle(IntVector2 startNodePosition, PlayerType castleOwner, out Building castle)
    {
        MapGridElement        startNode   = MapGridded.Instance.MapGrid[startNodePosition.Y, startNodePosition.X];
        List <MapGridElement> closedNodes = new List <MapGridElement>();
        List <MapGridElement> openNodes   = new List <MapGridElement>();

        openNodes.Add(startNode);
        startNode.PathNode.CostFromStart       = 0;
        startNode.PathNode.HeuristicCostToGoal = 0;
        startNode.PathNode.CostFunctionValue   = startNode.PathNode.CostFromStart + startNode.PathNode.HeuristicCostToGoal;
        while (openNodes.Count > 0)
        {
            MapGridElement actualOpenNode = GetPathNodeWithMinimumFunctionValue(openNodes);
            if (MapGridded.Instance.GetAdjacentGridElements(new IntVector2(actualOpenNode.X, actualOpenNode.Y)).Find(item => item.Building != null && item.Building.BuildingType == BuildingType.Castle && item.Building.Owner == castleOwner) != null)
            {
                castle = MapGridded.Instance.GetAdjacentGridElements(new IntVector2(actualOpenNode.X, actualOpenNode.Y)).Find(item => item.Building != null && item.Building.BuildingType == BuildingType.Castle && item.Building.Owner == castleOwner).Building;
                return(ReconstructPath(startNode, actualOpenNode));
            }
            openNodes.Remove(actualOpenNode);
            closedNodes.Add(actualOpenNode);
            foreach (MapGridElement nextNode in GetAdjacentNodesForPath(actualOpenNode, startNodePosition))
            {
                if (closedNodes.Contains(nextNode))
                {
                    continue;
                }
                int  testedCostFromStart           = actualOpenNode.PathNode.CostFromStart + nextNode.GetCostToGetHereFrom(actualOpenNode);
                bool isTestedCostFromStartIsBetter = false;
                if (!openNodes.Contains(nextNode))
                {
                    openNodes.Add(nextNode);
                    nextNode.PathNode.HeuristicCostToGoal = 0;
                    isTestedCostFromStartIsBetter         = true;
                }
                else if (testedCostFromStart < nextNode.PathNode.CostFromStart)
                {
                    isTestedCostFromStartIsBetter = true;
                }
                if (isTestedCostFromStartIsBetter)
                {
                    nextNode.PathNode.parentPathNode    = actualOpenNode;
                    nextNode.PathNode.CostFromStart     = testedCostFromStart;
                    nextNode.PathNode.CostFunctionValue = nextNode.PathNode.CostFromStart + nextNode.PathNode.HeuristicCostToGoal;
                }
            }
        }
        castle = null;
        return(null);
    }
Example #7
0
    public List <MapGridElement> FindPath(IntVector2 startNodePosition, IntVector2 goalNodePosition)
    {
        MapGridElement        startNode   = MapGridded.Instance.MapGrid[startNodePosition.Y, startNodePosition.X];
        MapGridElement        goalNode    = MapGridded.Instance.MapGrid[goalNodePosition.Y, goalNodePosition.X];
        List <MapGridElement> closedNodes = new List <MapGridElement>();
        List <MapGridElement> openNodes   = new List <MapGridElement>();

        openNodes.Add(startNode);
        startNode.PathNode.CostFromStart       = 0;
        startNode.PathNode.HeuristicCostToGoal = (Mathf.Abs(startNode.X - goalNode.X) + Mathf.Abs(startNode.Y - goalNode.Y)) * 10;
        startNode.PathNode.CostFunctionValue   = startNode.PathNode.CostFromStart + startNode.PathNode.HeuristicCostToGoal;
        while (openNodes.Count > 0)
        {
            MapGridElement actualOpenNode = GetPathNodeWithMinimumFunctionValue(openNodes);
            if (actualOpenNode == goalNode)
            {
                return(ReconstructPath(startNode, goalNode));
            }
            openNodes.Remove(actualOpenNode);
            closedNodes.Add(actualOpenNode);
            foreach (MapGridElement nextNode in GetAdjacentNodesForPath(actualOpenNode, startNodePosition))
            {
                if (closedNodes.Contains(nextNode))
                {
                    continue;
                }
                int  testedCostFromStart           = actualOpenNode.PathNode.CostFromStart + nextNode.GetCostToGetHereFrom(actualOpenNode);
                bool isTestedCostFromStartIsBetter = false;
                if (!openNodes.Contains(nextNode))
                {
                    openNodes.Add(nextNode);
                    nextNode.PathNode.HeuristicCostToGoal = (Mathf.Abs(nextNode.X - goalNode.X) + Mathf.Abs(nextNode.Y - goalNode.Y)) * 10;
                    isTestedCostFromStartIsBetter         = true;
                }
                else if (testedCostFromStart < nextNode.PathNode.CostFromStart)
                {
                    isTestedCostFromStartIsBetter = true;
                }
                if (isTestedCostFromStartIsBetter)
                {
                    nextNode.PathNode.parentPathNode    = actualOpenNode;
                    nextNode.PathNode.CostFromStart     = testedCostFromStart;
                    nextNode.PathNode.CostFunctionValue = nextNode.PathNode.CostFromStart + nextNode.PathNode.HeuristicCostToGoal;
                }
            }
        }
        return(null);
    }
Example #8
0
    public MapGridElement[] GetAdjacentNodesForPath(MapGridElement pathNode, IntVector2 startPathPosition)
    {
        List <MapGridElement> adjacentNodes = new List <MapGridElement>();

        if (pathNode.Y - 1 >= 0 && MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X]);
        }
        if (pathNode.Y + 1 < MapGridded.Instance.MapGrid.GetLength(0) && MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X]);
        }
        if (pathNode.X + 1 < MapGridded.Instance.MapGrid.GetLength(1) && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X + 1].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X + 1]);
        }
        if (pathNode.X - 1 >= 0 && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X - 1].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X - 1]);
        }
        if (pathNode.X - 1 >= 0 && pathNode.Y - 1 >= 0 && MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X - 1].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X - 1].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X - 1]);
        }
        if (pathNode.X + 1 < MapGridded.Instance.MapGrid.GetLength(1) && pathNode.Y - 1 >= 0 && MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X + 1].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X + 1].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y - 1, pathNode.X + 1]);
        }
        if (pathNode.X + 1 < MapGridded.Instance.MapGrid.GetLength(1) && pathNode.Y + 1 < MapGridded.Instance.MapGrid.GetLength(0) && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X + 1].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X + 1].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X + 1]);
        }
        if (pathNode.X - 1 >= 0 && pathNode.Y + 1 < MapGridded.Instance.MapGrid.GetLength(0) && MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X - 1].CheckIfIsGoodForPath(startPathPosition) && MapGridded.Instance.MapGrid[pathNode.Y, pathNode.X - 1].CheckIfIsGoodForPath(startPathPosition))
        {
            adjacentNodes.Add(MapGridded.Instance.MapGrid[pathNode.Y + 1, pathNode.X - 1]);
        }
        return(adjacentNodes.ToArray());
    }
Example #9
0
 public virtual void StartFollowingPath(List <MapGridElement> pathToFollow)
 {
     hasFinishedGoingToLastStep = true;
     if (pathToFollow != null)
     {
         followedPath = pathToFollow;
         if (pathToFollow.Count > 0)
         {
             nextNodeToFollow = pathToFollow[0];
         }
         else
         {
             nextNodeToFollow = MapGridded.Instance.MapGrid[positionInGrid.Y, positionInGrid.X];
         }
         indexOfFollowedPathNode = 0;
         if (MapGridded.Instance.MapGrid[nextNodeToFollow.Y, nextNodeToFollow.X].ChecklIfIsWalkableForUnit(this))
         {
             SetNewPositionOnMap(new IntVector2(nextNodeToFollow.X, nextNodeToFollow.Y));
             if (pathToFollow.Count > 0)
             {
                 requestedTargetPositionInGrid = new IntVector2(pathToFollow[pathToFollow.Count - 1].X, pathToFollow[pathToFollow.Count - 1].Y);
             }
             else
             {
                 requestedTargetPositionInGrid = positionInGrid;
             }
             isFollowingPath = true;
             RpcMoveFromTo(gameObject.transform.position, new Vector2(nextNodeToFollow.X, nextNodeToFollow.Y));
         }
         else
         {
             isFollowingPath = false;
             RequestGoTo(new IntVector2(pathToFollow[pathToFollow.Count - 1].X, pathToFollow[pathToFollow.Count - 1].Y));
         }
     }
 }
    public static bool CheckMap(string mapName)
    {
        try
        {
            string        wholeMapText            = new StreamReader(Application.dataPath + "/" + MapEditor.mapsFolderName + "/" + mapName + ".map").ReadToEnd();
            List <string> lines                   = new List <string>(wholeMapText.Split('\n'));
            int           mapSizeX                = 0;
            int           mapSizeY                = 0;
            Vector2       player1StartingPosition = -Vector2.one;
            Vector2       player2StartingPosition = -Vector2.one;
            MapGridElement[,] mapGrid = null;
            IntVector2 positionInMap;
            Vector2    postionToCreate;
            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                switch (words[0])
                {
                case MapEditor.mapSizeFileKey:
                    mapSizeX = int.Parse(words[1]);
                    mapSizeY = int.Parse(words[2]);
                    mapGrid  = new MapGridElement[mapSizeY, mapSizeX];
                    break;

                case MapEditor.tileKey:
                    positionInMap   = new IntVector2(int.Parse(words[1]), int.Parse(words[2]));
                    postionToCreate = MapGridded.MapToWorldPosition(new IntVector2(int.Parse(words[1]), int.Parse(words[2])));
                    mapGrid[positionInMap.Y, positionInMap.X] = new MapGridElement(Tiles.Instance.TilesPrefabs.Find(item => item.TileType == (TileType)System.Enum.Parse(typeof(TileType), words[3])));
                    break;

                case MapEditor.goldMineKey:
                    positionInMap   = new IntVector2(int.Parse(words[1]), int.Parse(words[2]));
                    postionToCreate = MapGridded.MapToWorldPosition(new IntVector2(int.Parse(words[1]), int.Parse(words[2])));
                    foreach (IntVector2 minePositionInMap in Resources.Instance.MinePrefab.GetComponent <Mine>().GetMapPositions(positionInMap))
                    {
                        mapGrid[minePositionInMap.Y, minePositionInMap.X].Mine = Resources.Instance.MinePrefab.GetComponent <Mine>();
                    }
                    break;

                case MapEditor.lumberKey:
                    positionInMap   = new IntVector2(int.Parse(words[1]), int.Parse(words[2]));
                    postionToCreate = MapGridded.MapToWorldPosition(new IntVector2(int.Parse(words[1]), int.Parse(words[2])));
                    mapGrid[positionInMap.Y, positionInMap.X].Lumber = Resources.Instance.TreePrefab.GetComponent <LumberInGame>();
                    break;

                case MapEditor.player1PositionKey:
                    player1StartingPosition = MapGridded.MapToWorldPosition(new IntVector2(int.Parse(words[1]), int.Parse(words[2])));
                    break;

                case MapEditor.player2PositionKey:
                    player2StartingPosition = MapGridded.MapToWorldPosition(new IntVector2(int.Parse(words[1]), int.Parse(words[2])));
                    break;
                }
            }
            if (mapGrid == null)
            {
                return(false);
            }
            foreach (MapGridElement mapGridElement in mapGrid)
            {
                if (mapGridElement == null)
                {
                    return(false);
                }
            }
            if (mapSizeX <= 0 || mapSizeY <= 0 || mapSizeX > 50 || mapSizeY > 50 || player1StartingPosition == null || player2StartingPosition == null || player1StartingPosition == player2StartingPosition || player1StartingPosition.x < 0 || player1StartingPosition.x > mapSizeX - 1 || player1StartingPosition.y < 0 || player1StartingPosition.y > mapSizeY - 1 || player2StartingPosition.x < 0 || player2StartingPosition.x > mapSizeX - 1 || player2StartingPosition.y < 0 || player2StartingPosition.y > mapSizeY - 1 || !mapGrid[MapGridded.WorldToMapPosition(player1StartingPosition).Y, MapGridded.WorldToMapPosition(player1StartingPosition).X].IsWalkable || !mapGrid[MapGridded.WorldToMapPosition(player2StartingPosition).Y, MapGridded.WorldToMapPosition(player2StartingPosition).X].IsWalkable)
            {
                return(false);
            }
        }
        catch (Exception e)
        {
            return(false);
        }
        return(true);
    }