Search in sources :

Example 1 with Relationship

use of games.strategy.engine.data.RelationshipTracker.Relationship 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 2 with Relationship

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

the class RulesAttachment method checkRelationships.

/**
 * checks if all relationship requirements are set
 *
 * @return whether all relationships as are required are set correctly.
 */
private boolean checkRelationships() {
    for (final String encodedRelationCheck : m_relationship) {
        final String[] relationCheck = encodedRelationCheck.split(":");
        final PlayerID p1 = getData().getPlayerList().getPlayerId(relationCheck[0]);
        final PlayerID p2 = getData().getPlayerList().getPlayerId(relationCheck[1]);
        final int relationshipsExistance = Integer.parseInt(relationCheck[3]);
        final Relationship currentRelationship = getData().getRelationshipTracker().getRelationship(p1, p2);
        final RelationshipType currentRelationshipType = currentRelationship.getRelationshipType();
        if (!relationShipExistsLongEnnough(currentRelationship, relationshipsExistance)) {
            return false;
        }
        if (!((relationCheck[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_ALLIED) && Matches.relationshipTypeIsAllied().test(currentRelationshipType)) || (relationCheck[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_NEUTRAL) && Matches.relationshipTypeIsNeutral().test(currentRelationshipType)) || (relationCheck[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_WAR) && Matches.relationshipTypeIsAtWar().test(currentRelationshipType)) || currentRelationshipType.equals(getData().getRelationshipTypeList().getRelationshipType(relationCheck[2])))) {
            return false;
        }
    }
    return true;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Relationship(games.strategy.engine.data.RelationshipTracker.Relationship) RelationshipType(games.strategy.engine.data.RelationshipType)

Aggregations

Relationship (games.strategy.engine.data.RelationshipTracker.Relationship)2 Change (games.strategy.engine.data.Change)1 CompositeChange (games.strategy.engine.data.CompositeChange)1 GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 RelationshipType (games.strategy.engine.data.RelationshipType)1 Resource (games.strategy.engine.data.Resource)1 Territory (games.strategy.engine.data.Territory)1 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)1