Exemplo n.º 1
0
    List <Tile> GetTilesFromWhichUnitCanAttack(Unit target)
    {
        UnitAction_ApplyEffectFromWeapon atk = getAttack();

        List <Tile> tiles      = TileManager.Instance.GetTilesInRange(target.currentTile, atk.GetRange());
        List <Tile> can_attack = tiles.Where(tile => atk.CanTarget(target, tile)).ToList();

        return(can_attack);
    }
Exemplo n.º 2
0
    List <Tile> GetTilesForAttack(Unit target, List <Tile> tiles_to_check)
    {
        UnitAction_ApplyEffectFromWeapon atk = getAttack();
        List <Tile> tiles = new List <Tile>();


        foreach (Tile t in tiles)
        {
            if (atk.CanTarget(target, t))
            {
                tiles.Add(t);
            }
        }

        return(tiles);
    }
Exemplo n.º 3
0
    IEnumerator AttackOrMove()
    {
        MDebug.Log("What to do..`?");

        UnitAction_ApplyEffectFromWeapon attack = getAttack();
        UnitAction_Move move = getMove();

        //dictionary of all units and the paths by which they are attackable
        Dictionary <Unit, List <List <Tile> > > attack_map = GetPathMapForTargets();

        MDebug.Log("^aiAttack Map Count:" + attack_map.Count);
        List <Unit> units_attackable = Unit.GetAllUnitsOfOwner(0, false).Where(unit => attack.CanTarget(unit)).ToList();

        MDebug.Log("^aiAttackable " + units_attackable.Count);
        List <Unit> units_move_attackable = attack_map.Where(kvp => CanFinishPathInMoves(1, kvp.Value)).Select(kvp => kvp.Key).ToList();
        List <Unit> units_movable         = attack_map.Where(kvp => !units_move_attackable.Contains(kvp.Key)).Select(kvp => kvp.Key).ToList();


        if (attack_map.Count > 0 && (preferred_target == null || !preferred_target.IsActive))
        {
            preferred_target = M_Math.GetRandomObject(attack_map.Select(kvp => kvp.Key).ToList());
        }

        if (preferred_target == null)
        {
            yield return(StartCoroutine(MoveRandom()));

            yield break;
        }

        if (!units_attackable.Contains(preferred_target) && (!units_attackable.IsNullOrEmpty() && (!attack_map.ContainsKey(preferred_target) || M_Math.Roll(Constants.AI_TARGET_SWITCH_WHEN_OUT_OF_ATTACK_RANGE))))
        {
            MDebug.Log("^ainew Prefferred  from" + units_attackable.Count);
            preferred_target = M_Math.GetRandomObject(units_attackable);
        }

        if (attack.CanTarget(preferred_target) && units_attackable.Contains(preferred_target))
        {
            Tile better_pos = GetReachableAttackPosition(preferred_target);

            if (m_Actions.HasAP(2) && better_pos != null)
            {
                yield return(StartCoroutine(Move(better_pos)));

                yield break;
            }
            else
            {
                yield return(StartCoroutine(Attack(preferred_target)));

                yield break;
            }
        }
        else
        {
            List <Unit> other_move_targets = units_move_attackable.Where(unit => unit != preferred_target && !units_attackable.Contains(unit)).ToList();

            if (!other_move_targets.IsNullOrEmpty() && M_Math.Roll(Constants.AI_TARGET_SWITCH_TO_ATTACK_MOVE_WHEN_OUT_OF_MOVE_ATTACK_RANGE))
            {
                preferred_target = M_Math.GetRandomObject(other_move_targets);
            }
            else
            {
                if (!attack_map.ContainsKey(preferred_target) && !units_movable.IsNullOrEmpty() && M_Math.Roll(Constants.AI_TARGET_SWITCH_TO_CHASE_WHEN_OUT_OF_ATTACK_RANGE))
                {
                    preferred_target = M_Math.GetRandomObject(units_movable);
                }
            }

            if (attack_map.ContainsKey(preferred_target))
            {
                if (units_move_attackable.Contains(preferred_target))
                {
                    Tile attack_pos = GetReachableAttackPosition(preferred_target);

                    if (attack_pos != null)
                    {
                        yield return(StartCoroutine(Move(attack_pos)));
                    }
                    else
                    {
                        yield return(StartCoroutine(MoveRandom()));
                    }

                    yield break;
                }
                else
                {
                    Tile Final_Target_Tile = GetBestAttackPosition(preferred_target, attack_map[preferred_target].Select(path => path[path.Count - 1]).ToList());

                    List <Tile> final_path = TileManager.Instance.FindPath(m_unit.currentTile, Final_Target_Tile, m_unit);

                    Tile target = move.GetFurthestMovibleTileOnPath(final_path);

                    yield return(StartCoroutine(Move(target)));

                    yield break;
                }
            }
            else
            {
                yield return(StartCoroutine(MoveRandom( )));

                yield break;
            }
        }
    }