def _get_piece_center(self, piece_coord, piece, terrain_centers): """Takes a piece's hex coordinate, the piece itself, and the terrain_centers dictionary which maps tile_id->(x,y) Returns the piece's center, as an (x,y) pair. Also returns the angle the piece should be rotated at, if any """ if piece.type == PieceType.road: # these pieces are on edges tile_id = hexgrid.nearest_tile_to_edge(piece_coord) tile_coord = hexgrid.tile_id_to_coord(tile_id) direction = hexgrid.tile_edge_offset_to_direction(piece_coord - tile_coord) terrain_x, terrain_y = terrain_centers[tile_id] angle = 60*self._edge_angle_order.index(direction) dx = math.cos(math.radians(angle)) * self.distance_tile_to_edge() dy = math.sin(math.radians(angle)) * self.distance_tile_to_edge() return terrain_x + dx, terrain_y + dy, angle + 90 elif piece.type in [PieceType.settlement, PieceType.city]: # these pieces are on nodes tile_id = hexgrid.nearest_tile_to_node(piece_coord) tile_coord = hexgrid.tile_id_to_coord(tile_id) direction = hexgrid.tile_node_offset_to_direction(piece_coord - tile_coord) terrain_x, terrain_y = terrain_centers[tile_id] angle = 30 + 60*self._node_angle_order.index(direction) dx = math.cos(math.radians(angle)) * self._tile_radius dy = math.sin(math.radians(angle)) * self._tile_radius return terrain_x + dx, terrain_y + dy, 0 elif piece.type == PieceType.robber: # these pieces are on tiles tile_id = hexgrid.tile_id_from_coord(piece_coord) terrain_x, terrain_y = terrain_centers[tile_id] return terrain_x, terrain_y, 0 else: logging.warning('Unknown piece={}'.format(piece))
def move_robber(self, tile_id): robbers = self.game.board.get_pieces((catan.pieces.PieceType.robber, ), hexgrid.tile_id_to_coord(self.game.robber_tile)) for robber in robbers: self.game.board.move_piece(robber, hexgrid.tile_id_to_coord(self.game.robber_tile), hexgrid.tile_id_to_coord(tile_id)) if len(robbers) > 1: logging.warning('More than one robber found in board.pieces') self.game.robber_tile = tile_id self.game.set_state(GameStateStealUsingKnight(self.game))
def move_robber(self, tile_id): robbers = self.game.board.get_pieces((catan.pieces.PieceType.robber, ), hexgrid.tile_id_to_coord(self.game.robber_tile)) for robber in robbers: self.game.board.move_piece(robber, hexgrid.tile_id_to_coord(self.game.robber_tile), hexgrid.tile_id_to_coord(tile_id)) if len(robbers) != 1: logging.warning('{} robbers found in board.pieces'.format(len(robbers))) self.game.robber_tile = tile_id self.game.set_state(GameStateSteal(self.game))
def move_robber(self, tile_id): robbers = self.game.board.get_pieces((catan.pieces.PieceType.robber, ), hexgrid.tile_id_to_coord(self.game.robber_tile)) to_coord = hexgrid.tile_id_to_coord(tile_id) if robbers: robber = robbers[0] from_coord = hexgrid.tile_id_to_coord(self.game.robber_tile) self.game.board.move_piece(robber, from_coord, to_coord) else: robber = catan.pieces.Piece(catan.pieces.PieceType.robber, None) self.game.board.place_piece(robber, to_coord) if len(robbers) != 1: logging.warning('{} robbers found in board.pieces'.format(len(robbers))) self.game.robber_tile = tile_id self.game.set_state(GameStateNotInGame(self.game))
def _get_pieces(tiles, ports, players_opts, pieces_opts): """ Generate a dictionary of pieces using the given options. pieces options supported: - Opt.empty -> no locations have pieces - Opt.random -> - Opt.preset -> robber is placed on the first desert found - Opt.debug -> a variety of pieces are placed around the board :param tiles: list of tiles from _generate_tiles :param ports: list of ports from _generate_ports :param players_opts: Opt :param pieces_opts: Opt :return: dictionary mapping (hexgrid.TYPE, coord:int) -> Piece """ print( '_get_pieces called with tiles={0}, ports={1}, players_opts={2}, pieces_opts={3}' .format(tiles, ports, players_opts, pieces_opts)) if pieces_opts == Opt.empty: return dict() elif pieces_opts == Opt.debug: players = catan.game.Game.get_debug_players() return { (hexgrid.NODE, 0x23): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[0]), (hexgrid.EDGE, 0x22): catan.pieces.Piece(catan.pieces.PieceType.road, players[0]), (hexgrid.NODE, 0x67): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[1]), (hexgrid.EDGE, 0x98): catan.pieces.Piece(catan.pieces.PieceType.road, players[1]), (hexgrid.NODE, 0x87): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[2]), (hexgrid.EDGE, 0x89): catan.pieces.Piece(catan.pieces.PieceType.road, players[2]), (hexgrid.EDGE, 0xA9): catan.pieces.Piece(catan.pieces.PieceType.road, players[3]), (hexgrid.TILE, 0x77): catan.pieces.Piece(catan.pieces.PieceType.robber, None), } elif pieces_opts in (Opt.preset, ): deserts = filter( lambda tile: tile.terrain == catan.board.Terrain.desert, tiles) coord = hexgrid.tile_id_to_coord(list(deserts)[0].tile_id) return { (hexgrid.TILE, coord): catan.pieces.Piece(catan.pieces.PieceType.robber, None) } elif pieces_opts in (Opt.random, ): logging.warning('{} option not yet implemented'.format(pieces_opts))
def _get_piece_center(self, piece_coord, piece, terrain_centers): """Takes a piece's hex coordinate, the piece itself, and the terrain_centers dictionary which maps tile_id->(x,y) Returns the piece's center, as an (x,y) pair. Also returns the angle the piece should be rotated at, if any """ if piece.type == PieceType.road: # these pieces are on edges tile_id = hexgrid.nearest_tile_to_edge(piece_coord) tile_coord = hexgrid.tile_id_to_coord(tile_id) direction = hexgrid.tile_edge_offset_to_direction(piece_coord - tile_coord) terrain_x, terrain_y = terrain_centers[tile_id] angle = 60 * self._edge_angle_order.index(direction) dx = math.cos(math.radians(angle)) * self.distance_tile_to_edge() dy = math.sin(math.radians(angle)) * self.distance_tile_to_edge() return terrain_x + dx, terrain_y + dy, angle + 90 elif piece.type in [PieceType.settlement, PieceType.city]: # these pieces are on nodes tile_id = hexgrid.nearest_tile_to_node(piece_coord) tile_coord = hexgrid.tile_id_to_coord(tile_id) direction = hexgrid.tile_node_offset_to_direction(piece_coord - tile_coord) terrain_x, terrain_y = terrain_centers[tile_id] angle = 30 + 60 * self._node_angle_order.index(direction) dx = math.cos(math.radians(angle)) * self._tile_radius dy = math.sin(math.radians(angle)) * self._tile_radius return terrain_x + dx, terrain_y + dy, 0 elif piece.type == PieceType.robber: # these pieces are on tiles tile_id = hexgrid.tile_id_from_coord(piece_coord) terrain_x, terrain_y = terrain_centers[tile_id] return terrain_x, terrain_y, 0 else: logging.warning('Unknown piece={}'.format(piece))
def _get_pieces(tiles, ports, players_opts, pieces_opts): """ Generate a dictionary of pieces using the given options. pieces options supported: - Opt.empty -> no locations have pieces - Opt.random -> - Opt.preset -> robber is placed on the first desert found - Opt.debug -> a variety of pieces are placed around the board :param tiles: list of tiles from _generate_tiles :param ports: list of ports from _generate_ports :param players_opts: Opt :param pieces_opts: Opt :return: dictionary mapping (hexgrid.TYPE, coord:int) -> Piece """ if pieces_opts == Opt.empty: return dict() elif pieces_opts == Opt.debug: players = catan.game.Game.get_debug_players() return { (hexgrid.NODE, 0x23): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[0]), (hexgrid.EDGE, 0x22): catan.pieces.Piece(catan.pieces.PieceType.road, players[0]), (hexgrid.NODE, 0x67): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[1]), (hexgrid.EDGE, 0x98): catan.pieces.Piece(catan.pieces.PieceType.road, players[1]), (hexgrid.NODE, 0x87): catan.pieces.Piece(catan.pieces.PieceType.settlement, players[2]), (hexgrid.EDGE, 0x89): catan.pieces.Piece(catan.pieces.PieceType.road, players[2]), (hexgrid.EDGE, 0xA9): catan.pieces.Piece(catan.pieces.PieceType.road, players[3]), (hexgrid.TILE, 0x77): catan.pieces.Piece(catan.pieces.PieceType.robber, None), } elif pieces_opts in (Opt.preset, ): deserts = filter(lambda tile: tile.terrain == catan.board.Terrain.desert, tiles) coord = hexgrid.tile_id_to_coord(list(deserts)[0].tile_id) return { (hexgrid.TILE, coord): catan.pieces.Piece(catan.pieces.PieceType.robber, None) } elif pieces_opts in (Opt.random, ): logging.warning('{} option not yet implemented'.format(pieces_opts))
def coord(self) -> int: """:returns coordinate of this hex tile, *this is not the same as hex id* (see hexgrid)""" return hexgrid.tile_id_to_coord(self.__hex_id + 1)