use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class BattleDelegate method fireKamikazeSuicideAttacks.
/**
* This rolls the dice and validates them to see if units died or not.
* It will use LowLuck or normal dice.
* If any units die, we remove them from the game, and if units take damage but live, we also do that here.
*/
private void fireKamikazeSuicideAttacks(final Unit unitUnderFire, final IntegerMap<Resource> numberOfAttacks, final IntegerMap<Resource> resourcesAndAttackValues, final PlayerID firingEnemy, final Territory location) {
// TODO: find a way to autosave after each dice roll.
final GameData data = getData();
final int diceSides = data.getDiceSides();
final CompositeChange change = new CompositeChange();
int hits = 0;
int[] rolls = null;
if (Properties.getLowLuck(data)) {
int power = 0;
for (final Entry<Resource, Integer> entry : numberOfAttacks.entrySet()) {
final Resource r = entry.getKey();
final int num = entry.getValue();
change.add(ChangeFactory.changeResourcesChange(firingEnemy, r, -num));
power += num * resourcesAndAttackValues.getInt(r);
}
if (power > 0) {
hits = power / diceSides;
final int remainder = power % diceSides;
if (remainder > 0) {
rolls = bridge.getRandom(diceSides, 1, firingEnemy, DiceType.COMBAT, "Rolling for remainder in Kamikaze Suicide Attack on unit: " + unitUnderFire.getType().getName());
if (remainder > rolls[0]) {
hits++;
}
}
}
} else {
// avoid multiple calls of getRandom, so just do it once at the beginning
final int numTokens = numberOfAttacks.totalValues();
rolls = bridge.getRandom(diceSides, numTokens, firingEnemy, DiceType.COMBAT, "Rolling for Kamikaze Suicide Attack on unit: " + unitUnderFire.getType().getName());
final int[] powerOfTokens = new int[numTokens];
int j = 0;
for (final Entry<Resource, Integer> entry : numberOfAttacks.entrySet()) {
final Resource r = entry.getKey();
int num = entry.getValue();
change.add(ChangeFactory.changeResourcesChange(firingEnemy, r, -num));
final int power = resourcesAndAttackValues.getInt(r);
while (num > 0) {
powerOfTokens[j] = power;
j++;
num--;
}
}
for (int i = 0; i < rolls.length; i++) {
if (powerOfTokens[i] > rolls[i]) {
hits++;
}
}
}
final String title = "Kamikaze Suicide Attack attacks " + MyFormatter.unitsToText(Collections.singleton(unitUnderFire));
final String dice = " scoring " + hits + " hits. Rolls: " + MyFormatter.asDice(rolls);
bridge.getHistoryWriter().startEvent(title + dice, unitUnderFire);
if (hits > 0) {
final UnitAttachment ua = UnitAttachment.get(unitUnderFire.getType());
final int currentHits = unitUnderFire.getHits();
if (ua.getHitPoints() <= currentHits + hits) {
// TODO: kill dependents
change.add(ChangeFactory.removeUnits(location, Collections.singleton(unitUnderFire)));
} else {
final IntegerMap<Unit> hitMap = new IntegerMap<>();
hitMap.put(unitUnderFire, hits);
change.add(createDamageChange(hitMap, bridge));
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
// kamikaze suicide attacks, even if unsuccessful, deny the ability to bombard from this sea zone
battleTracker.addNoBombardAllowedFromHere(location);
// TODO: display this as actual dice for both players
final Collection<PlayerID> playersInvolved = new ArrayList<>();
playersInvolved.add(player);
playersInvolved.add(firingEnemy);
this.getDisplay().reportMessageToPlayers(playersInvolved, null, title + dice, title);
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class TriggerAttachment method triggerResourceChange.
private static IntegerMap<Resource> triggerResourceChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen, final StringBuilder endOfTurnReport) {
final GameData data = bridge.getData();
Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, resourceMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final IntegerMap<Resource> resources = new IntegerMap<>();
for (final TriggerAttachment t : trigs) {
if (testChance && !t.testChance(bridge)) {
continue;
}
if (useUses) {
t.use(bridge);
}
final int eachMultiple = getEachMultiple(t);
for (final PlayerID player : t.getPlayers()) {
for (int i = 0; i < eachMultiple; ++i) {
int toAdd = t.getResourceCount();
if (t.getResource().equals(Constants.PUS)) {
toAdd *= Properties.getPuMultiplier(data);
}
resources.add(data.getResourceList().getResource(t.getResource()), toAdd);
int total = player.getResources().getQuantity(t.getResource()) + toAdd;
if (total < 0) {
toAdd -= total;
total = 0;
}
bridge.addChange(ChangeFactory.changeResourcesChange(player, data.getResourceList().getResource(t.getResource()), toAdd));
final String puMessage = MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " met a national objective for an additional " + t.getResourceCount() + " " + t.getResource() + "; end with " + total + " " + t.getResource();
bridge.getHistoryWriter().startEvent(puMessage);
endOfTurnReport.append(puMessage).append(" <br />");
}
}
}
return resources;
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class TriggerAttachment method setPlacement.
/**
* Fudging this, it really represents adding placements.
*/
private void setPlacement(final String place) throws GameParseException {
if (place == null) {
m_placement = null;
return;
}
final String[] s = place.split(":");
if (s.length < 1) {
throw new GameParseException("Empty placement list" + thisErrorMsg());
}
int count;
int i = 0;
try {
count = getInt(s[0]);
i++;
} catch (final Exception e) {
count = 1;
}
if (s.length < 1 || (s.length == 1 && count != -1)) {
throw new GameParseException("Empty placement list" + thisErrorMsg());
}
final Territory territory = getData().getMap().getTerritory(s[i]);
if (territory == null) {
throw new GameParseException("Territory does not exist " + s[i] + thisErrorMsg());
}
i++;
final IntegerMap<UnitType> map = new IntegerMap<>();
for (; i < s.length; i++) {
final UnitType type = getData().getUnitTypeList().getUnitType(s[i]);
if (type == null) {
throw new GameParseException("UnitType does not exist " + s[i] + thisErrorMsg());
}
map.add(type, count);
}
if (m_placement == null) {
m_placement = new HashMap<>();
}
if (m_placement.containsKey(territory)) {
map.add(m_placement.get(territory));
}
m_placement.put(territory, map);
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class UnitAttachment method getReceivesAbilityWhenWithMap.
private static IntegerMap<Tuple<String, String>> getReceivesAbilityWhenWithMap(final Collection<Unit> units, final String filterForAbility, final GameData data) {
final IntegerMap<Tuple<String, String>> map = new IntegerMap<>();
final Collection<UnitType> canReceive = getUnitTypesFromUnitList(CollectionUtils.getMatches(units, Matches.unitCanReceiveAbilityWhenWith()));
for (final UnitType ut : canReceive) {
final Collection<String> receives = UnitAttachment.get(ut).getReceivesAbilityWhenWith();
for (final String receive : receives) {
final String[] s = receive.split(":");
if (filterForAbility != null && !filterForAbility.equals(s[0])) {
continue;
}
map.put(Tuple.of(s[0], s[1]), CollectionUtils.countMatches(units, Matches.unitIsOfType(data.getUnitTypeList().getUnitType(s[1]))));
}
}
return map;
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class UnitAttachment method setWhenCapturedChangesInto.
@VisibleForTesting
void setWhenCapturedChangesInto(final String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length < 5 || s.length % 2 == 0) {
throw new GameParseException("whenCapturedChangesInto must have 5 or more values, " + "playerFrom:playerTo:keepAttributes:unitType:howMany " + "(you may have additional unitType:howMany:unitType:howMany, etc" + thisErrorMsg());
}
final PlayerID pfrom = getData().getPlayerList().getPlayerId(s[0]);
if (pfrom == null && !s[0].equals("any")) {
throw new GameParseException("whenCapturedChangesInto: No player named: " + s[0] + thisErrorMsg());
}
final PlayerID pto = getData().getPlayerList().getPlayerId(s[1]);
if (pto == null && !s[1].equals("any")) {
throw new GameParseException("whenCapturedChangesInto: No player named: " + s[1] + thisErrorMsg());
}
getBool(s[2]);
final IntegerMap<UnitType> unitsToMake = new IntegerMap<>();
for (int i = 3; i < s.length; i += 2) {
final UnitType ut = getData().getUnitTypeList().getUnitType(s[i]);
if (ut == null) {
throw new GameParseException("whenCapturedChangesInto: No unit named: " + s[i] + thisErrorMsg());
}
unitsToMake.put(ut, getInt(s[i + 1]));
}
m_whenCapturedChangesInto.put(s[0] + ":" + s[1], Tuple.of(s[2], unitsToMake));
}
Aggregations