388 return 0 <= idx && idx <
self->players_count;
396 return &
self->players[idx];
408 for (
int i = 0; i <
self->players_count; i++) {
409 if (self->players[i].id ==
id) {
424 if (penguin->
x == coords.
x && penguin->
y == coords.
y) {
GamePhase
The values of Game::phase.
@ GAME_PHASE_PLACEMENT
Set by placement_begin.
@ GAME_PHASE_MOVEMENT
Set by movement_begin.
@ GAME_PHASE_SETUP
Set by game_begin_setup.
@ GAME_PHASE_NONE
The default phase, set when a Game is initially constructed.
@ GAME_PHASE_SETUP_DONE
Set by game_end_setup, placement_end, movement_end.
@ GAME_PHASE_END
Set by game_end.
GameLogEntryType
The values of GameLogEntry::type.
@ GAME_LOG_ENTRY_PLAYER_CHANGE
See GameLogPlayerChange.
@ GAME_LOG_ENTRY_PHASE_CHANGE
See GameLogPhaseChange.
@ GAME_LOG_ENTRY_PLACEMENT
See GameLogPlacement.
@ GAME_LOG_ENTRY_MOVEMENT
See GameLogMovement.
A pair of 2D coordinates, used for addressing the Game::board_grid.
An entry in the Game::log_buffer, implemented as a tagged union.
union GameLogEntry::GameLogEntryData data
A GameLogEntry created by move_penguin.
A GameLogEntry created by game_set_phase.
A GameLogEntry created by place_penguin.
A GameLogEntry created by game_set_current_player.
The central struct of the application, holds the game data and settings.
void game_set_penguins_per_player(Game *self, int value)
Sets Game::penguins_per_player (the value mustn't be negative) and allocates Player::penguins lists o...
size_t log_current
The index of the currently selected log entry. Normally equals log_length, if less than log_length an...
Player * game_get_current_player(const Game *self)
A shorthand for calling game_get_player with Game::current_player_index.
int game_find_player_by_id(const Game *self, short id)
Returns an index of the player or -1 if no such player was found.
void game_set_log_capacity(Game *self, size_t capacity)
Sets Game::log_capacity and allocates that many elements in Game::log_buffer. If the new capacity is ...
const GameLogEntry * game_get_log_entry(const Game *self, size_t idx)
Returns a pointer to the entry at the given index. Note that the returned pointer is const because th...
void game_add_player_penguin(Game *self, int idx, Coords coords)
void game_set_current_player(Game *self, int idx)
Sets Game::current_player_index and creates a GameLogPlayerChange log entry.
Player * players
The list of players with length players_count. Initialized with game_set_players_count....
int penguins_per_player
Use game_set_penguins_per_player for setting this.
void game_begin_setup(Game *self)
Switches to the GAME_PHASE_SETUP phase, can only be called in GAME_PHASE_NONE. Should be called right...
GamePhase phase
The current state of the state machine, initially set to GAME_PHASE_NONE. Use game_set_phase for sett...
void game_end(Game *self)
Switches to the GAME_PHASE_END phase.
void game_set_phase(Game *self, GamePhase phase)
Sets the current Game::phase and creates a GameLogPhaseChange log entry.
void game_remove_player_penguin(Game *self, int idx, Coords coords)
void game_advance_state(Game *self)
The all-in-one phase switcher that progresses of the game.
const GameLogEntry * game_pop_log_entry(Game *self, GameLogEntryType expected_type)
Pops the last entry off the top of the stack if its type matches the expected_type (this is used as a...
int board_width
Use setup_board for setting this.
uint32_t game_compute_state_hash(const Game *self)
Computes a hash of the game state part of the Game, i.e. the fields that change while playing the gam...
short * tile_attributes
Stores auxilary data of grid tiles for use in the UIs. Use setup_board for initializing,...
size_t log_capacity
The total number of elements log_buffer was allocated for (i.e. pushing more requires reallocating it...
short * board_grid
A 2D grid represented as a 1D array which stores the tiles of the board. Use setup_board for initiali...
GameLogEntry * log_buffer
The stack of log entries. Use game_push_log_entry, game_pop_log_entry and game_get_log_entry for modi...
void game_end_setup(Game *self)
Verifies that all fields have been initialized and configured and switches the phase from GAME_PHASE_...
Game * game_clone(const Game *other)
Creates a (deep) copy of another Game.
size_t log_length
The actual number of entries in log_buffer. game_push_log_entry and game_pop_log_entry affects this.
Coords * game_find_player_penguin(const Game *self, int idx, Coords coords)
Finds a penguin with the given coordinates in the Player::penguins list of a player at idx and return...
int board_height
Use setup_board for setting this.
void game_rewind_state_to_log_entry(Game *self, size_t target_entry)
Successively undoes or redoes log entries in order to reset the game state to the entry at the given ...
int current_player_index
A negative value means that there is no current player selected. Use game_set_current_player for sett...
bool log_disabled
Signals whether new log entries can be created. See game_push_log_entry.
void game_set_player_name(Game *self, int idx, const char *name)
Sets the Player::name of a player at the given index. Only available in the GAME_PHASE_SETUP phase.
int players_count
Use game_set_players_count for setting this.
Player * game_get_player(const Game *self, int idx)
Returns a pointer to the player at the given index. Fails if the index isn't within the bounds of the...
Game * game_new(void)
Constructs a Game. Allocates memory for storing the struct itself, setting all fields to default valu...
bool game_check_player_index(const Game *self, int idx)
Checks if idx is within the bounds of Game::players.
void game_free(Game *self)
Destroys a Game, freeing the memory allocated for the struct itself and all associated internal lists...
GameLogEntry * game_push_log_entry(Game *self, GameLogEntryType type)
Creates a GameLogEntry, sets its GameLogEntry::type, pushes it on top of the stack (reallocating the ...
void game_set_players_count(Game *self, int count)
Sets Game::players_count (the value mustn't be negative) and allocates the Game::players list....
Holds the data of the players of the Game.
short id
The unique ID, usually (but not necessarily) is just index + 1.
char * name
Use game_set_player_name to set this.
int moves_count
The number of moves (penguin placements and movements) made by the player.
int color
The color of the penguins, currently used only in the TUI.
Coords * penguins
The list of the positions of all of the player's penguins.
int points
The score of the player, i.e. the number of collected fish.
int penguins_count
The length of the penguins array.
GameLogPlacement placement
GameLogPhaseChange phase_change
GameLogPlayerChange player_change