Search in sources :

Example 56 with Resource

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

the class EditDelegate method changeTechTokens.

@Override
public String changeTechTokens(final PlayerID player, final int newTotal) {
    final String result = checkEditMode();
    if (result != null) {
        return result;
    }
    final Resource techTokens = getData().getResourceList().getResource(Constants.TECH_TOKENS);
    final int oldTotal = player.getResources().getQuantity(techTokens);
    if (oldTotal == newTotal) {
        return "New token total is unchanged";
    }
    if (newTotal < 0) {
        return "New token total is invalid";
    }
    logEvent("Changing tech tokens for " + player.getName() + " from " + oldTotal + " to " + newTotal, null);
    bridge.addChange(ChangeFactory.changeResourcesChange(player, techTokens, (newTotal - oldTotal)));
    return null;
}
Also used : Resource(games.strategy.engine.data.Resource)

Example 57 with Resource

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

the class ProPurchaseUtils method findMaxPurchaseDefenders.

public static List<Unit> findMaxPurchaseDefenders(final PlayerID player, final Territory t, final List<ProPurchaseOption> landPurchaseOptions) {
    ProLogger.info("Find max purchase defenders for " + t.getName());
    final GameData data = ProData.getData();
    // Determine most cost efficient defender that can be produced in this territory
    final Resource pus = data.getResourceList().getResource(Constants.PUS);
    final int pusRemaining = player.getResources().getQuantity(pus);
    final List<ProPurchaseOption> purchaseOptionsForTerritory = findPurchaseOptionsForTerritory(player, landPurchaseOptions, t, false);
    ProPurchaseOption bestDefenseOption = null;
    double maxDefenseEfficiency = 0;
    for (final ProPurchaseOption ppo : purchaseOptionsForTerritory) {
        if (ppo.getDefenseEfficiency() > maxDefenseEfficiency && ppo.getCost() <= pusRemaining) {
            bestDefenseOption = ppo;
            maxDefenseEfficiency = ppo.getDefenseEfficiency();
        }
    }
    // Determine number of defenders I can purchase
    final List<Unit> placeUnits = new ArrayList<>();
    if (bestDefenseOption != null) {
        ProLogger.debug("Best defense option: " + bestDefenseOption.getUnitType().getName());
        int remainingUnitProduction = getUnitProduction(t, data, player);
        int pusSpent = 0;
        while (bestDefenseOption.getCost() <= (pusRemaining - pusSpent) && remainingUnitProduction >= bestDefenseOption.getQuantity()) {
            // If out of PUs or production then break
            // Create new temp defenders
            pusSpent += bestDefenseOption.getCost();
            remainingUnitProduction -= bestDefenseOption.getQuantity();
            placeUnits.addAll(bestDefenseOption.getUnitType().create(bestDefenseOption.getQuantity(), player, true));
        }
        ProLogger.debug("Potential purchased defenders: " + placeUnits);
    }
    return placeUnits;
}
Also used : GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) ProPurchaseOption(games.strategy.triplea.ai.pro.data.ProPurchaseOption) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 58 with Resource

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

the class AbstractEndTurnDelegateTest method testFindEstimatedIncome.

@Test
public void testFindEstimatedIncome() throws Exception {
    final GameData global40Data = TestMapGameData.GLOBAL1940.getGameData();
    final PlayerID germans = GameDataTestUtil.germans(global40Data);
    final IntegerMap<Resource> results = AbstractEndTurnDelegate.findEstimatedIncome(germans, global40Data);
    final int pus = results.getInt(new Resource(Constants.PUS, global40Data));
    assertEquals(40, pus);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) Resource(games.strategy.engine.data.Resource) Test(org.junit.jupiter.api.Test)

Example 59 with Resource

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

the class MapRouteDrawer method findMovementLeftImageWidth.

private static int findMovementLeftImageWidth(final String curMovement, final String unitMovementLeft, final ResourceCollection movementFuelCost) {
    // Create temp graphics to calculate necessary image width based on font sizes
    final BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D tempGraphics = tempImage.createGraphics();
    tempGraphics.setFont(MESSAGE_FONT);
    final FontMetrics fontMetrics = tempGraphics.getFontMetrics();
    // Determine widths of each element to draw
    int imageWidth = 2 * MESSAGE_PADDING;
    imageWidth += fontMetrics.stringWidth(curMovement);
    if (!unitMovementLeft.isEmpty()) {
        imageWidth += fontMetrics.stringWidth(unitMovementLeft);
        for (final Resource resource : movementFuelCost.getResourcesCopy().keySet()) {
            imageWidth += MESSAGE_TEXT_SPACING;
            imageWidth += fontMetrics.stringWidth("-" + movementFuelCost.getQuantity(resource));
            imageWidth += ResourceImageFactory.IMAGE_SIZE;
        }
    }
    tempGraphics.dispose();
    return imageWidth;
}
Also used : FontMetrics(java.awt.FontMetrics) Resource(games.strategy.engine.data.Resource) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 60 with Resource

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

the class EconomyPanel method initLayout.

@Override
protected void initLayout() {
    setLayout(new GridLayout(1, 1));
    resourceModel = new ResourceTableModel();
    final JTable table = new JTable(resourceModel);
    table.getTableHeader().setReorderingAllowed(false);
    final TableColumn column = table.getColumnModel().getColumn(0);
    column.setPreferredWidth(175);
    for (int i = 1; i < resourceModel.getColumnCount(); i++) {
        table.getColumnModel().getColumn(i).setHeaderRenderer(new DefaultTableCellRenderer());
        final JLabel label = (JLabel) table.getColumnModel().getColumn(i).getHeaderRenderer();
        final Resource resource = resourceStats.get(i - 1).resource;
        try {
            label.setIcon(uiContext.getResourceImageFactory().getIcon(resource, false));
        } catch (final IllegalStateException e) {
        // ignore missing resource image
        }
    }
    final JScrollPane scroll = new JScrollPane(table);
    add(scroll);
}
Also used : JScrollPane(javax.swing.JScrollPane) GridLayout(java.awt.GridLayout) JTable(javax.swing.JTable) Resource(games.strategy.engine.data.Resource) JLabel(javax.swing.JLabel) TableColumn(javax.swing.table.TableColumn) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

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