use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class TriggerAttachment method removeUnits.
private static void removeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
final CompositeChange change = new CompositeChange();
final Collection<Unit> totalRemoved = new ArrayList<>();
for (final UnitType ut : utMap.keySet()) {
final int removeNum = utMap.getInt(ut);
final Collection<Unit> toRemove = CollectionUtils.getNMatches(terr.getUnits().getUnits(), removeNum, Matches.unitIsOwnedBy(player).and(Matches.unitIsOfType(ut)));
if (!toRemove.isEmpty()) {
totalRemoved.addAll(toRemove);
change.add(ChangeFactory.removeUnits(terr, toRemove));
}
}
if (!change.isEmpty()) {
final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": has removed " + MyFormatter.unitsToTextNoOwner(totalRemoved) + " owned by " + player.getName() + " in " + terr.getName();
bridge.getHistoryWriter().startEvent(transcriptText, totalRemoved);
bridge.addChange(change);
}
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class UnitAttachment method getAllOfTypeAas.
public static List<String> getAllOfTypeAas(final Collection<Unit> aaUnitsAlreadyVerified) {
final Set<String> aaSet = new HashSet<>();
for (final Unit u : aaUnitsAlreadyVerified) {
aaSet.add(UnitAttachment.get(u.getType()).getTypeAa());
}
final List<String> aaTypes = new ArrayList<>(aaSet);
Collections.sort(aaTypes);
return aaTypes;
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MoveDelegate method giveBonusMovementToUnits.
static Change giveBonusMovementToUnits(final PlayerID player, final GameData data, final Territory t) {
final CompositeChange change = new CompositeChange();
for (final Unit u : t.getUnits().getUnits()) {
if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(t, player, data).test(u)) {
if (!Matches.isUnitAllied(player, data).test(u)) {
continue;
}
int bonusMovement = Integer.MIN_VALUE;
final Predicate<Unit> givesBonusUnit = Matches.alliedUnit(player, data).and(Matches.unitCanGiveBonusMovementToThisUnit(u));
final Collection<Unit> givesBonusUnits = new ArrayList<>(CollectionUtils.getMatches(t.getUnits().getUnits(), givesBonusUnit));
if (Matches.unitIsSea().test(u)) {
final Predicate<Unit> givesBonusUnitLand = givesBonusUnit.and(Matches.unitIsLand());
final Set<Territory> neighbors = new HashSet<>(data.getMap().getNeighbors(t, Matches.territoryIsLand()));
for (final Territory current : neighbors) {
givesBonusUnits.addAll(CollectionUtils.getMatches(current.getUnits().getUnits(), givesBonusUnitLand));
}
} else if (Matches.unitIsLand().test(u)) {
final Predicate<Unit> givesBonusUnitSea = givesBonusUnit.and(Matches.unitIsSea());
final Set<Territory> neighbors = new HashSet<>(data.getMap().getNeighbors(t, Matches.territoryIsWater()));
for (final Territory current : neighbors) {
givesBonusUnits.addAll(CollectionUtils.getMatches(current.getUnits().getUnits(), givesBonusUnitSea));
}
}
for (final Unit bonusGiver : givesBonusUnits) {
final int tempBonus = UnitAttachment.get(bonusGiver.getType()).getGivesMovement().getInt(u.getType());
if (tempBonus > bonusMovement) {
bonusMovement = tempBonus;
}
}
if (bonusMovement != Integer.MIN_VALUE && bonusMovement != 0) {
bonusMovement = Math.max(bonusMovement, (UnitAttachment.get(u.getType()).getMovement(player) * -1));
change.add(ChangeFactory.unitPropertyChange(u, bonusMovement, TripleAUnit.BONUS_MOVEMENT));
}
}
}
return change;
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MoveDelegate method move.
@Override
public String move(final Collection<Unit> units, final Route route, final Collection<Unit> transportsThatCanBeLoaded, final Map<Unit, Collection<Unit>> newDependents) {
final GameData data = getData();
// the reason we use this, is if we are in edit mode, we may have a different unit owner than the current player
final PlayerID player = getUnitsOwner(units);
final MoveValidationResult result = MoveValidator.validateMove(units, route, player, transportsThatCanBeLoaded, newDependents, GameStepPropertiesHelper.isNonCombatMove(data, false), movesToUndo, data);
final StringBuilder errorMsg = new StringBuilder(100);
final int numProblems = result.getTotalWarningCount() - (result.hasError() ? 0 : 1);
final String numErrorsMsg = numProblems > 0 ? ("; " + numProblems + " " + MyFormatter.pluralize("error", numProblems) + " not shown") : "";
if (result.hasError()) {
return errorMsg.append(result.getError()).append(numErrorsMsg).toString();
}
if (result.hasDisallowedUnits()) {
return errorMsg.append(result.getDisallowedUnitWarning(0)).append(numErrorsMsg).toString();
}
boolean isKamikaze = false;
final boolean getKamikazeAir = Properties.getKamikazeAirplanes(data);
Collection<Unit> kamikazeUnits = new ArrayList<>();
// confirm kamikaze moves, and remove them from unresolved units
if (getKamikazeAir || units.stream().anyMatch(Matches.unitIsKamikaze())) {
kamikazeUnits = result.getUnresolvedUnits(MoveValidator.NOT_ALL_AIR_UNITS_CAN_LAND);
if (kamikazeUnits.size() > 0 && getRemotePlayer().confirmMoveKamikaze()) {
for (final Unit unit : kamikazeUnits) {
if (getKamikazeAir || Matches.unitIsKamikaze().test(unit)) {
result.removeUnresolvedUnit(MoveValidator.NOT_ALL_AIR_UNITS_CAN_LAND, unit);
isKamikaze = true;
}
}
}
}
if (result.hasUnresolvedUnits()) {
return errorMsg.append(result.getUnresolvedUnitWarning(0)).append(numErrorsMsg).toString();
}
// allow user to cancel move if aa guns will fire
final AAInMoveUtil aaInMoveUtil = new AAInMoveUtil();
aaInMoveUtil.initialize(bridge);
final Collection<Territory> aaFiringTerritores = aaInMoveUtil.getTerritoriesWhereAaWillFire(route, units);
if (!aaFiringTerritores.isEmpty()) {
if (!getRemotePlayer().confirmMoveInFaceOfAa(aaFiringTerritores)) {
return null;
}
}
// do the move
final UndoableMove currentMove = new UndoableMove(units, route);
final String transcriptText = MyFormatter.unitsToTextNoOwner(units) + " moved from " + route.getStart().getName() + " to " + route.getEnd().getName();
bridge.getHistoryWriter().startEvent(transcriptText, currentMove.getDescriptionObject());
if (isKamikaze) {
bridge.getHistoryWriter().addChildToEvent("This was a kamikaze move, for at least some of the units", kamikazeUnits);
}
tempMovePerformer = new MovePerformer();
tempMovePerformer.initialize(this);
tempMovePerformer.moveUnits(units, route, player, transportsThatCanBeLoaded, newDependents, currentMove);
tempMovePerformer = null;
return null;
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MoveDelegate method repairMultipleHitPointUnits.
static void repairMultipleHitPointUnits(final IDelegateBridge bridge, final PlayerID player) {
final GameData data = bridge.getData();
final boolean repairOnlyOwn = Properties.getBattleshipsRepairAtBeginningOfRound(bridge.getData());
final Predicate<Unit> damagedUnits = Matches.unitHasMoreThanOneHitPointTotal().and(Matches.unitHasTakenSomeDamage());
final Predicate<Unit> damagedUnitsOwned = damagedUnits.and(Matches.unitIsOwnedBy(player));
final Map<Territory, Set<Unit>> damagedMap = new HashMap<>();
for (final Territory current : data.getMap().getTerritories()) {
final Set<Unit> damaged;
if (!Properties.getTwoHitPointUnitsRequireRepairFacilities(data)) {
damaged = new HashSet<>(current.getUnits().getMatches(repairOnlyOwn ? damagedUnitsOwned : damagedUnits));
} else {
damaged = new HashSet<>(current.getUnits().getMatches(damagedUnitsOwned.and(Matches.unitCanBeRepairedByFacilitiesInItsTerritory(current, player, data))));
}
if (!damaged.isEmpty()) {
damagedMap.put(current, damaged);
}
}
if (damagedMap.isEmpty()) {
return;
}
final Map<Unit, Territory> fullyRepaired = new HashMap<>();
final IntegerMap<Unit> newHitsMap = new IntegerMap<>();
for (final Entry<Territory, Set<Unit>> entry : damagedMap.entrySet()) {
for (final Unit u : entry.getValue()) {
final int repairAmount = getLargestRepairRateForThisUnit(u, entry.getKey(), data);
final int currentHits = u.getHits();
final int newHits = Math.max(0, Math.min(currentHits, (currentHits - repairAmount)));
if (newHits != currentHits) {
newHitsMap.put(u, newHits);
}
if (newHits <= 0) {
fullyRepaired.put(u, entry.getKey());
}
}
}
bridge.getHistoryWriter().startEvent(newHitsMap.size() + " " + MyFormatter.pluralize("unit", newHitsMap.size()) + " repaired.", new HashSet<>(newHitsMap.keySet()));
bridge.addChange(ChangeFactory.unitsHit(newHitsMap));
// now if damaged includes any carriers that are repairing, and have damaged abilities set for not allowing air
// units to leave while damaged, we need to remove those air units now
final Collection<Unit> damagedCarriers = CollectionUtils.getMatches(fullyRepaired.keySet(), Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLEAVEALLIEDCARRIER));
// now cycle through those now-repaired carriers, and remove allied air from being dependent
final CompositeChange clearAlliedAir = new CompositeChange();
for (final Unit carrier : damagedCarriers) {
final CompositeChange change = MustFightBattle.clearTransportedByForAlliedAirOnCarrier(Collections.singleton(carrier), fullyRepaired.get(carrier), carrier.getOwner(), data);
if (!change.isEmpty()) {
clearAlliedAir.add(change);
}
}
if (!clearAlliedAir.isEmpty()) {
bridge.addChange(clearAlliedAir);
}
// Check if any repaired units change into different unit types
for (final Territory territory : damagedMap.keySet()) {
repairedChangeInto(damagedMap.get(territory), territory, bridge);
}
}
Aggregations