use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class PlayerUnitsPanel method init.
/**
* Sets up components to an initial state.
*/
public void init(final PlayerID id, final List<Unit> units, final boolean land) {
isLand = land;
categories = new ArrayList<>(categorize(id, units));
categories.sort(Comparator.comparing(UnitCategory::getType, (ut1, ut2) -> {
final UnitAttachment u1 = UnitAttachment.get(ut1);
final UnitAttachment u2 = UnitAttachment.get(ut2);
// For land battles, sort by land, air, can't combat move (AA), bombarding
if (land) {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? 1 : -1;
}
final boolean u1CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut1) || !Matches.unitTypeCanMove(id).test(ut1);
final boolean u2CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut2) || !Matches.unitTypeCanMove(id).test(ut2);
if (u1CanNotCombatMove != u2CanNotCombatMove) {
return u1CanNotCombatMove ? 1 : -1;
}
if (u1.getIsAir() != u2.getIsAir()) {
return u1.getIsAir() ? 1 : -1;
}
} else {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? -1 : 1;
}
}
return u1.getName().compareTo(u2.getName());
}));
removeAll();
final Predicate<UnitType> predicate;
if (land) {
if (defender) {
predicate = Matches.unitTypeIsNotSea();
} else {
predicate = Matches.unitTypeIsNotSea().or(Matches.unitTypeCanBombard(id));
}
} else {
predicate = Matches.unitTypeIsSeaOrAir();
}
final IntegerMap<UnitType> costs;
try {
data.acquireReadLock();
costs = TuvUtils.getCostsForTuv(id, data);
} finally {
data.releaseReadLock();
}
for (final UnitCategory category : categories) {
if (predicate.test(category.getType())) {
final UnitPanel upanel = new UnitPanel(uiContext, category, costs);
upanel.addChangeListener(this::notifyListeners);
add(upanel);
}
}
// TODO: probably do not need to do this much revalidation.
invalidate();
validate();
revalidate();
getParent().invalidate();
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class MyFormatter method unitsToTextNoOwner.
public static String unitsToTextNoOwner(final Collection<Unit> units, final PlayerID owner) {
final IntegerMap<UnitType> map = new IntegerMap<>();
for (final Unit unit : units) {
if (owner == null || owner.equals(unit.getOwner())) {
map.add(unit.getType(), 1);
}
}
final StringBuilder buf = new StringBuilder();
// sort on unit name
final List<UnitType> sortedList = new ArrayList<>(map.keySet());
sortedList.sort(Comparator.comparing(UnitType::getName));
int count = map.keySet().size();
for (final UnitType type : sortedList) {
final int quantity = map.getInt(type);
buf.append(quantity);
buf.append(" ");
buf.append(quantity > 1 ? pluralize(type.getName()) : type.getName());
count--;
if (count > 1) {
buf.append(", ");
}
if (count == 1) {
buf.append(" and ");
}
}
return buf.toString();
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class MyFormatter method defaultNamedToTextList.
public static String defaultNamedToTextList(final Collection<? extends DefaultNamed> list, final String seperator, final boolean showQuantity) {
final IntegerMap<DefaultNamed> map = new IntegerMap<>();
for (final DefaultNamed unit : list) {
if (unit == null || unit.getName() == null) {
throw new IllegalStateException("Unit or Resource no longer exists?!?");
}
map.add(unit, 1);
}
final StringBuilder buf = new StringBuilder();
// sort on unit name
final List<DefaultNamed> sortedList = new ArrayList<>(map.keySet());
sortedList.sort(Comparator.comparing(DefaultNamed::getName));
int count = map.keySet().size();
for (final DefaultNamed type : sortedList) {
if (showQuantity) {
final int quantity = map.getInt(type);
buf.append(quantity);
buf.append(" ");
buf.append(quantity > 1 ? pluralize(type.getName()) : type.getName());
} else {
buf.append(type.getName());
}
count--;
if (count > 1) {
buf.append(seperator);
}
if (count == 1) {
buf.append(" and ");
}
}
return buf.toString();
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class TransportUtils method mapTransportsToLoad.
/**
* Returns a map of unit -> transport. Tries to load units evenly across all transports.
*/
public static Map<Unit, Unit> mapTransportsToLoad(final Collection<Unit> units, final Collection<Unit> transports) {
List<Unit> canBeTransported = CollectionUtils.getMatches(units, Matches.unitCanBeTransported());
canBeTransported = sortByTransportCostDescending(canBeTransported);
List<Unit> canTransport = CollectionUtils.getMatches(transports, Matches.unitCanTransport());
canTransport = sortByTransportCapacityDescendingThenMovesDescending(canTransport);
// Add units to transports evenly
final Map<Unit, Unit> mapping = new HashMap<>();
final IntegerMap<Unit> addedLoad = new IntegerMap<>();
for (final Unit unit : canBeTransported) {
final Optional<Unit> transport = loadUnitIntoFirstAvailableTransport(unit, canTransport, mapping, addedLoad);
// Move loaded transport to end of list
if (transport.isPresent()) {
canTransport.remove(transport.get());
canTransport.add(transport.get());
}
}
return mapping;
}
use of games.strategy.util.IntegerMap in project triplea by triplea-game.
the class TuvUtils method getResourceCostsForTuvForAllPlayersMergedAndAveraged.
/**
* Return a map where key are unit types and values are the AVERAGED for all players.
* Any production rule that produces multiple units
* (like artillery in NWO, costs 7 but makes 2 artillery, meaning effective price is 3.5 each)
* will have their costs rounded up on a per unit basis.
* Therefore, this map should NOT be used for Purchasing information!
*/
private static Map<UnitType, ResourceCollection> getResourceCostsForTuvForAllPlayersMergedAndAveraged(final GameData data) {
final Map<UnitType, ResourceCollection> average = new HashMap<>();
final Resource pus;
data.acquireReadLock();
try {
pus = data.getResourceList().getResource(Constants.PUS);
} finally {
data.releaseReadLock();
}
final IntegerMap<Resource> defaultMap = new IntegerMap<>();
defaultMap.put(pus, 1);
final ResourceCollection defaultResources = new ResourceCollection(data, defaultMap);
final Map<UnitType, List<ResourceCollection>> backups = new HashMap<>();
final Map<UnitType, ResourceCollection> backupAveraged = new HashMap<>();
for (final ProductionRule rule : data.getProductionRuleList().getProductionRules()) {
if (rule == null || rule.getResults() == null || rule.getResults().isEmpty() || rule.getCosts() == null || rule.getCosts().isEmpty()) {
continue;
}
final IntegerMap<NamedAttachable> unitMap = rule.getResults();
final ResourceCollection costPerGroup = new ResourceCollection(data, rule.getCosts());
final Set<UnitType> units = new HashSet<>();
for (final NamedAttachable resourceOrUnit : unitMap.keySet()) {
if (!(resourceOrUnit instanceof UnitType)) {
continue;
}
units.add((UnitType) resourceOrUnit);
}
if (units.isEmpty()) {
continue;
}
final int totalProduced = unitMap.totalValues();
if (totalProduced == 1) {
final UnitType ut = units.iterator().next();
final List<ResourceCollection> current = backups.computeIfAbsent(ut, k -> new ArrayList<>());
current.add(costPerGroup);
} else if (totalProduced > 1) {
costPerGroup.discount((double) 1 / (double) totalProduced);
for (final UnitType ut : units) {
final List<ResourceCollection> current = backups.computeIfAbsent(ut, k -> new ArrayList<>());
current.add(costPerGroup);
}
}
}
for (final Entry<UnitType, List<ResourceCollection>> entry : backups.entrySet()) {
final ResourceCollection avgCost = new ResourceCollection(entry.getValue().toArray(new ResourceCollection[0]), data);
if (entry.getValue().size() > 1) {
avgCost.discount((double) 1 / (double) entry.getValue().size());
}
backupAveraged.put(entry.getKey(), avgCost);
}
final Map<PlayerID, Map<UnitType, ResourceCollection>> allPlayersCurrent = getResourceCostsForTuv(data, false);
allPlayersCurrent.remove(null);
for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
final List<ResourceCollection> costs = new ArrayList<>();
for (final Map<UnitType, ResourceCollection> entry : allPlayersCurrent.values()) {
if (entry.get(ut) != null) {
costs.add(entry.get(ut));
}
}
if (costs.isEmpty()) {
final ResourceCollection backup = backupAveraged.get(ut);
if (backup != null) {
costs.add(backup);
} else {
costs.add(defaultResources);
}
}
final ResourceCollection avgCost = new ResourceCollection(costs.toArray(new ResourceCollection[0]), data);
if (costs.size() > 1) {
avgCost.discount((double) 1 / (double) costs.size());
}
average.put(ut, avgCost);
}
return average;
}
Aggregations