use of games.strategy.engine.data.NamedAttachable in project triplea by triplea-game.
the class EditProductionPanel method initRules.
@Override
protected void initRules(final PlayerID player, final IntegerMap<ProductionRule> initialPurchase) {
this.data.acquireReadLock();
try {
id = player;
final Set<UnitType> unitsAllowed = new HashSet<>();
if (player.getProductionFrontier() != null) {
for (final ProductionRule productionRule : player.getProductionFrontier()) {
final Rule rule = new Rule(productionRule, player);
for (final Entry<NamedAttachable, Integer> entry : productionRule.getResults().entrySet()) {
if (UnitType.class.isAssignableFrom(entry.getKey().getClass())) {
unitsAllowed.add((UnitType) entry.getKey());
}
}
final int initialQuantity = initialPurchase.getInt(productionRule);
rule.setQuantity(initialQuantity);
rules.add(rule);
}
}
// use the units on a map.
for (final Territory t : data.getMap()) {
for (final Unit u : t.getUnits()) {
if (u.getOwner().equals(player)) {
final UnitType ut = u.getType();
if (!unitsAllowed.contains(ut)) {
unitsAllowed.add(ut);
final IntegerMap<NamedAttachable> result = new IntegerMap<>();
result.add(ut, 1);
final IntegerMap<Resource> cost = new IntegerMap<>();
cost.add(data.getResourceList().getResource(Constants.PUS), 1);
final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
final Rule rule = new Rule(newRule, player);
rule.setQuantity(0);
rules.add(rule);
}
}
}
}
// now check if we have the art for anything that is left
for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
if (!unitsAllowed.contains(ut)) {
try {
final UnitImageFactory imageFactory = uiContext.getUnitImageFactory();
if (imageFactory != null) {
final Optional<Image> unitImage = imageFactory.getImage(ut, player, false, false);
if (unitImage.isPresent()) {
unitsAllowed.add(ut);
final IntegerMap<NamedAttachable> result = new IntegerMap<>();
result.add(ut, 1);
final IntegerMap<Resource> cost = new IntegerMap<>();
cost.add(data.getResourceList().getResource(Constants.PUS), 1);
final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
final Rule rule = new Rule(newRule, player);
rule.setQuantity(0);
rules.add(rule);
}
}
} catch (final Exception e) {
// ignore
}
}
}
} finally {
this.data.releaseReadLock();
}
}
use of games.strategy.engine.data.NamedAttachable in project triplea by triplea-game.
the class TabbedProductionPanel method getDefaultRuleLists.
private List<Tuple<String, List<Rule>>> getDefaultRuleLists() {
final List<Tuple<String, List<Rule>>> ruleLists = new ArrayList<>();
final ArrayList<Rule> allRules = new ArrayList<>();
final ArrayList<Rule> landRules = new ArrayList<>();
final ArrayList<Rule> airRules = new ArrayList<>();
final ArrayList<Rule> seaRules = new ArrayList<>();
final ArrayList<Rule> constructRules = new ArrayList<>();
final ArrayList<Rule> upgradeConsumesRules = new ArrayList<>();
final ArrayList<Rule> resourceRules = new ArrayList<>();
for (final Rule rule : rules) {
allRules.add(rule);
final NamedAttachable resourceOrUnit = rule.getProductionRule().getResults().keySet().iterator().next();
if (resourceOrUnit instanceof UnitType) {
final UnitType type = (UnitType) resourceOrUnit;
final UnitAttachment attach = UnitAttachment.get(type);
if (attach.getConsumesUnits() != null && attach.getConsumesUnits().totalValues() >= 1) {
upgradeConsumesRules.add(rule);
}
// anywhere (placed without needing a factory).
if (attach.getIsConstruction()) {
constructRules.add(rule);
} else if (attach.getIsSea()) {
seaRules.add(rule);
} else if (attach.getIsAir()) {
airRules.add(rule);
} else {
landRules.add(rule);
}
} else if (resourceOrUnit instanceof Resource) {
resourceRules.add(rule);
}
}
ruleLists.add(Tuple.of("All", allRules));
ruleLists.add(Tuple.of("Land", landRules));
ruleLists.add(Tuple.of("Air", airRules));
ruleLists.add(Tuple.of("Sea", seaRules));
ruleLists.add(Tuple.of("Construction", constructRules));
ruleLists.add(Tuple.of("Upgrades/Consumes", upgradeConsumesRules));
ruleLists.add(Tuple.of("Resources", resourceRules));
return ruleLists;
}
use of games.strategy.engine.data.NamedAttachable in project triplea by triplea-game.
the class TuvUtils method getCostsForTuvForAllPlayersMergedAndAveraged.
/**
* Return a map where key are unit types and values are the AVERAGED for all RULES (not 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 IntegerMap<UnitType> getCostsForTuvForAllPlayersMergedAndAveraged(final GameData data) {
data.acquireReadLock();
final Resource pus;
try {
pus = data.getResourceList().getResource(Constants.PUS);
} finally {
data.releaseReadLock();
}
final IntegerMap<UnitType> costs = new IntegerMap<>();
final HashMap<UnitType, List<Integer>> differentCosts = new HashMap<>();
for (final ProductionRule rule : data.getProductionRuleList().getProductionRules()) {
// only works for the first result, so we are assuming each purchase frontier only gives one type of unit
final NamedAttachable resourceOrUnit = rule.getResults().keySet().iterator().next();
if (!(resourceOrUnit instanceof UnitType)) {
continue;
}
final UnitType ut = (UnitType) resourceOrUnit;
final int numberProduced = rule.getResults().getInt(ut);
final int costPerGroup = rule.getCosts().getInt(pus);
// we round up the cost
final int roundedCostPerSingle = (int) Math.ceil((double) costPerGroup / (double) numberProduced);
if (differentCosts.containsKey(ut)) {
differentCosts.get(ut).add(roundedCostPerSingle);
} else {
final List<Integer> listTemp = new ArrayList<>();
listTemp.add(roundedCostPerSingle);
differentCosts.put(ut, listTemp);
}
}
for (final UnitType ut : differentCosts.keySet()) {
int totalCosts = 0;
final List<Integer> costsForType = differentCosts.get(ut);
for (final int cost : costsForType) {
totalCosts += cost;
}
final int averagedCost = (int) Math.round(((double) totalCosts / (double) costsForType.size()));
costs.put(ut, averagedCost);
}
return costs;
}
use of games.strategy.engine.data.NamedAttachable in project triplea by triplea-game.
the class TuvUtils method getCostsForTuv.
/**
* Return map where keys are unit types and values are PU costs of that unit type, based on a player.
* 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 (so NWO artillery will become 4).
* Therefore, this map should NOT be used for Purchasing information!
*
* @param player
* The player to get costs schedule for
* @param data
* The game data.
* @return a map of unit types to PU cost
*/
public static IntegerMap<UnitType> getCostsForTuv(final PlayerID player, final GameData data) {
data.acquireReadLock();
final Resource pus;
try {
pus = data.getResourceList().getResource(Constants.PUS);
} finally {
data.releaseReadLock();
}
final IntegerMap<UnitType> costs = new IntegerMap<>();
final ProductionFrontier frontier = player.getProductionFrontier();
// any one will do then
if (frontier == null) {
return TuvUtils.getCostsForTuvForAllPlayersMergedAndAveraged(data);
}
for (final ProductionRule rule : frontier.getRules()) {
final NamedAttachable resourceOrUnit = rule.getResults().keySet().iterator().next();
if (!(resourceOrUnit instanceof UnitType)) {
continue;
}
final UnitType type = (UnitType) resourceOrUnit;
final int costPerGroup = rule.getCosts().getInt(pus);
final int numberProduced = rule.getResults().getInt(type);
// we average the cost for a single unit, rounding up
final int roundedCostPerSingle = (int) Math.ceil((double) costPerGroup / (double) numberProduced);
costs.put(type, roundedCostPerSingle);
}
// since our production frontier may not cover all the units we control, and not the enemy units,
// we will add any unit types not in our list, based on the list for everyone
final IntegerMap<UnitType> costsAll = TuvUtils.getCostsForTuvForAllPlayersMergedAndAveraged(data);
for (final UnitType ut : costsAll.keySet()) {
if (!costs.keySet().contains(ut)) {
costs.put(ut, costsAll.getInt(ut));
}
}
// Override with XML TUV or consumesUnit sum
final IntegerMap<UnitType> result = new IntegerMap<>(costs);
for (final UnitType unitType : costs.keySet()) {
result.put(unitType, getTotalTuv(unitType, costs, new HashSet<>()));
}
return result;
}
Aggregations