Search in sources :

Example 26 with GameData

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

the class DiceRoll method rollAa.

static DiceRoll rollAa(final Collection<Unit> validAttackingUnitsForThisRoll, final Collection<Unit> defendingAaForThisRoll, final IDelegateBridge bridge, final Territory location, final boolean defending) {
    {
        final Set<Unit> duplicatesCheckSet1 = new HashSet<>(validAttackingUnitsForThisRoll);
        if (validAttackingUnitsForThisRoll.size() != duplicatesCheckSet1.size()) {
            throw new IllegalStateException("Duplicate Units Detected: Original List:" + validAttackingUnitsForThisRoll + "  HashSet:" + duplicatesCheckSet1);
        }
        final Set<Unit> duplicatesCheckSet2 = new HashSet<>(defendingAaForThisRoll);
        if (defendingAaForThisRoll.size() != duplicatesCheckSet2.size()) {
            throw new IllegalStateException("Duplicate Units Detected: Original List:" + defendingAaForThisRoll + "  HashSet:" + duplicatesCheckSet2);
        }
    }
    final List<Unit> defendingAa = CollectionUtils.getMatches(defendingAaForThisRoll, (defending ? Matches.unitAttackAaIsGreaterThanZeroAndMaxAaAttacksIsNotZero() : Matches.unitOffensiveAttackAaIsGreaterThanZeroAndMaxAaAttacksIsNotZero()));
    if (defendingAa.isEmpty()) {
        return new DiceRoll(new ArrayList<>(0), 0, 0);
    }
    final GameData data = bridge.getData();
    final int totalAAattacksTotal = getTotalAAattacks(defendingAa, validAttackingUnitsForThisRoll);
    if (totalAAattacksTotal <= 0) {
        return new DiceRoll(new ArrayList<>(0), 0, 0);
    }
    // determine dicesides for everyone (we are not going to consider the possibility of different dicesides within the
    // same typeAA)
    final Tuple<Integer, Integer> attackThenDiceSidesForAll = getAAattackAndMaxDiceSides(defendingAa, data, defending);
    // final int highestAttackPower = attackThenDiceSidesForAll.getFirst();
    final int chosenDiceSizeForAll = attackThenDiceSidesForAll.getSecond();
    int hits = 0;
    final List<Die> sortedDice = new ArrayList<>();
    final String typeAa = UnitAttachment.get(defendingAa.get(0).getType()).getTypeAa();
    // LOW LUCK
    final Triple<Integer, Integer, Boolean> triple = getTotalAaPowerThenHitsAndFillSortedDiceThenIfAllUseSameAttack(null, null, defending, defendingAa, validAttackingUnitsForThisRoll, data, false);
    final int totalPower = triple.getFirst();
    if (Properties.getLowLuck(data) || Properties.getLowLuckAaOnly(data)) {
        final String annotation = "Roll " + typeAa + " in " + location.getName();
        hits += getLowLuckHits(bridge, sortedDice, totalPower, chosenDiceSizeForAll, defendingAa.get(0).getOwner(), annotation);
    } else {
        final String annotation = "Roll " + typeAa + " in " + location.getName();
        final int[] dice = bridge.getRandom(chosenDiceSizeForAll, totalAAattacksTotal, defendingAa.get(0).getOwner(), DiceType.COMBAT, annotation);
        hits += getTotalAaPowerThenHitsAndFillSortedDiceThenIfAllUseSameAttack(dice, sortedDice, defending, defendingAa, validAttackingUnitsForThisRoll, data, true).getSecond();
    }
    final double expectedHits = ((double) totalPower) / chosenDiceSizeForAll;
    final DiceRoll roll = new DiceRoll(sortedDice, hits, expectedHits);
    final String annotation = typeAa + " fire in " + location + " : " + MyFormatter.asDice(roll);
    bridge.getHistoryWriter().addChildToEvent(annotation, roll);
    return roll;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 27 with GameData

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

the class EditDelegate method changeTerritoryOwner.

@Override
public String changeTerritoryOwner(final Territory territory, final PlayerID player) {
    String result = checkEditMode();
    if (result != null) {
        return result;
    }
    final GameData data = getData();
    // validate this edit
    result = EditValidator.validateChangeTerritoryOwner(data, territory);
    if (result != null) {
        return result;
    }
    logEvent("Changing ownership of " + territory.getName() + " from " + territory.getOwner().getName() + " to " + player.getName(), territory);
    if (!data.getRelationshipTracker().isAtWar(territory.getOwner(), player)) {
        // change ownership of friendly factories
        final Collection<Unit> units = territory.getUnits().getMatches(Matches.unitIsInfrastructure());
        for (final Unit unit : units) {
            bridge.addChange(ChangeFactory.changeOwner(unit, player, territory));
        }
    } else {
        final Predicate<Unit> enemyNonCom = Matches.unitIsInfrastructure().and(Matches.enemyUnit(player, data));
        final Collection<Unit> units = territory.getUnits().getMatches(enemyNonCom);
        // mark no movement for enemy units
        bridge.addChange(ChangeFactory.markNoMovementChange(units));
        // change ownership of enemy AA and factories
        for (final Unit unit : units) {
            bridge.addChange(ChangeFactory.changeOwner(unit, player, territory));
        }
    }
    // change ownership of territory
    bridge.addChange(ChangeFactory.changeOwner(territory, player));
    return null;
}
Also used : GameData(games.strategy.engine.data.GameData) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 28 with GameData

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

the class EndRoundDelegate method checkVictoryCities.

private void checkVictoryCities(final IDelegateBridge bridge, final String victoryMessage, final String victoryType) {
    final GameData data = bridge.getData();
    final Collection<Territory> territories = data.getMap().getTerritories();
    for (final String allianceName : data.getAllianceTracker().getAlliances()) {
        final int vcAmount = getVcAmount(data, allianceName, victoryType);
        final Set<PlayerID> teamMembers = data.getAllianceTracker().getPlayersInAlliance(allianceName);
        int teamVCs = 0;
        for (final Territory t : territories) {
            if (Matches.isTerritoryOwnedBy(teamMembers).test(t)) {
                final TerritoryAttachment ta = TerritoryAttachment.get(t);
                if (ta != null) {
                    teamVCs += ta.getVictoryCity();
                }
            }
        }
        if (teamVCs >= vcAmount) {
            bridge.getHistoryWriter().startEvent(allianceName + victoryMessage + vcAmount + " Victory Cities!");
            final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(allianceName);
            // Added this to end the game on victory conditions
            signalGameOver(allianceName + victoryMessage + vcAmount + " Victory Cities!", winners, bridge);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment)

Example 29 with GameData

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

the class ImprovedShipyardsAdvance method perform.

@Override
public void perform(final PlayerID id, final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    if (!Properties.getUseShipyards(data)) {
        return;
    }
    final ProductionFrontier current = id.getProductionFrontier();
    // they already have it
    if (current.getName().endsWith("Shipyards")) {
        return;
    }
    final String industrialTechName = current.getName() + "Shipyards";
    final ProductionFrontier advancedTech = data.getProductionFrontierList().getProductionFrontier(industrialTechName);
    // it doesnt exist, dont crash
    if (advancedTech == null) {
        Logger.getLogger(TechAdvance.class.getName()).log(Level.WARNING, "No tech named:" + industrialTechName + " not adding tech");
        return;
    }
    final Change prodChange = ChangeFactory.changeProductionFrontier(id, advancedTech);
    bridge.addChange(prodChange);
}
Also used : GameData(games.strategy.engine.data.GameData) Change(games.strategy.engine.data.Change) ProductionFrontier(games.strategy.engine.data.ProductionFrontier)

Example 30 with GameData

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

the class InitializationDelegate method initOriginalOwner.

private static void initOriginalOwner(final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    final CompositeChange changes = new CompositeChange();
    for (final Territory current : data.getMap()) {
        if (!current.getOwner().isNull()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
            if (territoryAttachment.getOriginalOwner() == null && current.getOwner() != null) {
                changes.add(OriginalOwnerTracker.addOriginalOwnerChange(current, current.getOwner()));
            }
            final Collection<Unit> factoryAndInfrastructure = current.getUnits().getMatches(Matches.unitIsInfrastructure());
            changes.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, current.getOwner()));
        } else if (!current.isWater()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
        }
    }
    bridge.getHistoryWriter().startEvent("Adding original owners");
    bridge.addChange(changes);
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Aggregations

GameData (games.strategy.engine.data.GameData)204 Unit (games.strategy.engine.data.Unit)100 PlayerID (games.strategy.engine.data.PlayerID)92 Territory (games.strategy.engine.data.Territory)92 ArrayList (java.util.ArrayList)83 TripleAUnit (games.strategy.triplea.TripleAUnit)64 HashSet (java.util.HashSet)50 CompositeChange (games.strategy.engine.data.CompositeChange)40 List (java.util.List)36 HashMap (java.util.HashMap)32 Set (java.util.Set)32 Route (games.strategy.engine.data.Route)31 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)30 Collection (java.util.Collection)29 UnitType (games.strategy.engine.data.UnitType)26 Change (games.strategy.engine.data.Change)24 Test (org.junit.jupiter.api.Test)23 Resource (games.strategy.engine.data.Resource)22 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)22 Map (java.util.Map)21