Search in sources :

Example 6 with SOCFortress

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

the class SOCDisplaylessPlayerClient method handlePIECEVALUE.

/**
 * Update a village piece's value on the board (cloth remaining) in _SC_CLVI,
 * or a pirate fortress's strength in _SC_PIRI.
 * @since 2.0.00
 */
protected void handlePIECEVALUE(final SOCPieceValue mes) {
    final String gaName = mes.getGame();
    SOCGame ga = games.get(gaName);
    if (ga == null)
        // Not one of our games
        return;
    if (!ga.hasSeaBoard)
        // should not happen
        return;
    final int coord = mes.getParam1();
    final int pv = mes.getParam2();
    if (ga.isGameOptionSet(SOCGameOption.K_SC_CLVI)) {
        SOCVillage vi = ((SOCBoardLarge) (ga.getBoard())).getVillageAtNode(coord);
        if (vi != null)
            vi.setCloth(pv);
    } else if (ga.isGameOptionSet(SOCGameOption.K_SC_PIRI)) {
        SOCFortress fort = ga.getFortress(coord);
        if (fort != null)
            fort.setStrength(pv);
    }
}
Also used : SOCVillage(soc.game.SOCVillage) SOCBoardLarge(soc.game.SOCBoardLarge) SOCFortress(soc.game.SOCFortress) SOCGame(soc.game.SOCGame)

Example 7 with SOCFortress

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

the class SOCRobotDM method scenarioGameStrategyPlan_SC_PIRI_buildNextShip.

/**
 * If possible, calculate where our next ship would be placed, and add it to {@link #buildingPlan}.
 * Assumes our player's {@link SOCPlayer#getFortress()} is west of all boats we've already placed.
 * If our line of ships has reached the fortress per {@link SOCPlayer#getMostRecentShip()},
 * nothing to do: That goal is complete.
 * @return True if next ship is possible and was added to {@link #buildingPlan}
 * @since 2.0.00
 */
private final boolean scenarioGameStrategyPlan_SC_PIRI_buildNextShip() {
    SOCShip prevShip = ourPlayerData.getMostRecentShip();
    if (prevShip == null)
        // player starts with 1 ship, so should never be null
        return false;
    final int fortressNode;
    {
        final SOCFortress fo = ourPlayerData.getFortress();
        if (fo == null)
            // already defeated it
            return false;
        fortressNode = fo.getCoordinates();
    }
    final int prevShipNode;
    {
        final int[] nodes = prevShip.getAdjacentNodes();
        final int c0 = nodes[0] & 0xFF, c1 = nodes[1] & 0xFF;
        if (c0 < c1)
            prevShipNode = nodes[0];
        else if (c1 < c0)
            prevShipNode = nodes[1];
        else {
            // prevShip goes north-south; check its node rows vs fortress row
            final int r0 = nodes[0] >> 8, r1 = nodes[1] >> 8, rFort = fortressNode >> 8;
            if (Math.abs(rFort - r0) < Math.abs(rFort - r1))
                prevShipNode = nodes[0];
            else
                prevShipNode = nodes[1];
        }
    }
    if (prevShipNode == fortressNode) {
        return false;
    }
    // Get the player's ship path towards fortressNode from prevShip.
    // We need to head west, possibly north or south.
    final HashSet<Integer> lse = ourPlayerData.getRestrictedLegalShips();
    if (lse == null)
        // null lse should not occur in _SC_PIRI
        return false;
    // Need 1 or 2 edges that are in lse and aren't prevShipEdge,
    // and the edge's far node is either further west than prevShipNode,
    // or is vertical and takes us closer north or south to the fortress.
    int edge1 = -9, edge2 = -9;
    final SOCBoard board = game.getBoard();
    final int prevShipEdge = prevShip.getCoordinates();
    int[] nextPossiEdges = board.getAdjacentEdgesToNode_arr(prevShipNode);
    for (int i = 0; i < nextPossiEdges.length; ++i) {
        final int edge = nextPossiEdges[i];
        if ((edge == -9) || (edge == prevShipEdge) || !lse.contains(Integer.valueOf(edge)))
            continue;
        // be sure this edge takes us towards fortressNode
        final int farNode = board.getAdjacentNodeFarEndOfEdge(edge, prevShipNode);
        final int cShip = prevShipNode & 0xFF, cEdge = farNode & 0xFF;
        if (cEdge > cShip) {
            // farNode is east, not west
            continue;
        } else if (cEdge == cShip) {
            final int rShip = prevShipNode >> 8, rEdge = farNode >> 8, rFort = fortressNode >> 8;
            if (Math.abs(rFort - rEdge) > Math.abs(rFort - rShip))
                // farNode isn't closer to fortress
                continue;
        }
        // OK
        if (edge1 == -9)
            edge1 = edge;
        else
            edge2 = edge;
    }
    if (edge1 == -9)
        // happens if we've built ships out to fortressNode already
        return false;
    final int newEdge;
    if ((edge2 == -9) || (Math.random() < 0.5))
        newEdge = edge1;
    else
        newEdge = edge2;
    buildingPlan.add(new SOCPossibleShip(ourPlayerData, newEdge, false, null));
    System.err.println("L2112 ** " + ourPlayerData.getName() + ": Planned possible ship at 0x" + Integer.toHexString(newEdge) + " towards fortress");
    return true;
}
Also used : SOCBoard(soc.game.SOCBoard) SOCShip(soc.game.SOCShip) SOCFortress(soc.game.SOCFortress)

Example 8 with SOCFortress

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

the class SOCPlayerInterface method updateAtPutPiece.

/**
 * Handle updates after putting a piece on the board,
 * or moving a ship that was already placed.
 * Place or move the piece within our {@link SOCGame}
 * and visually on our {@link SOCBoardPanel}.
 *
 * @param mesPn  The piece's player number
 * @param coord  The piece's coordinate.  If <tt>isMove</tt>, the coordinate to move <em>from</em>.
 * @param pieceType  Piece type, like {@link SOCPlayingPiece#CITY}
 * @param isMove   If true, it's a move, not a new placement; valid only for ships.
 * @param moveToCoord  If <tt>isMove</tt>, the coordinate to move <em>to</em>.  Otherwise ignored.
 *
 * @see #updateAtPiecesChanged()
 * @since 2.0.00
 */
public void updateAtPutPiece(final int mesPn, final int coord, final int pieceType, final boolean isMove, final int moveToCoord) {
    // TODO consider more effic way for flushBoardLayoutAndRepaint, without the =null
    final SOCPlayer pl = (pieceType != SOCPlayingPiece.VILLAGE) ? game.getPlayer(mesPn) : null;
    final SOCPlayer oldLongestRoadPlayer = game.getPlayerWithLongestRoad();
    final SOCHandPanel mesHp = (pieceType != SOCPlayingPiece.VILLAGE) ? getPlayerHandPanel(mesPn) : null;
    final boolean[] debugShowPotentials = boardPanel.debugShowPotentials;
    final SOCPlayingPiece pp;
    switch(pieceType) {
        case SOCPlayingPiece.ROAD:
            pp = new SOCRoad(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Road);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.SETTLEMENT:
            pp = new SOCSettlement(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Settlement);
            /**
             * if this is the second initial settlement, then update the resource display
             */
            mesHp.updateValue(PlayerClientListener.UpdateType.ResourceTotalAndDetails);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7] || debugShowPotentials[6])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.CITY:
            pp = new SOCCity(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Settlement);
            mesHp.updateValue(PlayerClientListener.UpdateType.City);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7] || debugShowPotentials[6])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.SHIP:
            pp = new SOCShip(pl, coord, null);
            if (!isMove) {
                game.putPiece(pp);
                mesHp.updateValue(PlayerClientListener.UpdateType.Ship);
            } else {
                game.moveShip((SOCShip) pp, moveToCoord);
                if (mesHp == clientHand)
                    // just in case; it probably wasn't enabled
                    mesHp.disableBankUndoButton();
            }
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.VILLAGE:
            // no need to refresh boardPanel after receiving each village
            pp = new SOCVillage(coord, game.getBoard());
            game.putPiece(pp);
            // <--- Early return: Piece is part of board initial layout, not player info ---
            return;
        case SOCPlayingPiece.FORTRESS:
            pp = new SOCFortress(pl, coord, game.getBoard());
            game.putPiece(pp);
            // <--- Early return: Piece is part of board initial layout, not added during game ---
            return;
        default:
            chatPrintDebug("* Unknown piece type " + pieceType + " at coord 0x" + Integer.toHexString(coord));
            // <--- Early return ---
            return;
    }
    mesHp.updateValue(PlayerClientListener.UpdateType.VictoryPoints);
    boardPanel.repaint();
    buildingPanel.updateButtonStatus();
    if (game.isDebugFreePlacement() && game.isInitialPlacement())
        // update here, since gamestate doesn't change to trigger update
        boardPanel.updateMode();
    if (hasCalledBegan && (game.getGameState() >= SOCGame.START1A))
        playSound(SOUND_PUT_PIECE);
    /**
     * Check for and announce change in longest road; update all players' victory points.
     */
    SOCPlayer newLongestRoadPlayer = game.getPlayerWithLongestRoad();
    if (newLongestRoadPlayer != oldLongestRoadPlayer) {
        updateLongestLargest(true, oldLongestRoadPlayer, newLongestRoadPlayer);
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCVillage(soc.game.SOCVillage) SOCCity(soc.game.SOCCity) SOCPlayingPiece(soc.game.SOCPlayingPiece) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCFortress(soc.game.SOCFortress) SOCRoad(soc.game.SOCRoad)

Example 9 with SOCFortress

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

the class SOCDisplaylessPlayerClient method handlePUTPIECE.

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

Example 10 with SOCFortress

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

the class SOCBoardPanel method pieceValueUpdated.

/**
 * A playing piece's value was updated:
 * {@code _SC_CLVI} village cloth count, or
 * {@code _SC_PIRI} pirate fortress strength.
 * Repaint that piece (if needed) on the board.
 * @param piece  Piece that was updated, includes its new value
 * @since 2.0.00
 */
public void pieceValueUpdated(final SOCPlayingPiece piece) {
    if (piece instanceof SOCFortress) {
        final SOCFortress fort = (SOCFortress) piece;
        if ((0 == fort.getStrength()) && (0 == ((SOCBoardLarge) board).getPirateHex())) {
            // All players have recaptured their fortresses: The pirate fleet & path is removed.
            flushBoardLayoutAndRepaint();
            // <--- Early return: repaint whole board ---
            return;
        }
        final int pn = piece.getPlayerNumber();
        // repaint this piece in the AWT thread
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                // Local var in case field becomes null in other thread during paint
                Image ibuf = buffer;
                if (ibuf != null)
                    drawFortress(ibuf.getGraphics(), fort, pn, false);
                Graphics bpanG = getGraphics();
                if (bpanG != null)
                    drawFortress(bpanG, fort, pn, false);
                else
                    repaint();
            }
        });
    } else if (piece instanceof SOCVillage) {
        // Otherwise no update needed, village cloth count is handled in tooltip hover.
        if (((SOCVillage) piece).getCloth() == 0)
            flushBoardLayoutAndRepaint();
    } else {
        // generic catch-all for future piece types: just repaint the board.
        flushBoardLayoutAndRepaint();
    }
}
Also used : Graphics(java.awt.Graphics) SOCVillage(soc.game.SOCVillage) SOCFortress(soc.game.SOCFortress) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage)

Aggregations

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