private void highlightBoardSquare(int row, int col, boolean highlight) { if (!highlight) { paintBoardSquare(row, col, null); } else { Cuboid sq = getSquare(row, col); MaterialWithData squareHighlightColor = boardStyle.getHighlightMaterial(col + (row % 2) % 2 == 1); switch (boardStyle.getHighlightStyle()) { case EDGES: sq.getFace(CuboidDirection.East).fill(squareHighlightColor); sq.getFace(CuboidDirection.North).fill(squareHighlightColor); sq.getFace(CuboidDirection.West).fill(squareHighlightColor); sq.getFace(CuboidDirection.South).fill(squareHighlightColor); break; case CORNERS: for (Block b : sq.corners()) { squareHighlightColor.applyToBlock(b); } break; case CHECKERED: case CHEQUERED: for (Block b : sq) { if ((b.getLocation().getBlockX() - b.getLocation().getBlockZ()) % 2 == 0) { squareHighlightColor.applyToBlock(b.getLocation().getBlock()); } } break; } } }
private void highlightBoardSquare(int row, int col) { Cuboid sq = getSquare(row, col); MaterialData squareHighlightColor = boardStyle.getHighlightMaterial(col + (row % 2) % 2 == 1); switch (boardStyle.getHighlightStyle()) { case EDGES: sq.getFace(CuboidDirection.East).fill(squareHighlightColor); sq.getFace(CuboidDirection.North).fill(squareHighlightColor); sq.getFace(CuboidDirection.West).fill(squareHighlightColor); sq.getFace(CuboidDirection.South).fill(squareHighlightColor); break; case CORNERS: for (Block b : sq.corners()) { b.setTypeIdAndData( squareHighlightColor.getItemTypeId(), squareHighlightColor.getData(), false); } break; case CHECKERED: case CHEQUERED: for (Block b : sq) { if ((b.getLocation().getBlockX() - b.getLocation().getBlockZ()) % 2 == 0) { b.setTypeIdAndData( squareHighlightColor.getItemTypeId(), squareHighlightColor.getData(), false); } } break; default: break; } if (ChessCraft.getInstance().getDynmapIntegration() != null) { ChessCraft.getInstance().getDynmapIntegration().triggerUpdate(sq); } }
/** * Highlight two squares on the chessboard, erasing previous highlight, if any. Use the highlight * square colors per-square color, if set, or just the global highlight block otherwise. * * @param from square index of the first square * @param to square index of the second square */ void highlightSquares(int from, int to) { if (boardStyle.getHighlightStyle() == HighlightStyle.NONE) { return; } // erase the old highlight, if any if (fromSquare >= 0 || toSquare >= 0) { if (boardStyle.getHighlightStyle() == HighlightStyle.LINE) { drawHighlightLine(fromSquare, toSquare, false); } else { paintBoardSquare(fromSquare, null); paintBoardSquare(toSquare, null); } } fromSquare = from; toSquare = to; // draw the new highlight if (from >= 0 || to >= 0) { if (boardStyle.getHighlightStyle() == HighlightStyle.LINE) { drawHighlightLine(fromSquare, toSquare, true); } else { highlightBoardSquare(fromSquare); highlightBoardSquare(toSquare); } } }
private void paintFrame(MassBlockUpdate mbu) { int fw = boardStyle.getFrameWidth(); MaterialData fm = boardStyle.getFrameMaterial(); frameBoard.getFace(CuboidDirection.West).expand(CuboidDirection.East, fw - 1).fill(fm, mbu); frameBoard.getFace(CuboidDirection.South).expand(CuboidDirection.North, fw - 1).fill(fm, mbu); frameBoard.getFace(CuboidDirection.East).expand(CuboidDirection.West, fw - 1).fill(fm, mbu); frameBoard.getFace(CuboidDirection.North).expand(CuboidDirection.South, fw - 1).fill(fm, mbu); }
private void paintBoardSquare(int row, int col, MassBlockUpdate mbu) { Cuboid square = getSquare(row, col); boolean black = (col + (row % 2)) % 2 == 0; if (mbu == null) { square.fill( black ? boardStyle.getBlackSquareMaterial() : boardStyle.getWhiteSquareMaterial()); } else { square.fill( black ? boardStyle.getBlackSquareMaterial() : boardStyle.getWhiteSquareMaterial(), mbu); } }
private void paintBoardSquare(int row, int col, MassBlockUpdate mbu) { Cuboid square = getSquare(row, col); boolean black = (col + (row % 2)) % 2 == 0; if (mbu == null) { square.fill( black ? boardStyle.getBlackSquareMaterial() : boardStyle.getWhiteSquareMaterial()); } else { square.fill( black ? boardStyle.getBlackSquareMaterial() : boardStyle.getWhiteSquareMaterial(), mbu); } if (ChessCraft.getInstance().getDynmapIntegration() != null) { ChessCraft.getInstance().getDynmapIntegration().triggerUpdate(square); } }
/** * Get the region above a square into which a chesspiece gets drawn * * @param row the board row * @param col the board column * @return a Cuboid representing the drawing space */ public Cuboid getPieceRegion(int row, int col) { Cuboid sq = getSquare(row, col) .expand(CuboidDirection.Up, boardStyle.getHeight() - 1) .shift(CuboidDirection.Up, 1); return sq; }
/** * Draw the chess piece represented by stone into the given row and column. The actual blocks * drawn depend on the board's current chess set. * * @param row * @param col * @param stone */ public void paintChessPiece(int row, int col, int stone) { // for entity sets, just check that the entity is still at (row,col) // for block sets, get the stone and paste its data into the region at (row,col) ChessSet cSet = designer != null ? designer.getChessSet() : chessSet; if (cSet.hasMovablePieces()) { // we don't paint movable pieces; moveChessPiece() can handle those return; } Cuboid region = getPieceRegion(row, col); MassBlockUpdate mbu = CraftMassBlockUpdate.createMassBlockUpdater( ChessCraft.getInstance(), getBoard().getWorld()); region.fill(0, (byte) 0, mbu); if (stone != Chess.NO_STONE) { ChessStone cStone = cSet.getStone(stone, getRotation()); if (cStone != null) { cStone.paint(region, mbu); } else { LogUtils.severe("unknown chess stone " + stone); } } region.expand(CuboidDirection.Down, 1).forceLightLevel(boardStyle.getLightLevel()); mbu.notifyClients(); if (ChessCraft.getInstance().getDynmapIntegration() != null) { ChessCraft.getInstance().getDynmapIntegration().triggerUpdate(region); } }
/** * Reload the board and piece styles in-use * * @throws ChessException if board or piece style cannot be loaded */ void reloadStyles() throws ChessException { if (boardStyle != null) { setBoardStyle(boardStyle.getName()); } if (chessSet != null) { setChessSet(chessSet.getName()); } }
/** * Board constructor. * * @param origin * @param rotation * @param boardStyleName * @param pieceStyleName * @throws ChessException */ public ChessBoard( Location origin, BoardRotation rotation, String boardStyleName, String pieceStyleName) throws ChessException { setBoardStyle(boardStyleName); setPieceStyle(pieceStyleName != null ? pieceStyleName : boardStyle.getPieceStyleName()); this.rotation = rotation; a1Center = new PersistableLocation(origin); a1Corner = initA1Corner(origin, rotation); h8Corner = initH8Corner(a1Corner.getLocation()); board = new Cuboid(a1Corner.getLocation(), h8Corner.getLocation()); areaBoard = board.expand(CuboidDirection.Up, boardStyle.getHeight()); frameBoard = board.outset(CuboidDirection.Horizontal, boardStyle.getFrameWidth()); aboveFullBoard = frameBoard .shift(CuboidDirection.Up, 1) .expand(CuboidDirection.Up, boardStyle.getHeight() - 1); fullBoard = frameBoard.expand(CuboidDirection.Up, boardStyle.getHeight() + 1); validateBoardPosition(); }
public final void setPieceStyle(String pieceStyle) throws ChessException { if (boardStyle == null) { return; } ChessSet newChessSet = ChessSet.getChessSet(pieceStyle); boardStyle.verifyCompatibility(newChessSet); chessPieceSet = newChessSet; }
private void highlightSelectedBoardSquare(int sqi) { Cuboid sq = getSquare(Chess.sqiToRow(sqi), Chess.sqiToCol(sqi)); MaterialData squareHighlightColor = boardStyle.getSelectedHighlightMaterial(); sq.getFace(CuboidDirection.East).fill(squareHighlightColor); sq.getFace(CuboidDirection.North).fill(squareHighlightColor); sq.getFace(CuboidDirection.West).fill(squareHighlightColor); sq.getFace(CuboidDirection.South).fill(squareHighlightColor); if (ChessCraft.getInstance().getDynmapIntegration() != null) { ChessCraft.getInstance().getDynmapIntegration().triggerUpdate(sq); } }
public final void setChessSet(String pieceStyle) throws ChessException { if (boardStyle == null) { return; } ChessSet newChessSet = ChessSetFactory.getChessSet(pieceStyle); boardStyle.verifyCompatibility(newChessSet); chessSet.syncToPosition(null, this); chessSet = newChessSet; redrawNeeded = true; }
/** * Get the Chesspresso square index of the given location * * @param loc desired location * @return the square index, or Chess.NO_SQUARE if not on the board */ int getSquareAt(Location loc) { if (!areaBoard.contains(loc)) { return Chess.NO_SQUARE; } int row = 0, col = 0; switch (rotation) { case NORTH: row = 7 - ((loc.getBlockX() - areaBoard.getLowerX()) / boardStyle.getSquareSize()); col = 7 - ((loc.getBlockZ() - areaBoard.getLowerZ()) / boardStyle.getSquareSize()); break; case EAST: row = 7 - ((loc.getBlockZ() - areaBoard.getLowerZ()) / boardStyle.getSquareSize()); col = -((areaBoard.getLowerX() - loc.getBlockX()) / boardStyle.getSquareSize()); break; case SOUTH: row = -((areaBoard.getLowerX() - loc.getBlockX()) / boardStyle.getSquareSize()); col = -((areaBoard.getLowerZ() - loc.getBlockZ()) / boardStyle.getSquareSize()); break; case WEST: row = -((areaBoard.getLowerZ() - loc.getBlockZ()) / boardStyle.getSquareSize()); col = 7 - ((loc.getBlockX() - areaBoard.getLowerX()) / boardStyle.getSquareSize()); break; } return Chess.coorToSqi(col, row); }
private void paintEnclosure(MassBlockUpdate mbu) { aboveFullBoard.getFace(CuboidDirection.North).fill(boardStyle.getEnclosureMaterial(), mbu); aboveFullBoard.getFace(CuboidDirection.East).fill(boardStyle.getEnclosureMaterial(), mbu); aboveFullBoard.getFace(CuboidDirection.South).fill(boardStyle.getEnclosureMaterial(), mbu); aboveFullBoard.getFace(CuboidDirection.West).fill(boardStyle.getEnclosureMaterial(), mbu); fullBoard.getFace(CuboidDirection.Up).fill(boardStyle.getEnclosureMaterial(), mbu); if (!boardStyle.getEnclosureMaterial().equals(boardStyle.getStrutsMaterial())) { paintStruts(mbu); } }
private void paintStruts(MassBlockUpdate mbu) { MaterialData struts = boardStyle.getStrutsMaterial(); // vertical struts at the frame corners Cuboid c = new Cuboid(frameBoard.getLowerNE()) .shift(CuboidDirection.Up, 1) .expand(CuboidDirection.Up, boardStyle.getHeight()); c.fill(struts, mbu); c = c.shift(CuboidDirection.South, frameBoard.getSizeX() - 1); c.fill(struts, mbu); c = c.shift(CuboidDirection.West, frameBoard.getSizeZ() - 1); c.fill(struts, mbu); c = c.shift(CuboidDirection.North, frameBoard.getSizeZ() - 1); c.fill(struts, mbu); // horizontal struts along roof edge Cuboid roof = frameBoard.shift(CuboidDirection.Up, boardStyle.getHeight() + 1); roof.getFace(CuboidDirection.East).fill(struts, mbu); roof.getFace(CuboidDirection.North).fill(struts, mbu); roof.getFace(CuboidDirection.West).fill(struts, mbu); roof.getFace(CuboidDirection.South).fill(struts, mbu); }
/** * Use Bresenham's algorithm to draw line between two squares on the board * * @param from Square index of the first square * @param to Square index of the second square * @param isHighlighting True if drawing a highlight, false if erasing it */ private void drawHighlightLine(int from, int to, boolean isHighlighting) { if (from < 0 || to < 0 || from >= 64 || to >= 64) { return; } Cuboid s1 = getSquare(Chess.sqiToRow(from), Chess.sqiToCol(from)); Cuboid s2 = getSquare(Chess.sqiToRow(to), Chess.sqiToCol(to)); Location loc1 = s1.getRelativeBlock(s1.getSizeX() / 2, 0, s1.getSizeZ() / 2).getLocation(); Location loc2 = s2.getRelativeBlock(s2.getSizeX() / 2, 0, s2.getSizeZ() / 2).getLocation(); int dx = Math.abs(loc1.getBlockX() - loc2.getBlockX()); int dz = Math.abs(loc1.getBlockZ() - loc2.getBlockZ()); int sx = loc1.getBlockX() < loc2.getBlockX() ? 1 : -1; int sz = loc1.getBlockZ() < loc2.getBlockZ() ? 1 : -1; int err = dx - dz; while (loc1.getBlockX() != loc2.getBlockX() || loc1.getBlockZ() != loc2.getBlockZ()) { int sqi = getSquareAt(loc1); MaterialData m = isHighlighting ? boardStyle.getHighlightMaterial(Chess.isWhiteSquare(sqi)) : (Chess.isWhiteSquare(sqi) ? boardStyle.getWhiteSquareMaterial() : boardStyle.getBlackSquareMaterial()); loc1.getBlock().setTypeIdAndData(m.getItemTypeId(), m.getData(), false); int e2 = 2 * err; if (e2 > -dz) { err -= dz; loc1.add(sx, 0, 0); } if (e2 < dx) { err += dx; loc1.add(0, 0, sz); } } }
/** Paint everything! (board, frame, enclosure, control panel, lighting) */ void paintAll(MassBlockUpdate mbu) { if (designer == null) { fullBoard.fill(0, (byte) 0, mbu); } paintEnclosure(mbu); paintFrame(mbu); paintBoard(mbu); if (designer != null) { paintDesignIndicators(mbu); } if (fromSquare >= 0 || toSquare >= 0) { highlightSquares(fromSquare, toSquare); } fullBoard.forceLightLevel(boardStyle.getLightLevel()); }
/** * Get the Cuboid region for this square <i>of the chessboard itself</i> * * @param row * @param col * @return a Cuboid representing the square */ public Cuboid getSquare(int row, int col) { if (row < 0 || col < 0 || row > 7 || col > 7) { throw new ChessException("ChessBoard: getSquare: bad (row, col): (" + row + "," + col + ")"); } Cuboid sq = new Cuboid(a1Corner.getLocation()); int s = boardStyle.getSquareSize(); CuboidDirection dir = rotation.getDirection(); CuboidDirection dirRight = rotation.getRight().getDirection(); sq = sq.shift(dir, row * s).shift(dirRight, col * s); sq = sq.expand(dir, s - 1).expand(dirRight, s - 1); return sq; }
public final void setBoardStyle(BoardStyle newStyle) { // We don't allow any changes to the board's dimensions; only changes to // the appearance of the board. if (boardStyle != null && (boardStyle.getFrameWidth() != newStyle.getFrameWidth() || boardStyle.getSquareSize() != newStyle.getSquareSize() || boardStyle.getHeight() != newStyle.getHeight())) { throw new ChessException( "New board style dimensions do not match the current board dimensions"); } boardStyle = newStyle; chessPieceSet = ChessSet.getChessSet(boardStyle.getPieceStyleName()); }
/** * Draw the chess piece represented by stone into the given row and column. The actual blocks * drawn depend on the board's current chess set. * * @param row * @param col * @param stone */ public void paintChessPiece(int row, int col, int stone) { Cuboid region = getPieceRegion(row, col); MassBlockUpdate mbu = CraftMassBlockUpdate.createMassBlockUpdater(getBoard().getWorld()); region.fill(0, (byte) 0, mbu); ChessSet cSet = designer != null ? designer.getChessSet() : chessPieceSet; if (stone != Chess.NO_STONE) { ChessStone cStone = cSet.getStone(stone, getRotation()); if (cStone != null) { paintChessPiece(region, cStone, mbu); } else { LogUtils.severe("unknown piece: " + stone); } } region.expand(CuboidDirection.Down, 1).forceLightLevel(boardStyle.getLightLevel()); mbu.notifyClients(); }
private PersistableLocation initH8Corner(Location a1) { Location h8 = new Location(a1.getWorld(), a1.getBlockX(), a1.getBlockY(), a1.getBlockZ()); int size = boardStyle.getSquareSize(); switch (rotation) { case NORTH: h8.add(-size * 8 + 1, 0, -size * 8 + 1); break; case EAST: h8.add(size * 8 - 1, 0, -size * 8 + 1); break; case SOUTH: h8.add(size * 8 - 1, 0, size * 8 - 1); break; case WEST: h8.add(-size * 8 + 1, 0, size * 8 - 1); break; } return new PersistableLocation(h8); }
/** Paint everything! (board, frame, enclosure, control panel, lighting) */ void paintAll(MassBlockUpdate mbu) { if (designer == null) { fullBoard.fill(0, (byte) 0, mbu); } paintEnclosure(mbu); paintFrame(mbu); paintBoard(mbu); if (designer != null) { paintDesignIndicators(mbu); } if (fromSquare >= 0 || toSquare >= 0) { highlightSquares(fromSquare, toSquare); } fullBoard.forceLightLevel(boardStyle.getLightLevel()); redrawNeeded = false; if (ChessCraft.getInstance().getDynmapIntegration() != null) { ChessCraft.getInstance().getDynmapIntegration().triggerUpdate(fullBoard); } }
private PersistableLocation initA1Corner(Location origin, BoardRotation rotation) { Location a1 = new Location(origin.getWorld(), origin.getBlockX(), origin.getBlockY(), origin.getBlockZ()); int offset = boardStyle.getSquareSize() / 2; switch (rotation) { case NORTH: a1.add(offset, 0, offset); break; case EAST: a1.add(-offset, 0, offset); break; case SOUTH: a1.add(-offset, 0, -offset); break; case WEST: a1.add(offset, 0, -offset); break; } return new PersistableLocation(a1); }
/** @return the name of the board style used */ public String getBoardStyleName() { return boardStyle != null ? boardStyle.getName() : null; }
public final void setBoardStyle(String boardStyleName) throws ChessException { BoardStyle newStyle = BoardStyle.loadStyle(boardStyleName); setBoardStyle(newStyle); }
public final void setBoardStyle(String boardStyleName) throws ChessException { BoardStyle newStyle = BoardStyle.loadStyle(boardStyleName); setBoardStyle( newStyle, boardStyle == null || !(boardStyle.getName().equals(newStyle.getName()))); }