-
Notifications
You must be signed in to change notification settings - Fork 2
/
bag.c
153 lines (132 loc) · 3.03 KB
/
bag.c
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
#include "bag.h"
#include "flags.h"
struct bag_item {
u16 itemid;
u16 qty;
};
struct bag_pocket {
struct bag_item *items;
u32 num_items;
};
struct item {
u8 name[14];
u16 index;
u16 market_price;
u8 field_12;
u8 quality;
u8 *description;
u8 is_unique;
u8 field_19;
u8 pocket_number;
u8 type;
void *overworld_routine;
u8 usage;
u8 field_21;
u16 field_22;
void *battle_routine;
u32 field_28;
};
#define NUM_ITEMS 375
// 0203988C
extern struct bag_pocket bag_pockets[5];
// 083DB028
extern struct item items[NUM_ITEMS];
// 0809A084
bool bag_add_item(itemid it, u16 qty) {
if (!(itemid_get_pocket_number(it) << 24))
return 0;
u8 pocket = itemid_get_pocket_number(it) - 1;
struct bag_item *entry = bag_pockets[pocket].items;
for (uint i = 0; i < bag_pockets[pocket].num_items; i++, entry++) {
if (entry->itemid == it) {
// increase qty of existing entry
u16 new_qty = sav2_read16_xor(&entry->qty) + qty;
if (new_qty > 999)
return 0;
sav2_write16_xor(&entry->qty, new_qty);
return 1;
}
}
// if necessary give the player a HM case
if (pocket == POCKET_HM && check_item_2(ITEM_HM_CASE, 1u)) {
int slot = bag_find_space(1);
if (slot == -1)
return 0;
bag_pockets[1].items[slot].itemid = 364;
sav2_write16_xor(&bag_pockets[1].items[slot].qty, 1);
}
// if necessary give the player a berry pouch
if (pocket == POCKET_BERRY && check_item_2(ITEM_BERRY_POUCH, 1u)) {
int slot = bag_find_space(1);
if (slot == -1)
return 0;
bag_pockets[1].items[slot].itemid = ITEM_BERRY_POUCH;
sav2_write16_xor(&bag_pockets[1].items[slot].qty, 1);
flag_set(FLAG_HAS_BERRY_POUCH);
}
if (it == ITEM_BERRY_POUCH)
flag_set(FLAG_HAS_BERRY_POUCH);
int slot = bag_find_space(pocket);
if (slot == -1)
return 0;
struct bag_pocket *p = &bag_pockets[pocket];
entry = &p->items[slot];
entry->itemid = it;
sav2_write16_xor(&entry->qty, qty);
return 1;
}
// 0809A8A4
itemid itemid_sanitize(itemid i) {
if (i >= NUM_ITEMS) return 0;
return i;
}
// 0809A8BC
struct item *item_by_id(itemid i) {
i = itemid_sanitize(i);
return &items[i];
}
// 0809A8DC
u16 itemid_get_index(itemid i) {
i = itemid_sanitize(i);
return items[i].index;
}
// 0809A900
u16 itemid_get_market_price(itemid i) {
i = itemid_sanitize(i);
return items[i].market_price;
}
// 0809A924
u8 itemid_get_x12(itemid i) {
i = itemid_sanitize(i);
return items[i].field_12;
}
// 0809A948
u8 itemid_get_quality(itemid i) {
i = itemid_sanitize(i);
return items[i].quality;
}
// 0809A96C
const char *itemid_get_description(itemid i) {
i = itemid_sanitize(i);
return items[i].description;
}
// 0809A990
bool itemid_is_unique(itemid i) {
i = itemid_sanitize(i);
return items[i].is_unique;
}
// 0809A9B4
u8 itemid_get_x19(itemid i) {
i = itemid_sanitize(i);
return items[i].field_19;
}
// 0809A9D8
u8 itemid_get_pocket_number(itemid i) {
i = itemid_sanitize(i);
return items[i].pocket_number;
}
// 0809A9FC itemid_get_type
// 0809AA20 itemid_get_overworld_function
// 0809AA44 itemid_get_usage
// 0809AA68 itemid_get_inbattle_function
// 0809AA8C itemid_get_x28