From: Elias Fleckenstein Date: Mon, 14 Jun 2021 09:17:53 +0000 (+0200) Subject: Add inventory_find X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=eaa7e66f3c11ecbe01f28dbc2b32271f4229438b;p=dungeon_game.git Add inventory_find --- diff --git a/plugins/inventory/inventory.c b/plugins/inventory/inventory.c index 9923561..66f663e 100644 --- a/plugins/inventory/inventory.c +++ b/plugins/inventory/inventory.c @@ -75,8 +75,10 @@ bool inventory_remove(struct inventory *self, struct itemstack *stack) */ -static void decrease_item_count(struct list **ptr, struct itemstack *stack) +static void decrease_item_count(struct list **ptr) { + struct itemstack *stack = (*ptr)->element; + stack->count--; if (stack->count == 0) { @@ -95,21 +97,36 @@ static void decrease_item_count(struct list **ptr, struct itemstack *stack) } } -bool inventory_remove(struct inventory *self, struct item *item) +static struct list **find_item(struct inventory *self, struct item *item) { for (struct list **ptr = &self->stacks; *ptr != NULL; ptr = &(*ptr)->next) { struct itemstack *stack = (*ptr)->element; - if (stack->item == item) { - decrease_item_count(ptr, stack); + if (stack->item == item) + return ptr; + } + + return NULL; +} - return true; - } +bool inventory_remove(struct inventory *self, struct item *item) +{ + struct list **ptr = find_item(self, item); + + if (ptr) { + decrease_item_count(ptr); + return true; } return false; } +struct itemstack *inventory_find(struct inventory *self, struct item *item) +{ + struct list **ptr = find_item(self, item); + return ptr ? (*ptr)->element : NULL; +} + static void handle_enter() { int i = 0; @@ -119,7 +136,7 @@ static void handle_enter() struct itemstack *stack = (*ptr)->element; if (stack->item->on_use && stack->item->on_use(stack)) - decrease_item_count(ptr, stack); + decrease_item_count(ptr); return; } diff --git a/plugins/inventory/inventory.h b/plugins/inventory/inventory.h index 437ffb6..62de9dc 100644 --- a/plugins/inventory/inventory.h +++ b/plugins/inventory/inventory.h @@ -26,6 +26,7 @@ struct inventory void inventory_add(struct inventory *self, struct itemstack stack); bool inventory_remove(struct inventory *self, struct item *item); +struct itemstack *inventory_find(struct inventory *self, struct item *item); extern struct inventory player_inventory; #endif