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

Implements the main game loop and state handling for DungeonCrawl. More...

#include "game.h"
#include "character/character.h"
#include "combat/combat_mode.h"
#include "database/database.h"
#include "database/game/character_database.h"
#include "database/game/gamestate_database.h"
#include "game_data.h"
#include "inventory/inventory_mode.h"
#include "io/input/input_types.h"
#include "io/io_handler.h"
#include "io/output/common/output_handler.h"
#include "logging/logger.h"
#include "map/map.h"
#include "map/map_generator.h"
#include "map/map_mode.h"
#include "menu/main_menu.h"
#include "menu/save_menu.h"
#include "src/common.h"
#include "stats/stats_mode.h"
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>

Go to the source code of this file.

Functions

void game_loop ()
 The main game loop of the application.
void combat_mode_state ()
 The state machine for the combat mode.
void stats_mode_state ()
 Handles the stats mode state of the game.
int loading_game (int game_state_id, player_pos_setter_t setter)
 Load the a game from the database.
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 loot_mode_state ()
 Handles the loot mode state of the game.
void inventory_mode_state ()
 Handles the inventory mode state of the game.

Variables

db_connection_t db_connection
bool game_in_progress
game_state_t current_state
int exit_code

Detailed Description

Implements the main game loop and state handling for DungeonCrawl.

Definition in file game.c.

Function Documentation

◆ combat_mode_state()

void combat_mode_state ( )

The state machine for the combat mode.

The state machine of the combat mode.

Definition at line 220 of file game.c.

220 {
221 switch (start_combat(player, goblin)) {
222 case CONTINUE_COMBAT:
223 break;
224 case PLAYER_WON:
225 clear_screen();
226 current_state = LOOT_MODE;
227 break;
228 case PLAYER_LOST:
229 //TODO: instead of exiting the game, a death screen should be shown
230 current_state = EXIT;
231 break;
232 case EXIT_GAME:
233 current_state = EXIT;
234 break;
235 }
236}
combat_result_t start_combat(character_t *player, character_t *monster)
Starts the loop for combat between the player and the monster.
void clear_screen(void)
Clear the screen.

◆ game_loop()

void game_loop ( )

The main game loop of the application.

Definition at line 70 of file game.c.

70 {
71 bool running = true;//should only be set in the state machine
72
73 while (running) {
74 // Process current game state
75 switch (current_state) {
76 case MAIN_MENU:
78 break;
79
80 case GENERATE_MAP:
82 current_state = MAP_MODE;
83 break;
84
85 case MAP_MODE:
87 break;
88
89 case COMBAT_MODE:
91 break;
92
93 case LOOT_MODE:
95 break;
96
97 case INVENTORY_MODE:
99 break;
100
101 case STATS_MODE:
103 break;
104
105 case EXIT:
106 running = false;
107 break;
108 }
109 }
110
111 // Close database connection
112 db_close(&db_connection);
113}
void db_close(db_connection_t *db_connection)
This function is for the closing of the database.
Definition database.c:22
void loot_mode_state()
Handles the loot mode state of the game.
Definition game.c:238
void inventory_mode_state()
Handles the inventory mode state of the game.
Definition game.c:249
void stats_mode_state()
Handles the stats mode state of the game.
Definition game.c:262
void main_menu_state()
Handles the main menu state of the game.
Definition game.c:115
void combat_mode_state()
The state machine for the combat mode.
Definition game.c:220
void map_mode_state()
Handles the map mode state of the game.
Definition game.c:186
void generate_map()
Generate the map and populate it with keys, enemies, and the exit.

◆ 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.

◆ loading_game()

int loading_game ( int game_state_id,
player_pos_setter_t setter )

Load the a game from the database.

Parameters
game_state_idThe id of the game state to be loaded.
setterA setter for the player position.
Returns
0 on success, 1 if the player could not be propperly reset, 2 if game state could not be loaded, 3 if player is NULL

Definition at line 272 of file game.c.

272 {
273 if (reset_player() != 0) return 1;
274 if (get_game_state_by_id(&db_connection, game_state_id, map, revealed_map, WIDTH, HEIGHT, setter) != 1) return 2;
275 get_character_from_db(&db_connection, player, game_state_id);
276 if (player == NULL) return 3;
277 return 0;
278}
void get_character_from_db(const db_connection_t *db_connection, character_t *character, const int game_state_id)
This function retrieves a character from the database.
int reset_player()
Resets the player character to its initial state, clearing all abilities, items, and stats.
Definition game_data.c:77
int get_game_state_by_id(const db_connection_t *db_connection, const int game_state_id, int *map, int *revealed_map, const int width, const int height, const player_pos_setter_t setter)
Load the game state for a specific id from the database.

◆ 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
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

◆ stats_mode_state()

void stats_mode_state ( )

Handles the stats mode state of the game.

The state machine for the stats mode.

This function manages the interactions and transitions that occur within the stats mode, including displaying player stats and managing skill points.

Definition at line 262 of file game.c.

262 {
263 switch (stats_mode(player)) {
264 case STATS_WINDOW:
265 break;
266 case STATS_EXIT:
267 current_state = MAP_MODE;
268 break;
269 }
270}
stats_result_t stats_mode(character_t *player)
Enter the stat mode for a given character.
Definition stats_mode.c:14

Variable Documentation

◆ current_state

game_state_t current_state

Definition at line 32 of file game.c.

◆ db_connection

db_connection_t db_connection

Definition at line 30 of file game.c.

◆ exit_code

int exit_code

Definition at line 33 of file game.c.

◆ game_in_progress

bool game_in_progress

Definition at line 31 of file game.c.