penguins  1.0.0
board.c
Go to the documentation of this file.
1 #include "board.h"
2 #include "game.h"
3 #include "utils.h"
4 #include <assert.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7 
12 void setup_board(Game* game, int width, int height) {
13  assert(game->phase == GAME_PHASE_SETUP);
14  assert(width > 0 && height > 0);
17  game->board_width = width;
18  game->board_height = height;
19  game->board_grid = calloc(width * height, sizeof(*game->board_grid));
20  game->tile_attributes = calloc(width * height, sizeof(*game->tile_attributes));
21  set_all_tiles_attr(game, TILE_DIRTY, true);
22 }
23 
26 void generate_board_random(Game* game, Rng* rng) {
27  for (int y = 0; y < game->board_height; y++) {
28  for (int x = 0; x < game->board_width; x++) {
29  Coords coords = { x, y };
30  short fish = (short)rng->random_range(rng, 0, 3);
31  set_tile(game, coords, FISH_TILE(fish));
32  }
33  }
34 }
35 
37 void generate_board_island(Game* game, Rng* rng) {
38  int w = game->board_width, h = game->board_height;
39  for (int y = 0; y < h; y++) {
40  for (int x = 0; x < w; x++) {
41  Coords coords = { x, y };
42  set_tile(game, coords, WATER_TILE);
43  }
44  }
45  for (int i = 0; i < w + h; i++) {
46  Coords coords = { w / 2, h / 2 };
47  for (int j = 0; j < w + h; j++) {
48  switch (rng->random_range(rng, 0, 3)) {
49  case 0: coords.x += 1; break;
50  case 1: coords.y += 1; break;
51  case 2: coords.x -= 1; break;
52  case 3: coords.y -= 1; break;
53  }
54  if (is_tile_in_bounds(game, coords)) {
55  short fish = (short)rng->random_range(rng, 1, 3);
56  set_tile(game, coords, FISH_TILE(fish));
57  } else {
58  break;
59  }
60  }
61  }
62 }
63 
64 extern bool is_tile_in_bounds(const Game* game, Coords coords);
65 extern bool get_tile_attr(const Game* game, Coords coords, short attr);
66 extern void set_tile_attr(Game* game, Coords coords, short attr, bool value);
67 extern void set_all_tiles_attr(Game* game, short attr, bool value);
68 extern short get_tile(const Game* game, Coords coords);
69 extern void set_tile(Game* game, Coords coords, short value);
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
Functions for working with the game board (and the specifics of its encoding)
#define FISH_TILE(fish)
Definition: board.h:22
#define WATER_TILE
Definition: board.h:21
@ 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
The core of the unified game logic library, contains the Game struct.
@ GAME_PHASE_SETUP
Set by game_begin_setup.
Definition: game.h:57
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
GamePhase phase
The current state of the state machine, initially set to GAME_PHASE_NONE. Use game_set_phase for sett...
Definition: game.h:242
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
int(* random_range)(struct Rng *self, int min, int max)
Generates and returns a value in the range [min; max) (i.e. from min inclusive to max exclusive).
Definition: utils.h:132
#define free_and_clear(ptr)
Calls free on a pointer and then sets it to NULL.
Definition: utils.h:92