use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.
the class EditDelegate method addUnits.
@Override
public String addUnits(final Territory territory, final Collection<Unit> units) {
String result = checkEditMode();
if (result != null) {
return result;
}
result = EditValidator.validateAddUnits(getData(), territory, units);
if (result != null) {
return result;
}
if (units == null || units.isEmpty()) {
return null;
}
// now make sure land units are put on transports properly
final PlayerID player = units.iterator().next().getOwner();
final GameData data = getData();
Map<Unit, Unit> mapLoading = null;
if (territory.isWater()) {
if (units.isEmpty() || !units.stream().allMatch(Matches.unitIsSea())) {
if (units.stream().anyMatch(Matches.unitIsLand())) {
// this should be exact same as the one in the EditValidator
if (units.isEmpty() || !units.stream().allMatch(Matches.alliedUnit(player, data))) {
return "Can't add mixed nationality units to water";
}
final Predicate<Unit> friendlySeaTransports = Matches.unitIsTransport().and(Matches.unitIsSea()).and(Matches.alliedUnit(player, data));
final Collection<Unit> seaTransports = CollectionUtils.getMatches(units, friendlySeaTransports);
final Collection<Unit> landUnitsToAdd = CollectionUtils.getMatches(units, Matches.unitIsLand());
if (landUnitsToAdd.isEmpty() || !landUnitsToAdd.stream().allMatch(Matches.unitCanBeTransported())) {
return "Can't add land units that can't be transported, to water";
}
seaTransports.addAll(territory.getUnits().getMatches(friendlySeaTransports));
if (seaTransports.isEmpty()) {
return "Can't add land units to water without enough transports";
}
mapLoading = TransportUtils.mapTransportsToLoad(landUnitsToAdd, seaTransports);
if (!mapLoading.keySet().containsAll(landUnitsToAdd)) {
return "Can't add land units to water without enough transports";
}
}
}
}
// now perform the changes
logEvent("Adding units owned by " + units.iterator().next().getOwner().getName() + " to " + territory.getName() + ": " + MyFormatter.unitsToTextNoOwner(units), units);
bridge.addChange(ChangeFactory.addUnits(territory, units));
if (Properties.getUnitsMayGiveBonusMovement(getData()) && GameStepPropertiesHelper.isGiveBonusMovement(data)) {
bridge.addChange(MoveDelegate.giveBonusMovementToUnits(player, data, territory));
}
if (mapLoading != null && !mapLoading.isEmpty()) {
for (final Entry<Unit, Unit> entry : mapLoading.entrySet()) {
bridge.addChange(TransportTracker.loadTransportChange((TripleAUnit) entry.getValue(), entry.getKey()));
}
}
return null;
}
use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.
the class EditDelegate method changeUnitBombingDamage.
@Override
public String changeUnitBombingDamage(final IntegerMap<Unit> unitDamageMap, final Territory territory) {
String result = checkEditMode();
if (result != null) {
return result;
}
result = EditValidator.validateChangeBombingDamage(getData(), unitDamageMap, territory);
if (result != null) {
return result;
}
// remove anyone who is the same
final Collection<Unit> units = new ArrayList<>(unitDamageMap.keySet());
for (final Unit u : units) {
final int dmg = unitDamageMap.getInt(u);
final int currentDamage = ((TripleAUnit) u).getUnitDamage();
if (currentDamage == dmg) {
unitDamageMap.removeKey(u);
}
}
if (unitDamageMap.isEmpty()) {
return null;
}
// we do damage to the unit
final Collection<Unit> unitsFinal = new ArrayList<>(unitDamageMap.keySet());
logEvent("Changing unit bombing damage for these " + unitsFinal.iterator().next().getOwner().getName() + " owned units to: " + MyFormatter.integerUnitMapToString(unitDamageMap, ", ", " = ", false), unitsFinal);
bridge.addChange(ChangeFactory.bombingUnitDamage(unitDamageMap));
// territory.notifyChanged();
return null;
}
use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.
the class EditValidator method validateChangeBombingDamage.
static String validateChangeBombingDamage(final GameData data, final IntegerMap<Unit> unitDamageMap, final Territory territory) {
if (unitDamageMap == null || unitDamageMap.isEmpty()) {
return "Damage map is empty";
}
final String result = validateTerritoryBasic(data, territory);
if (result != null) {
return result;
}
if (!Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
return "Game does not allow bombing damage";
}
final Collection<Unit> units = new ArrayList<>(unitDamageMap.keySet());
if (!territory.getUnits().getUnits().containsAll(units)) {
return "Selected Territory does not contain all of the selected units";
}
final PlayerID player = units.iterator().next().getOwner();
// all units should be same owner
if (units.isEmpty() || !units.stream().allMatch(Matches.unitIsOwnedBy(player))) {
return "Not all units have the same owner";
}
if (units.isEmpty() || !units.stream().allMatch(Matches.unitCanBeDamaged())) {
return "Not all units can take bombing damage";
}
for (final Unit u : units) {
final int dmg = unitDamageMap.getInt(u);
if (dmg < 0 || dmg > ((TripleAUnit) u).getHowMuchDamageCanThisUnitTakeTotal(u, territory)) {
return "Damage cannot be less than zero or greater than the max damage of the unit";
}
}
return null;
}
use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.
the class AirMovementValidator method maxMovementLeftForAllOwnedCarriers.
private static int maxMovementLeftForAllOwnedCarriers(final PlayerID player, final GameData data) {
int max = 0;
final Predicate<Unit> ownedCarrier = Matches.unitIsCarrier().and(Matches.unitIsOwnedBy(player));
for (final Territory t : data.getMap().getTerritories()) {
for (final Unit carrier : t.getUnits().getMatches(ownedCarrier)) {
max = Math.max(max, ((TripleAUnit) carrier).getMovementLeft());
}
}
return max;
}
use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.
the class AirMovementValidator method carrierCapacity.
/**
* Does not, and is not supposed to, account for any units already on this carrier (like allied/cargo fighters).
* Instead this method only adds up the total capacity of each unit, and accounts for damaged carriers with special
* properties and restrictions.
*/
public static int carrierCapacity(final Unit unit, final Territory territoryUnitsAreCurrentlyIn) {
if (Matches.unitIsCarrier().test(unit)) {
// here we check to see if the unit can no longer carry units
if (Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLANDONCARRIER).test(unit)) {
// and we must check to make sure we let any allied air that are cargo stay here
if (Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLEAVEALLIEDCARRIER).test(unit)) {
int cargo = 0;
final Collection<Unit> airCargo = territoryUnitsAreCurrentlyIn.getUnits().getMatches(Matches.unitIsAir().and(Matches.unitCanLandOnCarrier()));
for (final Unit airUnit : airCargo) {
final TripleAUnit taUnit = (TripleAUnit) airUnit;
if (taUnit.getTransportedBy() != null && taUnit.getTransportedBy().equals(unit)) {
// capacity = are cargo only
cargo += UnitAttachment.get(taUnit.getType()).getCarrierCost();
}
}
return cargo;
}
// capacity = zero 0
return 0;
}
final UnitAttachment ua = UnitAttachment.get(unit.getType());
return ua.getCarrierCapacity();
}
return 0;
}
Aggregations