Search in sources :

Example 11 with UnitType

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

the class TerritoryEffectAttachment method setNoBlitz.

private void setNoBlitz(final String noBlitzUnitTypes) throws GameParseException {
    final String[] s = noBlitzUnitTypes.split(":");
    if (s.length < 1) {
        throw new GameParseException("noBlitz must have at least one unitType" + thisErrorMsg());
    }
    for (final String unitTypeName : s) {
        final UnitType ut = getData().getUnitTypeList().getUnitType(unitTypeName);
        if (ut == null) {
            throw new GameParseException("No unit called:" + unitTypeName + thisErrorMsg());
        }
        m_noBlitz.add(ut);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException)

Example 12 with UnitType

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

the class TriggerAttachment method setRemoveUnits.

private void setRemoveUnits(final String value) throws GameParseException {
    if (value == null) {
        m_removeUnits = null;
        return;
    }
    if (m_removeUnits == null) {
        m_removeUnits = new HashMap<>();
    }
    final String[] s = value.split(":");
    if (s.length < 1) {
        throw new GameParseException("Empty removeUnits list" + thisErrorMsg());
    }
    int count;
    int i = 0;
    try {
        count = getInt(s[0]);
        i++;
    } catch (final Exception e) {
        count = 1;
    }
    if (s.length < 1 || (s.length == 1 && count != -1)) {
        throw new GameParseException("Empty removeUnits list" + thisErrorMsg());
    }
    final Collection<Territory> territories = new ArrayList<>();
    final Territory terr = getData().getMap().getTerritory(s[i]);
    if (terr == null) {
        if (s[i].equalsIgnoreCase("all")) {
            territories.addAll(getData().getMap().getTerritories());
        } else {
            throw new GameParseException("Territory does not exist " + s[i] + thisErrorMsg());
        }
    } else {
        territories.add(terr);
    }
    i++;
    final IntegerMap<UnitType> map = new IntegerMap<>();
    for (; i < s.length; i++) {
        final Collection<UnitType> types = new ArrayList<>();
        final UnitType tp = getData().getUnitTypeList().getUnitType(s[i]);
        if (tp == null) {
            if (s[i].equalsIgnoreCase("all")) {
                types.addAll(getData().getUnitTypeList().getAllUnitTypes());
            } else {
                throw new GameParseException("UnitType does not exist " + s[i] + thisErrorMsg());
            }
        } else {
            types.add(tp);
        }
        for (final UnitType type : types) {
            map.add(type, count);
        }
    }
    for (final Territory t : territories) {
        if (m_removeUnits.containsKey(t)) {
            map.add(m_removeUnits.get(t));
        }
        m_removeUnits.put(t, map);
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) ArrayList(java.util.ArrayList) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException) UnitType(games.strategy.engine.data.UnitType)

Example 13 with UnitType

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

the class TriggerAttachment method removeUnits.

private static void removeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
    final CompositeChange change = new CompositeChange();
    final Collection<Unit> totalRemoved = new ArrayList<>();
    for (final UnitType ut : utMap.keySet()) {
        final int removeNum = utMap.getInt(ut);
        final Collection<Unit> toRemove = CollectionUtils.getNMatches(terr.getUnits().getUnits(), removeNum, Matches.unitIsOwnedBy(player).and(Matches.unitIsOfType(ut)));
        if (!toRemove.isEmpty()) {
            totalRemoved.addAll(toRemove);
            change.add(ChangeFactory.removeUnits(terr, toRemove));
        }
    }
    if (!change.isEmpty()) {
        final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": has removed " + MyFormatter.unitsToTextNoOwner(totalRemoved) + " owned by " + player.getName() + " in " + terr.getName();
        bridge.getHistoryWriter().startEvent(transcriptText, totalRemoved);
        bridge.addChange(change);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Unit(games.strategy.engine.data.Unit)

Example 14 with UnitType

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

the class TriggerAttachment method triggerUnitPropertyChange.

public static void triggerUnitPropertyChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, unitPropertyMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final CompositeChange change = new CompositeChange();
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        for (final Tuple<String, String> property : t.getUnitProperty()) {
            for (final UnitType unitType : t.getUnitType()) {
                String newValue = property.getSecond();
                boolean clearFirst = false;
                // test if we are resetting the variable first, and if so, remove the leading "-reset-" or "-clear-"
                if (newValue.length() > 0 && (newValue.startsWith(PREFIX_CLEAR) || newValue.startsWith(PREFIX_RESET))) {
                    newValue = newValue.replaceFirst(PREFIX_CLEAR, "").replaceFirst(PREFIX_RESET, "");
                    clearFirst = true;
                }
                // covers UnitAttachment, UnitSupportAttachment
                if (t.getUnitAttachmentName().getFirst().equals("UnitAttachment")) {
                    final UnitAttachment attachment = UnitAttachment.get(unitType, t.getUnitAttachmentName().getSecond());
                    if (newValue.equals(attachment.getRawPropertyString(property.getFirst()))) {
                        continue;
                    }
                    if (clearFirst && newValue.length() < 1) {
                        change.add(ChangeFactory.attachmentPropertyReset(attachment, property.getFirst()));
                    } else {
                        change.add(ChangeFactory.attachmentPropertyChange(attachment, newValue, property.getFirst(), clearFirst));
                    }
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Setting " + property.getFirst() + (newValue.length() > 0 ? " to " + newValue : " cleared ") + " for " + t.getUnitAttachmentName().getSecond() + " attached to " + unitType.getName());
                } else if (t.getUnitAttachmentName().getFirst().equals("UnitSupportAttachment")) {
                    final UnitSupportAttachment attachment = UnitSupportAttachment.get(unitType, t.getUnitAttachmentName().getSecond());
                    if (newValue.equals(attachment.getRawPropertyString(property.getFirst()))) {
                        continue;
                    }
                    if (clearFirst && newValue.length() < 1) {
                        change.add(ChangeFactory.attachmentPropertyReset(attachment, property.getFirst()));
                    } else {
                        change.add(ChangeFactory.attachmentPropertyChange(attachment, newValue, property.getFirst(), clearFirst));
                    }
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Setting " + property.getFirst() + (newValue.length() > 0 ? " to " + newValue : " cleared ") + " for " + t.getUnitAttachmentName().getSecond() + " attached to " + unitType.getName());
                }
            // TODO add other attachment changes here if they attach to a unitType
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 15 with UnitType

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

the class TriggerAttachment method setPurchase.

private void setPurchase(final String place) throws GameParseException {
    if (place == null) {
        m_purchase = null;
        return;
    }
    final String[] s = place.split(":");
    if (s.length < 1) {
        throw new GameParseException("Empty purchase list" + thisErrorMsg());
    }
    int count;
    int i = 0;
    try {
        count = getInt(s[0]);
        i++;
    } catch (final Exception e) {
        count = 1;
    }
    if (s.length < 1 || (s.length == 1 && count != -1)) {
        throw new GameParseException("Empty purchase list" + thisErrorMsg());
    }
    if (m_purchase == null) {
        m_purchase = new IntegerMap<>();
    }
    for (; i < s.length; i++) {
        final UnitType type = getData().getUnitTypeList().getUnitType(s[i]);
        if (type == null) {
            throw new GameParseException("UnitType does not exist " + s[i] + thisErrorMsg());
        }
        m_purchase.add(type, count);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException)

Aggregations

UnitType (games.strategy.engine.data.UnitType)211 Test (org.junit.jupiter.api.Test)108 IntegerMap (games.strategy.util.IntegerMap)86 Route (games.strategy.engine.data.Route)76 Unit (games.strategy.engine.data.Unit)76 PlayerID (games.strategy.engine.data.PlayerID)64 Territory (games.strategy.engine.data.Territory)58 TripleAUnit (games.strategy.triplea.TripleAUnit)49 ArrayList (java.util.ArrayList)44 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)36 GameParseException (games.strategy.engine.data.GameParseException)29 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)23 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)21 HashSet (java.util.HashSet)17 GameData (games.strategy.engine.data.GameData)15 Change (games.strategy.engine.data.Change)14 CompositeChange (games.strategy.engine.data.CompositeChange)14 Resource (games.strategy.engine.data.Resource)13 NamedAttachable (games.strategy.engine.data.NamedAttachable)11 ProductionRule (games.strategy.engine.data.ProductionRule)11