Esempio n. 1
0
    public static int[,,] GetDistanceMap(Unit unit, Vector2 destination)
    {
        int[,,] distanceMap = new int[GridManager.Width(), GridManager.Height(), 2];
        for (int x = 0; x < distanceMap.GetLength(0); x++)
        {
            for (int y = 0; y < distanceMap.GetLength(1); y++)
            {
                distanceMap[x, y, 0] = 1000;
                distanceMap[x, y, 1] = 1000;
            }
        }
        distanceMap[(int)destination.x, (int)destination.y, 0] = 0;
        distanceMap[(int)destination.x, (int)destination.y, 1] = 0;
        List <Vector2> positionsToCheck = new List <Vector2>();

        positionsToCheck.Add(destination);
        int turnsAway = 1;

        while (positionsToCheck.Count > 0)
        {
            List <Vector2> addList = new List <Vector2>();
            foreach (Vector2 positionToCheck in positionsToCheck)
            {
                Pather.SetUpNodes(positionToCheck, unit.owner, unit.grouping, true);
                List <Vector2> movePositions = Pather.GetMoveCoordsForFloodFill(positionToCheck, unit.movePoints);
                foreach (Vector2 movePosition in movePositions)
                {
                    int pathCost = nodes[(int)movePosition.x, (int)movePosition.y].pathCost;
                    if (turnsAway < distanceMap[(int)movePosition.x, (int)movePosition.y, 0] ||
                        (turnsAway == distanceMap[(int)movePosition.x, (int)movePosition.y, 0] &&
                         pathCost < distanceMap[(int)movePosition.x, (int)movePosition.y, 1]))
                    {
                        distanceMap[(int)movePosition.x, (int)movePosition.y, 0] = turnsAway;
                        distanceMap[(int)movePosition.x, (int)movePosition.y, 1] = pathCost;
                        addList.Add(movePosition);
                    }
                }
            }
            positionsToCheck.Clear();
            foreach (Vector2 addVector in addList)
            {
                positionsToCheck.Add(addVector);
            }
            turnsAway++;
        }
        return(distanceMap);
    }
Esempio n. 2
0
    public static float[,] GetUnitInfluence(Unit unit, ref float[,] oneTurnUnitInfluence)
    {
        float[,] unitInfluenceMap = new float[GridManager.Width(), GridManager.Height()];
        int turnsAway = 1;
        // bool isArtillery = unit.grouping == UnitGroup.artillery;
        // if (isArtillery)
        // {
        //  List<Vector2> attackPositions = GridManager.GetCoordsToAttackHighlight(unit.xy, unit.range);
        //  foreach (Vector2 attackPosition in attackPositions)
        //  {
        //      float influence = (float)unit.GetPower() / (float)Math.Pow(2 , Math.Pow(turnsAway, 2));
        //      if (influence > unitInfluenceMap[(int)attackPosition.x,(int)attackPosition.y])
        //      {
        //          unitInfluenceMap[(int)attackPosition.x,(int)attackPosition.y] = influence;
        //          if (turnsAway == 1)
        //          {
        //              oneTurnUnitInfluence[(int)attackPosition.x,(int)attackPosition.y] = influence;
        //          }
        //      }
        //  }
        //  turnsAway++;
        // }
        List <Vector2> positionsToCheck = new List <Vector2>();
        UnitPather     pather           = new UnitPather(unit);

        positionsToCheck = pather.BehaviorMovePositions(attacking: true);
        while (positionsToCheck.Count > 0)
        {
            List <Vector2> addList = new List <Vector2>();
            foreach (Vector2 positionToCheck in positionsToCheck)
            {
                Pather.SetUpNodes(positionToCheck, unit.owner, unit.grouping, true);
                List <Vector2> movePositions = Pather.GetMoveCoordsForFloodFill(positionToCheck, unit.movePoints);
                foreach (Vector2 movePosition in movePositions)
                {
                    List <Vector2> attackPositions = GridManager.GetCoordsToAttackHighlight(movePosition, unit.range);
                    foreach (Vector2 attackPosition in attackPositions)
                    {
                        float influence = (float)unit.GetPower() / (float)Math.Pow(2, Math.Pow(turnsAway, 2));
                        if (influence > unitInfluenceMap[(int)attackPosition.x, (int)attackPosition.y])
                        {
                            unitInfluenceMap[(int)attackPosition.x, (int)attackPosition.y] = influence;
                            if (turnsAway == 1)
                            {
                                oneTurnUnitInfluence[(int)attackPosition.x, (int)attackPosition.y] = influence;
                            }
                            addList.Add(movePosition);
                        }
                    }
                }
            }
            positionsToCheck.Clear();
            if (unit.behaviour != Behaviour.defend)
            {
                foreach (Vector2 addVector in addList)
                {
                    positionsToCheck.Add(addVector);
                }
            }
            //Debug.LogFormat("There are {0} movePositions at {1} turnsAway", positionsToCheck.Count, turnsAway);
            turnsAway++;
        }
        return(unitInfluenceMap);
    }