Search in sources :

Example 46 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCDiscardOrGainResDialog method actionPerformed.

/**
 * React to clicking Discard/Pick button or Clear button.
 *<P>
 * ColorSquare clicks are handled in {@link #mousePressed(MouseEvent)}.
 *
 * @param e  ActionEvent for the click, with {@link ActionEvent#getSource()} == our button
 */
public void actionPerformed(ActionEvent e) {
    try {
        Object target = e.getSource();
        if (target == okBut) {
            SOCResourceSet rsrcs = new SOCResourceSet(pick[0].getIntValue(), pick[1].getIntValue(), pick[2].getIntValue(), pick[3].getIntValue(), pick[4].getIntValue(), 0);
            if (rsrcs.getTotal() == numPickNeeded) {
                SOCPlayerClient pcli = playerInterface.getClient();
                if (isDiscard)
                    pcli.getGameManager().discard(playerInterface.getGame(), rsrcs);
                else
                    pcli.getGameManager().pickResources(playerInterface.getGame(), rsrcs);
                dispose();
            }
        } else if (target == clearBut) {
            for (int i = pick.length - 1; i >= 0; --i) {
                if (isDiscard)
                    keep[i].addValue(pick[i].getIntValue());
                pick[i].setIntValue(0);
            }
            numChosen = 0;
            clearBut.setEnabled(false);
            okBut.setEnabled(numPickNeeded == numChosen);
        }
    } catch (Throwable th) {
        playerInterface.chatPrintStackTrace(th);
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 47 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCHandPanel method actionPerformed.

/**
 * handle interaction
 */
public void actionPerformed(ActionEvent e) {
    try {
        String target = e.getActionCommand();
        SOCPlayerClient client = playerInterface.getClient();
        SOCGame game = playerInterface.getGame();
        if (target == LOCKSEAT) {
            // Seat Lock while game forming (gamestate NEW); see below for ROBOTLOCKBUT_L etc
            client.getGameManager().setSeatLock(game, playerNumber, SOCGame.SeatLockState.LOCKED);
        } else if (target == UNLOCKSEAT) {
            // Unlock while game forming
            client.getGameManager().setSeatLock(game, playerNumber, SOCGame.SeatLockState.UNLOCKED);
        } else if (target == TAKEOVER) {
            client.getGameManager().sitDown(game, playerNumber);
        } else if (target == SIT) {
            client.getGameManager().sitDown(game, playerNumber);
        } else if ((target == START) && startBut.isVisible()) {
            client.getGameManager().startGame(game);
        // checks isVisible to guard against button action from hitting spacebar
        // when hidden but has focus because startBut is the first button added to panel;
        // this bug seen on OSX 10.9.1 (1.5.0 JVM)
        } else if (target == ROBOT) {
        // cf.cc.addRobot(cf.cname, playerNum);
        } else if (target == ROLL) {
            if (autoRollTimerTask != null) {
                autoRollTimerTask.cancel();
                autoRollTimerTask = null;
            }
            clickRollButton();
        } else if (target == QUIT) {
            SOCQuitConfirmDialog.createAndShow(playerInterface.getGameDisplay(), playerInterface);
        } else if (target == DONE) {
            // sqPanel.setValues(zero, zero);
            client.getGameManager().endTurn(game);
        } else if (target == DONE_RESTART) {
            playerInterface.resetBoardRequest(game.isPractice && !game.isInitialPlacement());
        } else if (target == CLEAR) {
            // Zero the square panel numbers, unless board-reset vote in progress
            clearOffer(true);
            if (game.getGameState() == SOCGame.PLAY1) {
                client.getGameManager().clearOffer(game);
            }
        } else if (target == BANK) {
            int gstate = game.getGameState();
            if (gstate == SOCGame.PLAY1) {
                int[] give = new int[5];
                int[] get = new int[5];
                sqPanel.getValues(give, get);
                client.getGameManager().clearOffer(game);
                createSendBankTradeRequest(game, give, get);
            } else if (gstate == SOCGame.OVER) {
                String msg = game.gameOverMessageToPlayer(player);
                // msg = "The game is over; you are the winner!";
                // msg = "The game is over; <someone> won.";
                // msg = "The game is over; no one won.";
                playerInterface.print("* " + msg);
            }
        } else if (target == BANK_UNDO) {
            if ((bankGive != null) && (bankGet != null)) {
                // reverse the previous order to undo it
                client.getGameManager().bankTrade(game, bankGet, bankGive);
                bankGive = null;
                bankGet = null;
                bankUndoBut.setEnabled(false);
            }
        } else if (target == ROBOTLOCKBUT_L) {
            // Seat Lock while game in progress; see above for UNLOCKSEAT etc
            clickRobotSeatLockButton(SOCGame.SeatLockState.LOCKED);
        } else if (target == ROBOTLOCKBUT_U) {
            clickRobotSeatLockButton(SOCGame.SeatLockState.UNLOCKED);
        } else if (target == ROBOTLOCKBUT_M) {
            clickRobotSeatLockButton(SOCGame.SeatLockState.CLEAR_ON_RESET);
        } else if (target == SEND) {
            if (playerTradingDisabled)
                return;
            if (game.getGameState() == SOCGame.PLAY1) {
                int[] give = new int[5];
                int[] get = new int[5];
                int giveSum = 0;
                int getSum = 0;
                sqPanel.getValues(give, get);
                for (int i = 0; i < 5; i++) {
                    giveSum += give[i];
                    getSum += get[i];
                }
                SOCResourceSet giveSet = new SOCResourceSet(give);
                SOCResourceSet getSet = new SOCResourceSet(get);
                if (!player.getResources().contains(giveSet)) {
                    playerInterface.print("*** " + strings.get("hpan.trade.msg.donthave"));
                // "You can't offer what you don't have."
                } else if ((giveSum == 0) || (getSum == 0)) {
                    playerInterface.print("*** " + strings.get("hpan.trade.msg.eachplayer"));
                // "A trade must contain at least one resource from each player."
                } else {
                    // bool array elements begin as false
                    boolean[] to = new boolean[game.maxPlayers];
                    boolean toAny = false;
                    if (game.getCurrentPlayerNumber() == playerNumber) {
                        for (int i = 0; i < (game.maxPlayers - 1); i++) {
                            if (playerSend[i].getBoolValue() && !game.isSeatVacant(playerSendMap[i])) {
                                to[playerSendMap[i]] = true;
                                toAny = true;
                                playerSendForPrevTrade[i] = true;
                            } else {
                                playerSendForPrevTrade[i] = false;
                            }
                        }
                    } else {
                        // can only offer to current player
                        to[game.getCurrentPlayerNumber()] = true;
                        toAny = true;
                    }
                    if (!toAny) {
                        playerInterface.print("*** " + strings.get("hpan.trade.msg.chooseoppo"));
                    // "Choose at least one opponent's checkbox."
                    } else {
                        SOCTradeOffer tradeOffer = new SOCTradeOffer(game.getName(), playerNumber, to, giveSet, getSet);
                        client.getGameManager().offerTrade(game, tradeOffer);
                        disableBankUndoButton();
                    }
                }
            } else {
                getPlayerInterface().print("* " + strings.get("hpan.trade.msg.notnow") + "\n");
            // "You cannot trade at this time."
            }
        } else if ((e.getSource() == inventory) || (e.getSource() == playCardBut)) {
            clickPlayCardButton();
        }
    } catch (Throwable th) {
        playerInterface.chatPrintStackTrace(th);
    }
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet) SOCGame(soc.game.SOCGame)

Example 48 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCHandPanel method createSendBankTradeRequest.

/**
 * Create and send a bank/port trade request.
 * Remember the resources for the "undo" button.
 * @param game  Our game
 * @param give  Resources to give, same format as {@link SOCResourceSet#SOCResourceSet(int[])}
 * @param get   Resources to get, same format as {@link SOCResourceSet#SOCResourceSet(int[])}
 * @since 1.1.13
 */
private void createSendBankTradeRequest(SOCGame game, final int[] give, final int[] get) {
    SOCResourceSet giveSet = new SOCResourceSet(give);
    SOCResourceSet getSet = new SOCResourceSet(get);
    getClient().getGameManager().bankTrade(game, giveSet, getSet);
    bankGive = giveSet;
    bankGet = getSet;
    // TODO what if trade is not allowed
    bankUndoBut.setEnabled(true);
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 49 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCHandPanel method updateValue.

/**
 * update the value of a player element.
 * Call this after updating game data.
 *<P>
 * If {@link #VICTORYPOINTS} is updated, and game state is {@link SOCGame#OVER}, check for winner
 * and update (player name label, victory-points tooltip, disable bank/trade btn)
 *
 * @param utype  the type of value update, such as {@link #VICTORYPOINTS}
 *            or {@link PlayerClientListener.UpdateType#Sheep}.
 */
public void updateValue(PlayerClientListener.UpdateType utype) {
    boolean updateTotalResCount = false;
    /**
     * We say that we're getting the total vp, but
     * for other players this will automatically get
     * the public vp because we will assume their
     * dev card vp total is zero.
     */
    switch(utype) {
        case VictoryPoints:
            {
                int newVP = player.getTotalVP();
                vpSq.setIntValue(newVP);
                if (game.getGameState() == SOCGame.OVER) {
                    if (game.getPlayerWithWin() == player) {
                        // "Winner with 12 victory points"
                        vpSq.setTooltipText(strings.get("hpan.winner.label.ttwithvp", newVP));
                        // "X - Winner"
                        pname.setText(strings.get("hpan.winner.label", player.getName()));
                    }
                    if (interactive) {
                        bankBut.setEnabled(false);
                        bankUndoBut.setEnabled(false);
                        playCardBut.setEnabled(false);
                    }
                    doneBut.setLabel(DONE_RESTART);
                    // In case it's another player's turn
                    doneBut.setEnabled(true);
                    doneButIsRestart = true;
                }
            }
            break;
        case SpecialVictoryPoints:
            if (svpSq != null) {
                final int newSVP = player.getSpecialVP();
                svpSq.setIntValue(newSVP);
                final boolean vis = (newSVP != 0) && !offerCounterHidingFace;
                svpSq.setVisible(vis);
                svpLab.setVisible(vis);
            }
            break;
        case LongestRoad:
            setLRoad(player.hasLongestRoad());
            break;
        case LargestArmy:
            setLArmy(player.hasLargestArmy());
            break;
        case Clay:
            claySq.setIntValue(player.getResources().getAmount(SOCResourceConstants.CLAY));
            updateTotalResCount = true;
            break;
        case Ore:
            oreSq.setIntValue(player.getResources().getAmount(SOCResourceConstants.ORE));
            updateTotalResCount = true;
            break;
        case Sheep:
            sheepSq.setIntValue(player.getResources().getAmount(SOCResourceConstants.SHEEP));
            updateTotalResCount = true;
            break;
        case Wheat:
            wheatSq.setIntValue(player.getResources().getAmount(SOCResourceConstants.WHEAT));
            updateTotalResCount = true;
            break;
        case Wood:
            woodSq.setIntValue(player.getResources().getAmount(SOCResourceConstants.WOOD));
            updateTotalResCount = true;
            break;
        case ResourceTotalAndDetails:
            if (playerIsClient) {
                // Update the 5 individual ones too, not just the total count
                final SOCResourceSet rsrc = player.getResources();
                claySq.setIntValue(rsrc.getAmount(SOCResourceConstants.CLAY));
                oreSq.setIntValue(rsrc.getAmount(SOCResourceConstants.ORE));
                sheepSq.setIntValue(rsrc.getAmount(SOCResourceConstants.SHEEP));
                wheatSq.setIntValue(rsrc.getAmount(SOCResourceConstants.WHEAT));
                woodSq.setIntValue(rsrc.getAmount(SOCResourceConstants.WOOD));
            }
        case Resources:
            updateTotalResCount = true;
            break;
        case Road:
            roadSq.setIntValue(player.getNumPieces(SOCPlayingPiece.ROAD));
            break;
        case Settlement:
            settlementSq.setIntValue(player.getNumPieces(SOCPlayingPiece.SETTLEMENT));
            if (playerIsClient)
                updateResourceTradeCosts(false);
            break;
        case City:
            citySq.setIntValue(player.getNumPieces(SOCPlayingPiece.CITY));
            break;
        case Ship:
            if (shipSq != null)
                shipSq.setIntValue(player.getNumPieces(SOCPlayingPiece.SHIP));
            break;
        case DevCards:
            developmentSq.setIntValue(player.getInventory().getTotal());
            break;
        case Knight:
            knightsSq.setIntValue(player.getNumKnights());
            break;
        case Cloth:
            if (clothSq != null)
                clothSq.setIntValue(player.getCloth());
            break;
        case WonderLevel:
            if (wonderLab != null) {
                SOCSpecialItem pWond = player.getSpecialItem(SOCGameOption.K_SC_WOND, 0);
                final int pLevel = (pWond != null) ? pWond.getLevel() : 0;
                if (pLevel == 0) {
                    wonderLab.setText("");
                    wonderLab.setToolTipText(null);
                } else {
                    String ofWonder = null;
                    try {
                        // "w3"
                        String sv = pWond.getStringValue();
                        if (sv != null)
                            // "of the Monument"
                            ofWonder = strings.get("game.specitem.sc_wond.of_" + sv);
                    } catch (MissingResourceException e) {
                        try {
                            // "of a Wonder"
                            ofWonder = strings.get("game.specitem.sc_wond.of_fallback");
                        } catch (MissingResourceException e2) {
                            ofWonder = "of a Wonder";
                        }
                    }
                    // "Wonder Level: #"
                    wonderLab.setText(strings.get("hpan.wonderlevel", pLevel));
                    wonderLab.setToolTipText(strings.get("hpan.wonderlevel.tip", pLevel, SOCSpecialItem.SC_WOND_WIN_LEVEL, ofWonder));
                // "Built # of # levels of the Monument"
                }
            }
            break;
        case GoldGains:
        case Warship:
        case Unknown:
    }
    if (updateTotalResCount) {
        resourceSq.setIntValue(player.getResources().getTotal());
        if (offerIsDiscardOrPickMessage) {
            final int gs = game.getGameState();
            if (gs != SOCGame.WAITING_FOR_PICK_GOLD_RESOURCE) {
                clearDiscardOrPickMsg();
            } else {
            // Clear pick-resources message is handled above
            // by updateValue(NUM_PICK_GOLD_HEX_RESOURCES)
            }
        }
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) SOCResourceSet(soc.game.SOCResourceSet) SOCSpecialItem(soc.game.SOCSpecialItem)

Example 50 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCDiceResultResources method buildIntList.

/**
 * Used by server constructor to build an outbound array of ints from these players and these resources.
 * See {@link #SOCDiceResultResources(String, int[])} for expected format.
 * @param pnum Player numbers, same format as {@link #playerNum}
 * @param rTotal  New total resource count for each {@code pn}
 * @param rsrc Resources gained by each {@code pn}, same format as {@link #playerRsrc}
 * @throws IllegalArgumentException if {@code pn}.size() != {@code rsrc}.size() or {@code rTotal}.size(),
 *     or if any of them is empty
 * @throws NullPointerException if any parameter is null
 */
private static final int[] buildIntList(final List<Integer> pnum, final List<Integer> rTotal, final List<SOCResourceSet> rsrc) {
    final int n = pnum.size();
    if ((n == 0) || (n != rsrc.size()) || (n != rTotal.size()))
        throw new IllegalArgumentException();
    // player count elem, then for each player: player number, total rsrc count,
    int len = 3 * n;
    // (so, +1 for player count elem, -1 for no 0 after last player)
    for (SOCResourceSet rs : rsrc) // for each rtype, amount and type number
    len += 2 * (rs.getResourceTypeCount());
    int[] pa = new int[len];
    pa[0] = n;
    // to write next value to pa[i]
    int i = 1;
    for (// player index for reading from pnum, rTotal, and rsrc
    int p = 0; // player index for reading from pnum, rTotal, and rsrc
    p < n; // player index for reading from pnum, rTotal, and rsrc
    ++p) {
        pa[i] = pnum.get(p);
        ++i;
        pa[i] = rTotal.get(p);
        ++i;
        final SOCResourceSet rs = rsrc.get(p);
        for (int rtype = SOCResourceConstants.MIN; rtype <= SOCResourceConstants.WOOD; ++rtype) {
            int amt = rs.getAmount(rtype);
            if (amt != 0) {
                pa[i] = amt;
                ++i;
                pa[i] = rtype;
                ++i;
            }
        }
        if (p != (n - 1)) {
            // separate from next player
            pa[i] = 0;
            // separate from next player
            ++i;
        }
    }
    return pa;
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Aggregations

SOCResourceSet (soc.game.SOCResourceSet)54 Test (org.junit.Test)14 SOCTradeOffer (soc.game.SOCTradeOffer)10 SOCPlayer (soc.game.SOCPlayer)8 Stack (java.util.Stack)4 SOCShip (soc.game.SOCShip)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 SOCBoardLarge (soc.game.SOCBoardLarge)2 SOCGame (soc.game.SOCGame)2 SOCRoad (soc.game.SOCRoad)2 Queue (soc.util.Queue)2 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1 Vector (java.util.Vector)1 SOCInventoryItem (soc.game.SOCInventoryItem)1 SOCLRPathData (soc.game.SOCLRPathData)1 SOCSpecialItem (soc.game.SOCSpecialItem)1