aboutsummaryrefslogtreecommitdiff
path: root/plugins/loot
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/loot')
-rw-r--r--plugins/loot/Makefile4
-rw-r--r--plugins/loot/dependencies.txt2
-rw-r--r--plugins/loot/loot.c63
-rw-r--r--plugins/loot/loot.h16
4 files changed, 85 insertions, 0 deletions
diff --git a/plugins/loot/Makefile b/plugins/loot/Makefile
new file mode 100644
index 0000000..bd65c69
--- /dev/null
+++ b/plugins/loot/Makefile
@@ -0,0 +1,4 @@
+plugins/loot/loot.so: plugins/loot/loot.c plugins/loot/loot.h plugins/game/game.h plugins/inventory/inventory.h
+ cc -g -shared -fpic -o plugins/loot/loot.so plugins/loot/loot.c
+
+PLUGINS := ${PLUGINS} plugins/loot/loot.so
diff --git a/plugins/loot/dependencies.txt b/plugins/loot/dependencies.txt
new file mode 100644
index 0000000..5968537
--- /dev/null
+++ b/plugins/loot/dependencies.txt
@@ -0,0 +1,2 @@
+game
+inventory
diff --git a/plugins/loot/loot.c b/plugins/loot/loot.c
new file mode 100644
index 0000000..87d51c9
--- /dev/null
+++ b/plugins/loot/loot.c
@@ -0,0 +1,63 @@
+#include <stdlib.h>
+#include "../loot/loot.h"
+#include "../game/game.h"
+
+static struct list *loot_list = NULL;
+
+void register_loot(struct loot loot)
+{
+ loot_list = add_element(loot_list, make_buffer(&loot, sizeof(struct loot)));
+}
+
+static void loot_step(struct entity *self, struct entity_step_data stepdata)
+{
+ if (stepdata.dx == 0 && stepdata.dy == 0) {
+ for (struct list *ptr = loot_list; ptr != NULL; ptr = ptr->next) {
+ struct loot *loot = ptr->element;
+ if (rand() % loot->chance == 0) {
+ inventory_add(&player_inventory, (struct itemstack) {
+ .item = loot->item,
+ .count = loot->min + (loot->max > loot->min ? rand() % (loot->max - loot->min + 1) : 0),
+ .meta = NULL,
+ });
+ }
+ }
+ self->remove = true;
+ }
+}
+
+static struct entity loot_entity = {
+ .name = "loot",
+ .x = 0,
+ .y = 0,
+ .color = {0},
+ .use_color = false,
+ .texture = "🎁",
+ .remove = false,
+ .meta = NULL,
+ .health = 1,
+ .max_health = 1,
+ .collide_with_entities = false,
+
+ .on_step = &loot_step,
+ .on_collide = NULL,
+ .on_collide_with_entity = NULL,
+ .on_spawn = NULL,
+ .on_remove = NULL,
+ .on_death = NULL,
+ .on_damage = NULL,
+};
+
+static void spawn_loot(int x, int y, enum mg_context ctx)
+{
+ spawn(loot_entity, x, y, NULL);
+}
+
+__attribute__((constructor)) static void init()
+{
+ register_air_function((struct generator_function) {
+ .corridor_chance = 0,
+ .room_chance = 250,
+ .callback = &spawn_loot,
+ });
+}
diff --git a/plugins/loot/loot.h b/plugins/loot/loot.h
new file mode 100644
index 0000000..41fda44
--- /dev/null
+++ b/plugins/loot/loot.h
@@ -0,0 +1,16 @@
+#ifndef _LOOT_H_
+#define _LOOT_H_
+
+#include "../inventory/inventory.h"
+
+struct loot
+{
+ struct item *item;
+ int chance;
+ int min;
+ int max;
+};
+
+void register_loot(struct loot loot);
+
+#endif