Backpack changes
This commit is contained in:
@@ -7,9 +7,72 @@
|
|||||||
|
|
||||||
#include "backpack.h"
|
#include "backpack.h"
|
||||||
|
|
||||||
inventorystack_t BACKPACK_STORAGE[BACKPACK_STORAGE_SIZE_MAX];
|
backpack_t BACKPACK;
|
||||||
inventory_t BACKPACK;
|
|
||||||
|
|
||||||
void backpackInit() {
|
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);
|
||||||
}
|
}
|
||||||
@@ -8,12 +8,90 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "inventory.h"
|
#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 backpack_t BACKPACK;
|
||||||
extern inventory_t BACKPACK;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the backpack inventory for the player.
|
* Initializes the backpack inventory for the player.
|
||||||
*/
|
*/
|
||||||
void backpackInit();
|
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
|
||||||
|
);
|
||||||
@@ -48,6 +48,11 @@ for i in item_ids:
|
|||||||
count += 1
|
count += 1
|
||||||
id_count = count
|
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
|
# Build output
|
||||||
out = [
|
out = [
|
||||||
"#pragma once",
|
"#pragma once",
|
||||||
@@ -91,13 +96,19 @@ for i in item_ids:
|
|||||||
out += [
|
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:
|
for t in item_types:
|
||||||
out.append(f" \"{type_enum(t)} = {type_values[t]}\\n\"")
|
out.append(f" [{type_enum(t)}] = {type_item_counts[t]},")
|
||||||
out += [";", ""]
|
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)
|
os.makedirs(os.path.dirname(args.output), exist_ok=True)
|
||||||
with open(args.output, "w", encoding="utf-8") as f:
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
|
|||||||
Reference in New Issue
Block a user