Added memory checks
This commit is contained in:
9
src/item/backpack.h
Normal file
9
src/item/backpack.h
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "inventory.h"
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "inventory.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/sort.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void inventoryInit(
|
||||
@@ -113,7 +114,11 @@ void inventoryRemove(inventory_t *inventory, const itemid_t item) {
|
||||
if(stack->item != item) continue;
|
||||
|
||||
// Match found, shift everything else down
|
||||
memoryMove(stack, stack + 1, end - (stack + 1));
|
||||
memoryMove(
|
||||
stack,
|
||||
stack + 1,
|
||||
(end - (stack + 1)) * sizeof(inventorystack_t)
|
||||
);
|
||||
|
||||
// Clear last stack.
|
||||
inventorystack_t *last = end - 1;
|
||||
@@ -162,4 +167,31 @@ bool_t inventoryIsFull(const inventory_t *inventory) {
|
||||
|
||||
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item) {
|
||||
return inventoryGetCount(inventory, item) == ITEM_STACK_QUANTITY_MAX;
|
||||
}
|
||||
|
||||
void inventorySortById(inventory_t *inventory) {
|
||||
assertNotNull(inventory, "Inventory pointer is NULL.");
|
||||
assertNotNull(inventory->storage, "Storage pointer is NULL.");
|
||||
assertTrue(inventory->storageSize > 0, "Storage too small.");
|
||||
|
||||
// Get count of used stacks
|
||||
size_t count = 0;
|
||||
inventorystack_t *stack = inventory->storage;
|
||||
inventorystack_t *end = stack + inventory->storageSize;
|
||||
do {
|
||||
if(stack->item == ITEM_ID_NULL) break;
|
||||
count++;
|
||||
} while(++stack < end);
|
||||
|
||||
if(count == 0) return; // Nothing to sort
|
||||
|
||||
int_t comparator(const void *a, const void *b) {
|
||||
const inventorystack_t *stackA = (const inventorystack_t*)a;
|
||||
const inventorystack_t *stackB = (const inventorystack_t*)b;
|
||||
if(stackA->item < stackB->item) return -1;
|
||||
if(stackA->item > stackB->item) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
sort((void*)inventory->storage, count, sizeof(inventorystack_t), comparator);
|
||||
}
|
||||
@@ -100,4 +100,18 @@ bool_t inventoryIsFull(const inventory_t *inventory);
|
||||
* @param item The item ID to check.
|
||||
* @return true if the item stack is full, false otherwise.
|
||||
*/
|
||||
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item);
|
||||
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item);
|
||||
|
||||
/**
|
||||
* Sorts the inventory by item ID in ascending order.
|
||||
*
|
||||
* @param inventory The inventory to sort.
|
||||
*/
|
||||
void inventorySortById(inventory_t *inventory);
|
||||
|
||||
/**
|
||||
* Sorts the inventory by item type in ascending order.
|
||||
*
|
||||
* @param inventory The inventory to sort.
|
||||
*/
|
||||
void inventorySortByType(inventory_t *inventory);
|
||||
@@ -16,4 +16,14 @@ const item_t ITEMS[] = {
|
||||
[ITEM_ID_POTION] = {
|
||||
.type = ITEM_TYPE_MEDICINE
|
||||
},
|
||||
|
||||
// Potato
|
||||
[ITEM_ID_POTATO] = {
|
||||
.type = ITEM_TYPE_INGREDIENT
|
||||
},
|
||||
|
||||
// Apple
|
||||
[ITEM_ID_APPLE] = {
|
||||
.type = ITEM_TYPE_INGREDIENT
|
||||
}
|
||||
};
|
||||
@@ -12,10 +12,12 @@ typedef struct {
|
||||
itemtype_t type;
|
||||
} item_t;
|
||||
|
||||
typedef uint8_t itemid_t;
|
||||
typedef enum {
|
||||
ITEM_ID_NULL = 0,
|
||||
|
||||
#define ITEM_ID_NULL 0
|
||||
#define ITEM_ID_POTION 1
|
||||
#define ITEM_ID_POTATO 2
|
||||
ITEM_ID_POTION,
|
||||
ITEM_ID_POTATO,
|
||||
ITEM_ID_APPLE
|
||||
} itemid_t;
|
||||
|
||||
extern const item_t ITEMS[];
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "dusk.h"
|
||||
|
||||
typedef enum {
|
||||
ITEM_TYPE_NULL,
|
||||
ITEM_TYPE_NULL = 0,
|
||||
|
||||
ITEM_TYPE_MEDICINE,
|
||||
ITEM_TYPE_INGREDIENT,
|
||||
|
||||
Reference in New Issue
Block a user