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

Declares functions and globals for initializing, resetting, and freeing game data such as player, goblin, abilities, and potions. More...

#include "combat/ability.h"
#include "item/gear.h"
#include "item/potion.h"

Go to the source code of this file.

Functions

int init_game_data (void)
 Initializes game data for the application.
int free_game_data (void)
 Frees game-related data structures and resources, such as ability tables, characters, and potions, by using the provided memory pool.
int reset_goblin (void)
 Resets the goblin character data by deallocating the current goblin instance and creating a new one.
int init_player (char *name)
 Initializes the player character with default abilities and items.
int reset_player (void)
 Resets the player character to its initial state, clearing all abilities, items, and stats.

Variables

ability_table_tability_table
character_tgoblin
character_tplayer
potion_thealing_potion
gear_table_tgear_table
potion_table_tpotion_table

Detailed Description

Declares functions and globals for initializing, resetting, and freeing game data such as player, goblin, abilities, and potions.

Definition in file game_data.h.

Function Documentation

◆ free_game_data()

int free_game_data ( void )

Frees game-related data structures and resources, such as ability tables, characters, and potions, by using the provided memory pool.

Returns
0 if all resources are successfully freed.

Definition at line 37 of file game_data.c.

37 {
43 return 0;
44}
void free_ability_table(memory_pool_t *memory_pool, ability_table_t *table)
Free the ability table, deallocates memory in the memory pool.
Definition ability.c:45
void free_character(memory_pool_t *memory_pool, character_t *character)
Frees the memory allocated for a character.
Definition character.c:60
memory_pool_t * main_memory_pool
Global memory pool for the application.
Definition common.c:7
void free_gear_table(memory_pool_t *memory_pool, gear_table_t *table)
Frees the memory allocated for a gear table.
Definition gear.c:74
void free_potion_table(memory_pool_t *memory_pool, potion_table_t *table)
Frees the memory allocated for a potion table.
Definition potion.c:53

◆ init_game_data()

int init_game_data ( void )

Initializes game data for the application.

This function sets up the game data structures, including the ability table, potion table, gear table, and player character. It also initializes the goblin character and adds potions to the player.

Returns
0 if successful, 1 if initialization failed.

Definition at line 23 of file game_data.c.

23 {
24 ability_table = init_ability_table(main_memory_pool, &db_connection);
25 potion_table = init_potion_table(main_memory_pool, &db_connection);
26 gear_table = init_gear_table(main_memory_pool, &db_connection, ability_table);
29
30 if (ability_table == NULL || potion_table == NULL || gear_table == NULL || player == NULL) return 1;
31
32 add_potion(goblin, &potion_table->potions[HEALING]);
33
34 return 0;
35}
ability_table_t * init_ability_table(memory_pool_t *memory_pool, const db_connection_t *db_connection)
Initialize the ability table, allocates memory and returns the pointer to the table.
Definition ability.c:16
void add_potion(character_t *character, potion_t *potion)
Adds a potion to a character's inventory.
Definition character.c:242
int reset_goblin()
Resets the goblin character data by deallocating the current goblin instance and creating a new one.
Definition game_data.c:46
gear_table_t * init_gear_table(memory_pool_t *memory_pool, const db_connection_t *db_connection, ability_table_t *ability_table)
Initializes a gear table.
Definition gear.c:45
character_t * create_new_player(memory_pool_t *memory_pool)
Creates and initializes a new player character.
Definition player.c:8
potion_table_t * init_potion_table(memory_pool_t *memory_pool, const db_connection_t *db_connection)
initializes a potion table with potions from the database
Definition potion.c:20

◆ init_player()

int init_player ( char * name)

Initializes the player character with default abilities and items.

This function sets up the player with a base attack ability, adds default potions, and equips a starting piece of gear. It is used to prepare the player only for a new game.

Parameters
nameThe name of the player character.
Returns
0 if successful, 1 if failed

Definition at line 57 of file game_data.c.

57 {
60 if (player == NULL) {
61 return 1;
62 }
63 player->base_attack = &ability_table->abilities[PUNCH];
64 add_ability(player, player->base_attack);
65 add_potion(player, &potion_table->potions[HEALING]);
66 add_potion(player, &potion_table->potions[MANA]);
67 add_potion(player, &potion_table->potions[STAMINA]);
68 equip_gear(player, gear_table->gears[ARMING_SWORD]);
69 if (name != NULL) {
70 snprintf(player->name, sizeof(player->name), "%s", name);
71 } else {
72 snprintf(player->name, sizeof(player->name), "Hero");
73 }
74 return 0;
75}
void equip_gear(character_t *character, gear_t *gear)
Equips a gear item to a character.
Definition character.c:272
void add_ability(character_t *character, ability_t *ability)
Adds an ability to a character.
Definition character.c:127

◆ reset_goblin()

int reset_goblin ( void )

Resets the goblin character data by deallocating the current goblin instance and creating a new one.

The new goblin is initialized and assigned with the "BITE" ability.

Returns
0 if the operation is successful, 1 if it fails due to invalid input or memory allocation issues.

Definition at line 46 of file game_data.c.

46 {
49 if (goblin == NULL) {
50 return 1;
51 }
52 add_ability(goblin, &ability_table->abilities[BITE]);
53 generate_loot(goblin, gear_table, potion_table, 1);
54 return 0;
55}
void generate_loot(character_t *c, gear_table_t *gear_table, potion_table_t *potion_table, int rolls)
Generates loot for a character by randomly selecting a number of gear and potions.
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.
character_t * create_new_goblin(memory_pool_t *memory_pool)
Creates and initializes a new goblin character.
Definition monster.c:8

◆ reset_player()

int reset_player ( void )

Resets the player character to its initial state, clearing all abilities, items, and stats.

This function is used when loading a saved game.

Returns
0 if successful, 1 if failed

Definition at line 77 of file game_data.c.

77 {
80 if (player == NULL) {
81 return 1;
82 }
83 player->base_attack = &ability_table->abilities[PUNCH];
84 add_ability(player, player->base_attack);
85 return 0;
86}

Variable Documentation

◆ ability_table

ability_table_t* ability_table
extern

Definition at line 17 of file game_data.c.

◆ gear_table

gear_table_t* gear_table
extern

Definition at line 19 of file game_data.c.

◆ goblin

character_t* goblin
extern

Definition at line 20 of file game_data.c.

◆ player

character_t* player
extern

Definition at line 21 of file game_data.c.

◆ potion_table

potion_table_t* potion_table
extern

Definition at line 18 of file game_data.c.