DungeonCrawl
Loading...
Searching...
No Matches
ability.c File Reference

This file contains functions for initializing and managing abilities. More...

#include "ability.h"
#include "../database/game/ability_database.h"
#include <stdio.h>

Go to the source code of this file.

Functions

void 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)
 Initializes an ability with the given parameters.
ability_table_tinit_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 free_ability_table (memory_pool_t *memory_pool, ability_table_t *table)
 Free the ability table, deallocates memory in the memory pool.

Detailed Description

This file contains functions for initializing and managing abilities.

Definition in file ability.c.

Function Documentation

◆ free_ability_table()

void free_ability_table ( memory_pool_t * memory_pool,
ability_table_t * table )

Free the ability table, deallocates memory in the memory pool.

Parameters
memory_poolPointer to the memory pool for deallocation.
tablePointer to the ability table to be freed.

Definition at line 45 of file ability.c.

45 {
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}
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.

◆ init_ability()

void 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 )

Initializes an ability with the given parameters.

Parameters
abilityPointer to the ability to be initialized.
nameName of the ability.
roll_amountNumber of dice to roll.
accuracyAccuracy of the ability.
resource_costResource cost of the ability.
dice_sizeSize of the dice used for rolling damage.
damage_typeType of damage dealt by the ability.

Definition at line 62 of file ability.c.

62 {
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}

◆ init_ability_table()

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.

Parameters
memory_poolPointer to the memory pool for allocation.
db_connectionPointer to the database connection
Returns
Pointer to the ability table.

Definition at line 16 of file ability.c.

16 {
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}
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
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.
void * memory_pool_alloc(memory_pool_t *pool, size_t size)
Allocates memory on the given memory pool.
To get the ability table from the database, we need to define a struct This struct is for the initial...