use of games.strategy.engine.data.GameData 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.GameData in project triplea by triplea-game.
the class ProTerritoryManager method findAirMoveOptions.
private static void findAirMoveOptions(final PlayerID player, final List<Territory> myUnitTerritories, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> unitMoveMap, final Predicate<Territory> moveToTerritoryMatch, final List<Territory> enemyTerritories, final List<Territory> alliedTerritories, final boolean isCombatMove, final boolean isCheckingEnemyAttacks, final boolean isIgnoringRelationships) {
final GameData data = ProData.getData();
// TODO: add carriers to landing possibilities for non-enemy attacks
// Find possible carrier landing territories
final Set<Territory> possibleCarrierTerritories = new HashSet<>();
if (isCheckingEnemyAttacks || !isCombatMove) {
final Map<Unit, Set<Territory>> unitMoveMap2 = new HashMap<>();
findNavalMoveOptions(player, myUnitTerritories, new HashMap<>(), unitMoveMap2, new HashMap<>(), Matches.territoryIsWater(), enemyTerritories, false, true);
for (final Unit u : unitMoveMap2.keySet()) {
if (Matches.unitIsCarrier().test(u)) {
possibleCarrierTerritories.addAll(unitMoveMap2.get(u));
}
}
for (final Territory t : data.getMap().getTerritories()) {
if (t.getUnits().anyMatch(Matches.unitIsAlliedCarrier(player, data))) {
possibleCarrierTerritories.add(t);
}
}
}
for (final Territory myUnitTerritory : myUnitTerritories) {
// Find my air units that have movement left
final List<Unit> myAirUnits = myUnitTerritory.getUnits().getMatches(ProMatches.unitCanBeMovedAndIsOwnedAir(player, isCombatMove));
// Check each air unit individually since they can have different ranges
for (final Unit myAirUnit : myAirUnits) {
// Find range
int range = TripleAUnit.get(myAirUnit).getMovementLeft();
if (isCheckingEnemyAttacks) {
range = UnitAttachment.get(myAirUnit.getType()).getMovement(player);
if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(myUnitTerritory, player, data).test(myAirUnit)) {
// assumes bonus of +1 for now
range++;
}
}
// Find potential territories to move to
Set<Territory> possibleMoveTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanMoveAirUnits(player, data, isCombatMove));
if (isIgnoringRelationships) {
possibleMoveTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanPotentiallyMoveAirUnits(player, data));
}
possibleMoveTerritories.add(myUnitTerritory);
final Set<Territory> potentialTerritories = new HashSet<>(CollectionUtils.getMatches(possibleMoveTerritories, moveToTerritoryMatch));
if (!isCombatMove && Matches.unitCanLandOnCarrier().test(myAirUnit)) {
potentialTerritories.addAll(CollectionUtils.getMatches(possibleMoveTerritories, Matches.territoryIsInList(possibleCarrierTerritories)));
}
for (final Territory potentialTerritory : potentialTerritories) {
// Find route ignoring impassable and territories with AA
Predicate<Territory> canFlyOverMatch = ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, isCombatMove);
if (isCheckingEnemyAttacks) {
canFlyOverMatch = ProMatches.territoryCanMoveAirUnits(player, data, isCombatMove);
}
final Route myRoute = data.getMap().getRoute_IgnoreEnd(myUnitTerritory, potentialTerritory, canFlyOverMatch);
if (myRoute == null) {
continue;
}
final int myRouteLength = myRoute.numberOfSteps();
final int remainingMoves = range - myRouteLength;
if (remainingMoves < 0) {
continue;
}
// Check if unit can land
if (isCombatMove && (remainingMoves < myRouteLength || myUnitTerritory.isWater())) {
final Set<Territory> possibleLandingTerritories = data.getMap().getNeighbors(potentialTerritory, remainingMoves, canFlyOverMatch);
final List<Territory> landingTerritories = CollectionUtils.getMatches(possibleLandingTerritories, ProMatches.territoryCanLandAirUnits(player, data, isCombatMove, enemyTerritories, alliedTerritories));
List<Territory> carrierTerritories = new ArrayList<>();
if (Matches.unitCanLandOnCarrier().test(myAirUnit)) {
carrierTerritories = CollectionUtils.getMatches(possibleLandingTerritories, Matches.territoryIsInList(possibleCarrierTerritories));
}
if (landingTerritories.isEmpty() && carrierTerritories.isEmpty()) {
continue;
}
}
// Populate enemy territories with air unit
if (moveMap.containsKey(potentialTerritory)) {
moveMap.get(potentialTerritory).addMaxUnit(myAirUnit);
} else {
final ProTerritory moveTerritoryData = new ProTerritory(potentialTerritory);
moveTerritoryData.addMaxUnit(myAirUnit);
moveMap.put(potentialTerritory, moveTerritoryData);
}
// Populate unit attack options map
if (unitMoveMap.containsKey(myAirUnit)) {
unitMoveMap.get(myAirUnit).add(potentialTerritory);
} else {
final Set<Territory> unitMoveTerritories = new HashSet<>();
unitMoveTerritories.add(potentialTerritory);
unitMoveMap.put(myAirUnit, unitMoveTerritories);
}
}
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProTerritoryManager method findBombardOptions.
private static void findBombardOptions(final PlayerID player, final List<Territory> myUnitTerritories, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> bombardMap, final List<ProTransport> transportMapList, final boolean isCheckingEnemyAttacks) {
final GameData data = ProData.getData();
// Find all transport unload from and to territories
final Set<Territory> unloadFromTerritories = new HashSet<>();
final Set<Territory> unloadToTerritories = new HashSet<>();
for (final ProTransport amphibData : transportMapList) {
unloadFromTerritories.addAll(amphibData.getSeaTransportMap().keySet());
unloadToTerritories.addAll(amphibData.getTransportMap().keySet());
}
// Loop through territories with my units
for (final Territory myUnitTerritory : myUnitTerritories) {
// Find my bombard units that have movement left
final List<Unit> mySeaUnits = myUnitTerritory.getUnits().getMatches(ProMatches.unitCanBeMovedAndIsOwnedBombard(player));
// Check each sea unit individually since they can have different ranges
for (final Unit mySeaUnit : mySeaUnits) {
// Find range
int range = TripleAUnit.get(mySeaUnit).getMovementLeft();
if (isCheckingEnemyAttacks) {
range = UnitAttachment.get(mySeaUnit.getType()).getMovement(player);
if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(myUnitTerritory, player, data).test(mySeaUnit)) {
// assumes bonus of +1 for now
range++;
}
}
// Find list of potential territories to move to
final Set<Territory> potentialTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanMoveSeaUnits(player, data, true));
potentialTerritories.add(myUnitTerritory);
potentialTerritories.retainAll(unloadFromTerritories);
for (final Territory bombardFromTerritory : potentialTerritories) {
// Find route over water with no enemy units blocking
Route myRoute = data.getMap().getRoute(myUnitTerritory, bombardFromTerritory, ProMatches.territoryCanMoveSeaUnitsThrough(player, data, true));
if (isCheckingEnemyAttacks) {
myRoute = data.getMap().getRoute(myUnitTerritory, bombardFromTerritory, ProMatches.territoryCanMoveSeaUnits(player, data, true));
}
if (myRoute == null) {
continue;
}
if (MoveValidator.validateCanal(myRoute, Collections.singletonList(mySeaUnit), player, data) != null) {
continue;
}
final int myRouteLength = myRoute.numberOfSteps();
if (myRouteLength > range) {
continue;
}
// Find potential unload to territories
final Set<Territory> bombardToTerritories = new HashSet<>(data.getMap().getNeighbors(bombardFromTerritory));
bombardToTerritories.retainAll(unloadToTerritories);
// Populate attack territories with bombard unit
for (final Territory bombardToTerritory : bombardToTerritories) {
if (moveMap.containsKey(bombardToTerritory)) {
// Should always contain it
moveMap.get(bombardToTerritory).addMaxBombardUnit(mySeaUnit);
moveMap.get(bombardToTerritory).addBombardOptionsMap(mySeaUnit, bombardFromTerritory);
}
}
// Populate bombard options map
if (bombardMap.containsKey(mySeaUnit)) {
bombardMap.get(mySeaUnit).addAll(bombardToTerritories);
} else {
bombardMap.put(mySeaUnit, bombardToTerritories);
}
}
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProTerritoryManager method findDefendOptions.
private static void findDefendOptions(final PlayerID player, final List<Territory> myUnitTerritories, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> unitMoveMap, final Map<Unit, Set<Territory>> transportMoveMap, final List<ProTransport> transportMapList, final List<Territory> clearedTerritories, final boolean isCheckingEnemyAttacks) {
final GameData data = ProData.getData();
final Map<Territory, Set<Territory>> landRoutesMap = new HashMap<>();
findNavalMoveOptions(player, myUnitTerritories, moveMap, unitMoveMap, transportMoveMap, ProMatches.territoryHasNoEnemyUnitsOrCleared(player, data, clearedTerritories), clearedTerritories, false, isCheckingEnemyAttacks);
findLandMoveOptions(player, myUnitTerritories, moveMap, unitMoveMap, landRoutesMap, Matches.isTerritoryAllied(player, data), new ArrayList<>(), clearedTerritories, false, isCheckingEnemyAttacks, false);
findAirMoveOptions(player, myUnitTerritories, moveMap, unitMoveMap, ProMatches.territoryCanLandAirUnits(player, data, false, new ArrayList<>(), new ArrayList<>()), new ArrayList<>(), new ArrayList<>(), false, isCheckingEnemyAttacks, false);
findAmphibMoveOptions(player, myUnitTerritories, moveMap, transportMapList, landRoutesMap, Matches.isTerritoryAllied(player, data), false, isCheckingEnemyAttacks, false);
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuckMixedWithRollingMiss.
@Test
public void testAaCasualtiesLowLuckMixedWithRollingMiss() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, false);
// 7 bombers and 7 fighters
// 2 extra units, roll once
final Collection<Unit> planes = bomber(data).create(7, british(data));
planes.addAll(fighter(data).create(7, british(data)));
final Collection<Unit> defendingAa = territory("Germany", data).getUnits().getMatches(Matches.unitIsAaForAnything());
// one roll, a miss
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 2, 0, 0, 0, ScriptedRandomSource.ERROR });
bridge.setRandomSource(randomSource);
final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(data))), defendingAa, bridge, territory("Germany", data), true);
// make sure we rolled once
assertEquals(1, randomSource.getTotalRolled());
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, null, null, null, territory("Germany", data), null, false, null).getKilled();
assertEquals(2, casualties.size());
assertEquals(4, randomSource.getTotalRolled());
// should be 1 fighter and 1 bomber
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
Aggregations