1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include <stdio.h>
#include <stdlib.h>
#include "../inventory/inventory.h"
static struct color gray;
static struct color darkgray;
static bool use_item(struct itemstack *stack)
{
return true;
}
struct inventory player_inventory = {NULL};
static int selected_index = 0;
void inventory_add(struct inventory *self, struct itemstack stack)
{
struct list **ptr = &self->stacks;
if (stack.item->stackable) {
for (; *ptr != NULL; ptr = &(*ptr)->next) {
struct itemstack *other = (*ptr)->element;
if (other->item == stack.item) {
other->count += stack.count;
return;
}
}
}
struct itemstack *buf = make_buffer(&stack, sizeof(struct itemstack));
*ptr = add_element(*ptr, buf);
if (buf->item->on_create)
buf->item->on_create(buf);
}
/*
bool inventory_remove(struct inventory *self, struct itemstack *stack)
{
stack.count = -stack.count;
for (struct list **ptr = &self->stacks; *ptr != NULL; ) {
struct itemstack *other = (*ptr)->element;
if (other->item == stack.item) {
stack.count += other->count;
if (stack.count > 0) {
other->count = stack.count;
return true;
} else {
struct list *next = ptr->next;
other->count = 0;
if (other->item->on_destroy)
other->item->on_destroy(other);
free(other);
free(*ptr);
*ptr = next;
continue;
}
}
ptr = &(*ptr)->next;
}
return false;
}
*/
static void decrease_item_count(struct list **ptr)
{
struct itemstack *stack = (*ptr)->element;
stack->count--;
if (stack->count == 0) {
struct list *next = (*ptr)->next;
if (stack->item->on_destroy)
stack->item->on_destroy(stack);
if (stack->meta)
free(stack->meta);
free(stack);
free(*ptr);
*ptr = next;
}
}
static struct list **find_item(struct inventory *self, struct item *item)
{
for (struct list **ptr = &self->stacks; *ptr != NULL; ptr = &(*ptr)->next) {
struct itemstack *stack = (*ptr)->element;
if (stack->item == item)
return ptr;
}
return NULL;
}
bool inventory_remove(struct inventory *self, struct item *item)
{
struct list **ptr = find_item(self, item);
if (ptr) {
decrease_item_count(ptr);
return true;
}
return false;
}
struct itemstack *inventory_find(struct inventory *self, struct item *item)
{
struct list **ptr = find_item(self, item);
return ptr ? (*ptr)->element : NULL;
}
static void handle_enter()
{
int i = 0;
for (struct list **ptr = &player_inventory.stacks; *ptr != NULL; ptr = &(*ptr)->next, i++) {
if (i == selected_index) {
struct itemstack *stack = (*ptr)->element;
if (stack->item->on_use && stack->item->on_use(stack))
decrease_item_count(ptr);
return;
}
}
}
static void handle_arrow()
{
char c = fgetc(stdin);
if (c == '[') {
char dir = fgetc(stdin);
int count = 0;
for (struct list *ptr = player_inventory.stacks; ptr != NULL; ptr = ptr->next)
count++;
if (count == 0)
return;
switch (dir) {
case 'A':
selected_index--;
if (selected_index < 0)
selected_index = count - 1;
break;
case 'B':
selected_index++;
if (selected_index >= count)
selected_index = 0;
break;
}
} else {
ungetc(c, stdin);
}
}
static void render_inventory(struct winsize ws)
{
printf("\e[3;0H");
printf(" \e[1mInventory\e[21m\n");
set_color(gray, false);
int i = 0;
for (struct list *ptr = player_inventory.stacks; ptr != NULL; ptr = ptr->next, i++) {
struct itemstack *stack = ptr->element;
if (i == selected_index) {
printf(" \e[39m→ ");
set_color(gray, false);
} else {
printf(" ");
}
printf("%s", stack->item->name);
if (stack->count > 1) {
set_color(darkgray, false);
printf(" (x%u)", stack->count);
set_color(gray, false);
}
printf("\n");
}
}
__attribute__ ((constructor)) static void init()
{
gray = get_color("#9E9E9E");
darkgray = get_color("#555555");
register_render_component(&render_inventory);
register_input_handler('\033', (struct input_handler) {
.run_if_dead = false,
.callback = &handle_arrow,
});
register_input_handler('\n', (struct input_handler) {
.run_if_dead = false,
.callback = &handle_enter,
});
}
|