Implementation for game_data.
More...
#include "game_data.h"
#include "character/monster.h"
#include "character/player.h"
#include "common.h"
#include "game.h"
#include "item/loot_generation.h"
#include <stddef.h>
#include <stdio.h>
Go to the source code of this file.
|
int | init_game_data () |
| Initializes game data for the application.
|
int | free_game_data () |
| Frees game-related data structures and resources, such as ability tables, characters, and potions, by using the provided memory pool.
|
int | reset_goblin () |
| 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 () |
| Resets the player character to its initial state, clearing all abilities, items, and stats.
|
Implementation for game_data.
Definition in file game_data.c.
◆ 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.
void free_character(memory_pool_t *memory_pool, character_t *character)
Frees the memory allocated for a character.
memory_pool_t * main_memory_pool
Global memory pool for the application.
void free_gear_table(memory_pool_t *memory_pool, gear_table_t *table)
Frees the memory allocated for a gear table.
void free_potion_table(memory_pool_t *memory_pool, potion_table_t *table)
Frees the memory allocated for a potion table.
◆ 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 {
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.
void add_potion(character_t *character, potion_t *potion)
Adds a potion to a character's inventory.
int reset_goblin()
Resets the goblin character data by deallocating the current goblin instance and creating a new one.
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.
character_t * create_new_player(memory_pool_t *memory_pool)
Creates and initializes a new player character.
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
◆ 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
-
name | The 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];
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.
void add_ability(character_t *character, ability_t *ability)
Adds an ability to a character.
◆ 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]);
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.
◆ 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];
85 return 0;
86}
◆ ability_table
◆ gear_table
◆ goblin
◆ player
◆ potion_table