use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class MustFightBattle method sortAmphib.
/**
* In an amphibious assault, sort on who is unloading from transports first as this will allow the marines with higher
* scores to get killed last.
*/
private void sortAmphib(final List<Unit> units) {
final Comparator<Unit> decreasingMovement = UnitComparator.getLowestToHighestMovementComparator();
units.sort(Comparator.comparing(Unit::getType, Comparator.comparing(UnitType::getName)).thenComparing((u1, u2) -> {
final UnitAttachment ua = UnitAttachment.get(u1.getType());
final UnitAttachment ua2 = UnitAttachment.get(u2.getType());
if (ua.getIsMarine() != 0 && ua2.getIsMarine() != 0) {
return compareAccordingToAmphibious(u1, u2);
}
return 0;
}).thenComparing(decreasingMovement));
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class NoPUPurchaseDelegate method getProductionUnits.
private Collection<Unit> getProductionUnits(final Collection<Territory> territories, final PlayerID player) {
final Collection<Unit> productionUnits = new ArrayList<>();
if (!(isProductionPerXTerritoriesRestricted() || isProductionPerValuedTerritoryRestricted())) {
return productionUnits;
}
IntegerMap<UnitType> productionPerXTerritories = new IntegerMap<>();
final RulesAttachment ra = (RulesAttachment) player.getAttachment(Constants.RULES_ATTACHMENT_NAME);
// isProductionPerValuedTerritoryRestricted, then they want 1 infantry for each territory with PU value > 0
if (isProductionPerValuedTerritoryRestricted() && (ra == null || ra.getProductionPerXTerritories() == null || ra.getProductionPerXTerritories().size() == 0)) {
productionPerXTerritories.put(getData().getUnitTypeList().getUnitType(Constants.UNIT_TYPE_INFANTRY), 1);
} else if (isProductionPerXTerritoriesRestricted()) {
productionPerXTerritories = ra.getProductionPerXTerritories();
} else {
return productionUnits;
}
final Collection<UnitType> unitTypes = new ArrayList<>(productionPerXTerritories.keySet());
for (final UnitType ut : unitTypes) {
int unitCount = 0;
final int prodPerXTerrs = productionPerXTerritories.getInt(ut);
if (isPacific) {
unitCount += getBurmaRoad(player);
}
int terrCount = 0;
for (final Territory current : territories) {
if (!isProductionPerValuedTerritoryRestricted()) {
terrCount++;
} else {
if (TerritoryAttachment.getProduction(current) > 0) {
terrCount++;
}
}
}
unitCount += terrCount / prodPerXTerrs;
productionUnits.addAll(getData().getUnitTypeList().getUnitType(ut.getName()).create(unitCount, player));
}
return productionUnits;
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class UnitAttachment method setAttackingLimit.
private void setAttackingLimit(final String value) throws GameParseException {
if (value == null) {
m_attackingLimit = null;
return;
}
final UnitType ut = (UnitType) this.getAttachedTo();
if (ut == null) {
throw new GameParseException("getAttachedTo returned null" + thisErrorMsg());
}
final String[] s = value.split(":");
if (s.length != 2) {
throw new GameParseException("attackingLimit must have 2 fields, value and count" + thisErrorMsg());
}
final int max = getInt(s[0]);
if (max < 0) {
throw new GameParseException("attackingLimit count must have a positive number" + thisErrorMsg());
}
if (!(s[1].equals("owned") || s[1].equals("allied") || s[1].equals("total"))) {
throw new GameParseException("attackingLimit value must owned, allied, or total" + thisErrorMsg());
}
m_attackingLimit = Tuple.of(max, s[1]);
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class AAInMoveUtil method fireAa.
/**
* Fire the aa units in the given territory, hits are removed from units.
*/
private void fireAa(final Territory territory, final Collection<Unit> units, final UndoableMove currentMove) {
if (units.isEmpty()) {
return;
}
final PlayerID movingPlayer = movingPlayer(units);
final HashMap<String, HashSet<UnitType>> airborneTechTargetsAllowed = TechAbilityAttachment.getAirborneTargettedByAa(movingPlayer, getData());
final List<Unit> defendingAa = territory.getUnits().getMatches(Matches.unitIsAaThatCanFire(units, airborneTechTargetsAllowed, movingPlayer, Matches.unitIsAaForFlyOverOnly(), 1, true, getData()));
// comes ordered alphabetically already
final List<String> aaTypes = UnitAttachment.getAllOfTypeAas(defendingAa);
// stacks are backwards
Collections.reverse(aaTypes);
for (final String currentTypeAa : aaTypes) {
final Collection<Unit> currentPossibleAa = CollectionUtils.getMatches(defendingAa, Matches.unitIsAaOfTypeAa(currentTypeAa));
final Set<UnitType> targetUnitTypesForThisTypeAa = UnitAttachment.get(currentPossibleAa.iterator().next().getType()).getTargetsAa(getData());
final Set<UnitType> airborneTypesTargettedToo = airborneTechTargetsAllowed.get(currentTypeAa);
final Collection<Unit> validTargetedUnitsForThisRoll = CollectionUtils.getMatches(units, Matches.unitIsOfTypes(targetUnitTypesForThisTypeAa).or(Matches.unitIsAirborne().and(Matches.unitIsOfTypes(airborneTypesTargettedToo))));
// once we fire the AA guns, we can't undo
// otherwise you could keep undoing and redoing
// until you got the roll you wanted
currentMove.setCantUndo("Move cannot be undone after " + currentTypeAa + " has fired.");
final DiceRoll[] dice = new DiceRoll[1];
final IExecutable rollDice = new IExecutable() {
private static final long serialVersionUID = 4714364489659654758L;
@Override
public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
// get rid of units already killed, so we don't target them twice
validTargetedUnitsForThisRoll.removeAll(m_casualties);
if (!validTargetedUnitsForThisRoll.isEmpty()) {
dice[0] = DiceRoll.rollAa(validTargetedUnitsForThisRoll, currentPossibleAa, AAInMoveUtil.this.bridge, territory, true);
}
}
};
final IExecutable selectCasualties = new IExecutable() {
private static final long serialVersionUID = -8633263235214834617L;
@Override
public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
if (!validTargetedUnitsForThisRoll.isEmpty()) {
final int hitCount = dice[0].getHits();
if (hitCount == 0) {
if (currentTypeAa.equals("AA")) {
AAInMoveUtil.this.bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AA_MISS, findDefender(currentPossibleAa, territory));
} else {
AAInMoveUtil.this.bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_X_PREFIX + currentTypeAa.toLowerCase() + SoundPath.CLIP_BATTLE_X_MISS, findDefender(currentPossibleAa, territory));
}
getRemotePlayer().reportMessage("No " + currentTypeAa + " hits in " + territory.getName(), "No " + currentTypeAa + " hits in " + territory.getName());
} else {
if (currentTypeAa.equals("AA")) {
AAInMoveUtil.this.bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AA_HIT, findDefender(currentPossibleAa, territory));
} else {
AAInMoveUtil.this.bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_X_PREFIX + currentTypeAa.toLowerCase() + SoundPath.CLIP_BATTLE_X_HIT, findDefender(currentPossibleAa, territory));
}
selectCasualties(dice[0], units, validTargetedUnitsForThisRoll, currentPossibleAa, defendingAa, territory, currentTypeAa);
}
}
}
};
// push in reverse order of execution
m_executionStack.push(selectCasualties);
m_executionStack.push(rollDice);
}
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class EndTurnDelegate method createUnits.
private String createUnits(final IDelegateBridge bridge) {
final StringBuilder endTurnReport = new StringBuilder();
final GameData data = getData();
final PlayerID player = data.getSequence().getStep().getPlayerId();
final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesUnits());
final CompositeChange change = new CompositeChange();
for (final Territory t : data.getMap().getTerritories()) {
final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
if (myCreators != null && !myCreators.isEmpty()) {
final Collection<Unit> toAdd = new ArrayList<>();
final Collection<Unit> toAddSea = new ArrayList<>();
final Collection<Unit> toAddLand = new ArrayList<>();
for (final Unit u : myCreators) {
final UnitAttachment ua = UnitAttachment.get(u.getType());
final IntegerMap<UnitType> createsUnitsMap = ua.getCreatesUnitsList();
final Collection<UnitType> willBeCreated = createsUnitsMap.keySet();
for (final UnitType ut : willBeCreated) {
if (UnitAttachment.get(ut).getIsSea() && Matches.territoryIsLand().test(t)) {
toAddSea.addAll(ut.create(createsUnitsMap.getInt(ut), player));
} else if (!UnitAttachment.get(ut).getIsSea() && !UnitAttachment.get(ut).getIsAir() && Matches.territoryIsWater().test(t)) {
toAddLand.addAll(ut.create(createsUnitsMap.getInt(ut), player));
} else {
toAdd.addAll(ut.create(createsUnitsMap.getInt(ut), player));
}
}
}
if (!toAdd.isEmpty()) {
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAdd) + " in " + t.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAdd);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(t, toAdd);
change.add(place);
}
if (!toAddSea.isEmpty()) {
final Predicate<Territory> myTerrs = Matches.territoryIsWater();
final Collection<Territory> waterNeighbors = data.getMap().getNeighbors(t, myTerrs);
if (waterNeighbors != null && !waterNeighbors.isEmpty()) {
final Territory tw = getRandomTerritory(waterNeighbors, bridge);
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddSea) + " in " + tw.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAddSea);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(tw, toAddSea);
change.add(place);
}
}
if (!toAddLand.isEmpty()) {
final Predicate<Territory> myTerrs = Matches.isTerritoryOwnedBy(player).and(Matches.territoryIsLand());
final Collection<Territory> landNeighbors = data.getMap().getNeighbors(t, myTerrs);
if (landNeighbors != null && !landNeighbors.isEmpty()) {
final Territory tl = getRandomTerritory(landNeighbors, bridge);
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddLand) + " in " + tl.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAddLand);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(tl, toAddLand);
change.add(place);
}
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
return endTurnReport.toString();
}
Aggregations