Exemple #1
0
    EnemyDecision FindBestAttackAt(Vector3Int move)
    {
        UnitTracker       ut = GetComponent <UnitTracker>();
        List <Vector3Int> possibleAttacks = ut.PossibleAttacksAtTile(move);
        Vector3Int        bestAttack      = new Vector3Int(-1, -1, -1);
        float             bestRating      = 0;
        bool freeAttackFound = false;

        foreach (Vector3Int attack in possibleAttacks)
        {
            if (TileManager.instance.allUnits.ContainsKey(attack) && TileManager.instance.allUnits[attack].tag != tag)
            {
                GameObject       target = TileManager.instance.allUnits[attack];
                BattleSimulation battle = new BattleSimulation(gameObject, target, move, target.GetComponent <UnitTracker>().GetLocation());

                if (!freeAttackFound && battle.GetDefenderAttacks() == 0)
                {
                    freeAttackFound = true;
                    bestRating      = CalculateRating(battle);
                    bestAttack      = attack;
                }
                else if (!freeAttackFound && battle.GetDefenderAttacks() != 0)
                {
                    float tempRating = CalculateRating(battle);
                    if (tempRating > bestRating)
                    {
                        bestRating = tempRating;
                        bestAttack = attack;
                    }
                }
                else if (battle.GetDefenderAttacks() == 0)
                {
                    float tempRating = CalculateRating(battle);
                    if (tempRating > bestRating)
                    {
                        bestRating = tempRating;
                        bestAttack = attack;
                    }
                }
            }
        }

        return(new EnemyDecision(bestAttack));
    }
Exemple #2
0
    EnemyDecision FindBestMoveAttack(List <Vector3Int> possibleMoves)
    {
        UnitTracker ut = GetComponent <UnitTracker>();

        if (possibleMoves.Count == 0)
        {
            return(FindBestAttackAt(ut.GetLocation()));
        }

        Vector3Int bestMove        = new Vector3Int(-1, -1, -1);
        Vector3Int bestAttack      = new Vector3Int(-1, -1, -1);
        float      bestRating      = 0;
        bool       freeAttackFound = false;

        foreach (Vector3Int move in possibleMoves)                                                                         //Loop through every possible move a character can make
        {
            foreach (Vector3Int attack in ut.PossibleAttacksAtTile(move))                                                  //Loop through every possible attack at each move
            {
                if (TileManager.instance.allUnits.ContainsKey(attack) && TileManager.instance.allUnits[attack].tag != tag) //Check to see if there is an attackable unit
                {
                    GameObject       target = TileManager.instance.allUnits[attack];
                    BattleSimulation battle = new BattleSimulation(gameObject, target, move, target.GetComponent <UnitTracker>().GetLocation());
                    if (!TileManager.instance.allUnits.ContainsKey(move))
                    {
                        if (!freeAttackFound && battle.GetDefenderAttacks() == 0)
                        {
                            freeAttackFound = true;
                            bestRating      = CalculateRating(battle);
                            bestMove        = move;
                            bestAttack      = attack;
                        }
                        else if (!freeAttackFound && battle.GetDefenderAttacks() != 0)
                        {
                            float tempRating = CalculateRating(battle);
                            if (tempRating > bestRating)
                            {
                                bestRating = tempRating;
                                bestMove   = move;
                                bestAttack = attack;
                            }
                        }
                        else if (battle.GetDefenderAttacks() == 0)
                        {
                            float tempRating = CalculateRating(battle);
                            if (tempRating > bestRating)
                            {
                                bestRating = tempRating;
                                bestMove   = move;
                                bestAttack = attack;
                            }
                        }
                    }
                }
            }
        }

        if (bestMove.z == -1 && bestAttack.z == -1)
        {
            return(new EnemyDecision());
        }
        List <Vector3Int> path = ShortestPath.instance.FindPath(gameObject.GetComponent <UnitTracker>().GetLocation(), bestMove, "Red");

        return(new EnemyDecision(path, bestAttack));
    }