Made a big mess of the codebase
This commit is contained in:
10
src/rpg/item/CMakeLists.txt
Normal file
10
src/rpg/item/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
inventory.c
|
||||
)
|
68
src/rpg/item/inventory.c
Normal file
68
src/rpg/item/inventory.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "inventory.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void inventoryInit(inventory_t *inventory, const uint8_t size) {
|
||||
assertNotNull(inventory, "inventory must not be NULL");
|
||||
assertTrue(size <= INVENTORY_SIZE_MAX, "size exceeding INVENTORY_SIZE_MAX");
|
||||
assertTrue(size > 0, "size must be greater than 0");
|
||||
|
||||
memoryZero(inventory, sizeof(inventory_t));
|
||||
inventory->size = size;
|
||||
}
|
||||
|
||||
uint8_t inventoryItemIndexByType(
|
||||
const inventory_t *inventory,
|
||||
const itemtype_t type
|
||||
) {
|
||||
assertNotNull(inventory, "inventory must not be NULL");
|
||||
assertTrue(type > ITEM_TYPE_NULL, "type must be greater than ITEM_TYPE_NULL");
|
||||
|
||||
uint8_t item = inventory->itemCount;
|
||||
while(item--) {
|
||||
if(inventory->items[item].type == type) return item;
|
||||
}
|
||||
return INVENTORY_SIZE_MAX;
|
||||
}
|
||||
|
||||
uint8_t inventoryItemCount(
|
||||
const inventory_t *inventory,
|
||||
const itemtype_t type
|
||||
) {
|
||||
assertNotNull(inventory, "inventory must not be NULL");
|
||||
assertTrue(type > ITEM_TYPE_NULL, "type must be greater than ITEM_TYPE_NULL");
|
||||
|
||||
const uint8_t index = inventoryItemIndexByType(inventory, type);
|
||||
if(index == INVENTORY_SIZE_MAX) return 0;
|
||||
return inventory->items[index].count;
|
||||
}
|
||||
|
||||
void inventoryItemSet(
|
||||
inventory_t *inventory,
|
||||
const itemtype_t type,
|
||||
const uint8_t count
|
||||
) {
|
||||
assertNotNull(inventory, "inventory must not be NULL");
|
||||
assertTrue(type > ITEM_TYPE_NULL, "type must be greater than ITEM_TYPE_NULL");
|
||||
assertTrue(count > 0, "count must be greater than 0");
|
||||
|
||||
const uint8_t index = inventoryItemIndexByType(inventory, type);
|
||||
|
||||
if(index == INVENTORY_SIZE_MAX) {
|
||||
// Item does not exist, add it
|
||||
assertTrue(inventory->itemCount < inventory->size, "inventory is full");
|
||||
inventory->items[inventory->itemCount].type = type;
|
||||
inventory->items[inventory->itemCount].count = count;
|
||||
inventory->itemCount++;
|
||||
} else {
|
||||
// Item exists, update the count
|
||||
inventory->items[index].count = count;
|
||||
}
|
||||
}
|
63
src/rpg/item/inventory.h
Normal file
63
src/rpg/item/inventory.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "itemstack.h"
|
||||
|
||||
#define INVENTORY_SIZE_MAX UINT8_MAX
|
||||
|
||||
typedef struct {
|
||||
itemstack_t items[INVENTORY_SIZE_MAX];
|
||||
uint8_t itemCount;
|
||||
uint8_t size;
|
||||
} inventory_t;
|
||||
|
||||
/**
|
||||
* Initializes an inventory with a specified size.
|
||||
*
|
||||
* @param inventory Pointer to the inventory to initialize.
|
||||
* @param size The size of the inventory (maximum is INVENTORY_SIZE_MAX).
|
||||
*/
|
||||
void inventoryInit(inventory_t *inventory, const uint8_t size);
|
||||
|
||||
/**
|
||||
* Finds the index of the item of a specified type in the inventory.
|
||||
*
|
||||
* @param inventory Pointer to the inventory to search.
|
||||
* @param type The type of item to find.
|
||||
* @return The index of the item, or INVENTORY_SIZE_MAX if not found.
|
||||
*/
|
||||
uint8_t inventoryItemIndexByType(
|
||||
const inventory_t *inventory,
|
||||
const itemtype_t type
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets the count of items of a specified type in the inventory.
|
||||
*
|
||||
* @param inventory Pointer to the inventory to check.
|
||||
* @param type The type of item to count.
|
||||
* @return The count of items of the specified type.
|
||||
*/
|
||||
uint8_t inventoryItemCount(
|
||||
const inventory_t *inventory,
|
||||
const itemtype_t type
|
||||
);
|
||||
|
||||
/**
|
||||
* Sets the count of items of a specified type in the inventory.
|
||||
* If the item does not exist, it will be added.
|
||||
*
|
||||
* @param inventory Pointer to the inventory to modify.
|
||||
* @param type The type of item to set.
|
||||
* @param count The count of items to set.
|
||||
*/
|
||||
void inventoryItemSet(
|
||||
inventory_t *inventory,
|
||||
const itemtype_t type,
|
||||
const uint8_t count
|
||||
);
|
14
src/rpg/item/itemstack.h
Normal file
14
src/rpg/item/itemstack.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "itemtype.h"
|
||||
|
||||
typedef struct {
|
||||
itemtype_t type;
|
||||
uint8_t count;
|
||||
} itemstack_t;
|
24
src/rpg/item/itemtype.h
Normal file
24
src/rpg/item/itemtype.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef enum {
|
||||
ITEM_TYPE_NULL = 0,
|
||||
|
||||
// MEDICINE
|
||||
ITEM_TYPE_POTION,
|
||||
|
||||
// INGREDIENTS
|
||||
ITEM_TYPE_ONION,
|
||||
ITEM_TYPE_SWEET_POTATO,
|
||||
ITEM_TYPE_CARROT,
|
||||
|
||||
// COOKED FOOD
|
||||
ITEM_TYPE_BAKED_SWEET_POTATO,
|
||||
} itemtype_t;
|
Reference in New Issue
Block a user