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);
|
||||
}
|
||||
Reference in New Issue
Block a user