use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class BattleDelegate method checkDefendingPlanesCanLand.
private void checkDefendingPlanesCanLand() {
final GameData data = getData();
final Map<Territory, Collection<Unit>> defendingAirThatCanNotLand = battleTracker.getDefendingAirThatCanNotLand();
final boolean isWW2v2orIsSurvivingAirMoveToLand = Properties.getWW2V2(data) || Properties.getSurvivingAirMoveToLand(data);
final Predicate<Unit> alliedDefendingAir = Matches.unitIsAir().and(Matches.unitWasScrambled().negate());
for (final Entry<Territory, Collection<Unit>> entry : defendingAirThatCanNotLand.entrySet()) {
final Territory battleSite = entry.getKey();
final Collection<Unit> defendingAir = entry.getValue();
if (defendingAir == null || defendingAir.isEmpty()) {
continue;
}
defendingAir.retainAll(battleSite.getUnits());
if (defendingAir.isEmpty()) {
continue;
}
final PlayerID defender = AbstractBattle.findDefender(battleSite, player, data);
// Get all land territories where we can land
final Set<Territory> neighbors = data.getMap().getNeighbors(battleSite);
final Predicate<Territory> alliedLandTerritories = Matches.airCanLandOnThisAlliedNonConqueredLandTerritory(defender, data);
// Get those that are neighbors
final Collection<Territory> canLandHere = CollectionUtils.getMatches(neighbors, alliedLandTerritories);
// Get all sea territories where there are allies
final Predicate<Territory> neighboringSeaZonesWithAlliedUnits = Matches.territoryIsWater().and(Matches.territoryHasAlliedUnits(defender, data));
// Get those that are neighbors
final Collection<Territory> areSeaNeighbors = CollectionUtils.getMatches(neighbors, neighboringSeaZonesWithAlliedUnits);
// Set up match criteria for allied carriers
final Predicate<Unit> alliedCarrier = Matches.unitIsCarrier().and(Matches.alliedUnit(defender, data));
// Set up match criteria for allied planes
final Predicate<Unit> alliedPlane = Matches.unitIsAir().and(Matches.alliedUnit(defender, data));
// See if neighboring carriers have any capacity available
for (final Territory currentTerritory : areSeaNeighbors) {
// get the capacity of the carriers and cost of fighters
final Collection<Unit> alliedCarriers = currentTerritory.getUnits().getMatches(alliedCarrier);
final Collection<Unit> alliedPlanes = currentTerritory.getUnits().getMatches(alliedPlane);
final int alliedCarrierCapacity = AirMovementValidator.carrierCapacity(alliedCarriers, currentTerritory);
final int alliedPlaneCost = AirMovementValidator.carrierCost(alliedPlanes);
// if there is free capacity, add the territory to landing possibilities
if (alliedCarrierCapacity - alliedPlaneCost >= 1) {
canLandHere.add(currentTerritory);
}
}
if (isWW2v2orIsSurvivingAirMoveToLand) {
Territory territory;
while (canLandHere.size() > 1 && defendingAir.size() > 0) {
territory = getRemotePlayer(defender).selectTerritoryForAirToLand(canLandHere, battleSite, "Select territory for air units to land. (Current territory is " + battleSite.getName() + "): " + MyFormatter.unitsToText(defendingAir));
// added for test script
if (territory == null) {
territory = canLandHere.iterator().next();
}
if (territory.isWater()) {
landPlanesOnCarriers(bridge, alliedDefendingAir, defendingAir, alliedCarrier, alliedPlane, territory, battleSite);
} else {
moveAirAndLand(bridge, defendingAir, defendingAir, territory, battleSite);
continue;
}
// remove the territory from those available
canLandHere.remove(territory);
}
// Land in the last remaining territory
if (canLandHere.size() > 0 && defendingAir.size() > 0) {
territory = canLandHere.iterator().next();
if (territory.isWater()) {
landPlanesOnCarriers(bridge, alliedDefendingAir, defendingAir, alliedCarrier, alliedPlane, territory, battleSite);
} else {
moveAirAndLand(bridge, defendingAir, defendingAir, territory, battleSite);
continue;
}
}
} else if (canLandHere.size() > 0) {
// check for an island in this sea zone
for (final Territory currentTerritory : canLandHere) {
// only one neighbor, its an island.
if (data.getMap().getNeighbors(currentTerritory).size() == 1) {
moveAirAndLand(bridge, defendingAir, defendingAir, currentTerritory, battleSite);
}
}
}
if (defendingAir.size() > 0) {
// no where to go, they must die
bridge.getHistoryWriter().addChildToEvent(MyFormatter.unitsToText(defendingAir) + " could not land and were killed", new ArrayList<>(defendingAir));
final Change change = ChangeFactory.removeUnits(battleSite, defendingAir);
bridge.addChange(change);
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class BattleDelegate method setupUnitsInSameTerritoryBattles.
/**
* Setup the battles where the battle occurs because units are in the
* same territory. This happens when subs emerge (after being submerged), and
* when naval units are placed in enemy occupied sea zones, and also
* when political relationships change and potentially leave units in now-hostile territories.
*/
private static void setupUnitsInSameTerritoryBattles(final BattleTracker battleTracker, final IDelegateBridge bridge) {
final PlayerID player = bridge.getPlayerId();
final GameData data = bridge.getData();
final boolean ignoreTransports = isIgnoreTransportInMovement(data);
final boolean ignoreSubs = isIgnoreSubInMovement(data);
final Predicate<Unit> seaTransports = Matches.unitIsTransportButNotCombatTransport().and(Matches.unitIsSea());
final Predicate<Unit> seaTranportsOrSubs = seaTransports.or(Matches.unitIsSub());
// we want to match all sea zones with our units and enemy units
final Predicate<Territory> anyTerritoryWithOwnAndEnemy = Matches.territoryHasUnitsOwnedBy(player).and(Matches.territoryHasEnemyUnits(player, data));
final Predicate<Territory> enemyTerritoryAndOwnUnits = Matches.isTerritoryEnemyAndNotUnownedWater(player, data).and(Matches.territoryHasUnitsOwnedBy(player));
final Predicate<Territory> enemyUnitsOrEnemyTerritory = anyTerritoryWithOwnAndEnemy.or(enemyTerritoryAndOwnUnits);
final List<Territory> battleTerritories = CollectionUtils.getMatches(data.getMap().getTerritories(), enemyUnitsOrEnemyTerritory);
for (final Territory territory : battleTerritories) {
final List<Unit> attackingUnits = territory.getUnits().getMatches(Matches.unitIsOwnedBy(player));
// now make sure to add any units that must move with these attacking units, so that they get included as
// dependencies
final Map<Unit, Collection<Unit>> transportMap = TransportTracker.transporting(territory.getUnits());
final HashSet<Unit> dependants = new HashSet<>();
for (final Entry<Unit, Collection<Unit>> entry : transportMap.entrySet()) {
// only consider those transports that we are attacking with. allied and enemy transports are not added.
if (attackingUnits.contains(entry.getKey())) {
dependants.addAll(entry.getValue());
}
}
// no duplicates
dependants.removeAll(attackingUnits);
// add the dependants to the attacking list
attackingUnits.addAll(dependants);
final List<Unit> enemyUnits = territory.getUnits().getMatches(Matches.enemyUnit(player, data));
final IBattle bombingBattle = battleTracker.getPendingBattle(territory, true, null);
if (bombingBattle != null) {
// we need to remove any units which are participating in bombing raids
attackingUnits.removeAll(bombingBattle.getAttackingUnits());
}
if (attackingUnits.stream().allMatch(Matches.unitIsInfrastructure())) {
continue;
}
IBattle battle = battleTracker.getPendingBattle(territory, false, BattleType.NORMAL);
if (battle == null) {
// fires)
if (enemyUnits.stream().allMatch(Matches.unitIsInfrastructure())) {
landParatroopers(player, territory, bridge);
}
bridge.getHistoryWriter().startEvent(player.getName() + " creates battle in territory " + territory.getName());
battleTracker.addBattle(new RouteScripted(territory), attackingUnits, player, bridge, null, null);
battle = battleTracker.getPendingBattle(territory, false, BattleType.NORMAL);
}
if (battle == null) {
continue;
}
if (bombingBattle != null) {
battleTracker.addDependency(battle, bombingBattle);
}
if (battle.isEmpty()) {
battle.addAttackChange(new RouteScripted(territory), attackingUnits, null);
}
if (!battle.getAttackingUnits().containsAll(attackingUnits)) {
List<Unit> attackingUnitsNeedToBeAdded = new ArrayList<>(attackingUnits);
attackingUnitsNeedToBeAdded.removeAll(battle.getAttackingUnits());
attackingUnitsNeedToBeAdded.removeAll(battle.getDependentUnits(battle.getAttackingUnits()));
if (territory.isWater()) {
attackingUnitsNeedToBeAdded = CollectionUtils.getMatches(attackingUnitsNeedToBeAdded, Matches.unitIsLand().negate());
} else {
attackingUnitsNeedToBeAdded = CollectionUtils.getMatches(attackingUnitsNeedToBeAdded, Matches.unitIsSea().negate());
}
if (!attackingUnitsNeedToBeAdded.isEmpty()) {
battle.addAttackChange(new RouteScripted(territory), attackingUnitsNeedToBeAdded, null);
}
}
// Reach stalemate if all attacking and defending units are transports
if ((ignoreTransports && !attackingUnits.isEmpty() && attackingUnits.stream().allMatch(seaTransports) && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTransports)) || (!attackingUnits.isEmpty() && attackingUnits.stream().allMatch(Matches.unitHasAttackValueOfAtLeast(1).negate()) && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(Matches.unitHasDefendValueOfAtLeast(1).negate()))) {
final BattleResults results = new BattleResults(battle, WhoWon.DRAW, data);
battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.STALEMATE, results);
battle.cancelBattle(bridge);
battleTracker.removeBattle(battle);
continue;
}
// possibility to ignore battle altogether
if (!attackingUnits.isEmpty()) {
final ITripleAPlayer remotePlayer = getRemotePlayer(bridge);
if (territory.isWater() && Properties.getSeaBattlesMayBeIgnored(data)) {
if (!remotePlayer.selectAttackUnits(territory)) {
final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
battle.cancelBattle(bridge);
battleTracker.removeBattle(battle);
}
continue;
}
// Check for ignored units
if (ignoreTransports || ignoreSubs) {
// if only enemy transports... attack them?
if (ignoreTransports && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTransports)) {
if (!remotePlayer.selectAttackTransports(territory)) {
final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
battle.cancelBattle(bridge);
battleTracker.removeBattle(battle);
}
continue;
}
// if only enemy subs... attack them?
if (ignoreSubs && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(Matches.unitIsSub())) {
if (!remotePlayer.selectAttackSubs(territory)) {
final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
battle.cancelBattle(bridge);
battleTracker.removeBattle(battle);
}
continue;
}
// if only enemy transports and subs... attack them?
if (ignoreSubs && ignoreTransports && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTranportsOrSubs)) {
if (!remotePlayer.selectAttackUnits(territory)) {
final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
battle.cancelBattle(bridge);
battleTracker.removeBattle(battle);
}
}
}
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class BattleTracker method captureOrDestroyUnits.
/**
* Called when a territory is conquered to determine if remaining enemy units should be
* captured, destroyed, or take damage.
*/
public static void captureOrDestroyUnits(final Territory territory, final PlayerID id, final PlayerID newOwner, final IDelegateBridge bridge, final UndoableMove changeTracker) {
final GameData data = bridge.getData();
// destroy any units that should be destroyed on capture
if (Properties.getUnitsCanBeDestroyedInsteadOfCaptured(data)) {
final Predicate<Unit> enemyToBeDestroyed = Matches.enemyUnit(id, data).and(Matches.unitDestroyedWhenCapturedByOrFrom(id));
final Collection<Unit> destroyed = territory.getUnits().getMatches(enemyToBeDestroyed);
if (!destroyed.isEmpty()) {
final Change destroyUnits = ChangeFactory.removeUnits(territory, destroyed);
bridge.getHistoryWriter().addChildToEvent("Some non-combat units are destroyed: ", destroyed);
bridge.addChange(destroyUnits);
if (changeTracker != null) {
changeTracker.addChange(destroyUnits);
}
}
}
// destroy any capture on entering units, IF the property to destroy them instead of capture is turned on
if (Properties.getOnEnteringUnitsDestroyedInsteadOfCaptured(data)) {
final Collection<Unit> destroyed = territory.getUnits().getMatches(Matches.unitCanBeCapturedOnEnteringToInThisTerritory(id, territory, data));
if (!destroyed.isEmpty()) {
final Change destroyUnits = ChangeFactory.removeUnits(territory, destroyed);
bridge.getHistoryWriter().addChildToEvent(id.getName() + " destroys some units instead of capturing them", destroyed);
bridge.addChange(destroyUnits);
if (changeTracker != null) {
changeTracker.addChange(destroyUnits);
}
}
}
// destroy any disabled units owned by the enemy that are NOT infrastructure or factories
final Predicate<Unit> enemyToBeDestroyed = Matches.enemyUnit(id, data).and(Matches.unitIsDisabled()).and(Matches.unitIsInfrastructure().negate());
final Collection<Unit> destroyed = territory.getUnits().getMatches(enemyToBeDestroyed);
if (!destroyed.isEmpty()) {
final Change destroyUnits = ChangeFactory.removeUnits(territory, destroyed);
bridge.getHistoryWriter().addChildToEvent(id.getName() + " destroys some disabled combat units", destroyed);
bridge.addChange(destroyUnits);
if (changeTracker != null) {
changeTracker.addChange(destroyUnits);
}
}
// take over non combatants
final Predicate<Unit> enemyNonCom = Matches.enemyUnit(id, data).and(Matches.unitIsInfrastructure());
final Predicate<Unit> willBeCaptured = enemyNonCom.or(Matches.unitCanBeCapturedOnEnteringToInThisTerritory(id, territory, data));
final Collection<Unit> nonCom = territory.getUnits().getMatches(willBeCaptured);
// change any units that change unit types on capture
if (Properties.getUnitsCanBeChangedOnCapture(data)) {
final Collection<Unit> toReplace = CollectionUtils.getMatches(nonCom, Matches.unitWhenCapturedChangesIntoDifferentUnitType());
for (final Unit u : toReplace) {
final Map<String, Tuple<String, IntegerMap<UnitType>>> map = UnitAttachment.get(u.getType()).getWhenCapturedChangesInto();
final PlayerID currentOwner = u.getOwner();
for (final String value : map.keySet()) {
final String[] s = value.split(":");
if (!(s[0].equals("any") || data.getPlayerList().getPlayerId(s[0]).equals(currentOwner))) {
continue;
}
// we could use "id" or "newOwner" here... not sure which to use
if (!(s[1].equals("any") || data.getPlayerList().getPlayerId(s[1]).equals(id))) {
continue;
}
final CompositeChange changes = new CompositeChange();
final Collection<Unit> toAdd = new ArrayList<>();
final Tuple<String, IntegerMap<UnitType>> toCreate = map.get(value);
final boolean translateAttributes = toCreate.getFirst().equalsIgnoreCase("true");
for (final UnitType ut : toCreate.getSecond().keySet()) {
toAdd.addAll(ut.create(toCreate.getSecond().getInt(ut), newOwner));
}
if (!toAdd.isEmpty()) {
if (translateAttributes) {
final Change translate = TripleAUnit.translateAttributesToOtherUnits(u, toAdd, territory);
if (!translate.isEmpty()) {
changes.add(translate);
}
}
changes.add(ChangeFactory.removeUnits(territory, Collections.singleton(u)));
changes.add(ChangeFactory.addUnits(territory, toAdd));
changes.add(ChangeFactory.markNoMovementChange(toAdd));
bridge.getHistoryWriter().addChildToEvent(id.getName() + " converts " + u.toStringNoOwner() + " into different units", toAdd);
bridge.addChange(changes);
if (changeTracker != null) {
changeTracker.addChange(changes);
}
// don't forget to remove this unit from the list
nonCom.remove(u);
break;
}
}
}
}
if (!nonCom.isEmpty()) {
// FYI: a dummy delegate will not do anything with this change,
// meaning that the battle calculator will think this unit lived,
// even though it died or was captured, etc!
final Change capture = ChangeFactory.changeOwner(nonCom, newOwner, territory);
bridge.addChange(capture);
if (changeTracker != null) {
changeTracker.addChange(capture);
}
final Change noMovementChange = ChangeFactory.markNoMovementChange(nonCom);
bridge.addChange(noMovementChange);
if (changeTracker != null) {
changeTracker.addChange(noMovementChange);
}
final IntegerMap<Unit> damageMap = new IntegerMap<>();
for (final Unit unit : CollectionUtils.getMatches(nonCom, Matches.unitWhenCapturedSustainsDamage())) {
final TripleAUnit taUnit = (TripleAUnit) unit;
final int damageLimit = taUnit.getHowMuchMoreDamageCanThisUnitTake(unit, territory);
final int sustainedDamage = UnitAttachment.get(unit.getType()).getWhenCapturedSustainsDamage();
final int actualDamage = Math.max(0, Math.min(sustainedDamage, damageLimit));
final int totalDamage = taUnit.getUnitDamage() + actualDamage;
damageMap.put(unit, totalDamage);
}
if (!damageMap.isEmpty()) {
final Change damageChange = ChangeFactory.bombingUnitDamage(damageMap);
bridge.addChange(damageChange);
if (changeTracker != null) {
changeTracker.addChange(damageChange);
}
// Kill any units that can die if they have reached max damage
if (damageMap.keySet().stream().anyMatch(Matches.unitCanDieFromReachingMaxDamage())) {
final List<Unit> unitsCanDie = CollectionUtils.getMatches(damageMap.keySet(), Matches.unitCanDieFromReachingMaxDamage());
unitsCanDie.retainAll(CollectionUtils.getMatches(unitsCanDie, Matches.unitIsAtMaxDamageOrNotCanBeDamaged(territory)));
if (!unitsCanDie.isEmpty()) {
final Change removeDead = ChangeFactory.removeUnits(territory, unitsCanDie);
bridge.addChange(removeDead);
if (changeTracker != null) {
changeTracker.addChange(removeDead);
}
}
}
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class AbstractPlaceDelegate method removeAirThatCantLand.
protected void removeAirThatCantLand() {
// for LHTR type games
final GameData data = getData();
final AirThatCantLandUtil util = new AirThatCantLandUtil(bridge);
util.removeAirThatCantLand(player, false);
// if edit mode has been on, we need to clean up after all players
for (final PlayerID player : data.getPlayerList()) {
if (!player.equals(this.player)) {
util.removeAirThatCantLand(player, false);
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class AirThatCantLandUtil method removeAirThatCantLand.
void removeAirThatCantLand(final PlayerID player, final boolean spareAirInSeaZonesBesideFactories) {
final GameData data = bridge.getData();
final GameMap map = data.getMap();
for (final Territory current : getTerritoriesWhereAirCantLand(player)) {
final Predicate<Unit> ownedAir = Matches.unitIsAir().and(Matches.alliedUnit(player, data));
final Collection<Unit> air = current.getUnits().getMatches(ownedAir);
final boolean hasNeighboringFriendlyFactory = map.getNeighbors(current, Matches.territoryHasAlliedIsFactoryOrCanProduceUnits(data, player)).size() > 0;
final boolean skip = spareAirInSeaZonesBesideFactories && current.isWater() && hasNeighboringFriendlyFactory;
if (!skip) {
removeAirThatCantLand(player, current, air);
}
}
}
Aggregations