penguins  1.0.0
board.h File Reference

Go to the source code of this file.

Detailed Description

Functions for working with the game board (and the specifics of its encoding)

Definition in file board.h.

Macros

Macros for encoding/decoding board tiles

See Game::board_grid for more info. Also note that since these are macros, they unfortunately may evaluate their arguments multiple times, so don't do stuff like FISH_TILE(rand() % 4) (if the parameter is evaluated two times here, it will give two different random values).

#define WATER_TILE   0
 
#define FISH_TILE(fish)   ((fish) > 0 ? (fish) : 0)
 
#define PENGUIN_TILE(player_id)   ((player_id) > 0 ? -(player_id) : 0)
 
#define is_water_tile(tile)   ((tile) == 0)
 
#define is_fish_tile(tile)   ((tile) > 0)
 
#define is_penguin_tile(tile)   ((tile) < 0)
 
#define get_tile_fish(tile)   ((tile) > 0 ? (tile) : 0)
 
#define get_tile_player_id(tile)   ((tile) < 0 ? -(tile) : 0)
 

Enumerations

enum  TileAttribute { TILE_DIRTY , TILE_ATTR_MAX }
 The list of attributes built into the common library. More...
 

Functions

void setup_board (Game *game, int width, int height)
 Sets Game::board_width and Game::board_height and allocates Game::board_grid and Game::tile_attributes. Can only be called within GAME_PHASE_SETUP. The width and height values must be positive. More...
 
void generate_board_random (Game *game, Rng *rng)
 Generates the board by setting every tile purely randomly. The resulting board will look sort of like a maze. More...
 
void generate_board_island (Game *game, Rng *rng)
 Generates the board which looks sort of like a big icy island. More...
 
Functions for accessing the board tiles and attributes

All of these are inline because they are used very very frequently in virtually every module of the app, thus in computation-heavy code, such as the bot, inlining those functions enables very efficient optimizations by the compiler (the bot in particular has been sped up 2 times).

ALWAYS_INLINE bool is_tile_in_bounds (const Game *game, Coords coords)
 Checks if the given coords are within the bounds of the board. More...
 
ALWAYS_INLINE bool get_tile_attr (const Game *game, Coords coords, short attr)
 Checks whether the attribute attr of the tile at coords is set. More...
 
ALWAYS_INLINE void set_tile_attr (Game *game, Coords coords, short attr, bool value)
 Sets (or resets) the attribute attr of the tile at coords. More...
 
ALWAYS_INLINE void set_all_tiles_attr (Game *game, short attr, bool value)
 Sets the attribute attr on all tiles. More...
 
ALWAYS_INLINE short get_tile (const Game *game, Coords coords)
 Returns the value of the tile at coords. Fails if coords are outside the bounds. More...
 
ALWAYS_INLINE void set_tile (Game *game, Coords coords, short value)
 Sets the value of the tile at coords (and also sets the attribute TILE_DIRTY). Fails if coords are outside the bounds. More...
 

Macro Definition Documentation

◆ WATER_TILE

#define WATER_TILE   0

Definition at line 21 of file board.h.

◆ FISH_TILE

#define FISH_TILE (   fish)    ((fish) > 0 ? (fish) : 0)

Definition at line 22 of file board.h.

◆ PENGUIN_TILE

#define PENGUIN_TILE (   player_id)    ((player_id) > 0 ? -(player_id) : 0)

Definition at line 23 of file board.h.

◆ is_water_tile

#define is_water_tile (   tile)    ((tile) == 0)

Definition at line 24 of file board.h.

◆ is_fish_tile

#define is_fish_tile (   tile)    ((tile) > 0)

Definition at line 25 of file board.h.

◆ is_penguin_tile

#define is_penguin_tile (   tile)    ((tile) < 0)

Definition at line 26 of file board.h.

◆ get_tile_fish

#define get_tile_fish (   tile)    ((tile) > 0 ? (tile) : 0)

Definition at line 27 of file board.h.

◆ get_tile_player_id

#define get_tile_player_id (   tile)    ((tile) < 0 ? -(tile) : 0)

Definition at line 28 of file board.h.

Enumeration Type Documentation

◆ TileAttribute

The list of attributes built into the common library.

The variants of this enum are indexes into the bits in the numeric values that the tile attributes are, so they must be sequential numbers starting at zero. Because of this property this enum can be "extended" by UIs as follows:

enum MyTileAttribute {
TILE_ABC = TILE_ATTR_MAX,
TILE_DEF,
TILE_XYZ,
MY_TILE_ATTR_MAX,
};
@ TILE_ATTR_MAX
Definition: board.h:53
See also
Game::tile_attributes
GuiTileAttribute
Enumerator
TILE_DIRTY 

Set when a tile is changed with set_tile, can be unset by the UI. All tiles initially have this attribute on when the board is initialized.

TILE_ATTR_MAX 

Definition at line 49 of file board.h.

Function Documentation

◆ setup_board()

void setup_board ( Game game,
int  width,
int  height 
)

Sets Game::board_width and Game::board_height and allocates Game::board_grid and Game::tile_attributes. Can only be called within GAME_PHASE_SETUP. The width and height values must be positive.

Definition at line 12 of file board.c.

Referenced by GamePanel::GamePanel().

◆ generate_board_random()

void generate_board_random ( Game game,
Rng rng 
)

Generates the board by setting every tile purely randomly. The resulting board will look sort of like a maze.

Definition at line 26 of file board.c.

Referenced by GamePanel::GamePanel(), run_autonomous_mode(), and run_interactive_mode().

◆ generate_board_island()

void generate_board_island ( Game game,
Rng rng 
)

Generates the board which looks sort of like a big icy island.

Definition at line 37 of file board.c.

Referenced by GamePanel::GamePanel(), and run_autonomous_mode().

◆ is_tile_in_bounds()

◆ get_tile_attr()

ALWAYS_INLINE bool get_tile_attr ( const Game game,
Coords  coords,
short  attr 
)
inline

Checks whether the attribute attr of the tile at coords is set.

See also
Game::tile_attributes

Definition at line 78 of file board.h.

Referenced by CanvasPanel::on_paint(), CanvasPanel::paint_board(), PlayerTurnController::paint_overlay(), and CanvasPanel::paint_tiles().

◆ set_tile_attr()

ALWAYS_INLINE void set_tile_attr ( Game game,
Coords  coords,
short  attr,
bool  value 
)
inline

◆ set_all_tiles_attr()

ALWAYS_INLINE void set_all_tiles_attr ( Game game,
short  attr,
bool  value 
)
inline

◆ get_tile()

ALWAYS_INLINE short get_tile ( const Game game,
Coords  coords 
)
inline

◆ set_tile()

ALWAYS_INLINE void set_tile ( Game game,
Coords  coords,
short  value 
)
inline

Sets the value of the tile at coords (and also sets the attribute TILE_DIRTY). Fails if coords are outside the bounds.

See also
Game::board_grid

Definition at line 117 of file board.h.