From 24d26b094c81390bcfd54a340fe94532e0be3945 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 27 Feb 2018 08:14:54 +0000 Subject: [PATCH] Sort inventory when item is removed. --- src/game/game.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/game/game.c b/src/game/game.c index ae725ec..b785c5d 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void loadMetaInfo(void); static void addKeyToStash(Item *item); +static int sortItems(const void *a, const void *b); void initGame(void) { @@ -141,6 +142,7 @@ void removeItem(char *name) if (item->type != ET_KEY) { world.bob->items[i] = NULL; + qsort(world.bob->items, MAX_ITEMS, sizeof(Entity*), sortItems); return; } else @@ -149,8 +151,9 @@ void removeItem(char *name) { item->flags &= ~EF_GONE; item->alive = ALIVE_DEAD; - world.bob->items[i] = NULL; + qsort(world.bob->items, MAX_ITEMS, sizeof(Entity*), sortItems); + return; } } } @@ -448,6 +451,25 @@ void restoreGameState(void) free(text); } +static int sortItems(const void *a, const void *b) +{ + Entity *e1 = *((Entity**)a); + Entity *e2 = *((Entity**)b); + + if (!e1) + { + return 1; + } + else if (!e2) + { + return -1; + } + else + { + return 0; + } +} + void destroyGame(void) { }