]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add loot boxes
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 19:37:10 +0000 (21:37 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 19:37:10 +0000 (21:37 +0200)
14 files changed:
plugins/cherry/Makefile
plugins/cherry/cherry.c
plugins/cherry/cherry.h [new file with mode: 0644]
plugins/cherry/dependencies.txt
plugins/fireball/Makefile
plugins/fireball/dependencies.txt
plugins/fireball/fireball.c
plugins/game/game.c
plugins/loot/Makefile [new file with mode: 0644]
plugins/loot/dependencies.txt [new file with mode: 0644]
plugins/loot/loot.c [new file with mode: 0644]
plugins/loot/loot.h [new file with mode: 0644]
plugins/sword/Makefile
plugins/sword/sword.c

index edb1f4479bcb2e5ad7ff33d7ae9d42e5d0e72a4e..a561baaddb0e1fc4e14a2cadd76ff4c3cf91ef97 100644 (file)
@@ -1,4 +1,4 @@
-plugins/cherry/cherry.so: plugins/cherry/cherry.c plugins/game/game.h plugins/score/score.h plugins/inventory/inventory.h
+plugins/cherry/cherry.so: plugins/cherry/cherry.c plugins/game/game.h plugins/score/score.h plugins/inventory/inventory.h plugins/loot/loot.h
        cc -g -shared -fpic -o plugins/cherry/cherry.so plugins/cherry/cherry.c
 
 PLUGINS := ${PLUGINS} plugins/cherry/cherry.so
index 1b0ac8a5c664c063c013dae6c8d84f74a7ba9f1f..96f0a2aeafe012758b9ac679fb0acb2ffdbc34f2 100644 (file)
@@ -3,6 +3,7 @@
 #include "../game/game.h"
 #include "../score/score.h"
 #include "../inventory/inventory.h"
+#include "../loot/loot.h"
 
 static bool use_cherry(struct itemstack *stack)
 {
@@ -66,5 +67,12 @@ __attribute__((constructor)) static void init()
                .room_chance = 100,
                .callback = &spawn_cherry,
        });
+
+       register_loot((struct loot) {
+               .item = &cherry_item,
+               .chance = 2,
+               .min = 3,
+               .max = 10,
+       });
 }
 
diff --git a/plugins/cherry/cherry.h b/plugins/cherry/cherry.h
new file mode 100644 (file)
index 0000000..87ccbec
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef _CHERRY_H_
+#define _CHERRY_H_
+
+#include "../inventory/inventory.h"
+
+extern struct item cherry_item;
+
+#endif
index 49525146babbbe164dc7b4c109af88a135f3b380..d721cfd40abc20250365c29636a24ea7d27bc928 100644 (file)
@@ -1,3 +1,4 @@
 game
 score
 inventory
+loot
index 117c092c248a46af33dcd084a52fb2b4dd5260f8..24b068ec8e732a163bb8329cbf7c677f6d1c3c9f 100644 (file)
@@ -1,4 +1,4 @@
-plugins/fireball/fireball.so: plugins/fireball/fireball.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h
+plugins/fireball/fireball.so: plugins/fireball/fireball.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/loot/loot.h
        cc -g -shared -fpic -o plugins/fireball/fireball.so plugins/fireball/fireball.c
 
 PLUGINS := ${PLUGINS} plugins/fireball/fireball.so
index b19003bde03be8102a4864e0d05aea6178d989b0..32ba50f835436f84c311f939f7591c222c376000 100644 (file)
@@ -1,3 +1,4 @@
 game
 movement
 inventory
+loot
index a0e9a13fa8aa9d73b7ca7bd8d2cd285806fdad66..e771d08b1cf89c922a75b8c5eff341bb76d9441a 100644 (file)
@@ -3,6 +3,7 @@
 #include "../game/game.h"
 #include "../movement/movement.h"
 #include "../inventory/inventory.h"
+#include "../loot/loot.h"
 
 struct fireball_data
 {
@@ -113,4 +114,11 @@ __attribute__((constructor)) static void init()
                .count = 7,
                .meta = NULL,
        });
+
+       register_loot((struct loot) {
+               .item = &fireball_item,
+               .chance = 3,
+               .min = 4,
+               .max = 7,
+       });
 }
index cc6d21dffff5a163fba85636a2286f68db771152..ef2651fda5033e4e32b40dcb6d8e22555e93c727 100644 (file)
@@ -334,7 +334,9 @@ static void mapgen_set_air(int x, int y, enum mg_context ctx)
        for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
                struct generator_function *func = ptr->element;
 
-               if (rand() % (ctx == MG_CTX_CORRIDOR ? func->corridor_chance : func->room_chance) == 0)
+               int chance = ctx == MG_CTX_CORRIDOR ? func->corridor_chance : func->room_chance;
+
+               if (chance && rand() % chance == 0)
                        func->callback(x, y, ctx);
        }
 }
diff --git a/plugins/loot/Makefile b/plugins/loot/Makefile
new file mode 100644 (file)
index 0000000..bd65c69
--- /dev/null
@@ -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 (file)
index 0000000..5968537
--- /dev/null
@@ -0,0 +1,2 @@
+game
+inventory
diff --git a/plugins/loot/loot.c b/plugins/loot/loot.c
new file mode 100644 (file)
index 0000000..87d51c9
--- /dev/null
@@ -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 (file)
index 0000000..41fda44
--- /dev/null
@@ -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
index 034ba74031876a5aec0d0ced4aea2c65d6962b70..2173fa16e3df33d604c2350715ea2b6facf10489 100644 (file)
@@ -1,4 +1,4 @@
-plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/recharge/recharge.h
+plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/recharge/recharge.h plugins/loot/loot.h
        cc -g -shared -fpic -o plugins/sword/sword.so plugins/sword/sword.c
 
 PLUGINS := ${PLUGINS} plugins/sword/sword.so
index bac963f44c73d76b9937df0bd2629666af3d2ac2..31bb87ad8a7b4248e3f6200c1154351ab2e4495f 100644 (file)
@@ -4,6 +4,7 @@
 #include "../movement/movement.h"
 #include "../inventory/inventory.h"
 #include "../recharge/recharge.h"
+#include "../loot/loot.h"
 
 static bool use_broken_sword(struct itemstack *stack)
 {
@@ -73,4 +74,11 @@ __attribute__((constructor)) static void init()
                .run_if_dead = false,
                .callback = &handle_e,
        });
+
+       register_loot((struct loot) {
+               .chance = 7,
+               .item = &sword,
+               .min = 1,
+               .max = 1,
+       });
 }