From: Elias Fleckenstein Date: Sun, 13 Jun 2021 18:41:17 +0000 (+0200) Subject: Add on_create callback for items X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=bfcda608695b83ce6a3caefceaa79c8e392405d7;p=dungeon_game.git Add on_create callback for items --- diff --git a/plugins/cherry/cherry.c b/plugins/cherry/cherry.c index 436fcf9..f998770 100644 --- a/plugins/cherry/cherry.c +++ b/plugins/cherry/cherry.c @@ -18,6 +18,7 @@ static struct item cherry_item = { .on_use = &use_cherry, .on_destroy = NULL, + .on_create = NULL, }; static void cherry_step(struct entity *self, struct entity_step_data stepdata) diff --git a/plugins/fireball/fireball.c b/plugins/fireball/fireball.c index fcb1ac1..39e93fb 100644 --- a/plugins/fireball/fireball.c +++ b/plugins/fireball/fireball.c @@ -94,6 +94,7 @@ static struct item fireball_item = { .on_use = &shoot_fireball_item, .on_destroy = NULL, + .on_create = NULL, }; static void shoot_if_has_fireball() diff --git a/plugins/inventory/inventory.c b/plugins/inventory/inventory.c index c3b533d..9923561 100644 --- a/plugins/inventory/inventory.c +++ b/plugins/inventory/inventory.c @@ -29,7 +29,11 @@ void inventory_add(struct inventory *self, struct itemstack stack) } } - *ptr = add_element(*ptr, make_buffer(&stack, sizeof(struct itemstack))); + struct itemstack *buf = make_buffer(&stack, sizeof(struct itemstack)); + *ptr = add_element(*ptr, buf); + + if (buf->item->on_create) + buf->item->on_create(buf); } /* @@ -81,6 +85,9 @@ static void decrease_item_count(struct list **ptr, struct itemstack *stack) if (stack->item->on_destroy) stack->item->on_destroy(stack); + if (stack->meta) + free(stack->meta); + free(stack); free(*ptr); diff --git a/plugins/inventory/inventory.h b/plugins/inventory/inventory.h index 4b13f94..437ffb6 100644 --- a/plugins/inventory/inventory.h +++ b/plugins/inventory/inventory.h @@ -16,6 +16,7 @@ struct item bool (*on_use)(struct itemstack *stack); void (*on_destroy)(struct itemstack *stack); + void (*on_create)(struct itemstack *stack); }; struct inventory