From 1e890f6d86ca3e77cd3cfb7bcfde30c43cbf1dcf Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 26 Jun 2026 09:04:57 -0500 Subject: [PATCH] Backpack changes --- src/dusk/rpg/item/backpack.c | 69 +++++++++++++++++++++++++++-- src/dusk/rpg/item/backpack.h | 86 ++++++++++++++++++++++++++++++++++-- tools/item/csv/__main__.py | 21 ++++++--- 3 files changed, 164 insertions(+), 12 deletions(-) diff --git a/src/dusk/rpg/item/backpack.c b/src/dusk/rpg/item/backpack.c index 4a36e0e8..4ed1be20 100644 --- a/src/dusk/rpg/item/backpack.c +++ b/src/dusk/rpg/item/backpack.c @@ -7,9 +7,72 @@ #include "backpack.h" -inventorystack_t BACKPACK_STORAGE[BACKPACK_STORAGE_SIZE_MAX]; -inventory_t BACKPACK; +backpack_t BACKPACK; void backpackInit() { - inventoryInit(&BACKPACK, BACKPACK_STORAGE, BACKPACK_STORAGE_SIZE_MAX); + for(uint8_t i = 0; i < ITEM_TYPE_COUNT; i++) { + inventoryInit( + &BACKPACK.inventories[i], + BACKPACK.storage[i], + ITEM_TYPE_COUNT_MAX + ); + } +} + +inventory_t *backpackGetInventory(const itemtype_t type) { + assertTrue(type > ITEM_TYPE_NULL, "Item type must not be null"); + assertTrue(type < ITEM_TYPE_COUNT, "Item type out of range"); + return &BACKPACK.inventories[type]; +} + +void backpackAdd(const itemid_t item, const uint8_t quantity) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + inventoryAdd(backpackGetInventory(ITEMS[item].type), item, quantity); +} + +void backpackRemove(const itemid_t item) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + inventoryRemove(backpackGetInventory(ITEMS[item].type), item); +} + +void backpackSet(const itemid_t item, const uint8_t quantity) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + inventorySet(backpackGetInventory(ITEMS[item].type), item, quantity); +} + +uint8_t backpackGetCount(const itemid_t item) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + return inventoryGetCount(backpackGetInventory(ITEMS[item].type), item); +} + +bool_t backpackItemExists(const itemid_t item) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + return inventoryItemExists(backpackGetInventory(ITEMS[item].type), item); +} + +bool_t backpackIsFull(const itemtype_t type) { + assertTrue(type > ITEM_TYPE_NULL, "Item type must not be null"); + assertTrue(type < ITEM_TYPE_COUNT, "Item type out of range"); + return inventoryIsFull(backpackGetInventory(type)); +} + +bool_t backpackItemFull(const itemid_t item) { + assertTrue(item > ITEM_ID_NULL, "Item ID must not be null"); + assertTrue(item < ITEM_ID_COUNT, "Item ID out of range"); + return inventoryItemFull(backpackGetInventory(ITEMS[item].type), item); +} + +void backpackSort( + const itemtype_t type, + const inventorysort_t sortBy, + const bool_t reverse +) { + assertTrue(type > ITEM_TYPE_NULL, "Item type must not be null"); + assertTrue(type < ITEM_TYPE_COUNT, "Item type out of range"); + inventorySort(backpackGetInventory(type), sortBy, reverse); } \ No newline at end of file diff --git a/src/dusk/rpg/item/backpack.h b/src/dusk/rpg/item/backpack.h index 4cb49358..5812befa 100644 --- a/src/dusk/rpg/item/backpack.h +++ b/src/dusk/rpg/item/backpack.h @@ -8,12 +8,90 @@ #pragma once #include "inventory.h" -#define BACKPACK_STORAGE_SIZE_MAX 20 +typedef struct { + inventorystack_t storage[ITEM_TYPE_COUNT][ITEM_TYPE_COUNT_MAX]; + inventory_t inventories[ITEM_TYPE_COUNT]; +} backpack_t; -extern inventorystack_t BACKPACK_STORAGE[BACKPACK_STORAGE_SIZE_MAX]; -extern inventory_t BACKPACK; +extern backpack_t BACKPACK; /** * Initializes the backpack inventory for the player. */ -void backpackInit(); \ No newline at end of file +void backpackInit(); + +/** + * Returns the inventory for a given item type. + * + * @param type The item type. + * @returns Pointer to the inventory for that type. + */ +inventory_t *backpackGetInventory(const itemtype_t type); + +/** + * Adds a quantity of an item to the backpack. + * + * @param item The item ID to add. + * @param quantity The quantity to add. + */ +void backpackAdd(const itemid_t item, const uint8_t quantity); + +/** + * Removes an item from the backpack. + * + * @param item The item ID to remove. + */ +void backpackRemove(const itemid_t item); + +/** + * Sets the quantity of an item in the backpack. + * + * @param item The item ID to set. + * @param quantity The quantity to set. + */ +void backpackSet(const itemid_t item, const uint8_t quantity); + +/** + * Gets the quantity of an item in the backpack. + * + * @param item The item ID to check. + * @returns The quantity held. + */ +uint8_t backpackGetCount(const itemid_t item); + +/** + * Checks if an item exists in the backpack (quantity > 0). + * + * @param item The item ID to check. + * @returns true if the item exists. + */ +bool_t backpackItemExists(const itemid_t item); + +/** + * Checks if the inventory for a given type is full. + * + * @param type The item type to check. + * @returns true if the type's inventory is full. + */ +bool_t backpackIsFull(const itemtype_t type); + +/** + * Checks if an item's stack is full in the backpack. + * + * @param item The item ID to check. + * @returns true if the item stack is full. + */ +bool_t backpackItemFull(const itemid_t item); + +/** + * Sorts the inventory for a given item type. + * + * @param type The item type whose inventory to sort. + * @param sortBy The sorting criteria. + * @param reverse Whether to sort in reverse order. + */ +void backpackSort( + const itemtype_t type, + const inventorysort_t sortBy, + const bool_t reverse +); \ No newline at end of file diff --git a/tools/item/csv/__main__.py b/tools/item/csv/__main__.py index bd3b75c7..40d0a0b8 100644 --- a/tools/item/csv/__main__.py +++ b/tools/item/csv/__main__.py @@ -48,6 +48,11 @@ for i in item_ids: count += 1 id_count = count +# Count items per type +type_item_counts = { t: 0 for t in item_types } +for i in item_ids: + type_item_counts[rows[i]["type"]] += 1 + # Build output out = [ "#pragma once", @@ -91,13 +96,19 @@ for i in item_ids: out += [ "};", "", - "static const char_t *ITEM_SCRIPT =", + "static const uint8_t ITEM_TYPE_COUNTS[] = {", ] -for i in item_ids: - out.append(f" \"{id_enum(i)} = {id_values[i]}\\n\"") for t in item_types: - out.append(f" \"{type_enum(t)} = {type_values[t]}\\n\"") -out += [";", ""] + out.append(f" [{type_enum(t)}] = {type_item_counts[t]},") +out += [ + "};", + "", +] +max_type_count = max(type_item_counts.values()) if type_item_counts else 0 +out += [ + f"#define ITEM_TYPE_COUNT_MAX {max_type_count}", + "", +] os.makedirs(os.path.dirname(args.output), exist_ok=True) with open(args.output, "w", encoding="utf-8") as f: