penguins  1.0.0
game.c File Reference

Go to the source code of this file.

Macros

#define fnv32_hash_value(value)   state = fnv32_hash(state, &value, sizeof(value))
 
#define fnv32_hash_array(ptr, len)   state = fnv32_hash(state, ptr, sizeof(*ptr) * len)
 

Functions

Gamegame_new (void)
 Constructs a Game. Allocates memory for storing the struct itself, setting all fields to default values, and returns a pointer to it. More...
 
Gamegame_clone (const Game *other)
 Creates a (deep) copy of another Game. More...
 
void game_free (Game *self)
 Destroys a Game, freeing the memory allocated for the struct itself and all associated internal lists. More...
 
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 game, excluding settings, log, etc. Was used for debugging, currently unused. More...
 
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 less than Game::log_length the Game::log_buffer will be truncated. More...
 
GameLogEntrygame_push_log_entry (Game *self, GameLogEntryType type)
 Creates a GameLogEntry, sets its GameLogEntry::type, pushes it on top of the stack (reallocating the Game::log_buffer if necessary) and returns a pointer to it. More...
 
const GameLogEntrygame_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 precaution) and returns a pointer to it. More...
 
const GameLogEntrygame_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 the log entries are read-only once they have been pushed. More...
 
void game_set_phase (Game *self, GamePhase phase)
 Sets the current Game::phase and creates a GameLogPhaseChange log entry. More...
 
void game_set_current_player (Game *self, int idx)
 Sets Game::current_player_index and creates a GameLogPlayerChange log entry. More...
 
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 away after constructing a Game. More...
 
void game_end_setup (Game *self)
 Verifies that all fields have been initialized and configured and switches the phase from GAME_PHASE_SETUP to GAME_PHASE_SETUP_DONE. More...
 
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 of all players. Available only in the GAME_PHASE_SETUP phase. More...
 
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. Available only in the GAME_PHASE_SETUP phase. More...
 
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. More...
 
void game_add_player_penguin (Game *self, int idx, Coords coords)
 
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. More...
 
void game_end (Game *self)
 Switches to the GAME_PHASE_END phase. More...
 
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 index. Sets Game::log_current to the selected entry afterwards. More...
 
bool game_check_player_index (const Game *self, int idx)
 Checks if idx is within the bounds of Game::players. More...
 
Playergame_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::players list. More...
 
Playergame_get_current_player (const Game *self)
 A shorthand for calling game_get_player with Game::current_player_index. More...
 
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. More...
 
Coordsgame_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 returns a pointer to it. Returns NULL if no such penguin is found. More...
 

Macro Definition Documentation

◆ fnv32_hash_value

#define fnv32_hash_value (   value)    state = fnv32_hash(state, &value, sizeof(value))

◆ fnv32_hash_array

#define fnv32_hash_array (   ptr,
  len 
)    state = fnv32_hash(state, ptr, sizeof(*ptr) * len)

Function Documentation

◆ game_new()

Game * game_new ( void  )

Constructs a Game. Allocates memory for storing the struct itself, setting all fields to default values, and returns a pointer to it.

Definition at line 15 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_clone()

Game * game_clone ( const Game other)

Creates a (deep) copy of another Game.

Definition at line 36 of file game.c.

Referenced by BotThread::BotThread().

◆ game_free()

void game_free ( Game self)

Destroys a Game, freeing the memory allocated for the struct itself and all associated internal lists.

Definition at line 68 of file game.c.

◆ game_compute_state_hash()

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 game, excluding settings, log, etc. Was used for debugging, currently unused.

Definition at line 87 of file game.c.

◆ game_set_log_capacity()

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 less than Game::log_length the Game::log_buffer will be truncated.

Definition at line 121 of file game.c.

◆ game_push_log_entry()

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 Game::log_buffer if necessary) and returns a pointer to it.

Note
Returns NULL if Game::log_disabled is set to true !

If some entries were undone and the user then pushes a new entry, discards all the undone entries.

Definition at line 137 of file game.c.

◆ game_pop_log_entry()

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 precaution) and returns a pointer to it.

Definition at line 156 of file game.c.

◆ game_get_log_entry()

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 the log entries are read-only once they have been pushed.

Definition at line 173 of file game.c.

Referenced by GamePanel::describe_game_log_entry(), LogEntryViewerController::on_activated(), and LogEntryViewerController::paint_overlay().

◆ game_set_phase()

void game_set_phase ( Game self,
GamePhase  phase 
)

Sets the current Game::phase and creates a GameLogPhaseChange log entry.

Definition at line 181 of file game.c.

◆ game_set_current_player()

void game_set_current_player ( Game self,
int  idx 
)

Sets Game::current_player_index and creates a GameLogPlayerChange log entry.

Definition at line 195 of file game.c.

◆ game_begin_setup()

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 away after constructing a Game.

Definition at line 209 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_end_setup()

void game_end_setup ( Game self)

Verifies that all fields have been initialized and configured and switches the phase from GAME_PHASE_SETUP to GAME_PHASE_SETUP_DONE.

The Game is considered completely initialized when:

  1. The board has been created with setup_board
  2. The players list has been created with game_set_players_count
  3. The penguins lists have been created with game_set_penguins_per_player
  4. The names of all players have been assigned with game_set_player_name

Definition at line 224 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_set_penguins_per_player()

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 of all players. Available only in the GAME_PHASE_SETUP phase.

Definition at line 248 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_set_players_count()

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. Available only in the GAME_PHASE_SETUP phase.

Definition at line 264 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_set_player_name()

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.

A copy of the name string will be created. A NULL pointer can be passed.

Definition at line 287 of file game.c.

Referenced by GamePanel::GamePanel().

◆ game_add_player_penguin()

void game_add_player_penguin ( Game self,
int  idx,
Coords  coords 
)

Fails when Player::penguins_count is already at the maximum value (Game::penguins_per_player).

Definition at line 297 of file game.c.

◆ game_remove_player_penguin()

void game_remove_player_penguin ( Game self,
int  idx,
Coords  coords 
)

Fails if the player doesn't have a penguin at the given coordinates.

Definition at line 306 of file game.c.

◆ game_advance_state()

void game_advance_state ( Game self)

The all-in-one phase switcher that progresses of the game.

Essentially handles the basic progression logic (and the edge cases): first switches the game to the placement phase, if in placement phase switches between players, then (once all penguins have been placed) moves on to the movement phase, afterwards again switches between player turns, and finally ends the game when no player can make a move.

Was added to simplify the logic of phase switching in the GUI.

Definition at line 329 of file game.c.

Referenced by GamePanel::update_game_state().

◆ game_end()

void game_end ( Game self)

Switches to the GAME_PHASE_END phase.

Definition at line 358 of file game.c.

◆ game_rewind_state_to_log_entry()

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 index. Sets Game::log_current to the selected entry afterwards.

Definition at line 368 of file game.c.

Referenced by LogEntryViewerController::on_activated(), and GamePanel::on_show_current_turn_clicked().

◆ game_check_player_index()

bool game_check_player_index ( const Game self,
int  idx 
)
inline

Checks if idx is within the bounds of Game::players.

Definition at line 387 of file game.h.

Referenced by GamePanel::get_controller_for_current_turn(), and CanvasPanel::get_selected_penguin_coords().

◆ game_get_player()

Player * game_get_player ( const Game self,
int  idx 
)
inline

Returns a pointer to the player at the given index. Fails if the index isn't within the bounds of the Game::players list.

Definition at line 394 of file game.h.

Referenced by GameEndDialog::GameEndDialog(), CanvasPanel::get_selected_penguin_coords(), and PlayerInfoBox::update_data().

◆ game_get_current_player()

Player * game_get_current_player ( const Game self)
inline

A shorthand for calling game_get_player with Game::current_player_index.

Definition at line 401 of file game.h.

Referenced by PlayerMovementController::update_tile_attributes().

◆ game_find_player_by_id()

int game_find_player_by_id ( const Game self,
short  id 
)
inline

Returns an index of the player or -1 if no such player was found.

Definition at line 407 of file game.h.

Referenced by CanvasPanel::paint_board().

◆ game_find_player_penguin()

Coords * game_find_player_penguin ( const Game self,
int  idx,
Coords  coords 
)
inline

Finds a penguin with the given coordinates in the Player::penguins list of a player at idx and returns a pointer to it. Returns NULL if no such penguin is found.

Definition at line 420 of file game.h.