]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add inventory_find
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 09:17:53 +0000 (11:17 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 09:17:53 +0000 (11:17 +0200)
plugins/inventory/inventory.c
plugins/inventory/inventory.h

index 99235610499abb0a2eec2c78f56457af6a7207bc..66f663ea07c154b3ba7cb43c3fcc5b15c2f046c0 100644 (file)
@@ -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;
                }
index 437ffb646d427c1cc071a3d8b212efbc043a116f..62de9dcacca8843957b907af606d7979e5b1f81e 100644 (file)
@@ -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