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

Implementation of the gear system. More...

#include "gear.h"
#include "../database/game/item_database.h"
#include "../logging/logger.h"
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

gear_tinit_gear (memory_pool_t *memory_pool, const char *name, gear_identifier_t gear_identifier, gear_slot_t slot, stats_t stats, defenses_t defenses, ability_table_t *ability_table, ability_names_t *abilities, int num_abilities)
 Initializes a gear object.
gear_table_tinit_gear_table (memory_pool_t *memory_pool, const db_connection_t *db_connection, ability_table_t *ability_table)
 Initializes a gear table.
void free_gear_table (memory_pool_t *memory_pool, gear_table_t *table)
 Frees the memory allocated for a gear table.
const char * gear_slot_to_string (gear_slot_t slot)
 Converts a gear slot enumeration value to its string representation.

Detailed Description

Implementation of the gear system.

Definition in file gear.c.

Function Documentation

◆ free_gear_table()

void free_gear_table ( memory_pool_t * memory_pool,
gear_table_t * table )

Frees the memory allocated for a gear table.

This function releases all memory associated with a gear_table_t object, including the memory for each individual gear object and the table itself. It ensures that the memory pool and table pointers are not NULL before attempting to free memory.

Parameters
memory_poolA pointer to the memory pool used for memory allocation.
tableA pointer to the gear_table_t object to be freed.

Definition at line 74 of file gear.c.

74 {
75 NULL_PTR_HANDLER_RETURN(memory_pool, , "Gear", "In free_gear_table memory pool is NULL");
76 NULL_PTR_HANDLER_RETURN(table, , "Gear", "In free_gear_table table is NULL");
77 for (int i = 0; i < table->num_gears; ++i) {
78 gear_t* gear = table->gears[i];
79 if (gear != NULL) {
80 memory_pool_free(memory_pool, gear);
81 }
82 }
83 memory_pool_free(memory_pool, table);
84}
void memory_pool_free(memory_pool_t *pool, void *ptr)
Sets the given data pointer to not active in the given memory pool.
Definition gear.h:32

◆ gear_slot_to_string()

const char * gear_slot_to_string ( gear_slot_t slot)

Converts a gear slot enumeration value to its string representation.

This function takes a gear_slot_t enumeration value and returns a human-readable string that represents the corresponding gear slot. If the provided slot does not match any known value, the function returns "Unknown Slot".

Parameters
slotThe gear slot enumeration value of type gear_slot_t.
Returns
A string representing the gear slot. If the slot is invalid, returns "Unknown Slot".

Definition at line 86 of file gear.c.

86 {
87 switch (slot) {
88 case SLOT_HEAD:
89 return "Head";
90 case SLOT_CHEST:
91 return "Chest";
92 case SLOT_LEGS:
93 return "Legs";
94 case SLOT_FEET:
95 return "Feet";
96 case SLOT_HANDS:
97 return "Hands";
98 case SLOT_NECK:
99 return "Neck";
100 case SLOT_FINGER_RIGHT:
101 return "Right Finger";
102 case SLOT_FINGER_LEFT:
103 return "Left Finger";
104 case SLOT_LEFT_HAND:
105 return "Left Hand";
106 case SLOT_RIGHT_HAND:
107 return "Right Hand";
108 case SLOT_BOTH_HANDS:
109 return "Both Hands";
110 default:
111 return "Unknown Slot";
112 }
113}

◆ init_gear()

gear_t * init_gear ( memory_pool_t * memory_pool,
const char * name,
gear_identifier_t gear_identifier,
gear_slot_t slot,
stats_t stats,
defenses_t defenses,
ability_table_t * ability_table,
ability_names_t * abilities,
int num_abilities )

Initializes a gear object.

This function creates and initializes a new gear_t object with the specified parameters. It allocates memory, sets the properties of the object, and links it with the provided abilities.

Parameters
memory_poolA pointer to the memory pool used for memory allocation.
nameThe name of the gear as a string.
gear_identifierThe unique identifier of the gear of type gear_identifier_t.
slotThe slot in which the gear will be equipped, of type gear_slot_t.
statsThe base stats of the gear, of type stats_t.
defensesThe defense values of the gear, of type defenses_t.
ability_tableA pointer to the ability table containing the available abilities.
abilitiesAn array of ability names to be assigned to the gear.
num_abilitiesThe number of abilities to be assigned to the gear.
Returns
A pointer to the initialized gear_t object or NULL if initialization fails.

Definition at line 14 of file gear.c.

14 {
15 NULL_PTR_HANDLER_RETURN(memory_pool, NULL, "Gear", "In init_gear memory pool is NULL");
16 NULL_PTR_HANDLER_RETURN(name, NULL, "Gear", "In init_gear name is NULL");
17 gear_t* gear = memory_pool_alloc(memory_pool, sizeof(gear_t));
18
19
20 NULL_PTR_HANDLER_RETURN(gear, NULL, "Gear", "Failed to allocate memory for gear: %s", name);
21
22 snprintf(gear->local_key, sizeof(gear->local_key), "%s", name);
23 gear->gear_identifier = gear_identifier;
24 gear->slot = slot;
25 gear->stats = stats;
26 gear->defenses = defenses;
27 gear->num_abilities = num_abilities;
28
29 int i = 0;
30 for (; i < MAX_ABILITY_PER_GEAR && i < num_abilities; ++i) {
31 ability_names_t idx = abilities[i];
32 if (idx < MAX_ABILITIES) {
33 gear->abilities[i] = &ability_table->abilities[abilities[i]];
34 } else {
35 gear->abilities[i] = &(ability_t) {0};
36 }
37 }
38 for (; i < MAX_ABILITY_PER_GEAR; ++i) {
39 gear->abilities[i] = &(ability_t) {0};
40 }
41 return gear;
42}
void * memory_pool_alloc(memory_pool_t *pool, size_t size)
Allocates memory on the given memory pool.

◆ init_gear_table()

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.

This function creates and initializes a gear_table_t object by fetching gear data from the database, allocating memory for the table, and populating it with gear objects.

Parameters
memory_poolA pointer to the memory pool used for memory allocation.
db_connectionA pointer to the database connection used to fetch gear data.
ability_tableA pointer to the ability table containing the available abilities.
Returns
A pointer to the initialized gear_table_t object or NULL if initialization fails.

Definition at line 45 of file gear.c.

45 {
46 NULL_PTR_HANDLER_RETURN(memory_pool, NULL, "Gear", "Memory pool is NULL");
47
48 gear_init_t* rows = init_gear_table_from_db(db_connection);
49 NULL_PTR_HANDLER_RETURN(rows, NULL, "Gear", "Could not fetch gear data from DB");
50
51 int count = count_gear_in_db(db_connection);
52
53 gear_table_t* table = memory_pool_alloc(memory_pool, sizeof(gear_table_t));
54 NULL_PTR_HANDLER_RETURN(table, NULL, "Gear", "Failed to allocate gear table");
55
56 table->num_gears = count;
57 NULL_PTR_HANDLER_RETURN(table->gears, NULL, "Gear", "Failed to allocate gear array for table");
58
59 for (int i = 0; i < count; ++i) {
60 table->gears[i] = init_gear(memory_pool,
61 rows[i].name,
62 rows[i].gear_identifier,
63 rows[i].slot,
64 rows[i].stats,
65 rows[i].defenses,
66 ability_table,
67 rows[i].ability_names,
68 rows[i].num_abilities);
69 }
70 free_gear_table_from_db(rows, db_connection);
71 return table;
72}
gear_t * init_gear(memory_pool_t *memory_pool, const char *name, gear_identifier_t gear_identifier, gear_slot_t slot, stats_t stats, defenses_t defenses, ability_table_t *ability_table, ability_names_t *abilities, int num_abilities)
Initializes a gear object.
Definition gear.c:14
gear_init_t * init_gear_table_from_db(const db_connection_t *db_connection)
Get the gear table from the database.
void free_gear_table_from_db(gear_init_t *gear_init_table, const db_connection_t *db_connection)
Clean up the gear table Call this function to free the memory allocated for the gear table.
int count_gear_in_db(const db_connection_t *db_connection)
Count the number of gears in the database.
To get gear table from the database, we need to define a struct This struct is for the initialization...