Search in sources :

Example 16 with SOCSettlement

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

the class SOCDisplaylessPlayerClient method handleCANCELBUILDREQUEST.

/**
 * handle the rare "cancel build request" message; usually not sent from
 * server to client.
 *<P>
 * - When sent from client to server, CANCELBUILDREQUEST means the player has changed
 *   their mind about spending resources to build a piece.  Only allowed during normal
 *   game play (PLACING_ROAD, PLACING_SETTLEMENT, or PLACING_CITY).
 *<P>
 *  When sent from server to client:
 *<P>
 * - During game startup (START1B or START2B): <BR>
 *       Sent from server, CANCELBUILDREQUEST means the current player
 *       wants to undo the placement of their initial settlement.
 *       This handler method calls <tt>{@link SOCGame#undoPutInitSettlement(SOCPlayingPiece) ga.undoPutInitSettlement}
 *       (new SOCSettlement {@link SOCPlayer#getLastSettlementCoord() (currPlayer.getLastSettlementCoord())})</tt>.
 *<P>
 * - During piece placement (PLACING_ROAD, PLACING_CITY, PLACING_SETTLEMENT,
 *                           PLACING_FREE_ROAD1 or PLACING_FREE_ROAD2):
 *<P>
 *      Sent from server, CANCELBUILDREQUEST means the player has sent
 *      an illegal PUTPIECE (bad building location). Humans can probably
 *      decide a better place to put their road, but robots must cancel
 *      the build request and decide on a new plan.
 *<P>
 *      Our client can ignore this case, because the server also sends a text
 *      message that the human player is capable of reading and acting on.
 *
 * @param mes  the message
 * @since 1.1.00
 */
protected void handleCANCELBUILDREQUEST(SOCCancelBuildRequest mes) {
    SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    final int sta = ga.getGameState();
    if ((sta != SOCGame.START1B) && (sta != SOCGame.START2B) && (sta != SOCGame.START3B)) {
        // The robot player will override this method and react.
        return;
    }
    if (mes.getPieceType() != SOCPlayingPiece.SETTLEMENT)
        return;
    SOCPlayer pl = ga.getPlayer(ga.getCurrentPlayerNumber());
    SOCSettlement pp = new SOCSettlement(pl, pl.getLastSettlementCoord(), null);
    ga.undoPutInitSettlement(pp);
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame)

Example 17 with SOCSettlement

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

the class SOCDisplaylessPlayerClient method handlePUTPIECE.

/**
 * handle the "put piece" message
 *<P>
 * This method is public static for access by
 * {@code SOCRobotBrain.handlePUTPIECE_updateGameData(SOCPutPiece)}.
 * @param mes  the message
 * @param ga  Message's game from {@link SOCPutPiece#getGame()}; if {@code null}, message is ignored
 */
public static void handlePUTPIECE(final SOCPutPiece mes, SOCGame ga) {
    if (ga != null) {
        final int pieceType = mes.getPieceType();
        final int coord = mes.getCoordinates();
        final SOCPlayer pl = (pieceType != SOCPlayingPiece.VILLAGE) ? ga.getPlayer(mes.getPlayerNumber()) : null;
        switch(pieceType) {
            case SOCPlayingPiece.ROAD:
                ga.putPiece(new SOCRoad(pl, coord, null));
                break;
            case SOCPlayingPiece.SETTLEMENT:
                ga.putPiece(new SOCSettlement(pl, coord, null));
                break;
            case SOCPlayingPiece.CITY:
                ga.putPiece(new SOCCity(pl, coord, null));
                break;
            case SOCPlayingPiece.SHIP:
                ga.putPiece(new SOCShip(pl, coord, null));
                break;
            case SOCPlayingPiece.FORTRESS:
                ga.putPiece(new SOCFortress(pl, coord, ga.getBoard()));
                break;
            case SOCPlayingPiece.VILLAGE:
                ga.putPiece(new SOCVillage(coord, ga.getBoard()));
                break;
            default:
                System.err.println("Displayless.handlePUTPIECE: game " + ga.getName() + ": Unknown pieceType " + pieceType);
        }
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCVillage(soc.game.SOCVillage) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCFortress(soc.game.SOCFortress) SOCRoad(soc.game.SOCRoad)

Example 18 with SOCSettlement

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

the class SOCGameMessageHandler method handleCANCELBUILDREQUEST.

/**
 * handle "cancel build request" message.
 * Cancel placement and send new game state, if cancel is allowed.
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handleCANCELBUILDREQUEST(SOCGame ga, Connection c, final SOCCancelBuildRequest mes) {
    ga.takeMonitor();
    try {
        final String gaName = ga.getName();
        if (handler.checkTurn(c, ga)) {
            final SOCPlayer player = ga.getPlayer(c.getData());
            final int pn = player.getPlayerNumber();
            final int gstate = ga.getGameState();
            final boolean usePlayerElements = (ga.clientVersionLowest >= SOCPlayerElements.MIN_VERSION);
            // If true, there was nothing cancelable: Don't call handler.sendGameState
            boolean noAction = false;
            switch(mes.getPieceType()) {
                case SOCPlayingPiece.ROAD:
                    if ((gstate == SOCGame.PLACING_ROAD) || (gstate == SOCGame.PLACING_FREE_ROAD2)) {
                        ga.cancelBuildRoad(pn);
                        if (gstate == SOCGame.PLACING_ROAD) {
                            if (usePlayerElements) {
                                srv.messageToGame(gaName, new SOCPlayerElements(gaName, pn, SOCPlayerElement.GAIN, SOCRoad.COST));
                            } else {
                                srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.CLAY, 1));
                                srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.WOOD, 1));
                            }
                        } else {
                            srv.messageToGameKeyed(ga, true, "action.card.roadbuilding.skip.r", player.getName());
                        // "{0} skipped placing the second road."
                        }
                    } else {
                        srv.messageToPlayer(c, gaName, /*I*/
                        "You didn't buy a road.");
                        noAction = true;
                    }
                    break;
                case SOCPlayingPiece.SETTLEMENT:
                    if (gstate == SOCGame.PLACING_SETTLEMENT) {
                        ga.cancelBuildSettlement(pn);
                        if (usePlayerElements) {
                            srv.messageToGame(gaName, new SOCPlayerElements(gaName, pn, SOCPlayerElement.GAIN, SOCSettlement.COST));
                        } else {
                            srv.gameList.takeMonitorForGame(gaName);
                            srv.messageToGameWithMon(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.CLAY, 1));
                            srv.messageToGameWithMon(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.SHEEP, 1));
                            srv.messageToGameWithMon(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.WHEAT, 1));
                            srv.messageToGameWithMon(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.WOOD, 1));
                            srv.gameList.releaseMonitorForGame(gaName);
                        }
                    } else if ((gstate == SOCGame.START1B) || (gstate == SOCGame.START2B) || (gstate == SOCGame.START3B)) {
                        SOCSettlement pp = new SOCSettlement(player, player.getLastSettlementCoord(), null);
                        ga.undoPutInitSettlement(pp);
                        // Re-send to all clients to announce it
                        srv.messageToGame(gaName, mes);
                        // (Safe since we've validated all message parameters)
                        // "{0} cancelled this settlement placement."
                        srv.messageToGameKeyed(ga, true, "action.built.stlmt.cancel", player.getName());
                    // The handler.sendGameState below is redundant if client reaction changes game state
                    } else {
                        srv.messageToPlayer(c, gaName, /*I*/
                        "You didn't buy a settlement.");
                        noAction = true;
                    }
                    break;
                case SOCPlayingPiece.CITY:
                    if (gstate == SOCGame.PLACING_CITY) {
                        ga.cancelBuildCity(pn);
                        if (usePlayerElements) {
                            srv.messageToGame(gaName, new SOCPlayerElements(gaName, pn, SOCPlayerElement.GAIN, SOCCity.COST));
                        } else {
                            srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.ORE, 3));
                            srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.WHEAT, 2));
                        }
                    } else {
                        srv.messageToPlayer(c, gaName, /*I*/
                        "You didn't buy a city.");
                        noAction = true;
                    }
                    break;
                case SOCPlayingPiece.SHIP:
                    if ((gstate == SOCGame.PLACING_SHIP) || (gstate == SOCGame.PLACING_FREE_ROAD2)) {
                        ga.cancelBuildShip(pn);
                        if (gstate == SOCGame.PLACING_SHIP) {
                            if (usePlayerElements) {
                                srv.messageToGame(gaName, new SOCPlayerElements(gaName, pn, SOCPlayerElement.GAIN, SOCShip.COST));
                            } else {
                                srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.SHEEP, 1));
                                srv.messageToGame(gaName, new SOCPlayerElement(gaName, pn, SOCPlayerElement.GAIN, SOCPlayerElement.WOOD, 1));
                            }
                        } else {
                            srv.messageToGameKeyed(ga, true, "action.card.roadbuilding.skip.s", player.getName());
                        // "{0} skipped placing the second ship."
                        }
                    } else {
                        srv.messageToPlayer(c, gaName, /*I*/
                        "You didn't buy a ship.");
                        noAction = true;
                    }
                    break;
                case SOCCancelBuildRequest.INV_ITEM_PLACE_CANCEL:
                    SOCInventoryItem item = null;
                    if (gstate == SOCGame.PLACING_INV_ITEM)
                        item = ga.cancelPlaceInventoryItem(false);
                    if (item != null)
                        srv.messageToGame(gaName, new SOCInventoryItemAction(gaName, pn, SOCInventoryItemAction.ADD_PLAYABLE, item.itype, item.isKept(), item.isVPItem(), item.canCancelPlay));
                    if ((item != null) || (gstate != ga.getGameState())) {
                        srv.messageToGameKeyed(ga, true, "reply.placeitem.cancel", player.getName());
                    // "{0} canceled placement of a special item."
                    } else {
                        srv.messageToPlayerKeyed(c, gaName, "reply.placeitem.cancel.cannot");
                        // "Cannot cancel item placement."
                        noAction = true;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unknown piece type " + mes.getPieceType());
            }
            if (!noAction) {
                handler.sendGameState(ga);
            } else {
                // bot is waiting for a gamestate reply, not text
                final SOCClientData scd = (SOCClientData) c.getAppData();
                if ((scd != null) && scd.isRobot)
                    c.put(SOCGameState.toCmd(gaName, gstate));
            }
        } else {
            // "It's not your turn."
            srv.messageToPlayerKeyed(c, gaName, "reply.not.your.turn");
        }
    } catch (Exception e) {
        D.ebugPrintStackTrace(e, "Exception caught");
    }
    ga.releaseMonitor();
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCInventoryItem(soc.game.SOCInventoryItem) SOCPlayer(soc.game.SOCPlayer)

Aggregations

SOCSettlement (soc.game.SOCSettlement)18 SOCPlayer (soc.game.SOCPlayer)11 SOCShip (soc.game.SOCShip)11 SOCCity (soc.game.SOCCity)10 SOCRoad (soc.game.SOCRoad)10 SOCFortress (soc.game.SOCFortress)4 SOCPlayingPiece (soc.game.SOCPlayingPiece)3 SOCBoardLarge (soc.game.SOCBoardLarge)2 SOCVillage (soc.game.SOCVillage)2 Graphics2D (java.awt.Graphics2D)1 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 SOCBoard (soc.game.SOCBoard)1 SOCGame (soc.game.SOCGame)1 SOCInventoryItem (soc.game.SOCInventoryItem)1