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

Exposes functions for working with potions. More...

Go to the source code of this file.

Data Structures

struct  potion_t
struct  potion_table_t

Enumerations

enum  potion_type_t { HEALING , MANA , STAMINA , MAX_POTION_TYPES }

Functions

potion_tinit_potion (potion_t *potion, const char *name, potion_type_t type, int value)
 creates a potion object with the given name, type and value
potion_table_tinit_potion_table (memory_pool_t *memory_pool, const db_connection_t *db_connection)
 initializes a potion table with potions from the database
const char * potion_type_to_string (potion_type_t type)
 Converts a potion type to a string representation.
void free_potion (memory_pool_t *memory_pool, potion_t *potion)
 Frees the memory allocated for a potion.
void free_potion_table (memory_pool_t *memory_pool, potion_table_t *table)
 Frees the memory allocated for a potion table.

Detailed Description

Exposes functions for working with potions.

Definition in file potion.h.

Enumeration Type Documentation

◆ potion_type_t

enum potion_type_t

Definition at line 12 of file potion.h.

12 {
13 HEALING,
14 MANA,
15 STAMINA,
16 MAX_POTION_TYPES
17} potion_type_t;

Function Documentation

◆ free_potion()

void free_potion ( memory_pool_t * memory_pool,
potion_t * potion )

Frees the memory allocated for a potion.

Parameters
memory_poolMemory pool to free the potion
potionPotion object to be freed

◆ free_potion_table()

void free_potion_table ( memory_pool_t * memory_pool,
potion_table_t * table )

Frees the memory allocated for a potion table.

Parameters
memory_poolMemory pool to free the potion table
tablePotion table to be freed

Definition at line 53 of file potion.c.

53 {
54 NULL_PTR_HANDLER_RETURN(memory_pool, , "Potion", "In free_potion_table memory pool is NULL");
55 NULL_PTR_HANDLER_RETURN(table, , "Potion", "In free_potion_table table is NULL");
56
57 memory_pool_free(memory_pool, table);
58}
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.

◆ init_potion()

potion_t * init_potion ( potion_t * potion,
const char * name,
potion_type_t type,
int value )

creates a potion object with the given name, type and value

Parameters
potionPotion object to be initialized
nameName of the potion
typeType defines the effect of the potion
valueValue defining the strength of the effect

Definition at line 11 of file potion.c.

11 {
12 NULL_PTR_HANDLER_RETURN(name, NULL, "Potion", "In init_potion name is NULL");
13
14 snprintf(potion->name, sizeof(potion->name), "%s", name);
15 potion->effectType = type;
16 potion->value = value;
17 return potion;
18}

◆ init_potion_table()

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

Parameters
memory_poolMemory pool to allocate the potion table
db_connectionDatabase connection to fetch the potions from
Returns
Pointer to the initialized potion table

Definition at line 20 of file potion.c.

20 {
21 NULL_PTR_HANDLER_RETURN(memory_pool, NULL, "Potion", "Memory pool is NULL");
22
23 potion_init_t* rows = init_potion_table_from_db(db_connection);
24 NULL_PTR_HANDLER_RETURN(rows, NULL, "Potion", "Could not fetch potion data from DB");
25
26 potion_table_t* table = memory_pool_alloc(memory_pool, sizeof(potion_table_t));
27 NULL_PTR_HANDLER_RETURN(table, NULL, "Potion", "Failed to allocate potion for potion table");
28
29 for (int i = 0; i < MAX_POTION_TYPES; ++i) {
30 if (rows[i].name == NULL)
31 break;
32
33 const int slot = rows[i].potion_type;
34 init_potion(&table->potions[slot], rows[i].name, rows[i].potion_type, rows[i].value);
35 }
36 free_potion_table_from_db(rows, db_connection);
37 return table;
38}
potion_init_t * init_potion_table_from_db(const db_connection_t *db_connection)
Get the potion table from the database.
void free_potion_table_from_db(potion_init_t *potion_init_table, const db_connection_t *db_connection)
Clean up the potion table Call this function to free the memory allocated for the potion table.
void * memory_pool_alloc(memory_pool_t *pool, size_t size)
Allocates memory on the given memory pool.
potion_t * init_potion(potion_t *potion, const char *name, const potion_type_t type, const int value)
creates a potion object with the given name, type and value
Definition potion.c:11
To get potion table from the database, we need to define a struct This struct is for the initializati...

◆ potion_type_to_string()

const char * potion_type_to_string ( potion_type_t type)

Converts a potion type to a string representation.

Parameters
typeThe potion type to convert
Returns
A string representation of the potion type

Definition at line 40 of file potion.c.

40 {
41 switch (type) {
42 case HEALING:
43 return "Health";
44 case MANA:
45 return "Mana";
46 case STAMINA:
47 return "Stamina";
48 default:
49 return "Unknown";
50 }
51}