]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/sword/sword.c
Add loot boxes
[dungeon_game.git] / plugins / sword / sword.c
1 #include <stdlib.h>
2 #include <stddef.h>
3 #include "../game/game.h"
4 #include "../movement/movement.h"
5 #include "../inventory/inventory.h"
6 #include "../recharge/recharge.h"
7 #include "../loot/loot.h"
8
9 static bool use_broken_sword(struct itemstack *stack)
10 {
11         return true;
12 }
13
14 static struct item broken_sword = {
15         .name = "Broken Sword",
16         .stackable = false,
17
18         .on_use = &use_broken_sword,
19         .on_destroy = NULL,
20         .on_create = NULL,
21 };
22
23 static bool use_sword(struct itemstack *stack)
24 {
25         if (! is_charged())
26                 return false;
27
28         int x, y;
29         x = player.x;
30         y = player.y;
31
32         dir_to_xy(last_player_move, &x, &y);
33
34         struct entity *entity = entity_collision_map[x][y];
35
36         if (entity) {
37                 add_health(entity, -1);
38
39                 if (rand() % 100 == 0)
40                         stack->item = &broken_sword;
41
42                 recharge(1.0, "⚔ ");
43         }
44
45         return false;
46 }
47
48 static struct item sword = {
49         .name = "Sword",
50         .stackable = false,
51
52         .on_use = &use_sword,
53         .on_destroy = NULL,
54         .on_create = NULL,
55 };
56
57 static void handle_e()
58 {
59         struct itemstack *stack = inventory_find(&player_inventory, &sword);
60
61         if (stack)
62                 use_sword(stack);
63 }
64
65 __attribute__((constructor)) static void init()
66 {
67         inventory_add(&player_inventory, (struct itemstack) {
68                 .item = &sword,
69                 .count = 1,
70                 .meta = NULL,
71         });
72
73         register_input_handler('e', (struct input_handler) {
74                 .run_if_dead = false,
75                 .callback = &handle_e,
76         });
77
78         register_loot((struct loot) {
79                 .chance = 7,
80                 .item = &sword,
81                 .min = 1,
82                 .max = 1,
83         });
84 }