penguins  1.0.0
board.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include "game.h"
7 #include "utils.h"
8 #include <assert.h>
9 #include <stdbool.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
21 #define WATER_TILE 0
22 #define FISH_TILE(fish) ((fish) > 0 ? (fish) : 0)
23 #define PENGUIN_TILE(player_id) ((player_id) > 0 ? -(player_id) : 0)
24 #define is_water_tile(tile) ((tile) == 0)
25 #define is_fish_tile(tile) ((tile) > 0)
26 #define is_penguin_tile(tile) ((tile) < 0)
27 #define get_tile_fish(tile) ((tile) > 0 ? (tile) : 0)
28 #define get_tile_player_id(tile) ((tile) < 0 ? -(tile) : 0)
30 
54 };
55 
56 void setup_board(Game* game, int width, int height);
57 
58 void generate_board_random(Game* game, Rng* rng);
59 void generate_board_island(Game* game, Rng* rng);
60 
67 
70 inline ALWAYS_INLINE bool is_tile_in_bounds(const Game* game, Coords coords) {
71  int x = coords.x, y = coords.y;
72  return 0 <= x && x < game->board_width && 0 <= y && y < game->board_height;
73 }
74 
78 inline ALWAYS_INLINE bool get_tile_attr(const Game* game, Coords coords, short attr) {
79  assert(is_tile_in_bounds(game, coords));
80  return test_bit(game->tile_attributes[coords.x + game->board_width * coords.y], 1 << attr);
81 }
82 
86 inline ALWAYS_INLINE void set_tile_attr(Game* game, Coords coords, short attr, bool value) {
87  assert(is_tile_in_bounds(game, coords));
88  short* ptr = &game->tile_attributes[coords.x + game->board_width * coords.y];
89  *ptr = change_bit(*ptr, 1 << attr, value);
90 }
91 
95 inline ALWAYS_INLINE void set_all_tiles_attr(Game* game, short attr, bool value) {
96  for (int y = 0; y < game->board_height; y++) {
97  for (int x = 0; x < game->board_width; x++) {
98  Coords coords = { x, y };
99  set_tile_attr(game, coords, attr, value);
100  }
101  }
102 }
103 
108 inline ALWAYS_INLINE short get_tile(const Game* game, Coords coords) {
109  assert(is_tile_in_bounds(game, coords));
110  return game->board_grid[coords.x + game->board_width * coords.y];
111 }
112 
117 inline ALWAYS_INLINE void set_tile(Game* game, Coords coords, short value) {
118  assert(is_tile_in_bounds(game, coords));
119  game->board_grid[coords.x + game->board_width * coords.y] = value;
120  set_tile_attr(game, coords, TILE_DIRTY, true);
121 }
122 
124 
125 #ifdef __cplusplus
126 }
127 #endif
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...
Definition: board.c:26
void generate_board_island(Game *game, Rng *rng)
Generates the board which looks sort of like a big icy island.
Definition: board.c:37
TileAttribute
The list of attributes built into the common library.
Definition: board.h:49
@ TILE_DIRTY
Set when a tile is changed with set_tile, can be unset by the UI. All tiles initially have this attri...
Definition: board.h:52
@ TILE_ATTR_MAX
Definition: board.h:53
The core of the unified game logic library, contains the Game struct.
A pair of 2D coordinates, used for addressing the Game::board_grid.
Definition: utils.h:15
int x
Definition: utils.h:16
int y
Definition: utils.h:17
The central struct of the application, holds the game data and settings.
Definition: game.h:237
ALWAYS_INLINE void set_all_tiles_attr(Game *game, short attr, bool value)
Sets the attribute attr on all tiles.
Definition: board.h:95
ALWAYS_INLINE bool is_tile_in_bounds(const Game *game, Coords coords)
Checks if the given coords are within the bounds of the board.
Definition: board.h:70
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 ou...
Definition: board.h:117
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.
Definition: board.h:108
int board_width
Use setup_board for setting this.
Definition: game.h:264
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_attribute...
Definition: board.c:12
short * tile_attributes
Stores auxilary data of grid tiles for use in the UIs. Use setup_board for initializing,...
Definition: game.h:330
short * board_grid
A 2D grid represented as a 1D array which stores the tiles of the board. Use setup_board for initiali...
Definition: game.h:306
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.
Definition: board.h:78
int board_height
Use setup_board for setting this.
Definition: game.h:266
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.
Definition: board.h:86
A wrapper around random number generators.
Definition: utils.h:129
#define change_bit(num, bit, val)
Definition: utils.h:108
#define ALWAYS_INLINE
Strongly suggests to the compiler that a function should be inlined.
Definition: utils.h:88
#define test_bit(num, bit)
Definition: utils.h:109