DungeonCrawl
Loading...
Searching...
No Matches
ability.c
Go to the documentation of this file.
1
5
6#include "ability.h"
7
9
10#include <stdio.h>
11
12// Internal functions
13void init_ability(ability_t* ability, char* name, int roll_amount, int accuracy, int resource_cost, dice_size_t dice_size, damage_type_t damage_type);
14
15
17 NULL_PTR_HANDLER_RETURN(memory_pool, NULL, "Ability", "Memory pool is NULL");
18
19 ability_init_t* rows = get_ability_table_from_db(db_connection);
20 NULL_PTR_HANDLER_RETURN(rows, NULL, "Ability", "Could not fetch ability data from DB");
21
22 ability_table_t* table = memory_pool_alloc(memory_pool, sizeof(ability_table_t));
23 NULL_PTR_HANDLER_RETURN(table, NULL, "Ability", "Failed to allocate memory for ability table");
24
25
26 for (int i = 0; i < MAX_ABILITIES; ++i) {
27 if (rows[i].name == NULL)
28 break;
29
30 const int slot = rows[i].ability_number;
31
32 init_ability(&table->abilities[slot],
33 rows[i].name,
34 rows[i].roll_amount,
35 rows[i].accuracy,
36 rows[i].resource_cost,
37 rows[i].dice_size,
38 rows[i].damage_type);
39 }
41 return table;
42}
43
44
46 NULL_PTR_HANDLER_RETURN(memory_pool, , "Ability", "In free_ability_table memory pool is NULL");
47 NULL_PTR_HANDLER_RETURN(table, , "Ability", "In free_ability_table table is NULL");
48
49 memory_pool_free(memory_pool, table);
50}
51
62void init_ability(ability_t* ability, char* name, const int roll_amount, const int accuracy, const int resource_cost, const dice_size_t dice_size, const damage_type_t damage_type) {
63 NULL_PTR_HANDLER_RETURN(ability, , "Ability", "In init_ability ability is NULL");
64 NULL_PTR_HANDLER_RETURN(name, , "Ability", "In init_ability name is NULL");
65
66 snprintf(ability->name, sizeof(ability->name), "%s", name);
67 ability->roll_amount = roll_amount;
68 ability->accuracy = accuracy;
69 ability->resource_cost = resource_cost;
70 ability->dice_size = dice_size;
71 ability->damage_type = damage_type;
72}
void init_ability(ability_t *ability, char *name, int roll_amount, int accuracy, int resource_cost, dice_size_t dice_size, damage_type_t damage_type)
Initializes an ability with the given parameters.
Definition ability.c:62
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
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
Exposes functions for working with abilities.
ability_init_t * get_ability_table_from_db(const db_connection_t *db_connection)
Get the ability table from the database.
void free_ability_table_from_db(ability_init_t *ability_init_table)
Clean up the ability table Call this function to free the memory allocated for the ability table.
Declares functions to load and free ability definitions 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.
To get the ability table from the database, we need to define a struct This struct is for the initial...
This struct is used for the database connection in SQLite.
Definition database.h:22