Search in sources :

Example 21 with SOCPlayer

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

the class SOCPlayerTracker method recalcLongestRoadETA.

/**
 * Calculate the longest road ETA.
 * Always 500 or more if {@link SOCGameOption#K_SC_0RVP} is set.
 * Updates fields for {@link #getLongestRoadETA()} and {@link #getRoadsToGo()}.
 */
public void recalcLongestRoadETA() {
    // TODO handle ships here (different resources, etc)
    D.ebugPrintln("===  recalcLongestRoadETA for player " + playerNumber);
    final int roadETA;
    SOCBuildingSpeedEstimate bse = new SOCBuildingSpeedEstimate(player.getNumbers());
    roadETA = bse.calculateRollsFast(SOCGame.EMPTY_RESOURCES, SOCRoad.COST, 500, player.getPortFlags());
    roadsToGo = 500;
    longestRoadETA = 500;
    int longestRoadLength;
    SOCPlayer lrPlayer = game.getPlayerWithLongestRoad();
    if ((lrPlayer != null) && (lrPlayer.getPlayerNumber() == playerNumber)) {
        // /
        // / we have longest road
        // /
        // D.ebugPrintln("===  we have longest road");
        longestRoadETA = 0;
        roadsToGo = 0;
    } else if (!game.isGameOptionSet(SOCGameOption.K_SC_0RVP)) {
        if (lrPlayer == null) {
            // /
            // / no one has longest road
            // /
            longestRoadLength = Math.max(4, player.getLongestRoadLength());
        } else {
            longestRoadLength = lrPlayer.getLongestRoadLength();
        }
        Iterator<SOCLRPathData> lrPathsIter = player.getLRPaths().iterator();
        int depth;
        while (lrPathsIter.hasNext()) {
            SOCLRPathData pathData = lrPathsIter.next();
            depth = Math.min(((longestRoadLength + 1) - pathData.getLength()), player.getNumPieces(SOCPlayingPiece.ROAD));
            int minRoads = recalcLongestRoadETAAux(pathData.getBeginning(), pathData.getLength(), longestRoadLength, depth);
            roadsToGo = Math.min(minRoads, roadsToGo);
            minRoads = recalcLongestRoadETAAux(pathData.getEnd(), pathData.getLength(), longestRoadLength, depth);
            roadsToGo = Math.min(minRoads, roadsToGo);
        }
    }
    D.ebugPrintln("--- roadsToGo = " + roadsToGo);
    longestRoadETA = roadsToGo * roadETA;
}
Also used : Iterator(java.util.Iterator) SOCPlayer(soc.game.SOCPlayer) SOCLRPathData(soc.game.SOCLRPathData)

Example 22 with SOCPlayer

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

the class SOCPlayerTracker method addOurNewSettlement.

/**
 * Add one of our settlements, and newly possible pieces from it.
 * Adds a new possible city; removes conflicting possible settlements (ours or other players).
 * On the large Sea board, if this is a coastal settlement adds newly possible ships, and if
 * we've just settled a new island, newly possible roads, because the coastal settlement is
 * a roads {@literal <->} ships transition.
 *<P>
 * Newly possible roads or ships next to the settlement are expanded by calling
 * {@link #expandRoadOrShip(SOCPossibleRoad, SOCPlayer, SOCPlayer, HashMap, int)}.
 * {@link #EXPAND_LEVEL} is the basic expansion length, and ships add
 * {@link #EXPAND_LEVEL_SHIP_EXTRA} to that for crossing the sea to nearby islands.
 *<P>
 * Called in 2 different conditions:
 *<UL>
 * <LI> To track an actual (not possible) settlement that's just been placed
 * <LI> To see the effects of trying to placing a possible settlement, in a copy of the PlayerTracker
 *      ({@link #tryPutPiece(SOCPlayingPiece, SOCGame, HashMap)})
 *</UL>
 *
 * @param settlement  the new settlement
 * @param trackers    player trackers for all of the players
 */
public synchronized void addOurNewSettlement(SOCSettlement settlement, HashMap<Integer, SOCPlayerTracker> trackers) {
    // D.ebugPrintln();
    D.ebugPrintln("$$$ addOurNewSettlement : " + settlement);
    SOCBoard board = game.getBoard();
    final Integer settlementCoords = Integer.valueOf(settlement.getCoordinates());
    /**
     * add a new possible city
     */
    possibleCities.put(settlementCoords, new SOCPossibleCity(player, settlement.getCoordinates()));
    /**
     * see if the new settlement was a possible settlement in
     * the list.  if so, remove it.
     */
    SOCPossibleSettlement ps = possibleSettlements.get(settlementCoords);
    if (ps != null) {
        D.ebugPrintln("$$$ was a possible settlement");
        /**
         * copy a list of all the conflicting settlements
         */
        List<SOCPossibleSettlement> conflicts = new ArrayList<SOCPossibleSettlement>(ps.getConflicts());
        /**
         * remove the possible settlement that is now a real settlement
         */
        D.ebugPrintln("$$$ removing " + Integer.toHexString(settlement.getCoordinates()));
        possibleSettlements.remove(settlementCoords);
        removeFromNecessaryRoads(ps);
        /**
         * remove possible settlements that this one cancels out
         */
        for (SOCPossibleSettlement conflict : conflicts) {
            D.ebugPrintln("$$$ checking conflict with " + conflict.getPlayer().getPlayerNumber() + ":" + Integer.toHexString(conflict.getCoordinates()));
            SOCPlayerTracker tracker = trackers.get(Integer.valueOf(conflict.getPlayer().getPlayerNumber()));
            if (tracker != null) {
                D.ebugPrintln("$$$ removing " + Integer.toHexString(conflict.getCoordinates()));
                tracker.getPossibleSettlements().remove(Integer.valueOf(conflict.getCoordinates()));
                removeFromNecessaryRoads(conflict);
                /**
                 * remove the conflicts that this settlement made
                 */
                for (SOCPossibleSettlement otherConflict : conflict.getConflicts()) {
                    D.ebugPrintln("$$$ removing conflict " + Integer.toHexString(conflict.getCoordinates()) + " from " + Integer.toHexString(otherConflict.getCoordinates()));
                    otherConflict.removeConflict(conflict);
                }
            }
        }
    } else {
        /**
         * if the new settlement wasn't a possible settlement,
         * we still need to cancel out other players possible settlements
         */
        D.ebugPrintln("$$$ wasn't possible settlement");
        ArrayList<SOCPossibleSettlement> trash = new ArrayList<SOCPossibleSettlement>();
        List<Integer> adjNodes = board.getAdjacentNodesToNode(settlement.getCoordinates());
        Iterator<SOCPlayerTracker> trackersIter = trackers.values().iterator();
        while (trackersIter.hasNext()) {
            SOCPlayerTracker tracker = trackersIter.next();
            SOCPossibleSettlement posSet = tracker.getPossibleSettlements().get(settlementCoords);
            D.ebugPrintln("$$$ tracker for player " + tracker.getPlayer().getPlayerNumber());
            /**
             * check the node that the settlement is on
             */
            D.ebugPrintln("$$$ checking node " + Integer.toHexString(settlement.getCoordinates()));
            if (posSet != null) {
                D.ebugPrintln("$$$ trashing " + Integer.toHexString(posSet.getCoordinates()));
                trash.add(posSet);
                /**
                 * remove the conflicts that this settlement made
                 */
                for (SOCPossibleSettlement conflict : posSet.getConflicts()) {
                    D.ebugPrintln("$$$ removing conflict " + Integer.toHexString(posSet.getCoordinates()) + " from " + Integer.toHexString(conflict.getCoordinates()));
                    conflict.removeConflict(posSet);
                }
            }
            /**
             * check adjacent nodes
             */
            for (Integer adjNode : adjNodes) {
                D.ebugPrintln("$$$ checking node " + Integer.toHexString(adjNode.intValue()));
                posSet = tracker.getPossibleSettlements().get(adjNode);
                if (posSet != null) {
                    D.ebugPrintln("$$$ trashing " + Integer.toHexString(posSet.getCoordinates()));
                    trash.add(posSet);
                    /**
                     * remove the conflicts that this settlement made
                     */
                    for (SOCPossibleSettlement conflict : posSet.getConflicts()) {
                        D.ebugPrintln("$$$ removing conflict " + Integer.toHexString(posSet.getCoordinates()) + " from " + Integer.toHexString(conflict.getCoordinates()));
                        conflict.removeConflict(posSet);
                    }
                }
            }
            /**
             * take out the trash
             * (no-longer-possible settlements, roads that support it)
             */
            D.ebugPrintln("$$$ removing trash for " + tracker.getPlayer().getPlayerNumber());
            for (SOCPossibleSettlement pset : trash) {
                D.ebugPrintln("$$$ removing " + Integer.toHexString(pset.getCoordinates()) + " owned by " + pset.getPlayer().getPlayerNumber());
                tracker.getPossibleSettlements().remove(Integer.valueOf(pset.getCoordinates()));
                removeFromNecessaryRoads(pset);
            }
            trash.clear();
        }
    }
    /**
     * Add possible road-ship transitions made possible by the new settlement.
     * Normally a new settlement placement doesn't need to add possible roads or ships,
     * because each road/ship placement adds possibles past the new far end of the route
     * in addOurNewRoadOrShip.
     */
    if (board instanceof SOCBoardLarge) {
        ArrayList<SOCPossibleRoad> roadsToExpand = null;
        /**
         * Only add new possible roads if we're on a new island
         * (that is, the newly placed settlement has no adjacent roads already).
         * Coastal ships/roads may still be added even if settleAlreadyHasRoad.
         */
        boolean settleAlreadyHasRoad = false;
        ArrayList<SOCPossibleRoad> possibleNewIslandRoads = null;
        final List<Integer> adjacEdges = board.getAdjacentEdgesToNode(settlementCoords);
        // First, loop to check for settleAlreadyHasRoad
        for (Integer edge : adjacEdges) {
            if (possibleRoads.get(edge) != null)
                // already a possible road or ship here
                continue;
            SOCRoad road = board.roadAtEdge(edge);
            if ((road != null) && road.isRoadNotShip()) {
                settleAlreadyHasRoad = true;
                break;
            }
        }
        // Now, possibly add new roads/ships/coastals
        for (Integer edge : adjacEdges) {
            // TODO remove these debug prints soon
            // System.err.println("L1348: examine edge 0x"
            // + Integer.toHexString(edge) + " for placed settle 0x"
            // + Integer.toHexString(settlementCoords));
            SOCPossibleRoad pRoad = possibleRoads.get(edge);
            if (pRoad != null) {
                // already a possible road or ship
                continue;
            }
            if (board.roadAtEdge(edge) != null) {
                // not new, something's already there
                continue;
            }
            if (player.isPotentialRoad(edge)) {
                // Add newly possible roads from settlement placement.
                // Probably won't need to happen (usually added in addOurNewRoadOrShip, see newPossibleRoads)
                // but could on a new island's first settlement
                final boolean isCoastline = player.isPotentialShip(edge);
                if (settleAlreadyHasRoad && !isCoastline)
                    continue;
                if (possibleNewIslandRoads == null)
                    possibleNewIslandRoads = new ArrayList<SOCPossibleRoad>();
                possibleNewIslandRoads.add((isCoastline) ? new SOCPossibleShip(player, edge, true, null) : new SOCPossibleRoad(player, edge, null));
                if (isCoastline)
                    System.err.println("L1675: " + toString() + ": new PossibleShip(true) at 0x" + Integer.toHexString(edge));
            } else if (player.isPotentialShip(edge)) {
                // A way out to a new island
                SOCPossibleShip newPS = new SOCPossibleShip(player, edge, false, null);
                possibleRoads.put(edge, newPS);
                System.err.println("L1685: " + toString() + ": new PossibleShip(false) at 0x" + Integer.toHexString(edge) + " from coastal settle 0x" + Integer.toHexString(settlementCoords));
                if (roadsToExpand == null)
                    roadsToExpand = new ArrayList<SOCPossibleRoad>();
                roadsToExpand.add(newPS);
                newPS.setExpandedFlag();
            }
        }
        if ((possibleNewIslandRoads != null) && !game.isInitialPlacement()) {
            // (Make sure this isn't initial placement, where nothing has adjacent roads)
            for (SOCPossibleRoad pr : possibleNewIslandRoads) {
                possibleRoads.put(Integer.valueOf(pr.getCoordinates()), pr);
                System.err.println("L1396: new possible road at edge 0x" + Integer.toHexString(pr.getCoordinates()) + " from coastal settle 0x" + Integer.toHexString(settlementCoords));
                if (roadsToExpand == null)
                    roadsToExpand = new ArrayList<SOCPossibleRoad>();
                roadsToExpand.add(pr);
                pr.setExpandedFlag();
            }
        }
        if (roadsToExpand != null) {
            // 
            // expand possible ships/roads that we've added
            // 
            SOCPlayer dummy = new SOCPlayer(player);
            for (SOCPossibleRoad expandPR : roadsToExpand) {
                final int expand = EXPAND_LEVEL + (expandPR.isRoadNotShip() ? 0 : EXPAND_LEVEL_SHIP_EXTRA);
                expandRoadOrShip(expandPR, player, dummy, trackers, expand);
            }
            dummy.destroyPlayer();
        }
    }
}
Also used : SOCBoardLarge(soc.game.SOCBoardLarge) SOCBoard(soc.game.SOCBoard) ArrayList(java.util.ArrayList) SOCRoad(soc.game.SOCRoad) SOCPlayer(soc.game.SOCPlayer)

Example 23 with SOCPlayer

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

the class SOCBoardPanel method drawBoard.

/**
 * Draw the whole board, including pieces and tooltip ({@link #hilight}, {@link #hoverTip}) if applicable.
 * The basic board without pieces is drawn just once, then buffered.
 * If the board layout changes (at start of game, for example),
 * call {@link #flushBoardLayoutAndRepaint()} to clear the buffered copy.
 *
 * @see #drawBoardEmpty(Graphics)
 */
private void drawBoard(Graphics g) {
    Image ebb = emptyBoardBuffer;
    if (scaledMissedImage || ebb == null) {
        if (ebb == null) {
            ebb = createImage(scaledPanelW, scaledPanelH);
            emptyBoardBuffer = ebb;
        }
        drawnEmptyAt = System.currentTimeMillis();
        // drawBoardEmpty, drawHex will set this flag if missed
        scaledMissedImage = false;
        drawBoardEmpty(ebb.getGraphics());
        ebb.flush();
        if (scaledMissedImage && (scaledAt != 0) && (RESCALE_MAX_RETRY_MS < (drawnEmptyAt - scaledAt)))
            // eventually give up scaling it
            scaledMissedImage = false;
    }
    // draw ebb from local variable, not emptyBoardBuffer field, to avoid occasional NPE
    g.setPaintMode();
    g.drawImage(ebb, 0, 0, this);
    // ask for antialiasing if available
    if (g instanceof Graphics2D)
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final boolean xlat = (panelMarginX != 0) || (panelMarginY != 0);
    if (xlat)
        g.translate(panelMarginX, panelMarginY);
    final int gameState = game.getGameState();
    if (board.getRobberHex() != -1) {
        drawRobber(g, board.getRobberHex(), (gameState != SOCGame.PLACING_ROBBER), true);
    }
    if (board.getPreviousRobberHex() != -1) {
        drawRobber(g, board.getPreviousRobberHex(), (gameState != SOCGame.PLACING_ROBBER), false);
    }
    if (isLargeBoard) {
        int hex = ((SOCBoardLarge) board).getPirateHex();
        if (hex > 0) {
            drawRoadOrShip(g, hex, -2, (gameState == SOCGame.PLACING_PIRATE), false, false);
        }
        hex = ((SOCBoardLarge) board).getPreviousPirateHex();
        if (hex > 0) {
            drawRoadOrShip(g, hex, -3, (gameState == SOCGame.PLACING_PIRATE), false, false);
        }
    }
    /**
     * draw the roads and ships
     */
    if (!game.isGameOptionSet(SOCGameOption.K_SC_PIRI)) {
        for (SOCRoad r : board.getRoads()) {
            drawRoadOrShip(g, r.getCoordinates(), r.getPlayerNumber(), false, !(r instanceof SOCShip), false);
        }
    } else {
        for (int pn = 0; pn < game.maxPlayers; ++pn) {
            final SOCPlayer pl = game.getPlayer(pn);
            // count warships here, for efficiency, instead of calling SOCGame.isShipWarship for each one
            int numWarships = pl.getNumWarships();
            for (SOCRoad r : pl.getRoads()) {
                final boolean isShip = (r instanceof SOCShip);
                final boolean isWarship = isShip && (numWarships > 0);
                drawRoadOrShip(g, r.getCoordinates(), pn, false, !isShip, isWarship);
                if (isWarship)
                    // this works since warships begin with player's 1st-placed ship in getRoads()
                    --numWarships;
            }
            /**
             * draw the player's fortress, if any
             */
            SOCFortress fo = pl.getFortress();
            if (fo != null)
                drawFortress(g, fo, pn, false);
        }
    }
    /**
     * draw the settlements
     */
    for (SOCSettlement s : board.getSettlements()) {
        drawSettlement(g, s.getCoordinates(), s.getPlayerNumber(), false, false);
    }
    /**
     * draw the cities
     */
    for (SOCCity c : board.getCities()) {
        drawCity(g, c.getCoordinates(), c.getPlayerNumber(), false);
    }
    if (xlat)
        g.translate(-panelMarginX, -panelMarginY);
    /**
     * draw the current-player arrow after ("above") pieces,
     * but below any hilighted piece, in case of overlap at
     * edge of board. More likely on 6-player board for the
     * two players whose handpanels are vertically centered.
     */
    if (gameState != SOCGame.NEW) {
        drawArrow(g, game.getCurrentPlayerNumber(), game.getCurrentDice());
    }
    if (player != null) {
        if (xlat)
            g.translate(panelMarginX, panelMarginY);
        /**
         * Draw the hilight when in interactive mode;
         * No hilight when null player (before game started).
         * The "hovering" road/settlement/city are separately painted
         * in {@link soc.client.SOCBoardPanel.BoardToolTip#paint()}.
         */
        switch(mode) {
            case MOVE_SHIP:
                if (moveShip_fromEdge != 0)
                    drawRoadOrShip(g, moveShip_fromEdge, -1, false, false, moveShip_isWarship);
            case PLACE_ROAD:
            case PLACE_INIT_ROAD:
            case PLACE_FREE_ROAD_OR_SHIP:
                if (hilight != 0) {
                    drawRoadOrShip(g, hilight, playerNumber, true, !hilightIsShip, (moveShip_isWarship && (moveShip_fromEdge != 0)));
                }
                break;
            case PLACE_SETTLEMENT:
            case PLACE_INIT_SETTLEMENT:
                if (hilight > 0) {
                    drawSettlement(g, hilight, playerNumber, true, false);
                }
                break;
            case PLACE_CITY:
                if (hilight > 0) {
                    drawCity(g, hilight, playerNumber, true);
                }
                break;
            case PLACE_SHIP:
                if (hilight > 0) {
                    drawRoadOrShip(g, hilight, playerNumber, true, false, false);
                }
                break;
            case CONSIDER_LM_SETTLEMENT:
            case CONSIDER_LT_SETTLEMENT:
                if (hilight > 0) {
                    drawSettlement(g, hilight, otherPlayer.getPlayerNumber(), true, false);
                }
                break;
            case CONSIDER_LM_ROAD:
            case CONSIDER_LT_ROAD:
                if (hilight != 0) {
                    drawRoadOrShip(g, hilight, otherPlayer.getPlayerNumber(), false, true, false);
                }
                break;
            case CONSIDER_LM_CITY:
            case CONSIDER_LT_CITY:
                if (hilight > 0) {
                    drawCity(g, hilight, otherPlayer.getPlayerNumber(), true);
                }
                break;
            case PLACE_ROBBER:
                if (hilight > 0) {
                    drawRobber(g, hilight, true, true);
                }
                break;
            case PLACE_PIRATE:
                if (hilight > 0) {
                    drawRoadOrShip(g, hilight, -2, false, false, false);
                }
                break;
            case SC_FTRI_PLACE_PORT:
                drawBoard_SC_FTRI_placePort(g);
                break;
        }
        if (xlat)
            g.translate(-panelMarginX, -panelMarginY);
    }
    if (superText1 != null) {
        drawSuperText(g);
    }
    if (superTextTop != null) {
        drawSuperTextTop(g);
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCBoardLarge(soc.game.SOCBoardLarge) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCFortress(soc.game.SOCFortress) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) SOCRoad(soc.game.SOCRoad) Graphics2D(java.awt.Graphics2D)

Example 24 with SOCPlayer

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

the class SOCBoardPanel method mouseClicked.

/**
 * DOCUMENT ME!
 *
 * @param evt DOCUMENT ME!
 */
public void mouseClicked(MouseEvent evt) {
    try {
        int x = evt.getX();
        int y = evt.getY();
        if (evt.isPopupTrigger()) {
            popupMenuSystime = evt.getWhen();
            evt.consume();
            doBoardMenuPopup(x, y);
            // <--- Pop up menu, nothing else to do ---
            return;
        }
        if (evt.getWhen() < (popupMenuSystime + POPUP_MENU_IGNORE_MS)) {
            // <--- Ignore click: too soon after popup click ---
            return;
        }
        boolean tempChangedMode = false;
        if ((mode == NONE) && hoverTip.isVisible()) {
            if (game.isDebugFreePlacement()) {
                if (hoverTip.hoverSettlementID != 0) {
                    hilight = hoverTip.hoverSettlementID;
                    hilightIsShip = false;
                    mode = PLACE_SETTLEMENT;
                    tempChangedMode = true;
                } else if (hoverTip.hoverCityID != 0) {
                    hilight = hoverTip.hoverCityID;
                    hilightIsShip = false;
                    mode = PLACE_CITY;
                    tempChangedMode = true;
                } else if (hoverTip.hoverRoadID != 0) {
                    hilight = hoverTip.hoverRoadID;
                    hilightIsShip = false;
                    mode = PLACE_ROAD;
                    tempChangedMode = true;
                } else if (hoverTip.hoverShipID != 0) {
                    hilight = hoverTip.hoverShipID;
                    hilightIsShip = true;
                    mode = PLACE_SHIP;
                    tempChangedMode = true;
                }
            } else if (((evt.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) && (player != null) && (game.getCurrentPlayerNumber() == playerNumber) && (player.getPublicVP() == 2) && (hintShownCount_RightClickToBuild < 2)) {
                // To help during the start of the game, display a hint message
                // reminding new users to right-click to build (OSX: control-click).
                // Show it at most twice to avoid annoying the user.
                ++hintShownCount_RightClickToBuild;
                final String prompt = (SOCPlayerClient.isJavaOnOSX) ? "board.popup.hint_build_click.osx" : // "To build pieces, hold Control while clicking the build location."
                "board.popup.hint_build_click";
                // "To build pieces, right-click the build location."
                NotifyDialog.createAndShow(playerInterface.getGameDisplay(), playerInterface, "\n" + strings.get(prompt), null, true);
            // start prompt with \n to prevent it being a lengthy popup-dialog title
            }
        }
        if ((hilight != 0) && (player != null) && (x == ptrOldX) && (y == ptrOldY)) {
            SOCPlayerClient client = playerInterface.getClient();
            switch(mode) {
                case NONE:
                    break;
                case TURN_STARTING:
                    break;
                case PLACE_INIT_ROAD:
                case PLACE_ROAD:
                case PLACE_FREE_ROAD_OR_SHIP:
                    if (hilight == -1)
                        // Road on edge 0x00
                        hilight = 0;
                    if (player.isPotentialRoad(hilight) && !hilightIsShip) {
                        client.getGameManager().putPiece(game, new SOCRoad(player, hilight, board));
                        // Now that we've placed, clear the mode and the hilight.
                        clearModeAndHilight(SOCPlayingPiece.ROAD);
                        if (tempChangedMode)
                            hoverTip.hideHoverAndPieces();
                    } else if (// checks isPotentialShip, pirate ship
                    game.canPlaceShip(player, hilight)) {
                        if (game.isGameOptionSet(SOCGameOption.K_SC_FTRI) && ((SOCBoardLarge) board).canRemovePort(hilight)) {
                            java.awt.EventQueue.invokeLater(new ConfirmPlaceShipDialog(hilight, false, -1));
                        } else {
                            client.getGameManager().putPiece(game, new SOCShip(player, hilight, board));
                            // Now that we've placed, clear the mode and the hilight.
                            clearModeAndHilight(SOCPlayingPiece.SHIP);
                        }
                        if (tempChangedMode)
                            hoverTip.hideHoverAndPieces();
                    }
                    break;
                case MOVE_SHIP:
                    // check and move ship to hilight from fromEdge;
                    // also sets moveShip_fromEdge = 0, calls clearModeAndHilight.
                    moveShip_toEdge = hilight;
                    tryMoveShipToEdge();
                    break;
                case PLACE_INIT_SETTLEMENT:
                    if (playerNumber == playerInterface.getClientPlayerNumber()) {
                        initSettlementNode = hilight;
                    }
                case PLACE_SETTLEMENT:
                    if (player.canPlaceSettlement(hilight)) {
                        client.getGameManager().putPiece(game, new SOCSettlement(player, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.SETTLEMENT);
                        if (tempChangedMode)
                            hoverTip.hideHoverAndPieces();
                    }
                    break;
                case PLACE_CITY:
                    if (player.isPotentialCity(hilight)) {
                        client.getGameManager().putPiece(game, new SOCCity(player, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.CITY);
                        if (tempChangedMode)
                            hoverTip.hideHoverAndPieces();
                    }
                    break;
                case PLACE_SHIP:
                    if (// checks isPotentialShip, pirate ship
                    game.canPlaceShip(player, hilight)) {
                        if (game.isGameOptionSet(SOCGameOption.K_SC_FTRI) && ((SOCBoardLarge) board).canRemovePort(hilight)) {
                            java.awt.EventQueue.invokeLater(new ConfirmPlaceShipDialog(hilight, false, -1));
                        } else {
                            client.getGameManager().putPiece(game, new SOCShip(player, hilight, board));
                            clearModeAndHilight(SOCPlayingPiece.SHIP);
                        }
                        if (tempChangedMode)
                            hoverTip.hideHoverAndPieces();
                    }
                    break;
                case PLACE_ROBBER:
                    if (hilight != board.getRobberHex()) {
                        // do we have an adjacent settlement/city?
                        boolean cliAdjacent = false;
                        {
                            for (SOCPlayer pl : game.getPlayersOnHex(hilight)) {
                                if (pl.getPlayerNumber() == playerNumber) {
                                    cliAdjacent = true;
                                    break;
                                }
                            }
                        }
                        if (cliAdjacent) {
                            // ask player to confirm first
                            java.awt.EventQueue.invokeLater(new MoveRobberConfirmDialog(player, hilight));
                        } else {
                            // ask server to move it
                            client.getGameManager().moveRobber(game, player, hilight);
                            clearModeAndHilight(-1);
                        }
                    }
                    break;
                case PLACE_PIRATE:
                    if (hilight != ((SOCBoardLarge) board).getPirateHex()) {
                        // do we have an adjacent ship?
                        boolean cliAdjacent = false;
                        {
                            for (SOCPlayer pl : game.getPlayersShipsOnHex(hilight)) {
                                if (pl.getPlayerNumber() == playerNumber) {
                                    cliAdjacent = true;
                                    break;
                                }
                            }
                        }
                        if (cliAdjacent) {
                            // ask player to confirm first
                            java.awt.EventQueue.invokeLater(new MoveRobberConfirmDialog(player, -hilight));
                        } else {
                            // ask server to move it
                            client.getGameManager().moveRobber(game, player, -hilight);
                            clearModeAndHilight(-1);
                        }
                    }
                    break;
                case SC_FTRI_PLACE_PORT:
                    if (hilight != 0) {
                        int edge = hilight;
                        if (edge == -1)
                            edge = 0;
                        if (game.canPlacePort(player, edge)) {
                            // Ask server to place here.
                            client.getGameManager().sendSimpleRequest(player, SOCSimpleRequest.TRADE_PORT_PLACE, hilight, 0);
                            hilight = 0;
                        }
                    }
                    break;
                case CONSIDER_LM_SETTLEMENT:
                    if (otherPlayer.canPlaceSettlement(hilight)) {
                        client.getGameManager().considerMove(game, otherPlayer.getName(), new SOCSettlement(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.SETTLEMENT);
                    }
                    break;
                case CONSIDER_LM_ROAD:
                    if (otherPlayer.isPotentialRoad(hilight)) {
                        client.getGameManager().considerMove(game, otherPlayer.getName(), new SOCRoad(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.ROAD);
                    }
                    break;
                case CONSIDER_LM_CITY:
                    if (otherPlayer.isPotentialCity(hilight)) {
                        client.getGameManager().considerMove(game, otherPlayer.getName(), new SOCCity(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.CITY);
                    }
                    break;
                case CONSIDER_LT_SETTLEMENT:
                    if (otherPlayer.canPlaceSettlement(hilight)) {
                        client.getGameManager().considerTarget(game, otherPlayer.getName(), new SOCSettlement(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.SETTLEMENT);
                    }
                    break;
                case CONSIDER_LT_ROAD:
                    if (otherPlayer.isPotentialRoad(hilight)) {
                        client.getGameManager().considerTarget(game, otherPlayer.getName(), new SOCRoad(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.ROAD);
                    }
                    break;
                case CONSIDER_LT_CITY:
                    if (otherPlayer.isPotentialCity(hilight)) {
                        client.getGameManager().considerTarget(game, otherPlayer.getName(), new SOCCity(otherPlayer, hilight, board));
                        clearModeAndHilight(SOCPlayingPiece.CITY);
                    }
                    break;
            }
        } else if ((player != null) && ((game.getCurrentPlayerNumber() == playerNumber) || game.isDebugFreePlacement())) {
            // No hilight. But, they clicked the board, expecting something.
            // It's possible the mode is incorrect.
            // Update and wait for the next click.
            updateMode();
            ptrOldX = 0;
            ptrOldY = 0;
            // mouseMoved will establish hilight using click's x,y
            mouseMoved(evt);
        }
        evt.consume();
        if (tempChangedMode)
            mode = NONE;
    } catch (Throwable th) {
        playerInterface.chatPrintStackTrace(th);
    }
}
Also used : SOCBoardLarge(soc.game.SOCBoardLarge) SOCShip(soc.game.SOCShip) SOCRoad(soc.game.SOCRoad) SOCSettlement(soc.game.SOCSettlement) SOCCity(soc.game.SOCCity) SOCPlayer(soc.game.SOCPlayer)

Example 25 with SOCPlayer

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

the class SOCBoardPanel method drawBoardEmpty_drawDebugShowPotentials.

/**
 * If any bit in {@link #debugShowPotentials}[] is set, besides 8,
 * draw it on the board. Shows potential/legal placement locations for player 0.
 * (<tt>debugShowPotentials[8]</tt> is drawn in the per-hex loop
 *  of {@link #drawBoardEmpty(Graphics)}).
 *<P>
 * <b>Note:</b> Currently implemented only for {@link #isLargeBoard} only for now (TODO).
 * @since 2.0.00
 * @throws IllegalStateException if ! isLargeBoard; temporary restriction
 */
private void drawBoardEmpty_drawDebugShowPotentials(Graphics g) throws IllegalStateException {
    if (!isLargeBoard)
        throw new IllegalStateException("not supported yet");
    final SOCPlayer pl = game.getPlayer(0);
    final int bw = board.getBoardWidth();
    if (debugShowPotentials[2]) {
        final int bh = board.getBoardHeight();
        int w = scaleToActual(halfdeltaX * bw), h = scaleToActual(halfdeltaY * bh + HEXY_OFF_SLOPE_HEIGHT);
        int y = scaleToActual(halfdeltaY);
        g.setColor(Color.YELLOW);
        g.drawRect(0, y, w, h);
        g.drawRect(1, y + 1, w - 2, h - 2);
    }
    // Iterate over all nodes for:
    // 1,5: settlements: squares (Legal yellow, potential green)
    // 2,6: cities: larger squares (potential green; there is no legal set)
    // 9: nodes on land: red round rects
    final int SC_3 = scaleToActual(3), SC_10 = scaleToActual(10), SC_12 = scaleToActual(12), SC_14 = scaleToActual(14), SC_18 = scaleToActual(18);
    for (int r = 0, y = halfdeltaY + (HEXY_OFF_SLOPE_HEIGHT / 2); r <= board.getBoardHeight(); ++r, y += halfdeltaY) {
        final int rshift = (r << 8);
        for (int c = 0, x = 0; c <= bw; ++c, x += halfdeltaX) {
            final int nodeCoord = rshift | c;
            // 1,5: settlements
            if (debugShowPotentials[1] && pl.isLegalSettlement(nodeCoord)) {
                g.setColor(Color.YELLOW);
                g.drawRect(scaleToActual(x - 6), scaleToActual(y - 6), SC_12, SC_12);
            }
            if (debugShowPotentials[5] && pl.isPotentialSettlement(nodeCoord)) {
                g.setColor(Color.GREEN);
                g.drawRect(scaleToActual(x - 7), scaleToActual(y - 7), SC_14, SC_14);
            }
            // 6: cities (potential only)
            if (debugShowPotentials[6] && pl.isPotentialCity(nodeCoord)) {
                g.setColor(Color.GREEN);
                g.drawRect(scaleToActual(x - 9), scaleToActual(y - 9), SC_18, SC_18);
            }
            // 9: nodes on land
            if (debugShowPotentials[9] && board.isNodeOnLand(nodeCoord)) {
                g.setColor(Color.RED);
                g.drawRoundRect(scaleToActual(x - 5), scaleToActual(y - 5), SC_10, SC_10, SC_3, SC_3);
            }
        }
    }
    for (int r = 0, y = halfdeltaY + (HEXY_OFF_SLOPE_HEIGHT / 2); r <= board.getBoardHeight(); ++r, y += halfdeltaY) {
        final int rshift = (r << 8);
        final boolean edgeIsVert = ((r % 2) == 1);
        int x = (edgeIsVert) ? 0 : (halfdeltaX / 2);
        for (int c = 0; c <= bw; ++c, x += halfdeltaX) {
            final int edgeCoord = rshift | c;
            // 3,7: ships - diamonds
            if (debugShowPotentials[3] && pl.isLegalShip(edgeCoord)) {
                g.setColor(Color.YELLOW);
                g.drawLine(scaleToActual(x - 4), scaleToActual(y), scaleToActual(x), scaleToActual(y - 4));
                g.drawLine(scaleToActual(x), scaleToActual(y - 4), scaleToActual(x + 4), scaleToActual(y));
                g.drawLine(scaleToActual(x + 4), scaleToActual(y), scaleToActual(x), scaleToActual(y + 4));
                g.drawLine(scaleToActual(x), scaleToActual(y + 4), scaleToActual(x - 4), scaleToActual(y));
            }
            if (debugShowPotentials[7] && pl.isPotentialShip(edgeCoord)) {
                g.setColor(Color.GREEN);
                g.drawLine(scaleToActual(x - 6), scaleToActual(y), scaleToActual(x), scaleToActual(y - 6));
                g.drawLine(scaleToActual(x), scaleToActual(y - 6), scaleToActual(x + 6), scaleToActual(y));
                g.drawLine(scaleToActual(x + 6), scaleToActual(y), scaleToActual(x), scaleToActual(y + 6));
                g.drawLine(scaleToActual(x), scaleToActual(y + 6), scaleToActual(x - 6), scaleToActual(y));
            }
            // 0,4: roads - parallel lines
            if (debugShowPotentials[0] && pl.isLegalRoad(edgeCoord)) {
                drawBoardEmpty_drawDebugShowPotentialRoad(g, x, y, r, c, edgeIsVert, Color.YELLOW, 4);
            }
            if (debugShowPotentials[4] && pl.isPotentialRoad(edgeCoord))
                drawBoardEmpty_drawDebugShowPotentialRoad(g, x, y, r, c, edgeIsVert, Color.GREEN, 6);
        }
    }
}
Also used : SOCPlayer(soc.game.SOCPlayer)

Aggregations

SOCPlayer (soc.game.SOCPlayer)61 SOCGame (soc.game.SOCGame)17 SOCShip (soc.game.SOCShip)12 SOCSettlement (soc.game.SOCSettlement)11 SOCRoad (soc.game.SOCRoad)9 SOCResourceSet (soc.game.SOCResourceSet)8 SOCCity (soc.game.SOCCity)7 SOCBoardLarge (soc.game.SOCBoardLarge)6 SOCFortress (soc.game.SOCFortress)5 ArrayList (java.util.ArrayList)4 SOCBoard (soc.game.SOCBoard)4 Connection (soc.server.genericServer.Connection)4 SOCInventoryItem (soc.game.SOCInventoryItem)3 SOCVillage (soc.game.SOCVillage)3 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 Stack (java.util.Stack)2 SOCLRPathData (soc.game.SOCLRPathData)2 SOCPlayingPiece (soc.game.SOCPlayingPiece)2 SOCSpecialItem (soc.game.SOCSpecialItem)2