Search in sources :

Example 6 with Resource

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

the class UnitAttachment method setFuelFlatCost.

private void setFuelFlatCost(final String value) throws GameParseException {
    final String[] s = value.split(":");
    if (s.length != 2) {
        throw new GameParseException("fuelFlatCost must have two fields" + thisErrorMsg());
    }
    final String resourceToProduce = s[1];
    // validate that this resource exists in the xml
    final Resource r = getData().getResourceList().getResource(resourceToProduce);
    if (r == null) {
        throw new GameParseException("fuelFlatCost: No resource called:" + resourceToProduce + thisErrorMsg());
    }
    final int n = getInt(s[0]);
    if (n < 0) {
        throw new GameParseException("fuelFlatCost must have positive values" + thisErrorMsg());
    }
    m_fuelFlatCost.put(r, n);
}
Also used : Resource(games.strategy.engine.data.Resource) GameParseException(games.strategy.engine.data.GameParseException)

Example 7 with Resource

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

the class AbstractEndTurnDelegate method rollWarBondsForFriends.

private String rollWarBondsForFriends(final IDelegateBridge delegateBridge, final PlayerID player, final GameData data) {
    final int count = TechAbilityAttachment.getWarBondDiceNumber(player, data);
    final int sides = TechAbilityAttachment.getWarBondDiceSides(player, data);
    if (sides <= 0 || count <= 0) {
        return "";
    }
    // basically, if we are sharing our technology with someone, and we have warbonds but they do not, then we roll our
    // warbonds and give
    // them the proceeds (Global 1940)
    final PlayerAttachment playerattachment = PlayerAttachment.get(player);
    if (playerattachment == null) {
        return "";
    }
    final Collection<PlayerID> shareWith = playerattachment.getShareTechnology();
    if (shareWith == null || shareWith.isEmpty()) {
        return "";
    }
    // take first one
    PlayerID giveWarBondsTo = null;
    for (final PlayerID p : shareWith) {
        final int diceCount = TechAbilityAttachment.getWarBondDiceNumber(p, data);
        final int diceSides = TechAbilityAttachment.getWarBondDiceSides(p, data);
        if (diceSides <= 0 && diceCount <= 0) {
            // they cannot have this tech)
            if (canPlayerCollectIncome(p, data)) {
                giveWarBondsTo = p;
                break;
            }
        }
    }
    if (giveWarBondsTo == null) {
        return "";
    }
    final String annotation = player.getName() + " rolling to resolve War Bonds, and giving results to " + giveWarBondsTo.getName() + ": ";
    final DiceRoll dice = DiceRoll.rollNDice(delegateBridge, count, sides, player, DiceType.NONCOMBAT, annotation);
    int totalWarBonds = 0;
    for (int i = 0; i < dice.size(); i++) {
        totalWarBonds += dice.getDie(i).getValue() + 1;
    }
    final Resource pus = data.getResourceList().getResource(Constants.PUS);
    final int currentPUs = giveWarBondsTo.getResources().getQuantity(pus);
    final String transcriptText = player.getName() + " rolls " + totalWarBonds + MyFormatter.pluralize(" PU", totalWarBonds) + " from War Bonds, giving the total to " + giveWarBondsTo.getName() + ", who ends with " + (currentPUs + totalWarBonds) + MyFormatter.pluralize(" PU", (currentPUs + totalWarBonds)) + " total";
    delegateBridge.getHistoryWriter().startEvent(transcriptText);
    final Change change = ChangeFactory.changeResourcesChange(giveWarBondsTo, pus, totalWarBonds);
    delegateBridge.addChange(change);
    getRemotePlayer(player).reportMessage(annotation + MyFormatter.asDice(dice), annotation + MyFormatter.asDice(dice));
    return transcriptText + "<br />";
}
Also used : PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) PlayerID(games.strategy.engine.data.PlayerID) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change)

Example 8 with Resource

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

the class AbstractEndTurnDelegate method start.

@Override
public void start() {
    // figure out our current PUs before we do anything else, including super methods
    final GameData data = bridge.getData();
    final Resource pus = data.getResourceList().getResource(Constants.PUS);
    final int leftOverPUs = bridge.getPlayerId().getResources().getQuantity(pus);
    final IntegerMap<Resource> leftOverResources = bridge.getPlayerId().getResources().getResourcesCopy();
    super.start();
    if (!needToInitialize) {
        return;
    }
    final StringBuilder endTurnReport = new StringBuilder();
    hasPostedTurnSummary = false;
    final PlayerAttachment pa = PlayerAttachment.get(player);
    // can't collect unless you own your own capital
    if (!canPlayerCollectIncome(player, data)) {
        endTurnReport.append(rollWarBondsForFriends(bridge, player, data));
    // we do not collect any income this turn
    } else {
        // just collect resources
        final Collection<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
        int toAdd = getProduction(territories);
        final int blockadeLoss = getBlockadeProductionLoss(player, data, bridge, endTurnReport);
        toAdd -= blockadeLoss;
        toAdd *= Properties.getPuMultiplier(data);
        int total = player.getResources().getQuantity(pus) + toAdd;
        final String transcriptText;
        if (blockadeLoss == 0) {
            transcriptText = player.getName() + " collect " + toAdd + MyFormatter.pluralize(" PU", toAdd) + "; end with " + total + MyFormatter.pluralize(" PU", total);
        } else {
            transcriptText = player.getName() + " collect " + toAdd + MyFormatter.pluralize(" PU", toAdd) + " (" + blockadeLoss + " lost to blockades)" + "; end with " + total + MyFormatter.pluralize(" PU", total);
        }
        bridge.getHistoryWriter().startEvent(transcriptText);
        endTurnReport.append(transcriptText).append("<br />");
        // do war bonds
        final int bonds = rollWarBonds(bridge, player, data);
        if (bonds > 0) {
            total += bonds;
            toAdd += bonds;
            final String bondText = player.getName() + " collect " + bonds + MyFormatter.pluralize(" PU", bonds) + " from War Bonds; end with " + total + MyFormatter.pluralize(" PU", total);
            bridge.getHistoryWriter().startEvent(bondText);
            endTurnReport.append("<br />").append(bondText).append("<br />");
        }
        if (total < 0) {
            toAdd -= total;
        }
        final Change change = ChangeFactory.changeResourcesChange(player, pus, toAdd);
        bridge.addChange(change);
        if (data.getProperties().get(Constants.PACIFIC_THEATER, false) && pa != null) {
            final Change changeVp = (ChangeFactory.attachmentPropertyChange(pa, (pa.getVps() + (toAdd / 10) + (pa.getCaptureVps() / 10)), "vps"));
            final Change changeCaptureVp = ChangeFactory.attachmentPropertyChange(pa, "0", "captureVps");
            final CompositeChange ccVp = new CompositeChange(changeVp, changeCaptureVp);
            bridge.addChange(ccVp);
        }
        endTurnReport.append("<br />").append(addOtherResources(bridge));
        endTurnReport.append("<br />").append(doNationalObjectivesAndOtherEndTurnEffects(bridge));
        final IntegerMap<Resource> income = player.getResources().getResourcesCopy();
        income.subtract(leftOverResources);
        endTurnReport.append("<br />").append(BonusIncomeUtils.addBonusIncome(income, bridge, player));
        // now we do upkeep costs, including upkeep cost as a percentage of our entire income for this turn (including
        // NOs)
        final int currentPUs = player.getResources().getQuantity(pus);
        int relationshipUpkeepCostFlat = 0;
        int relationshipUpkeepCostPercentage = 0;
        for (final Relationship r : data.getRelationshipTracker().getRelationships(player)) {
            final String[] upkeep = r.getRelationshipType().getRelationshipTypeAttachment().getUpkeepCost().split(":");
            if (upkeep.length == 1 || upkeep[1].equals(RelationshipTypeAttachment.UPKEEP_FLAT)) {
                relationshipUpkeepCostFlat += Integer.parseInt(upkeep[0]);
            } else if (upkeep[1].equals(RelationshipTypeAttachment.UPKEEP_PERCENTAGE)) {
                relationshipUpkeepCostPercentage += Integer.parseInt(upkeep[0]);
            }
        }
        relationshipUpkeepCostPercentage = Math.min(100, relationshipUpkeepCostPercentage);
        int relationshipUpkeepTotalCost = 0;
        if (relationshipUpkeepCostPercentage != 0) {
            final float gainedPus = Math.max(0, currentPUs - leftOverPUs);
            relationshipUpkeepTotalCost += Math.round(gainedPus * (relationshipUpkeepCostPercentage) / 100f);
        }
        if (relationshipUpkeepCostFlat != 0) {
            relationshipUpkeepTotalCost += relationshipUpkeepCostFlat;
        }
        // we can't remove more than we have, and we also must flip the sign
        relationshipUpkeepTotalCost = Math.min(currentPUs, relationshipUpkeepTotalCost);
        relationshipUpkeepTotalCost = -1 * relationshipUpkeepTotalCost;
        if (relationshipUpkeepTotalCost != 0) {
            final int newTotal = currentPUs + relationshipUpkeepTotalCost;
            final String transcriptText2 = player.getName() + (relationshipUpkeepTotalCost < 0 ? " pays " : " taxes ") + (-1 * relationshipUpkeepTotalCost) + MyFormatter.pluralize(" PU", relationshipUpkeepTotalCost) + " in order to maintain current relationships with other players, and ends the turn with " + newTotal + MyFormatter.pluralize(" PU", newTotal);
            bridge.getHistoryWriter().startEvent(transcriptText2);
            endTurnReport.append("<br />").append(transcriptText2).append("<br />");
            final Change upkeep = ChangeFactory.changeResourcesChange(player, pus, relationshipUpkeepTotalCost);
            bridge.addChange(upkeep);
        }
    }
    if (GameStepPropertiesHelper.isRepairUnits(data)) {
        MoveDelegate.repairMultipleHitPointUnits(bridge, bridge.getPlayerId());
    }
    if (isGiveUnitsByTerritory() && pa != null && pa.getGiveUnitControl() != null && !pa.getGiveUnitControl().isEmpty()) {
        changeUnitOwnership(bridge);
    }
    needToInitialize = false;
    showEndTurnReport(endTurnReport.toString());
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) Relationship(games.strategy.engine.data.RelationshipTracker.Relationship) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 9 with Resource

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

the class AbstractEndTurnDelegate method findEstimatedIncome.

/**
 * Find estimated income for given player. This only takes into account income from territories,
 * units, NOs, and triggers. It ignores blockades, war bonds, relationship upkeep, and bonus income.
 */
public static IntegerMap<Resource> findEstimatedIncome(final PlayerID player, final GameData data) {
    final IntegerMap<Resource> resources = new IntegerMap<>();
    // Only add territory resources if endTurn not endTurnNoPU
    for (GameStep step : data.getSequence()) {
        if (player.equals(step.getPlayerId()) && step.getDelegate().getName().equals("endTurn")) {
            final List<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
            final int pusFromTerritories = getProduction(territories, data) * Properties.getPuMultiplier(data);
            resources.add(new Resource(Constants.PUS, data), pusFromTerritories);
            resources.add(EndTurnDelegate.getResourceProduction(territories, data));
        }
    }
    // Add unit generated resources, NOs, and triggers
    resources.add(EndTurnDelegate.findUnitCreatedResources(player, data));
    resources.add(EndTurnDelegate.findNationalObjectiveAndTriggerResources(player, data));
    return resources;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) Resource(games.strategy.engine.data.Resource) GameStep(games.strategy.engine.data.GameStep)

Example 10 with Resource

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

the class EndTurnDelegate method findUnitCreatedResources.

/**
 * Find all of the resources that will be created by units on the map.
 */
public static IntegerMap<Resource> findUnitCreatedResources(final PlayerID player, final GameData data) {
    final IntegerMap<Resource> resourceTotalsMap = new IntegerMap<>();
    final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesResources());
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
        for (final Unit unit : myCreators) {
            final IntegerMap<Resource> generatedResourcesMap = UnitAttachment.get(unit.getType()).getCreatesResourcesList();
            resourceTotalsMap.add(generatedResourcesMap);
        }
    }
    final Resource pus = new Resource(Constants.PUS, data);
    if (resourceTotalsMap.containsKey(pus)) {
        resourceTotalsMap.put(pus, resourceTotalsMap.getInt(pus) * Properties.getPuMultiplier(data));
    }
    return resourceTotalsMap;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit)

Aggregations

Resource (games.strategy.engine.data.Resource)71 PlayerID (games.strategy.engine.data.PlayerID)22 IntegerMap (games.strategy.util.IntegerMap)16 GameData (games.strategy.engine.data.GameData)15 Unit (games.strategy.engine.data.Unit)15 ArrayList (java.util.ArrayList)15 Territory (games.strategy.engine.data.Territory)14 UnitType (games.strategy.engine.data.UnitType)13 Change (games.strategy.engine.data.Change)11 CompositeChange (games.strategy.engine.data.CompositeChange)11 ProductionRule (games.strategy.engine.data.ProductionRule)10 TripleAUnit (games.strategy.triplea.TripleAUnit)10 NamedAttachable (games.strategy.engine.data.NamedAttachable)9 ResourceCollection (games.strategy.engine.data.ResourceCollection)8 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)7 GameParseException (games.strategy.engine.data.GameParseException)6 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Test (org.junit.jupiter.api.Test)5