use of soc.game.SOCShip in project JSettlers2 by jdmonin.
the class SOCPlayerTracker method updateScenario_SC_PIRI_closestShipToFortress.
/**
* For scenario {@code _SC_PIRI}, update the player's ship closest to their Fortress.
* Assumes no ship will ever be west of the fortress (smaller column number).
* Must be called after adding or removing a ship from our player's {@link SOCPlayer#getRoads()}.
* @param ship Ship that was added or removed, or {@code null} to check all ships after removal
* @param shipAdded True if {@code ship} was added; false if {@code ship} or any other ship was removed
* or if we're updating Closest Ship without adding or removing a ship
* @throws IllegalArgumentException if {@code shipAdded} is true, but null {@code ship}
* @since 2.0.00
*/
void updateScenario_SC_PIRI_closestShipToFortress(final SOCShip ship, final boolean shipAdded) throws IllegalArgumentException {
if (shipAdded && (ship == null))
throw new IllegalArgumentException();
if ((scen_SC_PIRI_closestShipToFortress == null) && (ship != null)) {
if (shipAdded)
// closest by default
scen_SC_PIRI_closestShipToFortress = ship;
// <--- Early return: no other ships to compare ---
return;
}
if (!shipAdded) {
if ((ship != null) && (scen_SC_PIRI_closestShipToFortress != null) && (ship.getCoordinates() != scen_SC_PIRI_closestShipToFortress.getCoordinates()))
// <--- Early return: Not the closest ship ---
return;
}
// may be null towards end of game
final SOCFortress fort = player.getFortress();
// If fort's null, we can still compare columns, just not rows, of ship coordinates.
final int fortR = (fort != null) ? (fort.getCoordinates() >> 8) : -1;
if (shipAdded) {
final int shipEdge = ship.getCoordinates(), prevShipEdge = scen_SC_PIRI_closestShipToFortress.getCoordinates();
final int shipR = shipEdge >> 8, shipC = shipEdge & 0xFF, prevR = prevShipEdge >> 8, prevC = prevShipEdge & 0xFF;
if ((shipC < prevC) || ((shipC == prevC) && (fortR != -1) && (Math.abs(shipR - fortR) < Math.abs(prevR - fortR)))) {
scen_SC_PIRI_closestShipToFortress = ship;
}
} else {
// A ship has been removed. We don't know which one.
// So, check all ships for distance from fortress.
Enumeration<SOCRoad> roadAndShipEnum = player.getRoads().elements();
SOCShip closest = null;
int closeR = -1, closeC = -1;
while (roadAndShipEnum.hasMoreElements()) {
final SOCRoad rs = roadAndShipEnum.nextElement();
if (!(rs instanceof SOCShip))
continue;
final int shipEdge = rs.getCoordinates();
final int shipR = shipEdge >> 8, shipC = shipEdge & 0xFF;
if ((closest == null) || (shipC < closeC) || ((shipC == closeC) && (fortR != -1) && (Math.abs(shipR - fortR) < Math.abs(closeR - fortR)))) {
closest = (SOCShip) rs;
closeR = shipR;
closeC = shipC;
}
}
// null if no ships
scen_SC_PIRI_closestShipToFortress = closest;
}
}
use of soc.game.SOCShip 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);
}
}
use of soc.game.SOCShip 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);
}
}
use of soc.game.SOCShip in project JSettlers2 by jdmonin.
the class SOCPlayerInterface method updateAtPieceRemoved.
/**
* A player's piece has been removed from the board.
* Updates game state and refreshes the board display by calling {@link #updateAtPiecesChanged()}.
*<P>
* Currently, only ships can be removed, in game scenario {@code _SC_PIRI}.
* Other {@code pieceType}s are ignored.
*
* @param player Player who owns the ship
* @param pieceCoordinate Ship's node coordinate
* @param pieceType The piece type identifier {@link SOCPlayingPiece#SHIP}
* @since 2.0.00
*/
public void updateAtPieceRemoved(SOCPlayer player, int pieceCoordinate, int pieceType) {
switch(pieceType) {
case SOCPlayingPiece.SHIP:
game.removeShip(new SOCShip(player, pieceCoordinate, null));
updateAtPiecesChanged();
break;
default:
System.err.println("PI.updateAtPieceRemoved called for un-handled type " + pieceType);
}
}
use of soc.game.SOCShip in project JSettlers2 by jdmonin.
the class SOCDisplaylessPlayerClient method handleMOVEPIECE.
/**
* Handle moving a piece (a ship) around on the board.
* @since 2.0.00
*/
protected void handleMOVEPIECE(SOCMovePiece mes) {
final String gaName = mes.getGame();
SOCGame ga = games.get(gaName);
if (ga == null)
// Not one of our games
return;
SOCShip sh = new SOCShip(ga.getPlayer(mes.getPlayerNumber()), mes.getFromCoord(), null);
ga.moveShip(sh, mes.getToCoord());
}
Aggregations