use of games.strategy.engine.data.ProductionRule in project triplea by triplea-game.
the class ProPurchaseAi method bid.
/**
* Default settings for bidding:
* 1) Limit one bid unit in a territory or sea zone (until set in all territories then 2, etc).
* 2) The nation placing a unit in a territory or sea zone must have started with a unit in said territory or sea zone
* prior to placing the bid.
*/
Map<Territory, ProPurchaseTerritory> bid(final int pus, final IPurchaseDelegate purchaseDelegate, final GameData startOfTurnData) {
// Current data fields
data = ProData.getData();
this.startOfTurnData = startOfTurnData;
player = ProData.getPlayer();
resourceTracker = new ProResourceTracker(pus, data);
territoryManager = new ProTerritoryManager(calc);
isBid = true;
final ProPurchaseOptionMap purchaseOptions = ProData.purchaseOptions;
ProLogger.info("Starting bid phase with resources: " + resourceTracker);
if (!player.getUnits().getUnits().isEmpty()) {
ProLogger.info("Starting bid phase with unplaced units=" + player.getUnits().getUnits());
}
// Find all purchase/place territories
final Map<Territory, ProPurchaseTerritory> purchaseTerritories = ProPurchaseUtils.findBidTerritories(player);
int previousNumUnits = 0;
while (true) {
// Determine max enemy attack units and current allied defenders
territoryManager.populateEnemyAttackOptions(new ArrayList<>(), new ArrayList<>(purchaseTerritories.keySet()));
findDefendersInPlaceTerritories(purchaseTerritories);
// Prioritize land territories that need defended and purchase additional defenders
final List<ProPlaceTerritory> needToDefendLandTerritories = prioritizeTerritoriesToDefend(purchaseTerritories, true);
purchaseDefenders(purchaseTerritories, needToDefendLandTerritories, purchaseOptions.getLandFodderOptions(), purchaseOptions.getAirOptions(), true);
// Find strategic value for each territory
ProLogger.info("Find strategic value for place territories");
final Map<Territory, Double> territoryValueMap = ProTerritoryValueUtils.findTerritoryValues(player, new ArrayList<>(), new ArrayList<>());
for (final ProPurchaseTerritory t : purchaseTerritories.values()) {
for (final ProPlaceTerritory ppt : t.getCanPlaceTerritories()) {
ppt.setStrategicValue(territoryValueMap.get(ppt.getTerritory()));
ProLogger.debug(ppt.getTerritory() + ", strategicValue=" + territoryValueMap.get(ppt.getTerritory()));
}
}
// Prioritize land place options purchase AA then land units
final List<ProPlaceTerritory> prioritizedLandTerritories = prioritizeLandTerritories(purchaseTerritories);
purchaseAaUnits(purchaseTerritories, prioritizedLandTerritories, purchaseOptions.getAaOptions());
purchaseLandUnits(purchaseTerritories, prioritizedLandTerritories, purchaseOptions, territoryValueMap);
// Prioritize sea territories that need defended and purchase additional defenders
final List<ProPlaceTerritory> needToDefendSeaTerritories = prioritizeTerritoriesToDefend(purchaseTerritories, false);
purchaseDefenders(purchaseTerritories, needToDefendSeaTerritories, purchaseOptions.getSeaDefenseOptions(), purchaseOptions.getAirOptions(), false);
// Prioritize sea place options and purchase units
final List<ProPlaceTerritory> prioritizedSeaTerritories = prioritizeSeaTerritories(purchaseTerritories);
purchaseSeaAndAmphibUnits(purchaseTerritories, prioritizedSeaTerritories, territoryValueMap, purchaseOptions);
// Try to use any remaining PUs on high value units
purchaseUnitsWithRemainingProduction(purchaseTerritories, purchaseOptions.getLandOptions(), purchaseOptions.getAirOptions());
upgradeUnitsWithRemainingPUs(purchaseTerritories, purchaseOptions);
// Check if no remaining PUs or no unit built this iteration
final int numUnits = purchaseTerritories.values().stream().map(ProPurchaseTerritory::getCanPlaceTerritories).map(t -> t.get(0)).map(ProPlaceTerritory::getPlaceUnits).mapToInt(List::size).sum();
if (resourceTracker.isEmpty() || numUnits == previousNumUnits) {
break;
}
previousNumUnits = numUnits;
ProPurchaseUtils.incrementUnitProductionForBidTerritories(purchaseTerritories);
}
// Determine final count of each production rule
final IntegerMap<ProductionRule> purchaseMap = populateProductionRuleMap(purchaseTerritories, purchaseOptions);
// Purchase units
ProMetricUtils.collectPurchaseStats(purchaseMap);
final String error = purchaseDelegate.purchase(purchaseMap);
if (error != null) {
ProLogger.warn("Purchase error: " + error);
}
return purchaseTerritories;
}
use of games.strategy.engine.data.ProductionRule in project triplea by triplea-game.
the class ProPurchaseUtils method getCost.
/**
* How many PU's does it cost the given player to produce the given unit including any dependents.
*/
public static double getCost(final Unit unit) {
final Resource pus = unit.getData().getResourceList().getResource(Constants.PUS);
final Collection<Unit> units = TransportTracker.transportingAndUnloaded(unit);
units.add(unit);
double cost = 0.0;
for (final Unit u : units) {
final ProductionRule rule = getProductionRule(u.getType(), u.getOwner());
if (rule == null) {
cost += ProData.unitValueMap.getInt(u.getType());
} else {
cost += ((double) rule.getCosts().getInt(pus)) / rule.getResults().totalValues();
}
}
return cost;
}
use of games.strategy.engine.data.ProductionRule in project triplea by triplea-game.
the class TripleAPlayer method purchase.
private void purchase(final boolean bid) {
if (getPlayerBridge().isGameOver()) {
return;
}
final PlayerID id = getPlayerId();
// play a sound for this phase
if (!bid && !soundPlayedAlreadyPurchase) {
ClipPlayer.play(SoundPath.CLIP_PHASE_PURCHASE, id);
soundPlayedAlreadyPurchase = true;
}
// Check if any factories need to be repaired
if (id.getRepairFrontier() != null && id.getRepairFrontier().getRules() != null && !id.getRepairFrontier().getRules().isEmpty()) {
final GameData data = getGameData();
if (isDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
final Predicate<Unit> myDamaged = Matches.unitIsOwnedBy(id).and(Matches.unitHasTakenSomeBombingUnitDamage());
final Collection<Unit> damagedUnits = new ArrayList<>();
for (final Territory t : data.getMap().getTerritories()) {
damagedUnits.addAll(CollectionUtils.getMatches(t.getUnits().getUnits(), myDamaged));
}
if (damagedUnits.size() > 0) {
final HashMap<Unit, IntegerMap<RepairRule>> repair = ui.getRepair(id, bid, GameStepPropertiesHelper.getRepairPlayers(data, id));
if (repair != null) {
final IPurchaseDelegate purchaseDel;
try {
purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
} catch (final ClassCastException e) {
final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
// for some reason the client is not seeing or getting these errors, so print to err too
System.err.println(errorContext);
ClientLogger.logQuietly(errorContext, e);
throw new IllegalStateException(errorContext, e);
}
final String error = purchaseDel.purchaseRepair(repair);
if (error != null) {
ui.notifyError(error);
// dont give up, keep going
purchase(bid);
}
}
}
}
}
final IntegerMap<ProductionRule> prod = ui.getProduction(id, bid);
if (prod == null) {
return;
}
final IPurchaseDelegate purchaseDel;
try {
purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
} catch (final ClassCastException e) {
final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
// for some reason the client is not seeing or getting these errors, so print to err too
System.err.println(errorContext);
ClientLogger.logQuietly(errorContext, e);
throw new IllegalStateException(errorContext, e);
}
final String purchaseError = purchaseDel.purchase(prod);
if (purchaseError != null) {
ui.notifyError(purchaseError);
// dont give up, keep going
purchase(bid);
}
}
use of games.strategy.engine.data.ProductionRule in project triplea by triplea-game.
the class AiUtils method getCost.
/**
* How many PU's does it cost the given player to produce the given unit type.
*
* <p>
* If the player cannot produce the given unit, return Integer.MAX_VALUE
* </p>
*/
static int getCost(final UnitType unitType, final PlayerID player, final GameData data) {
final Resource pus = data.getResourceList().getResource(Constants.PUS);
final ProductionRule rule = getProductionRule(unitType, player);
return (rule == null) ? Integer.MAX_VALUE : rule.getCosts().getInt(pus);
}
use of games.strategy.engine.data.ProductionRule in project triplea by triplea-game.
the class ProductionPanel method initRules.
protected void initRules(final PlayerID player, final IntegerMap<ProductionRule> initialPurchase) {
this.data.acquireReadLock();
try {
id = player;
for (final ProductionRule productionRule : player.getProductionFrontier()) {
final Rule rule = new Rule(productionRule, player);
final int initialQuantity = initialPurchase.getInt(productionRule);
rule.setQuantity(initialQuantity);
rules.add(rule);
}
} finally {
this.data.releaseReadLock();
}
}
Aggregations