Search in sources :

Example 16 with ResourceCollection

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

the class BidPurchaseDelegate method canWePurchaseOrRepair.

@Override
protected boolean canWePurchaseOrRepair() {
    final ResourceCollection bidCollection = new ResourceCollection(getData());
    // TODO: allow bids to have more than just PUs
    bidCollection.addResource(getData().getResourceList().getResource(Constants.PUS), bid);
    if (player.getProductionFrontier() != null && player.getProductionFrontier().getRules() != null) {
        for (final ProductionRule rule : player.getProductionFrontier().getRules()) {
            if (bidCollection.has(rule.getCosts())) {
                return true;
            }
        }
    }
    if (player.getRepairFrontier() != null && player.getRepairFrontier().getRules() != null) {
        for (final RepairRule rule : player.getRepairFrontier().getRules()) {
            if (bidCollection.has(rule.getCosts())) {
                return true;
            }
        }
    }
    return false;
}
Also used : ProductionRule(games.strategy.engine.data.ProductionRule) RepairRule(games.strategy.engine.data.RepairRule) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 17 with ResourceCollection

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

the class BidPurchaseDelegate method canAfford.

@Override
protected boolean canAfford(final IntegerMap<Resource> costs, final PlayerID player) {
    final ResourceCollection bidCollection = new ResourceCollection(getData());
    // TODO: allow bids to have more than just PUs
    bidCollection.addResource(getData().getResourceList().getResource(Constants.PUS), bid);
    return bidCollection.has(costs);
}
Also used : ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 18 with ResourceCollection

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

the class TerritoryAttachment method setResources.

private void setResources(final String value) throws GameParseException {
    if (value == null) {
        m_resources = null;
        return;
    }
    if (m_resources == null) {
        m_resources = new ResourceCollection(getData());
    }
    final String[] s = value.split(":");
    final int amount = getInt(s[0]);
    if (s[1].equals(Constants.PUS)) {
        throw new GameParseException("Please set PUs using production, not resource" + thisErrorMsg());
    }
    final Resource resource = getData().getResourceList().getResource(s[1]);
    if (resource == null) {
        throw new GameParseException("No resource named: " + s[1] + thisErrorMsg());
    }
    m_resources.putResource(resource, amount);
}
Also used : Resource(games.strategy.engine.data.Resource) GameParseException(games.strategy.engine.data.GameParseException) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 19 with ResourceCollection

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

the class RouteTest method testCorrectParameterHandling.

@Test
public void testCorrectParameterHandling() {
    final MapPanel mockedMapPanel = mock(MapPanel.class);
    final MapRouteDrawer routeDrawer = spy(new MapRouteDrawer(mockedMapPanel, dummyMapData));
    when(mockedMapPanel.getXOffset()).thenReturn(0);
    when(mockedMapPanel.getYOffset()).thenReturn(0);
    when(mockedMapPanel.getScale()).thenReturn(0.0);
    when(mockedMapPanel.getImageWidth()).thenReturn(1);
    when(mockedMapPanel.getImageHeight()).thenReturn(1);
    final Shape mockShape = mock(Shape.class);
    final Graphics2D mockGraphics = mock(Graphics2D.class);
    when(mockShape.contains(any(Point2D.class))).thenReturn(true);
    final ResourceCollection mockResourceCollection = mock(ResourceCollection.class);
    when(mockResourceCollection.getResourcesCopy()).thenReturn(new IntegerMap<>());
    final ResourceImageFactory mockResourceImageFactory = mock(ResourceImageFactory.class);
    routeDrawer.drawRoute(mockGraphics, dummyRouteDescription, "2", mockResourceCollection, mockResourceImageFactory);
    verify(mockGraphics, atLeastOnce()).fill(any(Shape.class));
    verify(mockGraphics, atLeastOnce()).draw(any(Shape.class));
    // Those methods are needed
    verify(mockedMapPanel, atLeastOnce()).getXOffset();
    verify(mockedMapPanel, atLeastOnce()).getYOffset();
    verify(mockedMapPanel, atLeastOnce()).getScale();
    verify(dummyRouteDescription, times(2)).getRoute();
    verify(dummyRouteDescription.getRoute(), atLeastOnce()).getAllTerritories();
}
Also used : Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) ResourceImageFactory(games.strategy.triplea.image.ResourceImageFactory) Graphics2D(java.awt.Graphics2D) ResourceCollection(games.strategy.engine.data.ResourceCollection) Test(org.junit.jupiter.api.Test)

Example 20 with ResourceCollection

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

the class TripleAFrame method scrambleUnitsQuery.

public HashMap<Territory, Collection<Unit>> scrambleUnitsQuery(final Territory scrambleTo, final Map<Territory, Tuple<Collection<Unit>, Collection<Unit>>> possibleScramblers) {
    if (messageAndDialogThreadPool == null) {
        return null;
    }
    messageAndDialogThreadPool.waitForAll();
    if (SwingUtilities.isEventDispatchThread()) {
        throw new IllegalStateException("Should not be called from dispatch thread");
    }
    final CountDownLatch continueLatch = new CountDownLatch(1);
    final HashMap<Territory, Collection<Unit>> selection = new HashMap<>();
    final Collection<Tuple<Territory, UnitChooser>> choosers = new ArrayList<>();
    SwingUtilities.invokeLater(() -> {
        mapPanel.centerOn(scrambleTo);
        final JDialog dialog = new JDialog(this, "Select units to scramble to " + scrambleTo.getName());
        final JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        final String optionScramble = "Scramble";
        final String optionNone = "None";
        final JButton scrambleButton = new JButton(optionScramble);
        scrambleButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(scrambleButton));
        final JButton noneButton = new JButton(optionNone);
        noneButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(noneButton));
        final Object[] options = { scrambleButton, noneButton };
        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[1]);
        final JLabel whereTo = new JLabel("Scramble To: " + scrambleTo.getName());
        whereTo.setFont(new Font("Arial", Font.ITALIC, 12));
        panel.add(whereTo, BorderLayout.NORTH);
        final JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createEmptyBorder());
        panel2.setLayout(new FlowLayout());
        final JPanel fuelCostPanel = new JPanel(new GridBagLayout());
        panel.add(fuelCostPanel, BorderLayout.SOUTH);
        for (final Territory from : possibleScramblers.keySet()) {
            final JPanel panelChooser = new JPanel();
            panelChooser.setLayout(new BoxLayout(panelChooser, BoxLayout.Y_AXIS));
            panelChooser.setBorder(BorderFactory.createLineBorder(getBackground()));
            final JLabel whereFrom = new JLabel("From: " + from.getName());
            whereFrom.setHorizontalAlignment(SwingConstants.LEFT);
            whereFrom.setFont(new Font("Arial", Font.BOLD, 12));
            panelChooser.add(whereFrom);
            panelChooser.add(new JLabel(" "));
            final Collection<Unit> possible = possibleScramblers.get(from).getSecond();
            final int maxAllowed = Math.min(BattleDelegate.getMaxScrambleCount(possibleScramblers.get(from).getFirst()), possible.size());
            final UnitChooser chooser = new UnitChooser(possible, Collections.emptyMap(), false, uiContext);
            chooser.setMaxAndShowMaxButton(maxAllowed);
            chooser.addChangeListener(field -> {
                final Collection<Unit> units = new HashSet<>();
                final Map<PlayerID, ResourceCollection> playerFuelCost = new HashMap<>();
                for (final Tuple<Territory, UnitChooser> tuple : choosers) {
                    units.addAll(tuple.getSecond().getSelected(false));
                    final Map<PlayerID, ResourceCollection> map = Route.getScrambleFuelCostCharge(tuple.getSecond().getSelected(false), tuple.getFirst(), scrambleTo, data);
                    for (final Entry<PlayerID, ResourceCollection> playerAndCost : map.entrySet()) {
                        if (playerFuelCost.containsKey(playerAndCost.getKey())) {
                            playerFuelCost.get(playerAndCost.getKey()).add(playerAndCost.getValue());
                        } else {
                            playerFuelCost.put(playerAndCost.getKey(), playerAndCost.getValue());
                        }
                    }
                }
                fuelCostPanel.removeAll();
                boolean hasEnoughFuel = true;
                int count = 0;
                for (final Entry<PlayerID, ResourceCollection> entry : playerFuelCost.entrySet()) {
                    final JLabel label = new JLabel(entry.getKey().getName() + ": ");
                    fuelCostPanel.add(label, new GridBagConstraints(0, count, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                    fuelCostPanel.add(uiContext.getResourceImageFactory().getResourcesPanel(entry.getValue(), data), new GridBagConstraints(1, count++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                    if (!entry.getKey().getResources().has(entry.getValue().getResourcesCopy())) {
                        hasEnoughFuel = false;
                        label.setForeground(Color.RED);
                    }
                }
                scrambleButton.setEnabled(hasEnoughFuel);
                dialog.pack();
            });
            choosers.add(Tuple.of(from, chooser));
            panelChooser.add(chooser);
            final JScrollPane chooserScrollPane = new JScrollPane(panelChooser);
            panel2.add(chooserScrollPane);
        }
        panel.add(panel2, BorderLayout.CENTER);
        dialog.setContentPane(optionPane);
        dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        dialog.setLocationRelativeTo(getParent());
        dialog.setAlwaysOnTop(true);
        dialog.pack();
        dialog.setVisible(true);
        dialog.requestFocusInWindow();
        optionPane.addPropertyChangeListener(e -> {
            if (!dialog.isVisible()) {
                return;
            }
            final String option = ((JButton) optionPane.getValue()).getText();
            if (option.equals(optionNone)) {
                choosers.clear();
                selection.clear();
                dialog.setVisible(false);
                dialog.removeAll();
                dialog.dispose();
                continueLatch.countDown();
            } else if (option.equals(optionScramble)) {
                for (final Tuple<Territory, UnitChooser> terrChooser : choosers) {
                    selection.put(terrChooser.getFirst(), terrChooser.getSecond().getSelected());
                }
                dialog.setVisible(false);
                dialog.removeAll();
                dialog.dispose();
                continueLatch.countDown();
            }
        });
    });
    mapPanel.getUiContext().addShutdownLatch(continueLatch);
    Interruptibles.await(continueLatch);
    mapPanel.getUiContext().removeShutdownLatch(continueLatch);
    return selection;
}
Also used : JPanel(javax.swing.JPanel) PlayerID(games.strategy.engine.data.PlayerID) GridBagConstraints(java.awt.GridBagConstraints) FlowLayout(java.awt.FlowLayout) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) HashMap(java.util.HashMap) BoxLayout(javax.swing.BoxLayout) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Font(java.awt.Font) BorderLayout(java.awt.BorderLayout) HashSet(java.util.HashSet) JScrollPane(javax.swing.JScrollPane) Territory(games.strategy.engine.data.Territory) JLabel(javax.swing.JLabel) CountDownLatch(java.util.concurrent.CountDownLatch) EventThreadJOptionPane(games.strategy.util.EventThreadJOptionPane) JOptionPane(javax.swing.JOptionPane) Point(java.awt.Point) ResourceCollection(games.strategy.engine.data.ResourceCollection) Collection(java.util.Collection) Tuple(games.strategy.util.Tuple) JDialog(javax.swing.JDialog) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Aggregations

ResourceCollection (games.strategy.engine.data.ResourceCollection)23 Resource (games.strategy.engine.data.Resource)7 Test (org.junit.jupiter.api.Test)6 PlayerID (games.strategy.engine.data.PlayerID)4 ProductionRule (games.strategy.engine.data.ProductionRule)4 IntegerMap (games.strategy.util.IntegerMap)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Territory (games.strategy.engine.data.Territory)3 Unit (games.strategy.engine.data.Unit)3 UnitType (games.strategy.engine.data.UnitType)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Map (java.util.Map)3 GameData (games.strategy.engine.data.GameData)2 NamedAttachable (games.strategy.engine.data.NamedAttachable)2 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)2 RepairRule (games.strategy.engine.data.RepairRule)2 TripleAUnit (games.strategy.triplea.TripleAUnit)2 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)2