DungeonCrawl
Loading...
Searching...
No Matches
game.c
Go to the documentation of this file.
1
5#include "game.h"
6
9#include "database/database.h"
12#include "game_data.h"
15#include "io/io_handler.h"
17#include "logging/logger.h"
18#include "map/map.h"
19#include "map/map_generator.h"
20#include "map/map_mode.h"
21#include "menu/main_menu.h"
22#include "menu/save_menu.h"
23#include "src/common.h"
24#include "stats/stats_mode.h"
25
26#include <locale.h>
27#include <stdbool.h>
28#include <stdio.h>
29
30db_connection_t db_connection;
31bool game_in_progress;
32game_state_t current_state;
33int exit_code;
34
38void game_loop();
39
51void stats_mode_state();
52
60int loading_game(int game_state_id, player_pos_setter_t setter);
61
62void run_game() {
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}
69
70void game_loop() {
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}
114
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}
185
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}
216
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}
237
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}
248
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}
258
263 switch (stats_mode(player)) {
264 case STATS_WINDOW:
265 break;
266 case STATS_EXIT:
267 current_state = MAP_MODE;
268 break;
269 }
270}
271
272int loading_game(const int game_state_id, const player_pos_setter_t setter) {
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 reset_player_stats(character_t *player)
Resets the player stats to their base values.
Definition character.c:320
Exposes functions for working working with the character.
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.
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.
Exposes functions for working with the character and database.
combat_result_t start_combat(character_t *player, character_t *monster)
Starts the loop for combat between the player and the monster.
Declares combat mode state machine, including menus and combat operations.
Defines common macros, types, and global variables for color schemes and utilities.
void db_close(db_connection_t *db_connection)
This function is for the closing of the database.
Definition database.c:22
Exposes functions for working with the database.
void loot_mode_state()
Handles the loot mode state of the game.
Definition game.c:238
void game_loop()
The main game loop of the application.
Definition game.c:70
int loading_game(int game_state_id, player_pos_setter_t setter)
Load the a game from the database.
Definition game.c:272
void run_game()
Starts the game loop.
Definition game.c:62
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
Declares core game states, global database connection, and main game control functions.
int reset_goblin()
Resets the goblin character data by deallocating the current goblin instance and creating a new one.
Definition game_data.c:46
int reset_player()
Resets the player character to its initial state, clearing all abilities, items, and stats.
Definition game_data.c:77
int init_player(char *name)
Initializes the player character with default abilities and items.
Definition game_data.c:57
Declares functions and globals for initializing, resetting, and freeing game data such as player,...
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)
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.
Declares functions to create, save, load, and manage game state data in the SQLite database.
Declares structs for possible input types.
inventory_result_t start_inventory(character_t *player, character_t *monster)
Starts the inventory mode.
Exposes functions for working with the inventory mode.
Exposes functions for the IO-Handler.
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
Header file for logging functionality of the game.
menu_result_t show_main_menu(const bool game_in_progress)
Display and handle the main menu.
Definition main_menu.c:43
Exposes functions for the main menu.
Exposes common types and constants for the games map.
void generate_map()
Generate the map and populate it with keys, enemies, and the exit.
Exposes functions for generating the game map.
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
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
Defines and manages functions for map exploration, player movement, and map interactions in map mode.
void clear_screen(void)
Clear the screen.
Exposes functions for outputting to the console.
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
Exposes functions for the save menu.
stats_result_t stats_mode(character_t *player)
Enter the stat mode for a given character.
Definition stats_mode.c:14
Exposes functions and enums for the stats_mode.
This struct is used for the database connection in SQLite.
Definition database.h:22