aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-06-14 11:17:53 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-06-14 11:17:53 +0200
commiteaa7e66f3c11ecbe01f28dbc2b32271f4229438b (patch)
tree646858156045ea1a7b9d8f61c5312ec2936ed04f
parenta084f521e94bba28c4c2f3ecee8e9912f87c20d7 (diff)
downloaddungeon_game-eaa7e66f3c11ecbe01f28dbc2b32271f4229438b.tar.xz
Add inventory_find
-rw-r--r--plugins/inventory/inventory.c31
-rw-r--r--plugins/inventory/inventory.h1
2 files changed, 25 insertions, 7 deletions
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