Search in sources :

Example 61 with Resource

use of games.strategy.engine.data.Resource 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();
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit) Image(java.awt.Image) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ProductionRule(games.strategy.engine.data.ProductionRule) HashSet(java.util.HashSet) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Example 62 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class ExtendedStats method fillExtendedStats.

private void fillExtendedStats(final GameData data) {
    // add other resources, other than PUs and tech tokens
    final List<Resource> resources = data.getResourceList().getResources();
    for (final Resource r : resources) {
        if (r.getName().equals(Constants.PUS) || r.getName().equals(Constants.TECH_TOKENS)) {
            continue;
        }
        final GenericResourceStat resourceStat = new GenericResourceStat();
        resourceStat.init(r.getName());
        final List<IStat> statsExtended = new ArrayList<>(Arrays.asList(this.statsExtended));
        statsExtended.add(resourceStat);
        this.statsExtended = statsExtended.toArray(new IStat[0]);
    }
    // add tech related stuff
    if (Properties.getTechDevelopment(data)) {
        // add tech tokens
        if (data.getResourceList().getResource(Constants.TECH_TOKENS) != null) {
            final List<IStat> statsExtended = new ArrayList<>(Arrays.asList(this.statsExtended));
            statsExtended.add(new TechTokenStat());
            this.statsExtended = statsExtended.toArray(new IStat[0]);
        }
        // add number of techs
        final List<IStat> techStatsExtended = new ArrayList<>(Arrays.asList(statsExtended));
        techStatsExtended.add(new TechCountStat());
        statsExtended = techStatsExtended.toArray(new IStat[0]);
        // add individual techs
        for (final TechAdvance ta : TechAdvance.getTechAdvances(gameData)) {
            final GenericTechNameStat techNameStat = new GenericTechNameStat();
            techNameStat.init(ta);
            final List<IStat> statsExtended = new ArrayList<>(Arrays.asList(this.statsExtended));
            statsExtended.add(techNameStat);
            this.statsExtended = statsExtended.toArray(new IStat[0]);
        }
    }
    // now add actual number of each unit type (holy gumdrops batman, this is going to be long!)
    final UnitTypeList allUnitTypes = data.getUnitTypeList();
    for (final UnitType ut : allUnitTypes) {
        final GenericUnitNameStat unitNameStat = new GenericUnitNameStat();
        unitNameStat.init(ut);
        final List<IStat> statsExtended = new ArrayList<>(Arrays.asList(this.statsExtended));
        statsExtended.add(unitNameStat);
        this.statsExtended = statsExtended.toArray(new IStat[0]);
    }
}
Also used : Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) IStat(games.strategy.engine.stats.IStat) UnitTypeList(games.strategy.engine.data.UnitTypeList) UnitType(games.strategy.engine.data.UnitType) TechAdvance(games.strategy.triplea.delegate.TechAdvance)

Example 63 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class PuInfo method saveToFile.

void saveToFile(final PrintGenerationData printData) {
    final GameData gameData = printData.getData();
    for (final PlayerID currentPlayer : gameData.getPlayerList()) {
        infoMap.put(currentPlayer, gameData.getResourceList().getResources().stream().collect(Collectors.toMap(Function.identity(), currentPlayer.getResources()::getQuantity)));
    }
    try {
        final File outFile = new File(printData.getOutDir(), "General Information.csv");
        try (Writer resourceWriter = Files.newBufferedWriter(outFile.toPath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
            // Print Title
            final int numResources = gameData.getResourceList().size();
            for (int i = 0; i < numResources / 2 - 1 + numResources % 2; i++) {
                resourceWriter.write(",");
            }
            resourceWriter.write("Resource Chart");
            for (int i = 0; i < numResources / 2 - numResources % 2; i++) {
                resourceWriter.write(",");
            }
            resourceWriter.write("\r\n");
            // Print Resources
            resourceWriter.write(",");
            for (final Resource currentResource : gameData.getResourceList().getResources()) {
                resourceWriter.write(currentResource.getName() + ",");
            }
            resourceWriter.write("\r\n");
            // Print Player's and Resource Amount's
            for (final PlayerID currentPlayer : gameData.getPlayerList()) {
                resourceWriter.write(currentPlayer.getName());
                final Map<Resource, Integer> resourceMap = infoMap.get(currentPlayer);
                for (final int amountResource : resourceMap.values()) {
                    resourceWriter.write("," + amountResource);
                }
                resourceWriter.write("\r\n");
            }
            resourceWriter.write("\r\n");
        }
    } catch (final IOException e) {
        ClientLogger.logQuietly("Failed to save print generation data general information", e);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer)

Example 64 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class TripleAPlayer method selectKamikazeSuicideAttacks.

@Override
public HashMap<Territory, HashMap<Unit, IntegerMap<Resource>>> selectKamikazeSuicideAttacks(final HashMap<Territory, Collection<Unit>> possibleUnitsToAttack) {
    final PlayerID id = getPlayerId();
    final PlayerAttachment pa = PlayerAttachment.get(id);
    if (pa == null) {
        return null;
    }
    final IntegerMap<Resource> resourcesAndAttackValues = pa.getSuicideAttackResources();
    if (resourcesAndAttackValues.size() <= 0) {
        return null;
    }
    final IntegerMap<Resource> playerResourceCollection = id.getResources().getResourcesCopy();
    final IntegerMap<Resource> attackTokens = new IntegerMap<>();
    for (final Resource possible : resourcesAndAttackValues.keySet()) {
        final int amount = playerResourceCollection.getInt(possible);
        if (amount > 0) {
            attackTokens.put(possible, amount);
        }
    }
    if (attackTokens.size() <= 0) {
        return null;
    }
    final HashMap<Territory, HashMap<Unit, IntegerMap<Resource>>> kamikazeSuicideAttacks = new HashMap<>();
    for (final Entry<Resource, Integer> entry : attackTokens.entrySet()) {
        final Resource resource = entry.getKey();
        final int max = entry.getValue();
        final Map<Territory, IntegerMap<Unit>> selection = ui.selectKamikazeSuicideAttacks(possibleUnitsToAttack, resource, max);
        for (final Entry<Territory, IntegerMap<Unit>> selectionEntry : selection.entrySet()) {
            final Territory territory = selectionEntry.getKey();
            final HashMap<Unit, IntegerMap<Resource>> currentTerr = kamikazeSuicideAttacks.getOrDefault(territory, new HashMap<>());
            for (final Entry<Unit, Integer> unitEntry : selectionEntry.getValue().entrySet()) {
                final Unit unit = unitEntry.getKey();
                final IntegerMap<Resource> currentUnit = currentTerr.getOrDefault(unit, new IntegerMap<>());
                currentUnit.add(resource, unitEntry.getValue());
                currentTerr.put(unit, currentUnit);
            }
            kamikazeSuicideAttacks.put(territory, currentTerr);
        }
    }
    return kamikazeSuicideAttacks;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment)

Example 65 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class AbstractAi method start.

@Override
public final void start(final String name) {
    super.start(name);
    final PlayerID id = getPlayerId();
    if (name.endsWith("Bid")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final String propertyName = id.getName() + " bid";
        final int bidAmount = getGameData().getProperties().get(propertyName, 0);
        purchase(true, bidAmount, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Purchase")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final Resource pus = getGameData().getResourceList().getResource(Constants.PUS);
        final int leftToSpend = id.getResources().getQuantity(pus);
        purchase(false, leftToSpend, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Tech")) {
        final ITechDelegate techDelegate = (ITechDelegate) getPlayerBridge().getRemoteDelegate();
        tech(techDelegate, getGameData(), id);
    } else if (name.endsWith("Move")) {
        final IMoveDelegate moveDel = (IMoveDelegate) getPlayerBridge().getRemoteDelegate();
        if (name.endsWith("AirborneCombatMove")) {
        // do nothing
        } else {
            move(name.endsWith("NonCombatMove"), moveDel, getGameData(), id);
        }
    } else if (name.endsWith("Battle")) {
        battle((IBattleDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    } else if (name.endsWith("Politics")) {
        politicalActions();
    } else if (name.endsWith("Place")) {
        final IAbstractPlaceDelegate placeDel = (IAbstractPlaceDelegate) getPlayerBridge().getRemoteDelegate();
        place(name.contains("Bid"), placeDel, getGameData(), id);
    } else if (name.endsWith("EndTurn")) {
        endTurn((IAbstractForumPosterDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    }
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) ITechDelegate(games.strategy.triplea.delegate.remote.ITechDelegate) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) IAbstractForumPosterDelegate(games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate) IAbstractPlaceDelegate(games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate) Resource(games.strategy.engine.data.Resource)

Aggregations

Resource (games.strategy.engine.data.Resource)71 PlayerID (games.strategy.engine.data.PlayerID)22 IntegerMap (games.strategy.util.IntegerMap)16 GameData (games.strategy.engine.data.GameData)15 Unit (games.strategy.engine.data.Unit)15 ArrayList (java.util.ArrayList)15 Territory (games.strategy.engine.data.Territory)14 UnitType (games.strategy.engine.data.UnitType)13 Change (games.strategy.engine.data.Change)11 CompositeChange (games.strategy.engine.data.CompositeChange)11 ProductionRule (games.strategy.engine.data.ProductionRule)10 TripleAUnit (games.strategy.triplea.TripleAUnit)10 NamedAttachable (games.strategy.engine.data.NamedAttachable)9 ResourceCollection (games.strategy.engine.data.ResourceCollection)8 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)7 GameParseException (games.strategy.engine.data.GameParseException)6 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Test (org.junit.jupiter.api.Test)5