use of games.strategy.triplea.attachments.ICondition in project triplea by triplea-game.
the class EndTurnDelegate method determineNationalObjectives.
/**
* Determine if National Objectives have been met, and then do them.
*/
private String determineNationalObjectives(final IDelegateBridge bridge) {
final GameData data = getData();
final PlayerID player = data.getSequence().getStep().getPlayerId();
// Find and test all the conditions for triggers and national objectives
final Set<TriggerAttachment> triggers = new HashSet<>();
final List<RulesAttachment> objectives = new ArrayList<>();
final HashMap<ICondition, Boolean> testedConditions = testNationalObjectivesAndTriggers(player, data, bridge, triggers, objectives);
// Execute triggers
final StringBuilder endTurnReport = new StringBuilder();
final boolean useTriggers = Properties.getTriggers(data);
if (useTriggers && !triggers.isEmpty()) {
final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(triggers, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
endTurnReport.append(TriggerAttachment.triggerResourceChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true)).append("<br />");
}
// Execute national objectives
for (final RulesAttachment rule : objectives) {
int uses = rule.getUses();
if (uses == 0 || !rule.isSatisfied(testedConditions)) {
continue;
}
int toAdd = rule.getObjectiveValue();
toAdd *= Properties.getPuMultiplier(data);
toAdd *= rule.getEachMultiple();
int total = player.getResources().getQuantity(Constants.PUS) + toAdd;
if (total < 0) {
toAdd -= total;
total = 0;
}
final Change change = ChangeFactory.changeResourcesChange(player, data.getResourceList().getResource(Constants.PUS), toAdd);
bridge.addChange(change);
if (uses > 0) {
uses--;
final Change use = ChangeFactory.attachmentPropertyChange(rule, Integer.toString(uses), "uses");
bridge.addChange(use);
}
final String puMessage = MyFormatter.attachmentNameToText(rule.getName()) + ": " + player.getName() + " met a national objective for an additional " + toAdd + MyFormatter.pluralize(" PU", toAdd) + "; end with " + total + MyFormatter.pluralize(" PU", total);
bridge.getHistoryWriter().startEvent(puMessage);
endTurnReport.append(puMessage).append("<br />");
}
return endTurnReport.toString();
}
use of games.strategy.triplea.attachments.ICondition in project triplea by triplea-game.
the class UserActionDelegate method getValidActions.
@Override
public Collection<UserActionAttachment> getValidActions() {
final GameData data = bridge.getData();
data.acquireReadLock();
final HashMap<ICondition, Boolean> testedConditions;
try {
testedConditions = getTestedConditions();
} finally {
data.releaseReadLock();
}
return UserActionAttachment.getValidActions(player, testedConditions);
}
use of games.strategy.triplea.attachments.ICondition in project triplea by triplea-game.
the class EndRoundDelegate method start.
@Override
public void start() {
super.start();
if (gameOver) {
return;
}
String victoryMessage;
final GameData data = getData();
if (isPacificTheater()) {
final PlayerID japanese = data.getPlayerList().getPlayerId(Constants.PLAYER_NAME_JAPANESE);
final PlayerAttachment pa = PlayerAttachment.get(japanese);
if (pa != null && pa.getVps() >= 22) {
victoryMessage = "Axis achieve VP victory";
bridge.getHistoryWriter().startEvent(victoryMessage);
final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(data.getAllianceTracker().getAlliancesPlayerIsIn(japanese).iterator().next());
signalGameOver(victoryMessage, winners, bridge);
}
}
// Check for Winning conditions
if (isTotalVictory()) {
// Check for Win by Victory Cities
victoryMessage = " achieve TOTAL VICTORY with ";
checkVictoryCities(bridge, victoryMessage, " Total Victory VCs");
}
if (isHonorableSurrender()) {
victoryMessage = " achieve an HONORABLE VICTORY with ";
checkVictoryCities(bridge, victoryMessage, " Honorable Victory VCs");
}
if (isProjectionOfPower()) {
victoryMessage = " achieve victory through a PROJECTION OF POWER with ";
checkVictoryCities(bridge, victoryMessage, " Projection of Power VCs");
}
if (isEconomicVictory()) {
// Check for regular economic victory
for (final String allianceName : data.getAllianceTracker().getAlliances()) {
final int victoryAmount = getEconomicVictoryAmount(data, allianceName);
final Set<PlayerID> teamMembers = data.getAllianceTracker().getPlayersInAlliance(allianceName);
int teamProd = 0;
for (final PlayerID player : teamMembers) {
teamProd += getProduction(player);
if (teamProd >= victoryAmount) {
victoryMessage = allianceName + " achieve economic victory";
bridge.getHistoryWriter().startEvent(victoryMessage);
final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(allianceName);
// Added this to end the game on victory conditions
signalGameOver(victoryMessage, winners, bridge);
}
}
}
}
// now check for generic trigger based victories
if (isTriggeredVictory()) {
// First set up a match for what we want to have fire as a default in this delegate. List out as a composite match
// OR.
// use 'null, null' because this is the Default firing location for any trigger that does NOT have 'when' set.
final Predicate<TriggerAttachment> endRoundDelegateTriggerMatch = AbstractTriggerAttachment.availableUses.and(AbstractTriggerAttachment.whenOrDefaultMatch(null, null)).and(TriggerAttachment.activateTriggerMatch().or(TriggerAttachment.victoryMatch()));
// get all possible triggers based on this match.
final HashSet<TriggerAttachment> toFirePossible = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(data.getPlayerList().getPlayers()), endRoundDelegateTriggerMatch);
if (!toFirePossible.isEmpty()) {
// get all conditions possibly needed by these triggers, and then test them.
final HashMap<ICondition, Boolean> testedConditions = TriggerAttachment.collectTestsForAllTriggers(toFirePossible, bridge);
// get all triggers that are satisfied based on the tested conditions.
final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(toFirePossible, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
// now list out individual types to fire, once for each of the matches above.
TriggerAttachment.triggerActivateTriggerOther(testedConditions, toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
// will call
TriggerAttachment.triggerVictory(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
// signalGameOver itself
}
}
if (isWW2V2() || isWW2V3()) {
return;
}
final PlayerList playerList = data.getPlayerList();
// now test older maps that only use these 5 players, to see if someone has won
final PlayerID russians = playerList.getPlayerId(Constants.PLAYER_NAME_RUSSIANS);
final PlayerID germans = playerList.getPlayerId(Constants.PLAYER_NAME_GERMANS);
final PlayerID british = playerList.getPlayerId(Constants.PLAYER_NAME_BRITISH);
final PlayerID japanese = playerList.getPlayerId(Constants.PLAYER_NAME_JAPANESE);
final PlayerID americans = playerList.getPlayerId(Constants.PLAYER_NAME_AMERICANS);
if (germans == null || russians == null || british == null || japanese == null || americans == null || playerList.size() > 5) {
return;
}
// Quick check to see who still owns their own capital
final boolean russia = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(russians, data).getOwner().equals(russians);
final boolean germany = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(germans, data).getOwner().equals(germans);
final boolean britain = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(british, data).getOwner().equals(british);
final boolean japan = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(japanese, data).getOwner().equals(japanese);
final boolean america = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(americans, data).getOwner().equals(americans);
int count = 0;
if (!russia) {
count++;
}
if (!britain) {
count++;
}
if (!america) {
count++;
}
victoryMessage = " achieve a military victory";
if (germany && japan && count >= 2) {
bridge.getHistoryWriter().startEvent("Axis" + victoryMessage);
final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance("Axis");
signalGameOver("Axis" + victoryMessage, winners, bridge);
}
if (russia && !germany && britain && !japan && america) {
bridge.getHistoryWriter().startEvent("Allies" + victoryMessage);
final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance("Allies");
signalGameOver("Allies" + victoryMessage, winners, bridge);
}
}
Aggregations