DungeonCrawl
Loading...
Searching...
No Matches
potion.c
Go to the documentation of this file.
1
5#include "potion.h"
6
8
9#include <stdio.h>
10
11potion_t* init_potion(potion_t* potion, const char* name, const potion_type_t type, const int value) {
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}
19
20potion_table_t* init_potion_table(memory_pool_t* memory_pool, const db_connection_t* db_connection) {
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}
39
40const char* potion_type_to_string(potion_type_t type) {
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}
52
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}
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.
Declares functions to load and free potion and gear tables from the game database,...
void * memory_pool_alloc(memory_pool_t *pool, size_t size)
Allocates memory on the given memory pool.
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.
const char * potion_type_to_string(potion_type_t type)
Converts a potion type to a string representation.
Definition potion.c:40
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
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
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
Exposes functions for working with potions.
This struct is used for the database connection in SQLite.
Definition database.h:22
To get potion table from the database, we need to define a struct This struct is for the initializati...