DungeonCrawl
Loading...
Searching...
No Matches
game.h File Reference

Declares core game states, global database connection, and main game control functions. More...

Go to the source code of this file.

Macros

#define COLOR_FG_RGB   255, 255, 255
#define COLOR_BG_RGB   0, 0, 0

Enumerations

enum  game_state_t {
  MAIN_MENU , MAP_MODE , COMBAT_MODE , LOOT_MODE ,
  INVENTORY_MODE , GENERATE_MAP , STATS_MODE , EXIT
}

Functions

void run_game ()
 Starts the game loop.
void main_menu_state ()
 Handles the main menu state of the game.
void map_mode_state ()
 Handles the map mode state of the game.
void inventory_mode_state ()
 Handles the inventory mode state of the game.
void loot_mode_state ()
 Handles the loot mode state of the game.

Variables

struct notcurses * nc
struct ncplane * stdplane
db_connection_t db_connection

Detailed Description

Declares core game states, global database connection, and main game control functions.

Definition in file game.h.

Macro Definition Documentation

◆ COLOR_BG_RGB

#define COLOR_BG_RGB   0, 0, 0

Definition at line 12 of file game.h.

◆ COLOR_FG_RGB

#define COLOR_FG_RGB   255, 255, 255

Definition at line 11 of file game.h.

Enumeration Type Documentation

◆ game_state_t

enum game_state_t

Definition at line 18 of file game.h.

18 {
19 MAIN_MENU,
20 MAP_MODE,
21 COMBAT_MODE,
22 LOOT_MODE,
23 INVENTORY_MODE,
24 GENERATE_MAP,
25 STATS_MODE,
26 EXIT
27} game_state_t;

Function Documentation

◆ inventory_mode_state()

void inventory_mode_state ( )

Handles the inventory mode state of the game.

This function manages the interactions and transitions that occur within the inventory mode, including item management, usage, and equipping items.

Definition at line 249 of file game.c.

249 {
250 switch (start_inventory(player, NULL)) {
251 case CONTINUE_INVENTORY:
252 break;
253 case EXIT_TO_MAP:
254 current_state = MAP_MODE;
255 break;
256 }
257}
inventory_result_t start_inventory(character_t *player, character_t *monster)
Starts the inventory mode.

◆ loot_mode_state()

void loot_mode_state ( )

Handles the loot mode state of the game.

This function manages the interactions and transitions that occur when the player is looting after a combat encounter, including selecting items to take and managing inventory.

Definition at line 238 of file game.c.

238 {
239 switch (start_inventory(player, goblin)) {
240 case CONTINUE_INVENTORY:
241 break;
242 case EXIT_TO_MAP:
243 reset_goblin();
244 current_state = MAP_MODE;
245 break;
246 }
247}
int reset_goblin()
Resets the goblin character data by deallocating the current goblin instance and creating a new one.
Definition game_data.c:46

◆ main_menu_state()

void main_menu_state ( )

Handles the main menu state of the game.

This function manages the interactions and transitions that occur within the main menu, including starting a new game, loading a game, and changing settings.

Definition at line 115 of file game.c.

115 {
116 switch (show_main_menu(game_in_progress)) {
117 case MENU_START_GAME:
118 // TODO: Add a function to get the player name from the user
119 init_player("Hero");
120 game_in_progress = true;// Mark that a game is now in progress
121 clear_screen();
122 current_state = GENERATE_MAP;
123 break;
124 case MENU_CONTINUE:
125 clear_screen();
126 current_state = MAP_MODE;
127 break;
128 case MENU_SAVE_GAME: {
129 // Get the save name from the menu
130 const char* save_name = get_save_name();
131 if (save_name == NULL) {
132 save_name = "Unnamed Save";// Default name if none provided
133 }
134
135 // Save the game with the provided name
136 const sqlite_int64 game_state_id = save_game_state(&db_connection, map, revealed_map, WIDTH, HEIGHT, get_player_pos(), save_name);
137 save_character(&db_connection, *player, game_state_id);
138
139 clear_screen();
140 current_state = MAP_MODE;
141 break;
142 }
143 case MENU_LOAD_GAME: {
144 const int save_id = get_selected_save_file_id() != -1 ? get_selected_save_file_id() : get_latest_save_id(&db_connection);
145 const int load_status = loading_game(save_id, set_player_start_pos);
146 switch (load_status) {
147 case 0:
148 log_msg(INFO, "Game", "Game loaded successfully");
149 // Set game_in_progress flag
150 game_in_progress = true;
151 clear_screen();
152 current_state = MAP_MODE;
153 break;
154 case 1:
155 log_msg(ERROR, "Game", "Failed to reset player character - generating new map");
156 clear_screen();
157 current_state = GENERATE_MAP;
158 break;
159 case 2:
160 log_msg(ERROR, "Game", "Failed to load game state - generating new map");
161 clear_screen();
162 current_state = GENERATE_MAP;
163 break;
164 case 3:
165 log_msg(ERROR, "Game", "Failed to load player character - generating new map");
166 clear_screen();
167 current_state = GENERATE_MAP;
168 break;
169 default:
170 log_msg(ERROR, "Game", "Unknown error loading game state");
171 clear_screen();
172 current_state = GENERATE_MAP;
173 break;
174 }
175 break;
176 }
177 case MENU_CHANGE_LANGUAGE:
178 current_state = MAIN_MENU;
179 break;
180 case MENU_EXIT:
181 current_state = EXIT;
182 break;
183 }
184}
void save_character(const db_connection_t *db_connection, const character_t character, const sqlite3_int64 game_state_id)
This function saves the character to the database.
int loading_game(int game_state_id, player_pos_setter_t setter)
Load the a game from the database.
Definition game.c:272
int init_player(char *name)
Initializes the player character with default abilities and items.
Definition game_data.c:57
sqlite_int64 save_game_state(const db_connection_t *db_connection, const int *map, const int *revealed_map, const int width, const int height, const vector2d_t player, const char *save_name)
Save the game state.
int get_latest_save_id(const db_connection_t *db_connection)
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
menu_result_t show_main_menu(const bool game_in_progress)
Display and handle the main menu.
Definition main_menu.c:43
vector2d_t get_player_pos()
Get the current player position.
Definition map_mode.c:33
void set_player_start_pos(const int player_x, const int player_y)
Sets the starting position of the player.
Definition map_mode.c:26
void clear_screen(void)
Clear the screen.
int get_selected_save_file_id(void)
Get the ID of the save file selected by the user.
Definition save_menu.c:40
const char * get_save_name(void)
Get the name of the latest save entered by the user.
Definition save_menu.c:44

◆ map_mode_state()

void map_mode_state ( )

Handles the map mode state of the game.

This function manages the interactions and transitions that occur within the map mode, including player movement, map exploration, and interactions with map elements.

Definition at line 186 of file game.c.

186 {
187 switch (map_mode_update(player)) {
188 case CONTINUE:
189 break;
190 case QUIT:
191 current_state = EXIT;
192 break;
193 case NEXT_FLOOR:
194 clear_screen();
195 reset_player_stats(player);// Heal player before entering new floor
196 current_state = GENERATE_MAP;
197 break;
198 case COMBAT:
199 current_state = COMBAT_MODE;
200 break;
201 case SHOW_INVENTORY:
202 current_state = INVENTORY_MODE;
203 break;
204 case SHOW_MENU:
205 clear_screen();
206 current_state = MAIN_MENU;
207 break;
208 case SHOW_STATS:
209 clear_screen();
210 current_state = STATS_MODE;
211 break;
212 default:
213 log_msg(ERROR, "game", "Unknown return value from map_mode_update");
214 }
215}
void reset_player_stats(character_t *player)
Resets the player stats to their base values.
Definition character.c:320
map_mode_result_t map_mode_update(character_t *player)
Updates the player position based on the player's input and redraws the maze.
Definition map_mode.c:116

◆ run_game()

void run_game ( )

Starts the game loop.

This function initializes and runs the main game loop, handling the various game states and transitions until the game is exited.

Definition at line 62 of file game.c.

62 {
63 game_in_progress = false;// Flag to track if a game has been started
64
65 current_state = MAIN_MENU;
66 //start the game loop
67 game_loop();
68}
void game_loop()
The main game loop of the application.
Definition game.c:70

Variable Documentation

◆ db_connection

db_connection_t db_connection
extern

Definition at line 30 of file game.c.

◆ nc

struct notcurses* nc
extern

Definition at line 26 of file io_handler.c.

◆ stdplane

struct ncplane* stdplane
extern

Definition at line 27 of file io_handler.c.