use of games.strategy.engine.data.PlayerID in project triplea by triplea-game.
the class RulesAttachment method setBattle.
private void setBattle(final String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length < 5) {
throw new GameParseException("battle must have at least 5 fields, attacker:defender:resultType:round:territory1..." + thisErrorMsg());
}
final PlayerID attacker = getData().getPlayerList().getPlayerId(s[0]);
if (attacker == null && !s[0].equalsIgnoreCase("any")) {
throw new GameParseException("no player named: " + s[0] + thisErrorMsg());
}
final PlayerID defender = getData().getPlayerList().getPlayerId(s[1]);
if (defender == null && !s[1].equalsIgnoreCase("any")) {
throw new GameParseException("no player named: " + s[1] + thisErrorMsg());
}
if (!s[2].equalsIgnoreCase("any")) {
throw new GameParseException("battle allows the following for resultType: any" + thisErrorMsg());
}
if (!s[3].equalsIgnoreCase("currentRound")) {
try {
getInt(s[3].split("-")[0]);
getInt(s[3].split("-")[1]);
} catch (final Exception e) {
throw new GameParseException("round must either be currentRound or two numbers like: 2-4" + thisErrorMsg());
}
}
final ArrayList<Territory> terrs = new ArrayList<>();
final GameMap map = getData().getMap();
// this loop starts on 4, so do not replace with an enhanced for loop
for (int i = 4; i < s.length; i++) {
final Territory t = map.getTerritory(s[i]);
if (t == null) {
throw new GameParseException("no such territory called: " + s[i] + thisErrorMsg());
}
terrs.add(t);
}
m_battle.add(Tuple.of((s[0] + ":" + s[1] + ":" + s[2] + ":" + s[3]), terrs));
}
use of games.strategy.engine.data.PlayerID in project triplea by triplea-game.
the class RulesAttachment method isSatisfied.
@Override
public boolean isSatisfied(Map<ICondition, Boolean> testedConditions, final IDelegateBridge delegateBridge) {
if (testedConditions != null) {
if (testedConditions.containsKey(this)) {
return testedConditions.get(this);
}
}
boolean objectiveMet = true;
final List<PlayerID> players = getPlayers();
final GameData data = delegateBridge.getData();
// check meta conditions (conditions which hold other conditions)
if (objectiveMet && m_conditions.size() > 0) {
if (testedConditions == null) {
testedConditions = testAllConditionsRecursive(getAllConditionsRecursive(new HashSet<>(m_conditions), null), null, delegateBridge);
}
objectiveMet = areConditionsMet(new ArrayList<>(m_conditions), testedConditions, m_conditionType);
}
// check switch (on/off)
if (objectiveMet) {
objectiveMet = m_switch;
}
// check turn limits
if (objectiveMet && m_turns != null) {
objectiveMet = checkTurns(data);
}
// check custom game property options
if (objectiveMet && m_gameProperty != null) {
objectiveMet = this.getGamePropertyState(data);
}
// Check for unit presence (Veqryn)
if (objectiveMet && getDirectPresenceTerritories() != null) {
// Get the listed territories
final String[] terrs = getDirectPresenceTerritories();
objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "direct", getTerritoryCount(), players, data);
}
// Check for unit presence (Veqryn)
if (objectiveMet && getAlliedPresenceTerritories() != null) {
// Get the listed territories
final String[] terrs = getAlliedPresenceTerritories();
objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "allied", getTerritoryCount(), players, data);
}
// Check for unit presence (Veqryn)
if (objectiveMet && getEnemyPresenceTerritories() != null) {
// Get the listed territories
final String[] terrs = getEnemyPresenceTerritories();
objectiveMet = checkUnitPresence(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy", getTerritoryCount(), players, data);
}
// Check for direct unit exclusions (veqryn)
if (objectiveMet && getDirectExclusionTerritories() != null) {
// Get the listed territories
final String[] terrs = getDirectExclusionTerritories();
objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "direct", getTerritoryCount(), players, data);
}
// Check for allied unit exclusions
if (objectiveMet && getAlliedExclusionTerritories() != null) {
// Get the listed territories
final String[] terrs = getAlliedExclusionTerritories();
objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "allied", getTerritoryCount(), players, data);
}
// Check for enemy unit exclusions (ANY UNITS)
if (objectiveMet && getEnemyExclusionTerritories() != null) {
// Get the listed territories
final String[] terrs = getEnemyExclusionTerritories();
objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy", getTerritoryCount(), players, data);
}
// Check for enemy unit exclusions (SURFACE UNITS with ATTACK POWER)
if (objectiveMet && getEnemySurfaceExclusionTerritories() != null) {
// Get the listed territories
final String[] terrs = getEnemySurfaceExclusionTerritories();
objectiveMet = checkUnitExclusions(getTerritoryListBasedOnInputFromXml(terrs, players, data), "enemy_surface", getTerritoryCount(), players, data);
}
// Check for Territory Ownership rules
if (objectiveMet && getAlliedOwnershipTerritories() != null) {
// Get the listed territories
final String[] terrs = getAlliedOwnershipTerritories();
final Set<Territory> listedTerritories;
if (terrs.length == 1) {
if (terrs[0].equals("original")) {
final Collection<PlayerID> allies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, allies, data);
} else if (terrs[0].equals("enemy")) {
final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
} else if (terrs.length == 2) {
if (terrs[1].equals("original")) {
final Collection<PlayerID> allies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, allies, data);
} else if (terrs[1].equals("enemy")) {
final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
objectiveMet = checkAlliedOwnership(listedTerritories, getTerritoryCount(), players, data);
}
// Check for Direct Territory Ownership rules
if (objectiveMet && getDirectOwnershipTerritories() != null) {
// Get the listed territories
final String[] terrs = getDirectOwnershipTerritories();
final Set<Territory> listedTerritories;
if (terrs.length == 1) {
if (terrs[0].equals("original")) {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
} else if (terrs[0].equals("enemy")) {
final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
} else if (terrs.length == 2) {
if (terrs[1].equals("original")) {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
} else if (terrs[1].equals("enemy")) {
final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWarWithAnyOfThesePlayers(players, data));
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, enemies, data);
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
} else {
listedTerritories = getTerritoryListBasedOnInputFromXml(terrs, players, data);
}
objectiveMet = checkDirectOwnership(listedTerritories, getTerritoryCount(), players);
}
// get attached to player
final PlayerID playerAttachedTo = (PlayerID) getAttachedTo();
if (objectiveMet && getAtWarPlayers() != null) {
objectiveMet = checkAtWar(playerAttachedTo, getAtWarPlayers(), getAtWarCount(), data);
}
if (objectiveMet && m_techs != null) {
objectiveMet = checkTechs(playerAttachedTo, data);
}
// check for relationships
if (objectiveMet && m_relationship.size() > 0) {
objectiveMet = checkRelationships();
}
// check for battle stats
if (objectiveMet && m_destroyedTUV != null) {
final String[] s = m_destroyedTUV.split(":");
final int requiredDestroyedTuv = getInt(s[0]);
if (requiredDestroyedTuv >= 0) {
final boolean justCurrentRound = s[1].equals("currentRound");
final int destroyedTuvForThisRoundSoFar = BattleRecordsList.getTuvDamageCausedByPlayer(playerAttachedTo, data.getBattleRecordsList(), 0, data.getSequence().getRound(), justCurrentRound, false);
if (requiredDestroyedTuv > destroyedTuvForThisRoundSoFar) {
objectiveMet = false;
}
if (getCountEach()) {
m_eachMultiple = destroyedTuvForThisRoundSoFar;
}
}
}
// check for battles
if (objectiveMet && !m_battle.isEmpty()) {
final BattleRecordsList brl = data.getBattleRecordsList();
final int round = data.getSequence().getRound();
for (final Tuple<String, List<Territory>> entry : m_battle) {
final String[] type = entry.getFirst().split(":");
// they could be "any", and if they are "any" then this would be null, which is good!
final PlayerID attacker = data.getPlayerList().getPlayerId(type[0]);
final PlayerID defender = data.getPlayerList().getPlayerId(type[1]);
final String resultType = type[2];
final String roundType = type[3];
int start = 0;
int end = round;
final boolean currentRound = roundType.equalsIgnoreCase("currentRound");
if (!currentRound) {
final String[] rounds = roundType.split("-");
start = getInt(rounds[0]);
end = getInt(rounds[1]);
}
objectiveMet = BattleRecordsList.getWereThereBattlesInTerritoriesMatching(attacker, defender, resultType, entry.getSecond(), brl, start, end, currentRound);
if (!objectiveMet) {
break;
}
}
}
// "chance" should ALWAYS be checked last!
final int hitTarget = getChanceToHit();
final int diceSides = getChanceDiceSides();
final int incrementOnFailure = this.getChanceIncrementOnFailure();
final int decrementOnSuccess = this.getChanceDecrementOnSuccess();
if (objectiveMet && (hitTarget != diceSides || incrementOnFailure != 0 || decrementOnSuccess != 0)) {
if (diceSides <= 0 || hitTarget >= diceSides) {
objectiveMet = true;
changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, false);
} else if (hitTarget <= 0) {
objectiveMet = false;
changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, false);
} else {
// there is an issue with maps using thousands of chance triggers: they are causing the cypted random source
// (ie: live and pbem
// games) to lock up or error out
// so we need to slow them down a bit, until we come up with a better solution (like aggregating all the chances
// together, then
// getting a ton of random numbers at once instead of one at a time)
Interruptibles.sleep(100);
final int rollResult = delegateBridge.getRandom(diceSides, null, DiceType.ENGINE, "Attempting the Condition: " + MyFormatter.attachmentNameToText(this.getName())) + 1;
objectiveMet = rollResult <= hitTarget;
final String notificationMessage = (objectiveMet ? TRIGGER_CHANCE_SUCCESSFUL : TRIGGER_CHANCE_FAILURE) + " (Rolled at " + hitTarget + " out of " + diceSides + " Result: " + rollResult + " for " + MyFormatter.attachmentNameToText(this.getName()) + ")";
delegateBridge.getHistoryWriter().startEvent(notificationMessage);
changeChanceDecrementOrIncrementOnSuccessOrFailure(delegateBridge, objectiveMet, true);
((ITripleAPlayer) delegateBridge.getRemotePlayer(delegateBridge.getPlayerId())).reportMessage(notificationMessage, notificationMessage);
}
}
return objectiveMet != m_invert;
}
use of games.strategy.engine.data.PlayerID in project triplea by triplea-game.
the class RulesAttachment method checkAlliedOwnership.
/**
* Checks for allied ownership of the collection of territories. Once the needed number threshold is reached, the
* satisfied flag is set
* to true and returned
*/
private boolean checkAlliedOwnership(final Collection<Territory> listedTerrs, final int numberNeeded, final Collection<PlayerID> players, final GameData data) {
int numberMet = 0;
boolean satisfied = false;
final Collection<PlayerID> allies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedWithAnyOfThesePlayers(players, data));
for (final Territory listedTerr : listedTerrs) {
// if the territory owner is an ally
if (Matches.isTerritoryOwnedBy(allies).test(listedTerr)) {
numberMet += 1;
if (numberMet >= numberNeeded) {
satisfied = true;
if (!getCountEach()) {
break;
}
}
}
}
if (getCountEach()) {
m_eachMultiple = numberMet;
}
return satisfied;
}
use of games.strategy.engine.data.PlayerID in project triplea by triplea-game.
the class DefaultSoundChannel method playSoundToPlayers.
@Override
public void playSoundToPlayers(final String clipName, final Collection<PlayerID> playersToSendTo, final Collection<PlayerID> butNotThesePlayers, final boolean includeObservers) {
if (playersToSendTo == null || playersToSendTo.isEmpty()) {
return;
}
if (butNotThesePlayers != null) {
for (final PlayerID p : butNotThesePlayers) {
if (localPlayers.playing(p)) {
return;
}
}
}
final boolean isPlaying = playersToSendTo.stream().anyMatch(localPlayers::playing);
final boolean includingObserversLocalNotEmpty = localPlayers.getLocalPlayers().isEmpty();
if (isPlaying || includingObserversLocalNotEmpty) {
ClipPlayer.play(clipName);
}
}
use of games.strategy.engine.data.PlayerID in project triplea by triplea-game.
the class AirThatCantLandUtilTest method testSpareNextToFactory.
@Test
public void testSpareNextToFactory() {
final PlayerID player = americansPlayer;
final ITestDelegateBridge bridge = getDelegateBridge(player);
final Territory sz55 = gameData.getMap().getTerritory("55 Sea Zone");
final Change addAir = ChangeFactory.addUnits(sz55, fighterType.create(2, player));
gameData.performChange(addAir);
final AirThatCantLandUtil airThatCantLandUtil = new AirThatCantLandUtil(bridge);
airThatCantLandUtil.removeAirThatCantLand(player, true);
assertEquals(2, sz55.getUnits().getMatches(Matches.unitIsAir()).size());
}
Aggregations