Search in sources :

Example 1 with SOCCity

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

the class SOCGameMessageHandler method handlePUTPIECE.

/**
 * handle "put piece" message.
 *<P>
 * Because the current player changes during initial placement,
 * this method has a simplified version of some of the logic from
 * {@link SOCGameHandler#endGameTurn(SOCGame, SOCPlayer, boolean)}
 * to detect and announce the new turn.
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handlePUTPIECE(SOCGame ga, Connection c, SOCPutPiece mes) {
    ga.takeMonitor();
    try {
        final String gaName = ga.getName();
        final String plName = c.getData();
        SOCPlayer player = ga.getPlayer(plName);
        /**
         * make sure the player can do it
         */
        if (handler.checkTurn(c, ga)) {
            boolean sendDenyReply = false;
            /*
                   if (D.ebugOn) {
                   D.ebugPrintln("BEFORE");
                   for (int pn = 0; pn < SOCGame.MAXPLAYERS; pn++) {
                   SOCPlayer tmpPlayer = ga.getPlayer(pn);
                   D.ebugPrintln("Player # "+pn);
                   for (int i = 0x22; i < 0xCC; i++) {
                   if (tmpPlayer.isPotentialRoad(i))
                   D.ebugPrintln("### POTENTIAL ROAD AT "+Integer.toHexString(i));
                   }
                   }
                   }
                 */
            int gameState = ga.getGameState();
            final int coord = mes.getCoordinates();
            final int pieceType = mes.getPieceType();
            final int pn = player.getPlayerNumber();
            final boolean isBuyAndPut = (gameState == SOCGame.PLAY1) || (gameState == SOCGame.SPECIAL_BUILDING);
            if (isBuyAndPut) {
                if (!handleBUILDREQUEST(ga, player, c, pieceType, false)) {
                    // <--- Can't build right now ---
                    return;
                // will call ga.releaseMonitor() in finally-block before returning
                }
                // updated by handleBUILDREQUEST
                gameState = ga.getGameState();
            }
            switch(pieceType) {
                case SOCPlayingPiece.ROAD:
                    if ((gameState == SOCGame.START1B) || (gameState == SOCGame.START2B) || (gameState == SOCGame.START3B) || (gameState == SOCGame.PLACING_ROAD) || (gameState == SOCGame.PLACING_FREE_ROAD1) || (gameState == SOCGame.PLACING_FREE_ROAD2)) {
                        if (player.isPotentialRoad(coord) && (player.getNumPieces(SOCPlayingPiece.ROAD) >= 1)) {
                            final SOCRoad rd = new SOCRoad(player, coord, null);
                            // Changes game state and (if initial placement) player
                            ga.putPiece(rd);
                            // If placing this piece reveals a fog hex, putPiece will call srv.gameEvent
                            // which will send a SOCRevealFogHex message to the game.
                            /*
                               if (D.ebugOn) {
                               D.ebugPrintln("AFTER");
                               for (int pn = 0; pn < SOCGame.MAXPLAYERS; pn++) {
                               SOCPlayer tmpPlayer = ga.getPlayer(pn);
                               D.ebugPrintln("Player # "+pn);
                               for (int i = 0x22; i < 0xCC; i++) {
                               if (tmpPlayer.isPotentialRoad(i))
                               D.ebugPrintln("### POTENTIAL ROAD AT "+Integer.toHexString(i));
                               }
                               }
                               }
                             */
                            srv.gameList.takeMonitorForGame(gaName);
                            // "Joe built a road."
                            srv.messageToGameKeyed(ga, false, "action.built.road", plName);
                            srv.messageToGameWithMon(gaName, new SOCPutPiece(gaName, pn, SOCPlayingPiece.ROAD, coord));
                            if (!ga.pendingMessagesOut.isEmpty())
                                handler.sendGamePendingMessages(ga, false);
                            srv.gameList.releaseMonitorForGame(gaName);
                            // If needed, call sendTurn or send SOCRollDicePrompt
                            handler.sendTurnStateAtInitialPlacement(ga, player, c, gameState);
                            int newState = ga.getGameState();
                            if ((newState == SOCGame.STARTS_WAITING_FOR_PICK_GOLD_RESOURCE) || (newState == SOCGame.WAITING_FOR_PICK_GOLD_RESOURCE)) {
                                // gold hex revealed from fog (scenario SC_FOG)
                                handler.sendGameState_sendGoldPickAnnounceText(ga, gaName, c, null);
                            }
                        } else {
                            D.ebugPrintln("ILLEGAL ROAD: 0x" + Integer.toHexString(coord) + ": player " + pn);
                            if (player.isRobot() && D.ebugOn) {
                                D.ebugPrintln(" - pl.isPotentialRoad: " + player.isPotentialRoad(coord));
                                SOCPlayingPiece pp = ga.getBoard().roadAtEdge(coord);
                                D.ebugPrintln(" - roadAtEdge: " + ((pp != null) ? pp : "none"));
                            }
                            srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.there.road");
                            // "You can't build a road there."
                            sendDenyReply = true;
                        }
                    } else {
                        srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.now.road");
                    // "You can't build a road now."
                    }
                    break;
                case SOCPlayingPiece.SETTLEMENT:
                    if ((gameState == SOCGame.START1A) || (gameState == SOCGame.START2A) || (gameState == SOCGame.START3A) || (gameState == SOCGame.PLACING_SETTLEMENT)) {
                        if (player.canPlaceSettlement(coord) && (player.getNumPieces(SOCPlayingPiece.SETTLEMENT) >= 1)) {
                            final SOCSettlement se = new SOCSettlement(player, coord, null);
                            // Changes game state and (if initial placement) player
                            ga.putPiece(se);
                            srv.gameList.takeMonitorForGame(gaName);
                            // "Joe built a settlement."
                            srv.messageToGameKeyed(ga, false, "action.built.stlmt", plName);
                            srv.messageToGameWithMon(gaName, new SOCPutPiece(gaName, pn, SOCPlayingPiece.SETTLEMENT, coord));
                            if (!ga.pendingMessagesOut.isEmpty())
                                handler.sendGamePendingMessages(ga, false);
                            srv.gameList.releaseMonitorForGame(gaName);
                            // Check player and send new game state
                            if (!handler.checkTurn(c, ga))
                                // Announce new state and new current player
                                handler.sendTurn(ga, false);
                            else
                                handler.sendGameState(ga);
                            if (ga.hasSeaBoard && (ga.getGameState() == SOCGame.STARTS_WAITING_FOR_PICK_GOLD_RESOURCE)) {
                                // Prompt to pick from gold: send text and SOCSimpleRequest(PROMPT_PICK_RESOURCES)
                                handler.sendGameState_sendGoldPickAnnounceText(ga, gaName, c, null);
                            }
                        } else {
                            D.ebugPrintln("ILLEGAL SETTLEMENT: 0x" + Integer.toHexString(coord) + ": player " + pn);
                            if (player.isRobot() && D.ebugOn) {
                                D.ebugPrintln(" - pl.isPotentialSettlement: " + player.isPotentialSettlement(coord));
                                SOCPlayingPiece pp = ga.getBoard().settlementAtNode(coord);
                                D.ebugPrintln(" - settlementAtNode: " + ((pp != null) ? pp : "none"));
                            }
                            srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.there.stlmt");
                            // "You can't build a settlement there."
                            sendDenyReply = true;
                        }
                    } else {
                        srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.now.stlmt");
                    // "You can't build a settlement now."
                    }
                    break;
                case SOCPlayingPiece.CITY:
                    if (gameState == SOCGame.PLACING_CITY) {
                        if (player.isPotentialCity(coord) && (player.getNumPieces(SOCPlayingPiece.CITY) >= 1)) {
                            boolean houseRuleFirstCity = ga.isGameOptionSet("N7C") && !ga.hasBuiltCity();
                            if (houseRuleFirstCity && ga.isGameOptionSet("N7") && (ga.getRoundCount() < ga.getGameOptionIntValue("N7"))) {
                                // If "No 7s for first # rounds" is active, and this isn't its last round, 7s won't
                                // be rolled soon: Don't announce "Starting next turn, dice rolls of 7 may occur"
                                houseRuleFirstCity = false;
                            }
                            final SOCCity ci = new SOCCity(player, coord, null);
                            // changes game state and maybe player
                            ga.putPiece(ci);
                            srv.gameList.takeMonitorForGame(gaName);
                            // "Joe built a city."
                            srv.messageToGameKeyed(ga, false, "action.built.city", plName);
                            srv.messageToGameWithMon(gaName, new SOCPutPiece(gaName, pn, SOCPlayingPiece.CITY, coord));
                            if (!ga.pendingMessagesOut.isEmpty())
                                handler.sendGamePendingMessages(ga, false);
                            if (houseRuleFirstCity)
                                srv.messageToGameKeyed(ga, false, "action.built.nextturn.7.houserule");
                            // "Starting next turn, dice rolls of 7 may occur (house rule)."
                            srv.gameList.releaseMonitorForGame(gaName);
                            // Check player and send new game state
                            if (!handler.checkTurn(c, ga))
                                // Announce new state and new current player
                                handler.sendTurn(ga, false);
                            else
                                handler.sendGameState(ga);
                        } else {
                            D.ebugPrintln("ILLEGAL CITY: 0x" + Integer.toHexString(coord) + ": player " + pn);
                            if (player.isRobot() && D.ebugOn) {
                                D.ebugPrintln(" - pl.isPotentialCity: " + player.isPotentialCity(coord));
                                SOCPlayingPiece pp = ga.getBoard().settlementAtNode(coord);
                                D.ebugPrintln(" - city/settlementAtNode: " + ((pp != null) ? pp : "none"));
                            }
                            srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.there.city");
                            // "You can't build a city there."
                            sendDenyReply = true;
                        }
                    } else {
                        srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.now.city");
                    // "You can't build a city now."
                    }
                    break;
                case SOCPlayingPiece.SHIP:
                    if ((gameState == SOCGame.START1B) || (gameState == SOCGame.START2B) || (gameState == SOCGame.START3B) || (gameState == SOCGame.PLACING_SHIP) || (gameState == SOCGame.PLACING_FREE_ROAD1) || (gameState == SOCGame.PLACING_FREE_ROAD2)) {
                        // Place it if we can; canPlaceShip checks potentials and pirate ship location
                        if (ga.canPlaceShip(player, coord) && (player.getNumPieces(SOCPlayingPiece.SHIP) >= 1)) {
                            final SOCShip sh = new SOCShip(player, coord, null);
                            // Changes game state and (during initial placement) sometimes player
                            ga.putPiece(sh);
                            srv.gameList.takeMonitorForGame(gaName);
                            // "Joe built a ship."
                            srv.messageToGameKeyed(ga, false, "action.built.ship", plName);
                            srv.messageToGameWithMon(gaName, new SOCPutPiece(gaName, pn, SOCPlayingPiece.SHIP, coord));
                            if (!ga.pendingMessagesOut.isEmpty())
                                handler.sendGamePendingMessages(ga, false);
                            srv.gameList.releaseMonitorForGame(gaName);
                            // If needed, call sendTurn or send SOCRollDicePrompt
                            handler.sendTurnStateAtInitialPlacement(ga, player, c, gameState);
                            int newState = ga.getGameState();
                            if ((newState == SOCGame.STARTS_WAITING_FOR_PICK_GOLD_RESOURCE) || (newState == SOCGame.WAITING_FOR_PICK_GOLD_RESOURCE)) {
                                // gold hex revealed from fog (scenario SC_FOG)
                                handler.sendGameState_sendGoldPickAnnounceText(ga, gaName, c, null);
                            }
                        } else {
                            D.ebugPrintln("ILLEGAL SHIP: 0x" + Integer.toHexString(coord) + ": player " + pn);
                            if (player.isRobot() && D.ebugOn) {
                                D.ebugPrintln(" - pl.isPotentialShip: " + player.isPotentialShip(coord));
                                SOCPlayingPiece pp = ga.getBoard().roadAtEdge(coord);
                                D.ebugPrintln(" - ship/roadAtEdge: " + ((pp != null) ? pp : "none"));
                            }
                            srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.there.ship");
                            // "You can't build a ship there."
                            sendDenyReply = true;
                        }
                    } else {
                        srv.messageToPlayerKeyed(c, gaName, "action.build.cannot.now.ship");
                    // "You can't build a ship now."
                    }
                    break;
            }
            if (sendDenyReply) {
                if (isBuyAndPut)
                    // is probably now PLACING_*, was PLAY1 or SPECIAL_BUILDING
                    handler.sendGameState(ga);
                srv.messageToPlayer(c, new SOCCancelBuildRequest(gaName, mes.getPieceType()));
                if (player.isRobot()) {
                    // Set the "force end turn soon" field
                    ga.lastActionTime = 0L;
                }
            }
        } else {
            // "It's not your turn."
            srv.messageToPlayerKeyed(c, gaName, "reply.not.your.turn");
        }
    } catch (Exception e) {
        D.ebugPrintStackTrace(e, "Exception caught in handlePUTPIECE");
    } finally {
        ga.releaseMonitor();
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCPlayingPiece(soc.game.SOCPlayingPiece) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCRoad(soc.game.SOCRoad)

Example 2 with SOCCity

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

the class SOCGameMessageHandler method handleDEBUGFREEPLACE.

/**
 * Handle the client's debug Free Placement putpiece request.
 * @since 1.1.12
 */
private void handleDEBUGFREEPLACE(final SOCGame ga, final Connection c, final SOCDebugFreePlace mes) {
    if (!ga.isDebugFreePlacement())
        return;
    final String gaName = ga.getName();
    final int coord = mes.getCoordinates();
    final SOCPlayer player = ga.getPlayer(mes.getPlayerNumber());
    if (player == null)
        return;
    boolean didPut = false;
    final int pieceType = mes.getPieceType();
    final boolean initialDeny = ga.isInitialPlacement() && !player.canBuildInitialPieceType(pieceType);
    switch(pieceType) {
        case SOCPlayingPiece.ROAD:
            if (player.isPotentialRoad(coord) && !initialDeny) {
                ga.putPiece(new SOCRoad(player, coord, null));
                didPut = true;
            }
            break;
        case SOCPlayingPiece.SETTLEMENT:
            if (player.canPlaceSettlement(coord) && !initialDeny) {
                ga.putPiece(new SOCSettlement(player, coord, null));
                didPut = true;
            }
            break;
        case SOCPlayingPiece.CITY:
            if (player.isPotentialCity(coord) && !initialDeny) {
                ga.putPiece(new SOCCity(player, coord, null));
                didPut = true;
            }
            break;
        case SOCPlayingPiece.SHIP:
            if (ga.canPlaceShip(player, coord) && !initialDeny) {
                ga.putPiece(new SOCShip(player, coord, null));
                didPut = true;
            }
            break;
        default:
            srv.messageToPlayer(c, gaName, "* Unknown piece type: " + pieceType);
    }
    if (didPut) {
        srv.messageToGame(gaName, new SOCPutPiece(gaName, mes.getPlayerNumber(), pieceType, coord));
        // Check for initial settlement next to gold hex
        if (pieceType == SOCPlayingPiece.SETTLEMENT) {
            final int numGoldRes = player.getNeedToPickGoldHexResources();
            if (numGoldRes > 0)
                srv.messageToPlayer(c, new SOCSimpleRequest(gaName, player.getPlayerNumber(), SOCSimpleRequest.PROMPT_PICK_RESOURCES, numGoldRes));
        }
        if (ga.getGameState() >= SOCGame.OVER) {
            // exit debug mode, announce end of game
            handler.processDebugCommand_freePlace(c, gaName, "0");
            handler.sendGameState(ga, false, false);
        }
    } else {
        if (initialDeny) {
            final String pieceTypeFirst = ((player.getPieces().size() % 2) == 0) ? "settlement" : "road";
            srv.messageToPlayer(c, gaName, "Place a " + pieceTypeFirst + " before placing that.");
        } else {
            srv.messageToPlayer(c, gaName, "Not a valid location to place that.");
        }
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCRoad(soc.game.SOCRoad)

Example 3 with SOCCity

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

the class SOCRobotBrain method buildRequestPlannedPiece.

/**
 * Have the client ask to build our top planned piece
 * (calls {@link #buildingPlan}{@link Stack#pop() .pop()}),
 * unless we've already been told by the server to not build it.
 * Sets {@link #whatWeWantToBuild}, {@link #waitingForDevCard},
 * or {@link #waitingForPickSpecialItem}.
 * Called from {@link #buildOrGetResourceByTradeOrCard()}.
 *<P>
 * Checks against {@link #whatWeFailedToBuild} to see if server has rejected this already.
 * Calls <tt>client.buyDevCard()</tt> or <tt>client.buildRequest()</tt>.
 * Sets {@link #waitingForDevCard} or {@link #waitingForPickSpecialItem},
 * or sets {@link #waitingForGameState} and a flag like {@link #expectPLACING_SETTLEMENT} (etc).
 *<P>
 * Preconditions: Call only when:
 *<UL>
 * <LI> Gamestate is {@link SOCGame#PLAY1} or {@link SOCGame#SPECIAL_BUILDING}
 * <LI> <tt>! ({@link #waitingForTradeMsg} || {@link #waitingForTradeResponse})</tt>
 * <LI> ourPlayerData.getResources().{@link SOCResourceSet#contains(soc.game.ResourceSet) contains}(targetPieceResources)
 *</UL>
 *
 * @see #placeIfExpectPlacing()
 * @since 1.1.08
 */
private void buildRequestPlannedPiece() {
    final SOCPossiblePiece targetPiece = buildingPlan.pop();
    D.ebugPrintln("$ POPPED " + targetPiece);
    lastMove = targetPiece;
    currentDRecorder = (currentDRecorder + 1) % 2;
    negotiator.setTargetPiece(ourPlayerNumber, targetPiece);
    switch(targetPiece.getType()) {
        case SOCPossiblePiece.CARD:
            client.buyDevCard(game);
            waitingForDevCard = true;
            break;
        case SOCPossiblePiece.ROAD:
            waitingForGameState = true;
            counter = 0;
            expectPLACING_ROAD = true;
            whatWeWantToBuild = new SOCRoad(ourPlayerData, targetPiece.getCoordinates(), null);
            if (!whatWeWantToBuild.equals(whatWeFailedToBuild)) {
                D.ebugPrintln("!!! BUILD REQUEST FOR A ROAD AT " + Integer.toHexString(targetPiece.getCoordinates()) + " !!!");
                client.buildRequest(game, SOCPlayingPiece.ROAD);
            } else {
                // We already tried to build this.
                cancelWrongPiecePlacementLocal(whatWeWantToBuild);
            // cancel sets whatWeWantToBuild = null;
            }
            break;
        case SOCPlayingPiece.SETTLEMENT:
            waitingForGameState = true;
            counter = 0;
            expectPLACING_SETTLEMENT = true;
            whatWeWantToBuild = new SOCSettlement(ourPlayerData, targetPiece.getCoordinates(), null);
            if (!whatWeWantToBuild.equals(whatWeFailedToBuild)) {
                D.ebugPrintln("!!! BUILD REQUEST FOR A SETTLEMENT " + Integer.toHexString(targetPiece.getCoordinates()) + " !!!");
                client.buildRequest(game, SOCPlayingPiece.SETTLEMENT);
            } else {
                // We already tried to build this.
                cancelWrongPiecePlacementLocal(whatWeWantToBuild);
            // cancel sets whatWeWantToBuild = null;
            }
            break;
        case SOCPlayingPiece.CITY:
            waitingForGameState = true;
            counter = 0;
            expectPLACING_CITY = true;
            whatWeWantToBuild = new SOCCity(ourPlayerData, targetPiece.getCoordinates(), null);
            if (!whatWeWantToBuild.equals(whatWeFailedToBuild)) {
                D.ebugPrintln("!!! BUILD REQUEST FOR A CITY " + Integer.toHexString(targetPiece.getCoordinates()) + " !!!");
                client.buildRequest(game, SOCPlayingPiece.CITY);
            } else {
                // We already tried to build this.
                cancelWrongPiecePlacementLocal(whatWeWantToBuild);
            // cancel sets whatWeWantToBuild = null;
            }
            break;
        case SOCPlayingPiece.SHIP:
            waitingForGameState = true;
            counter = 0;
            expectPLACING_SHIP = true;
            whatWeWantToBuild = new SOCShip(ourPlayerData, targetPiece.getCoordinates(), null);
            if (!whatWeWantToBuild.equals(whatWeFailedToBuild)) {
                System.err.println("L2733: " + ourPlayerData.getName() + ": !!! BUILD REQUEST FOR A SHIP AT " + Integer.toHexString(targetPiece.getCoordinates()) + " !!!");
                D.ebugPrintln("!!! BUILD REQUEST FOR A SHIP AT " + Integer.toHexString(targetPiece.getCoordinates()) + " !!!");
                client.buildRequest(game, SOCPlayingPiece.SHIP);
            } else {
                // We already tried to build this.
                cancelWrongPiecePlacementLocal(whatWeWantToBuild);
            // cancel sets whatWeWantToBuild = null;
            }
            break;
        case SOCPossiblePiece.PICK_SPECIAL:
            {
                final SOCPossiblePickSpecialItem psi = (SOCPossiblePickSpecialItem) targetPiece;
                waitingForPickSpecialItem = psi.typeKey;
                // targetPiece isn't a SOCPlayingPiece
                whatWeWantToBuild = null;
                counter = 0;
                client.pickSpecialItem(game, psi.typeKey, psi.gi, psi.pi);
            }
            break;
        default:
            // shouldn't occur: print for debugging
            System.err.println(ourPlayerData.getName() + ": buildRequestPlannedPiece: Unknown piece type " + targetPiece.getType());
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCRoad(soc.game.SOCRoad)

Example 4 with SOCCity

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

the class SOCRobotBrain method handlePUTPIECE_updateTrackers.

/**
 * Handle a PUTPIECE for this game, by updating {@link SOCPlayerTracker}s.
 * Also handles the "move piece to here" part of MOVEPIECE.
 *<P>
 * For initial placement of our own pieces, this method also checks
 * and clears expectPUTPIECE_FROM_START1A, and sets expectSTART1B, etc.
 * The final initial putpiece clears expectPUTPIECE_FROM_START2B and sets expectROLL_OR_CARD.
 * As part of the PUTPIECE request, brain set those expectPUTPIECE flags in {@link #placeIfExpectPlacing()}.
 *<P>
 * For initial settlements, won't track here:
 * Delay tracking until the corresponding road is placed,
 * in {@link #handlePUTPIECE_updateGameData(SOCPutPiece)}.
 * This prevents the need for tracker "undo" work if a human
 * player changes their mind on where to place the settlement.
 *
 * @param pn  Piece's player number
 * @param coord  Piece coordinate
 * @param pieceType  Piece type, as in {@link SOCPlayingPiece#SETTLEMENT}
 * @since 1.1.08
 */
private void handlePUTPIECE_updateTrackers(final int pn, final int coord, final int pieceType) {
    switch(pieceType) {
        case SOCPlayingPiece.ROAD:
            SOCRoad newRoad = new SOCRoad(game.getPlayer(pn), coord, null);
            trackNewRoadOrShip(newRoad, false);
            break;
        case SOCPlayingPiece.SETTLEMENT:
            SOCPlayer newSettlementPl = game.getPlayer(pn);
            SOCSettlement newSettlement = new SOCSettlement(newSettlementPl, coord, null);
            if ((game.getGameState() == SOCGame.START1B) || (game.getGameState() == SOCGame.START2B) || (game.getGameState() == SOCGame.START3B)) {
                // Track it soon, after the road is placed
                // (in handlePUTPIECE_updateGameData)
                // but not yet, in case player cancels placement.
                SOCPlayerTracker tr = playerTrackers.get(Integer.valueOf(newSettlementPl.getPlayerNumber()));
                tr.setPendingInitSettlement(newSettlement);
            } else {
                // Track it now
                trackNewSettlement(newSettlement, false);
            }
            break;
        case SOCPlayingPiece.CITY:
            SOCCity newCity = new SOCCity(game.getPlayer(pn), coord, null);
            trackNewCity(newCity, false);
            break;
        case SOCPlayingPiece.SHIP:
            SOCShip newShip = new SOCShip(game.getPlayer(pn), coord, null);
            trackNewRoadOrShip(newShip, false);
            break;
        case SOCPlayingPiece.VILLAGE:
            // <--- Early return: Piece is part of board initial layout, not tracked player info ---
            return;
    }
    if (D.ebugOn) {
        SOCPlayerTracker.playerTrackersDebug(playerTrackers);
    }
    if (pn != ourPlayerNumber) {
        // <---- Not our piece ----
        return;
    }
    if (expectPUTPIECE_FROM_START1A && (pieceType == SOCPlayingPiece.SETTLEMENT) && (coord == ourPlayerData.getLastSettlementCoord())) {
        expectPUTPIECE_FROM_START1A = false;
        expectSTART1B = true;
    }
    if (expectPUTPIECE_FROM_START1B && ((pieceType == SOCPlayingPiece.ROAD) || (pieceType == SOCPlayingPiece.SHIP)) && (coord == ourPlayerData.getLastRoadCoord())) {
        expectPUTPIECE_FROM_START1B = false;
        expectSTART2A = true;
    }
    if (expectPUTPIECE_FROM_START2A && (pieceType == SOCPlayingPiece.SETTLEMENT) && (coord == ourPlayerData.getLastSettlementCoord())) {
        expectPUTPIECE_FROM_START2A = false;
        expectSTART2B = true;
    }
    if (expectPUTPIECE_FROM_START2B && ((pieceType == SOCPlayingPiece.ROAD) || (pieceType == SOCPlayingPiece.SHIP)) && (coord == ourPlayerData.getLastRoadCoord())) {
        expectPUTPIECE_FROM_START2B = false;
        if (!game.isGameOptionSet(SOCGameOption.K_SC_3IP))
            // wait for regular game play to start; other players might still place first
            expectROLL_OR_CARD = true;
        else
            expectSTART3A = true;
    }
    if (expectPUTPIECE_FROM_START3A && (pieceType == SOCPlayingPiece.SETTLEMENT) && (coord == ourPlayerData.getLastSettlementCoord())) {
        expectPUTPIECE_FROM_START3A = false;
        expectSTART3B = true;
    }
    if (expectPUTPIECE_FROM_START3B && ((pieceType == SOCPlayingPiece.ROAD) || (pieceType == SOCPlayingPiece.SHIP)) && (coord == ourPlayerData.getLastRoadCoord())) {
        expectPUTPIECE_FROM_START3B = false;
        expectROLL_OR_CARD = true;
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCRoad(soc.game.SOCRoad)

Example 5 with SOCCity

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

the class SOCRobotBrain method cancelWrongPiecePlacement.

/**
 *  We've asked for an illegal piece placement.
 *  Cancel and invalidate this planned piece, make a new plan.
 *  If {@link SOCGame#isSpecialBuilding()}, will set variables to
 *  force the end of our special building turn.
 *  Also handles illegal requests to buy development cards
 *  (piece type -2 in {@link SOCCancelBuildRequest}).
 *<P>
 *  Must update game data by calling {@link SOCGame#setGameState(int)} before calling this method.
 *<P>
 *  This method increments {@link #failedBuildingAttempts},
 *  but won't leave the game if we've failed too many times.
 *  The brain's run loop should make that decision.
 *<UL>
 * <LI> If {@link SOCGame#getGameState()} is {@link SOCGame#PLAY1},
 *   server likely denied us due to resources, not due to building plan
 *   being interrupted by another player's building before our special building phase.
 *   (Could also be due to a bug in the chosen building plan.)
 *   Will clear our building plan so we'll make a new one.
 * <LI> In other gamestates, assumes requested piece placement location was illegal.
 *   Will call {@link #cancelWrongPiecePlacementLocal(SOCPlayingPiece)}
 *   so we don't try again to build there.
 * <LI> Either way, sends a {@link CancelBuildRequest} message to the server.
 *</UL>
 *
 * @param mes  Cancel message from server, including piece type
 */
protected void cancelWrongPiecePlacement(SOCCancelBuildRequest mes) {
    // == -2
    final boolean cancelBuyDevCard = (mes.getPieceType() == SOCPossiblePiece.CARD);
    if (cancelBuyDevCard) {
        waitingForDevCard = false;
    } else {
        whatWeFailedToBuild = whatWeWantToBuild;
        ++failedBuildingAttempts;
    }
    waitingForGameState = false;
    final int gameState = game.getGameState();
    /**
     * if true, server likely denied us due to resources, not due to building plan
     * being interrupted by another player's building before our special building phase.
     * (Could also be due to a bug in the chosen building plan.)
     */
    final boolean gameStateIsPLAY1 = (gameState == SOCGame.PLAY1);
    if (!(gameStateIsPLAY1 || cancelBuyDevCard)) {
        int coord = -1;
        switch(gameState) {
            case SOCGame.START1A:
            case SOCGame.START1B:
            case SOCGame.START2A:
            case SOCGame.START2B:
            case SOCGame.START3A:
            case SOCGame.START3B:
                coord = lastStartingPieceCoord;
                break;
            default:
                if (whatWeWantToBuild != null)
                    coord = whatWeWantToBuild.getCoordinates();
        }
        if (coord != -1) {
            SOCPlayingPiece cancelPiece;
            /**
             * First, invalidate that piece in trackers, so we don't try again to
             * build it. If we treat it like another player's new placement, we
             * can remove any of our planned pieces depending on this one.
             */
            switch(mes.getPieceType()) {
                case SOCPlayingPiece.ROAD:
                    cancelPiece = new SOCRoad(dummyCancelPlayerData, coord, null);
                    break;
                case SOCPlayingPiece.SETTLEMENT:
                    cancelPiece = new SOCSettlement(dummyCancelPlayerData, coord, null);
                    break;
                case SOCPlayingPiece.CITY:
                    cancelPiece = new SOCCity(dummyCancelPlayerData, coord, null);
                    break;
                case SOCPlayingPiece.SHIP:
                    cancelPiece = new SOCShip(dummyCancelPlayerData, coord, null);
                    break;
                default:
                    // To satisfy javac
                    cancelPiece = null;
            }
            cancelWrongPiecePlacementLocal(cancelPiece);
        }
    } else {
        /**
         *  stop trying to build it now, but don't prevent
         *  us from trying later to build it.
         */
        whatWeWantToBuild = null;
        buildingPlan.clear();
    }
    if (gameStateIsPLAY1 || game.isSpecialBuilding()) {
        // Shouldn't have asked to build this piece at this time.
        // End our confusion by ending our current turn. Can re-plan on next turn.
        failedBuildingAttempts = MAX_DENIED_BUILDING_PER_TURN;
        expectPLACING_ROAD = false;
        expectPLACING_SETTLEMENT = false;
        expectPLACING_CITY = false;
        expectPLACING_SHIP = false;
        decidedIfSpecialBuild = true;
        if (!cancelBuyDevCard) {
            // special building, currently in state PLACING_* ;
            // get our resources back, get state PLAY1 or SPECIALBUILD
            waitingForGameState = true;
            expectPLAY1 = true;
            client.cancelBuildRequest(game, mes.getPieceType());
        }
    } else if (gameState <= SOCGame.START3B) {
        switch(gameState) {
            case SOCGame.START1A:
                expectPUTPIECE_FROM_START1A = false;
                expectSTART1A = true;
                break;
            case SOCGame.START1B:
                expectPUTPIECE_FROM_START1B = false;
                expectSTART1B = true;
                break;
            case SOCGame.START2A:
                expectPUTPIECE_FROM_START2A = false;
                expectSTART2A = true;
                break;
            case SOCGame.START2B:
                expectPUTPIECE_FROM_START2B = false;
                expectSTART2B = true;
                break;
            case SOCGame.START3A:
                expectPUTPIECE_FROM_START3A = false;
                expectSTART3A = true;
                break;
            case SOCGame.START3B:
                expectPUTPIECE_FROM_START3B = false;
                expectSTART3B = true;
                break;
        }
    // The run loop will check if failedBuildingAttempts > (2 * MAX_DENIED_BUILDING_PER_TURN).
    // This bot will leave the game there if it can't recover.
    } else {
        expectPLAY1 = true;
        waitingForGameState = true;
        counter = 0;
        client.cancelBuildRequest(game, mes.getPieceType());
    // Now wait for the play1 message, then can re-plan another piece.
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCPlayingPiece(soc.game.SOCPlayingPiece) SOCShip(soc.game.SOCShip) SOCRoad(soc.game.SOCRoad)

Aggregations

SOCCity (soc.game.SOCCity)12 SOCRoad (soc.game.SOCRoad)10 SOCSettlement (soc.game.SOCSettlement)10 SOCShip (soc.game.SOCShip)10 SOCPlayer (soc.game.SOCPlayer)7 SOCBoardLarge (soc.game.SOCBoardLarge)3 SOCFortress (soc.game.SOCFortress)3 SOCPlayingPiece (soc.game.SOCPlayingPiece)3 SOCVillage (soc.game.SOCVillage)2 Graphics2D (java.awt.Graphics2D)1 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector (java.util.Vector)1 SOCBoard (soc.game.SOCBoard)1 SOCLRPathData (soc.game.SOCLRPathData)1 SOCPlayerNumbers (soc.game.SOCPlayerNumbers)1