3 #include "../inventory/inventory.h"
5 static struct color gray;
6 static struct color darkgray;
8 static bool use_item(struct itemstack *stack)
14 struct inventory player_inventory = {NULL};
16 static int selected_index = 0;
18 void inventory_add(struct inventory *self, struct itemstack stack)
20 struct list **ptr = &self->stacks;
22 if (stack.item->stackable) {
23 for (; *ptr != NULL; ptr = &(*ptr)->next) {
24 struct itemstack *other = (*ptr)->element;
25 if (other->item == stack.item) {
26 other->count += stack.count;
32 struct itemstack *buf = make_buffer(&stack, sizeof(struct itemstack));
33 *ptr = add_element(*ptr, buf);
35 if (buf->item->on_create)
36 buf->item->on_create(buf);
41 bool inventory_remove(struct inventory *self, struct itemstack *stack)
43 stack.count = -stack.count;
45 for (struct list **ptr = &self->stacks; *ptr != NULL; ) {
46 struct itemstack *other = (*ptr)->element;
48 if (other->item == stack.item) {
49 stack.count += other->count;
51 if (stack.count > 0) {
52 other->count = stack.count;
55 struct list *next = ptr->next;
59 if (other->item->on_destroy)
60 other->item->on_destroy(other);
78 static void decrease_item_count(struct list **ptr, struct itemstack *stack)
82 if (stack->count == 0) {
83 struct list *next = (*ptr)->next;
85 if (stack->item->on_destroy)
86 stack->item->on_destroy(stack);
98 bool inventory_remove(struct inventory *self, struct item *item)
100 for (struct list **ptr = &self->stacks; *ptr != NULL; ptr = &(*ptr)->next) {
101 struct itemstack *stack = (*ptr)->element;
103 if (stack->item == item) {
104 decrease_item_count(ptr, stack);
113 static void handle_enter()
117 for (struct list **ptr = &player_inventory.stacks; *ptr != NULL; ptr = &(*ptr)->next, i++) {
118 if (i == selected_index) {
119 struct itemstack *stack = (*ptr)->element;
121 if (stack->item->on_use && stack->item->on_use(stack))
122 decrease_item_count(ptr, stack);
129 static void handle_arrow()
131 char c = fgetc(stdin);
133 char dir = fgetc(stdin);
137 for (struct list *ptr = player_inventory.stacks; ptr != NULL; ptr = ptr->next)
147 if (selected_index < 0)
148 selected_index = count - 1;
153 if (selected_index >= count)
163 static void render_inventory(struct winsize ws)
167 printf(" \e[1mInventory\e[21m\n");
169 set_color(gray, false);
172 for (struct list *ptr = player_inventory.stacks; ptr != NULL; ptr = ptr->next, i++) {
173 struct itemstack *stack = ptr->element;
175 if (i == selected_index) {
177 set_color(gray, false);
182 printf("%s", stack->item->name);
184 if (stack->count > 1) {
185 set_color(darkgray, false);
186 printf(" (x%u)", stack->count);
187 set_color(gray, false);
194 __attribute__ ((constructor)) static void init()
196 gray = get_color("#9E9E9E");
197 darkgray = get_color("#555555");
199 register_render_component(&render_inventory);
201 register_input_handler('\033', (struct input_handler) {
202 .run_if_dead = false,
203 .callback = &handle_arrow,
206 register_input_handler('\n', (struct input_handler) {
207 .run_if_dead = false,
208 .callback = &handle_enter,