Improve sorting
This commit is contained in:
@@ -169,10 +169,52 @@ bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item) {
|
||||
return inventoryGetCount(inventory, item) == ITEM_STACK_QUANTITY_MAX;
|
||||
}
|
||||
|
||||
void inventorySortById(inventory_t *inventory) {
|
||||
// Sorters
|
||||
int_t inventorySortById(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;
|
||||
}
|
||||
|
||||
int_t inventorySortByIdReverse(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;
|
||||
}
|
||||
|
||||
int_t inventorySortByType(const void *a, const void *b) {
|
||||
const inventorystack_t *stackA = (const inventorystack_t*)a;
|
||||
const inventorystack_t *stackB = (const inventorystack_t*)b;
|
||||
const itemtype_t typeA = ITEMS[stackA->item].type;
|
||||
const itemtype_t typeB = ITEMS[stackB->item].type;
|
||||
if(typeA < typeB) return -1;
|
||||
if(typeA > typeB) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int_t inventorySortByTypeReverse(const void *a, const void *b) {
|
||||
const inventorystack_t *stackA = (const inventorystack_t*)a;
|
||||
const inventorystack_t *stackB = (const inventorystack_t*)b;
|
||||
const itemtype_t typeA = ITEMS[stackA->item].type;
|
||||
const itemtype_t typeB = ITEMS[stackB->item].type;
|
||||
if(typeA < typeB) return 1;
|
||||
if(typeA > typeB) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void inventorySort(
|
||||
inventory_t *inventory,
|
||||
const inventorysort_t sortBy,
|
||||
const bool_t reverse
|
||||
) {
|
||||
assertNotNull(inventory, "Inventory pointer is NULL.");
|
||||
assertNotNull(inventory->storage, "Storage pointer is NULL.");
|
||||
assertTrue(inventory->storageSize > 0, "Storage too small.");
|
||||
assertTrue(sortBy < INVENTORY_SORT_COUNT, "Invalid sort type.");
|
||||
|
||||
// Get count of used stacks
|
||||
size_t count = 0;
|
||||
@@ -185,13 +227,25 @@ void inventorySortById(inventory_t *inventory) {
|
||||
|
||||
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;
|
||||
// Comparator
|
||||
sortcompare_t comparator = NULL;
|
||||
switch(sortBy) {
|
||||
case INVENTORY_SORT_BY_ID: {
|
||||
comparator = reverse ? inventorySortByIdReverse : inventorySortById;
|
||||
break;
|
||||
};
|
||||
|
||||
case INVENTORY_SORT_BY_TYPE: {
|
||||
comparator = reverse ? inventorySortByTypeReverse : inventorySortByType;
|
||||
break;
|
||||
};
|
||||
|
||||
default:
|
||||
assertUnreachable("Invalid sort type.");
|
||||
break;
|
||||
}
|
||||
|
||||
assertNotNull(comparator, "Comparator function is NULL.");
|
||||
|
||||
sort((void*)inventory->storage, count, sizeof(inventorystack_t), comparator);
|
||||
}
|
||||
@@ -10,6 +10,13 @@
|
||||
|
||||
#define ITEM_STACK_QUANTITY_MAX UINT8_MAX
|
||||
|
||||
typedef enum {
|
||||
INVENTORY_SORT_BY_ID,
|
||||
INVENTORY_SORT_BY_TYPE,
|
||||
|
||||
INVENTORY_SORT_COUNT
|
||||
} inventorysort_t;
|
||||
|
||||
typedef struct {
|
||||
itemid_t item;
|
||||
uint8_t quantity;
|
||||
@@ -103,15 +110,14 @@ bool_t inventoryIsFull(const inventory_t *inventory);
|
||||
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item);
|
||||
|
||||
/**
|
||||
* Sorts the inventory by item ID in ascending order.
|
||||
* Sorts the inventory based on the specified criteria.
|
||||
*
|
||||
* @param inventory The inventory to sort.
|
||||
* @param sortBy The sorting criteria.
|
||||
* @param reverse Whether to sort in reverse order.
|
||||
*/
|
||||
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);
|
||||
void inventorySort(
|
||||
inventory_t *inventory,
|
||||
const inventorysort_t sortBy,
|
||||
const bool_t reverse
|
||||
);
|
||||
@@ -19,11 +19,11 @@ const item_t ITEMS[] = {
|
||||
|
||||
// Potato
|
||||
[ITEM_ID_POTATO] = {
|
||||
.type = ITEM_TYPE_INGREDIENT
|
||||
.type = ITEM_TYPE_FOOD
|
||||
},
|
||||
|
||||
// Apple
|
||||
[ITEM_ID_APPLE] = {
|
||||
.type = ITEM_TYPE_INGREDIENT
|
||||
.type = ITEM_TYPE_FOOD
|
||||
}
|
||||
};
|
||||
@@ -13,6 +13,7 @@ typedef enum {
|
||||
|
||||
ITEM_TYPE_MEDICINE,
|
||||
ITEM_TYPE_INGREDIENT,
|
||||
ITEM_TYPE_FOOD,
|
||||
ITEM_TYPE_KEYITEM,
|
||||
|
||||
ITEM_TYPE_COUNT
|
||||
|
||||
Reference in New Issue
Block a user