Search in sources :

Example 1 with BattleRecordsList

use of games.strategy.engine.data.BattleRecordsList in project triplea by triplea-game.

the class RulesAttachment method isSatisfied.

@Override
public boolean isSatisfied(Map<ICondition, Boolean> testedConditions, final IDelegateBridge delegateBridge) {
    if (testedConditions != null) {
        if (testedConditions.containsKey(this)) {
            return testedConditions.get(this);
        }
    }
    boolean objectiveMet = true;
    final List<PlayerID> players = getPlayers();
    final GameData data = delegateBridge.getData();
    // check meta conditions (conditions which hold other conditions)
    if (objectiveMet && m_conditions.size() > 0) {
        if (testedConditions == null) {
            testedConditions = testAllConditionsRecursive(getAllConditionsRecursive(new HashSet<>(m_conditions), null), null, delegateBridge);
        }
        objectiveMet = areConditionsMet(new ArrayList<>(m_conditions), testedConditions, m_conditionType);
    }
    // check switch (on/off)
    if (objectiveMet) {
        objectiveMet = m_switch;
    }
    // check turn limits
    if (objectiveMet && m_turns != null) {
        objectiveMet = checkTurns(data);
    }
    // check custom game property options
    if (objectiveMet && m_gameProperty != null) {
        objectiveMet = this.getGamePropertyState(data);
    }
    // Check for unit presence (Veqryn)
    if (objectiveMet && getDirectPresenceTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getDirectPresenceTerritories();
        objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "direct", getTerritoryCount(), players, data);
    }
    // Check for unit presence (Veqryn)
    if (objectiveMet && getAlliedPresenceTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getAlliedPresenceTerritories();
        objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "allied", getTerritoryCount(), players, data);
    }
    // Check for unit presence (Veqryn)
    if (objectiveMet && getEnemyPresenceTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getEnemyPresenceTerritories();
        objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy", getTerritoryCount(), players, data);
    }
    // Check for direct unit exclusions (veqryn)
    if (objectiveMet && getDirectExclusionTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getDirectExclusionTerritories();
        objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "direct", getTerritoryCount(), players, data);
    }
    // Check for allied unit exclusions
    if (objectiveMet && getAlliedExclusionTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getAlliedExclusionTerritories();
        objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "allied", getTerritoryCount(), players, data);
    }
    // Check for enemy unit exclusions (ANY UNITS)
    if (objectiveMet && getEnemyExclusionTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getEnemyExclusionTerritories();
        objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy", getTerritoryCount(), players, data);
    }
    // Check for enemy unit exclusions (SURFACE UNITS with ATTACK POWER)
    if (objectiveMet && getEnemySurfaceExclusionTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getEnemySurfaceExclusionTerritories();
        objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy_surface", getTerritoryCount(), players, data);
    }
    // Check for Territory Ownership rules
    if (objectiveMet && getAlliedOwnershipTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getAlliedOwnershipTerritories();
        final Set<Territory> listedTerritories;
        if (terrs.length == 1) {
            if (terrs[0].equals("original")) {
                final Collection<PlayerID> allies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, allies, data);
            } else if (terrs[0].equals("enemy")) {
                final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
            } else {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            }
        } else if (terrs.length == 2) {
            if (terrs[1].equals("original")) {
                final Collection<PlayerID> allies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, allies, data);
            } else if (terrs[1].equals("enemy")) {
                final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
            } else {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            }
        } else {
            listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
        }
        objectiveMet = checkAlliedOwnership(listedTerritories, getTerritoryCount(), players, data);
    }
    // Check for Direct Territory Ownership rules
    if (objectiveMet && getDirectOwnershipTerritories() != null) {
        // Get the listed territories
        final String[] terrs = getDirectOwnershipTerritories();
        final Set<Territory> listedTerritories;
        if (terrs.length == 1) {
            if (terrs[0].equals("original")) {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            } else if (terrs[0].equals("enemy")) {
                final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
            } else {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            }
        } else if (terrs.length == 2) {
            if (terrs[1].equals("original")) {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            } else if (terrs[1].equals("enemy")) {
                final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
            } else {
                listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
            }
        } else {
            listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
        }
        objectiveMet = checkDirectOwnership(listedTerritories, getTerritoryCount(), players);
    }
    // get attached to player
    final PlayerID playerAttachedTo = (PlayerID) getAttachedTo();
    if (objectiveMet && getAtWarPlayers() != null) {
        objectiveMet = checkAtWar(playerAttachedTo, getAtWarPlayers(), getAtWarCount(), data);
    }
    if (objectiveMet && m_techs != null) {
        objectiveMet = checkTechs(playerAttachedTo, data);
    }
    // check for relationships
    if (objectiveMet && m_relationship.size() > 0) {
        objectiveMet = checkRelationships();
    }
    // check for battle stats
    if (objectiveMet && m_destroyedTUV != null) {
        final String[] s = m_destroyedTUV.split(":");
        final int requiredDestroyedTuv = getInt(s[0]);
        if (requiredDestroyedTuv >= 0) {
            final boolean justCurrentRound = s[1].equals("currentRound");
            final int destroyedTuvForThisRoundSoFar = BattleRecordsList.getTuvDamageCausedByPlayer(playerAttachedTo, data.getBattleRecordsList(), 0, data.getSequence().getRound(), justCurrentRound, false);
            if (requiredDestroyedTuv > destroyedTuvForThisRoundSoFar) {
                objectiveMet = false;
            }
            if (getCountEach()) {
                m_eachMultiple = destroyedTuvForThisRoundSoFar;
            }
        }
    }
    // check for battles
    if (objectiveMet && !m_battle.isEmpty()) {
        final BattleRecordsList brl = data.getBattleRecordsList();
        final int round = data.getSequence().getRound();
        for (final Tuple<String, List<Territory>> entry : m_battle) {
            final String[] type = entry.getFirst().split(":");
            // they could be "any", and if they are "any" then this would be null, which is good!
            final PlayerID attacker = data.getPlayerList().getPlayerId(type[0]);
            final PlayerID defender = data.getPlayerList().getPlayerId(type[1]);
            final String resultType = type[2];
            final String roundType = type[3];
            int start = 0;
            int end = round;
            final boolean currentRound = roundType.equalsIgnoreCase("currentRound");
            if (!currentRound) {
                final String[] rounds = roundType.split("-");
                start = getInt(rounds[0]);
                end = getInt(rounds[1]);
            }
            objectiveMet = BattleRecordsList.getWereThereBattlesInTerritoriesMatching(attacker, defender, resultType, entry.getSecond(), brl, start, end, currentRound);
            if (!objectiveMet) {
                break;
            }
        }
    }
    // "chance" should ALWAYS be checked last!
    final int hitTarget = getChanceToHit();
    final int diceSides = getChanceDiceSides();
    final int incrementOnFailure = this.getChanceIncrementOnFailure();
    final int decrementOnSuccess = this.getChanceDecrementOnSuccess();
    if (objectiveMet && (hitTarget != diceSides || incrementOnFailure != 0 || decrementOnSuccess != 0)) {
        if (diceSides <= 0 || hitTarget >= diceSides) {
            objectiveMet = true;
            changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, false);
        } else if (hitTarget <= 0) {
            objectiveMet = false;
            changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, false);
        } else {
            // there is an issue with maps using thousands of chance triggers: they are causing the cypted random source
            // (ie: live and pbem
            // games) to lock up or error out
            // so we need to slow them down a bit, until we come up with a better solution (like aggregating all the chances
            // together, then
            // getting a ton of random numbers at once instead of one at a time)
            Interruptibles.sleep(100);
            final int rollResult = delegateBridge.getRandom(diceSides, null, DiceType.ENGINE, "Attempting the Condition: " + MyFormatter.attachmentNameToText(this.getName())) + 1;
            objectiveMet = rollResult <= hitTarget;
            final String notificationMessage = (objectiveMet ? TRIGGER_CHANCE_SUCCESSFUL : TRIGGER_CHANCE_FAILURE) + " (Rolled at " + hitTarget + " out of " + diceSides + " Result: " + rollResult + "  for " + MyFormatter.attachmentNameToText(this.getName()) + ")";
            delegateBridge.getHistoryWriter().startEvent(notificationMessage);
            changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, true);
            ((ITripleAPlayer) delegateBridge.getRemotePlayer(delegateBridge.getPlayerId())).reportMessage(notificationMessage, notificationMessage);
        }
    }
    return objectiveMet != m_invert;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) BattleRecordsList(games.strategy.engine.data.BattleRecordsList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) BattleRecordsList(games.strategy.engine.data.BattleRecordsList)

Aggregations

BattleRecordsList (games.strategy.engine.data.BattleRecordsList)1 GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 Territory (games.strategy.engine.data.Territory)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1