Search in sources :

Example 1 with SOCRoad

use of soc.game.SOCRoad 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 SOCRoad

use of soc.game.SOCRoad 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 SOCRoad

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

the class SOCRobotBrain method trackNewSettlement.

/**
 * Run a newly placed settlement through the playerTrackers.
 * Called only after {@link SOCGame#putPiece(SOCPlayingPiece)}
 * or {@link SOCGame#putTempPiece(SOCPlayingPiece)}.
 *<P>
 * During initial board setup, settlements aren't tracked when placed.
 * They are deferred until their corresponding road placement, in case
 * a human player decides to cancel their settlement and place it elsewhere.
 *
 * During normal play, the settlements are tracked immediately when placed.
 *
 * (Code previously in body of the run method.)
 * Placing the code in its own method allows tracking that settlement when the
 * road's putPiece message arrives.
 *
 * @param newSettlement The newly placed settlement for the playerTrackers
 * @param isCancel Is this our own robot's settlement placement, rejected by the server?
 *     If so, this method call will cancel its placement within the game data / robot data.
 */
protected void trackNewSettlement(SOCSettlement newSettlement, final boolean isCancel) {
    Iterator<SOCPlayerTracker> trackersIter = playerTrackers.values().iterator();
    while (trackersIter.hasNext()) {
        SOCPlayerTracker tracker = trackersIter.next();
        if (!isCancel)
            tracker.addNewSettlement(newSettlement, playerTrackers);
        else
            tracker.cancelWrongSettlement(newSettlement);
    }
    trackersIter = playerTrackers.values().iterator();
    while (trackersIter.hasNext()) {
        SOCPlayerTracker tracker = trackersIter.next();
        Iterator<SOCPossibleRoad> posRoadsIter = tracker.getPossibleRoads().values().iterator();
        while (posRoadsIter.hasNext()) {
            posRoadsIter.next().clearThreats();
        }
        Iterator<SOCPossibleSettlement> posSetsIter = tracker.getPossibleSettlements().values().iterator();
        while (posSetsIter.hasNext()) {
            posSetsIter.next().clearThreats();
        }
    }
    trackersIter = playerTrackers.values().iterator();
    while (trackersIter.hasNext()) {
        SOCPlayerTracker tracker = trackersIter.next();
        tracker.updateThreats(playerTrackers);
    }
    if (isCancel) {
        // <--- Early return, nothing else to do ---
        return;
    }
    // /
    // / see if this settlement bisected someone else's road
    // /
    // Length should be SOCGame.MAXPLAYERS
    int[] roadCount = { 0, 0, 0, 0, 0, 0 };
    SOCBoard board = game.getBoard();
    Enumeration<Integer> adjEdgeEnum = board.getAdjacentEdgesToNode(newSettlement.getCoordinates()).elements();
    while (adjEdgeEnum.hasMoreElements()) {
        final int adjEdge = adjEdgeEnum.nextElement().intValue();
        Enumeration<SOCRoad> roadEnum = board.getRoads().elements();
        while (roadEnum.hasMoreElements()) {
            SOCRoad road = roadEnum.nextElement();
            if (road.getCoordinates() == adjEdge) {
                final int roadPN = road.getPlayerNumber();
                roadCount[roadPN]++;
                if (roadCount[roadPN] == 2) {
                    if (roadPN != ourPlayerNumber) {
                        // /
                        // / this settlement bisects another players road
                        // /
                        trackersIter = playerTrackers.values().iterator();
                        while (trackersIter.hasNext()) {
                            SOCPlayerTracker tracker = trackersIter.next();
                            if (tracker.getPlayer().getPlayerNumber() == roadPN) {
                            // D.ebugPrintln("$$ updating LR Value for player "+tracker.getPlayer().getPlayerNumber());
                            // tracker.updateLRValues();
                            }
                        // tracker.recalcLongestRoadETA();
                        }
                    }
                    break;
                }
            }
        }
    }
    final int pNum = newSettlement.getPlayerNumber();
    // /
    // / update the speedups from possible settlements
    // /
    trackersIter = playerTrackers.values().iterator();
    while (trackersIter.hasNext()) {
        SOCPlayerTracker tracker = trackersIter.next();
        if (tracker.getPlayer().getPlayerNumber() == pNum) {
            Iterator<SOCPossibleSettlement> posSetsIter = tracker.getPossibleSettlements().values().iterator();
            while (posSetsIter.hasNext()) {
                posSetsIter.next().updateSpeedup();
            }
            break;
        }
    }
    // /
    // / update the speedups from possible cities
    // /
    trackersIter = playerTrackers.values().iterator();
    while (trackersIter.hasNext()) {
        SOCPlayerTracker tracker = trackersIter.next();
        if (tracker.getPlayer().getPlayerNumber() == pNum) {
            Iterator<SOCPossibleCity> posCitiesIter = tracker.getPossibleCities().values().iterator();
            while (posCitiesIter.hasNext()) {
                posCitiesIter.next().updateSpeedup();
            }
            break;
        }
    }
}
Also used : SOCBoard(soc.game.SOCBoard) SOCRoad(soc.game.SOCRoad)

Example 4 with SOCRoad

use of soc.game.SOCRoad 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 5 with SOCRoad

use of soc.game.SOCRoad 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)

Aggregations

SOCRoad (soc.game.SOCRoad)21 SOCShip (soc.game.SOCShip)17 SOCCity (soc.game.SOCCity)10 SOCSettlement (soc.game.SOCSettlement)10 SOCPlayer (soc.game.SOCPlayer)9 SOCBoard (soc.game.SOCBoard)5 SOCBoardLarge (soc.game.SOCBoardLarge)4 SOCFortress (soc.game.SOCFortress)4 SOCPlayingPiece (soc.game.SOCPlayingPiece)3 ArrayList (java.util.ArrayList)2 SOCResourceSet (soc.game.SOCResourceSet)2 SOCVillage (soc.game.SOCVillage)2 Graphics2D (java.awt.Graphics2D)1 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 Stack (java.util.Stack)1 CutoffExceededException (soc.util.CutoffExceededException)1