DungeonCrawl
Loading...
Searching...
No Matches
gear.c
Go to the documentation of this file.
1
5#include "gear.h"
6
8#include "../logging/logger.h"
9
10#include <stdio.h>
11#include <stdlib.h>
12
13
14gear_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) {
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}
43
44
45gear_table_t* init_gear_table(memory_pool_t* memory_pool, const db_connection_t* db_connection, ability_table_t* ability_table) {
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}
73
74void free_gear_table(memory_pool_t* memory_pool, gear_table_t* table) {
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}
85
86const char* gear_slot_to_string(gear_slot_t slot) {
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}
void free_gear_table(memory_pool_t *memory_pool, gear_table_t *table)
Frees the memory allocated for a gear table.
Definition gear.c:74
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_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.
Definition gear.c:45
const char * gear_slot_to_string(gear_slot_t slot)
Converts a gear slot enumeration value to its string representation.
Definition gear.c:86
Exposes functions for working with gear.
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.
Declares functions to load and free potion and gear tables from the game database,...
Header file for logging functionality of the game.
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.
This struct is used for the database connection in SQLite.
Definition database.h:22
To get gear table from the database, we need to define a struct This struct is for the initialization...
Definition gear.h:32