summaryrefslogtreecommitdiff
path: root/src/ItemManager.cpp
blob: f21a763c20b1c792d001059ba2f135c88d462c8b (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "ItemManager.hpp"

extern u32 *data_027e0ce0[];

THUMB ItemManager* ItemManager::Create() {
    gItemManager = new(data_027e0ce0[1], 4) ItemManager();
    return gItemManager;
}

THUMB void ItemManager::Destroy() {
    delete gItemManager;
    gItemManager = 0;
}

THUMB void ItemManager::ClearPrevEquippedItem() {
    this->mPrevEquippedItem = ItemFlag_None;
}

THUMB NONMATCH void ItemManager::Save(SaveItemManager *save) {
    #ifndef NONMATCHING
    #include "../asm/ov00/ItemManager/ItemManager_Save.inc"
    #else
    save->itemFlags = this->mItemFlags;
    save->numRupees = this->mNumRupees;
    for (s32 i = 0; i < NUM_POTIONS; ++i) {
        save->potions[i] = this->mPotions[i];
    }
    save->numBombs = (u8) (*this->mAmmo)[ItemFlag_BombBag];
    save->numBombchus = (u8) (*this->mAmmo)[ItemFlag_BombchuBag];
    save->numArrows = (u8) (*this->mAmmo)[ItemFlag_Bow];
    save->equippedItem = (u8) this->mEquippedItem;
    save->salvagedTreasureFlags = this->mSalvagedTreasureFlags;
    save->hourglassSeconds = FastDivide(this->mHourglassSandFrames, 60);
    for (s32 i = 0; i < Gem_COUNT; ++i) {
        save->numGems[i] = this->mNumGems[i];
    }
    u8 *saveEquippedParts = save->equippedShipParts;
    const u32 *equippedParts = this->mEquippedShipParts;

    s32 i = 0;
    SaveItemManager *save2 = save;
    ItemManager *this2 = this;
    ShipParts (ItemManager::*shipParts)[ShipPart_COUNT] = &ItemManager::mShipParts;
    for (; i < ShipPart_COUNT; ++i) {
        save->equippedShipParts[i] = this->mEquippedShipParts[i];
        for (s32 j = 0; j < ShipType_COUNT; ++j) {
            u8 shipPartCount = ((ItemManager*) ((u32)this2 + j)->*shipParts)[0].parts[0];
            ((SaveItemManager*) ((u32)save2 + j))->shipParts[0].parts[0] = shipPartCount;
        }
        this2 = (ItemManager*) ((u32)this2 + sizeof(this2->mShipParts[0]));
        save2 = (SaveItemManager*) ((u32)save2 + sizeof(save2->shipParts[0]));
    }

    save->shipPartPricesShown = this->mShipPartPricesShown;
    for (s32 i = 0; i < Treasure_COUNT; ++i) {
        save->treasure[i] = this->mTreasure[i];
    }
    save->treasurePriceShownFlags[0] = this->mTreasurePriceShownFlags[0];
    for (s32 i = 0; i < 6; ++i) {
        save->unk_9f[i] = this->mUnk_098[i];
        save->unk_82[i] = this->mUnk_09e[i];
    }
    save->quiverSize = this->mQuiverSize;
    save->bombBagSize = this->mBombBagSize;
    save->bombchuBagSize = this->mBombchuBagSize;
    if (this->mEquippedFairy == FairyId_None) {
        save->equippedFairy = 3;
        return;
    }
    save->equippedFairy = (u8) this->mEquippedFairy;
    #endif
}

extern "C" bool _ZN11ItemManager7HasItemEj();
THUMB NONMATCH void ItemManager::Load(const SaveItemManager *save) {
    #ifndef NONMATCHING
    #include "../asm/ov00/ItemManager/ItemManager_Load.inc"
    #else
    this->mItemFlags = save->itemFlags;
    this->mNumRupees = save->numRupees;
    this->mHourglassSandFrames = save->hourglassSeconds <= MAX_HOURGLASS_SECONDS
        ? save->hourglassSeconds * 60
        : MAX_HOURGLASS_SECONDS * 60;
    for (s32 i = ItemFlag_EQUIP_START; i < ItemFlag_EQUIP_END; ++i) {
        if (GET_FLAG(this->mItemFlags.flags, (u32) i)) {
            (*this->mAmmo)[i] = 1;
        }
    }
    (*this->mAmmo)[ItemFlag_BombBag] = save->numBombs;
    (*this->mAmmo)[ItemFlag_BombchuBag] = save->numBombchus;
    (*this->mAmmo)[ItemFlag_Bow] = save->numArrows;
    for (s32 i = 0; i < NUM_POTIONS; ++i) {
        this->mPotions[i] = save->potions[i];
    }
    this->mEquippedItem = save->equippedItem;
    this->mSalvagedTreasureFlags = save->salvagedTreasureFlags;
    for (s32 i = 0; i < Gem_COUNT; ++i) {
        this->mNumGems[i] = save->numGems[i];
    }

    s32 i = 0; // ip (r0)
    ItemManager *this2 = this; // sp+0
    const SaveItemManager *save2 = save; // r3
    ItemManager *this3 = this; // r5
    ShipParts (SaveItemManager::*shipParts)[ShipPart_COUNT] = &SaveItemManager::shipParts; // r7
    do {
        this2->mEquippedShipParts[0] = save->equippedShipParts[i];
        for (s32 j = 0; j < ShipType_COUNT; ++j) {
            u8 partCount = (((const SaveItemManager*) ((u32)save2 + j))->*shipParts)[0].parts[0];
            ((ItemManager*) ((u32)this3 + j))->mShipParts[0].parts[0] = partCount;
        }
        save2 = (const SaveItemManager*) ((u32)save2 + sizeof(save->shipParts[0]));
        this2 = (ItemManager*) ((u32)this2 + sizeof(this->mEquippedShipParts[0]));
        ++i;
        this3 = (ItemManager*) ((u32)this3 + sizeof(this->mShipParts[0]));
    }
    while(i < ShipPart_COUNT);
    
    this->mShipPartPricesShown = save->shipPartPricesShown;
    for (s32 i = 0; i < Treasure_COUNT; ++i) {
        this->mTreasure[i] = save->treasure[i];
    }
    this->mTreasurePriceShownFlags[0] = save->treasurePriceShownFlags[0];
    for (s32 i = 0; i < 6; ++i) {
        this->mUnk_098[i] = save->unk_9f[i];
        this->mUnk_09e[i] = save->unk_82[i];
    }
    this->mQuiverSize = save->quiverSize;
    this->mBombBagSize = save->bombBagSize;
    this->mBombchuBagSize = save->bombchuBagSize;
    this->mEquippedFairy = save->equippedFairy;
    if (this->mEquippedFairy >= FairyId_COUNT) {
        this->mEquippedFairy = FairyId_None;
    }
    if (this->mEquippedItem - 9 <= 1) {
        if (this->HasItem(ItemFlag_Boomerang)) {
            this->mEquippedItem = ItemFlag_Boomerang;
        } else {
            this->mEquippedItem = ItemFlag_None;
        }
    } else if (this->mEquippedItem == ItemFlag_None) {
        if (this->HasItem(ItemFlag_Boomerang)) {
            this->mEquippedItem = ItemFlag_Boomerang;
        }
    }
    #endif
}

ARM FairyId ItemManager::GetEquippedFairy() const {
    FairyId fairy = this->mEquippedFairy;
    if (fairy == FairyId_None) return FairyId_Courage;
    return fairy;
}

ARM Navi* ItemManager::GetFairy(FairyId id) const {
    return this->mFairies[id];
}

extern UnkStruct_027e0d38 *data_027e0d38;
extern unk32 gPlayerAnimHandler;
extern "C" void LoadEquipItemModel(unk32 param1, ItemFlag param2);
extern "C" void _ZNK11ItemManager15GetEquippedItemEv();
extern "C" void _ZN14OverlayManager13LoadEquipItemEj();
ARM void ItemManager::TickEquipItem(void) {
    ItemFlag equip = this->GetEquippedItem();
    if (this->mEquipLoadTimer != 0) {
        this->mEquipLoadTimer -= 1;
        if (this->mEquipLoadTimer == 0 && equip != ItemFlag_None && data_027e0d38->mUnk_14 != 1) {
            gOverlayManager.LoadEquipItem(equip);
            LoadEquipItemModel(gPlayerAnimHandler, equip);
            (*this->mEquipItems)[equip]->vfunc_00();
        }
    }
    (*this->mEquipItems)[ItemFlag_OshusSword]->vfunc_30();
    (*this->mEquipItems)[ItemFlag_WoodenShield]->vfunc_30();
    if (data_027e0d38->mUnk_14 == 1) return;
    if (equip != ItemFlag_None && this->mEquipLoadTimer == 0) {
        (*this->mEquipItems)[equip]->vfunc_30();
    }
}

ARM void ItemManager::func_ov00_020ad528() {}

ARM ItemModel* ItemManager::GetItemModel(ItemModelId id) {
    return this->mItemModels[id];
}

extern unk32 data_027e0fc4;
extern "C" void* func_ov00_020bb3a8(unk32 param1, u32 index);
extern "C" void func_ov00_020c0bdc(void *param1, unk32 param2);
ARM void ItemManager::func_ov00_020ad538(unk32 param1) const {
    void* unk1 = func_ov00_020bb3a8(data_027e0fc4, 6);
    func_ov00_020c0bdc(unk1, param1);
}

ARM void ItemManager::func_ov00_020ad560(unk32 param1) const {
    void* unk1 = func_ov00_020bb3a8(data_027e0fc4, 7);
    func_ov00_020c0bdc(unk1, param1);
}

ARM ItemModel* ItemManager::GetDungeonItemModel(u32 index) {
    return this->mDungeonItemModels[index];
}

ARM void ItemManager::func_ov00_020ad594(unk32 param1) const {
    void* unk1 = func_ov00_020bb3a8(data_027e0fc4, 11);
    func_ov00_020c0bdc(unk1, param1);
}

ARM void ItemManager::Sword_vfunc_38(unk32 param1) {
    (*this->mEquipItems)[ItemFlag_OshusSword]->vfunc_38(param1);
}

ARM void ItemManager::Shield_vfunc_38(unk32 param1) {
    (*this->mEquipItems)[ItemFlag_WoodenShield]->vfunc_38(param1);
}

extern unk32 data_027e0618;
ARM void ItemManager::EquipItem_vfunc_38(unk32 param1) {
    if (data_027e0618 != 6) {
        this->Sword_vfunc_38(param1);
        this->Shield_vfunc_38(param1);
    }
    if (data_027e0d38->mUnk_14 == 1) return;
    
    ItemFlag equip = this->GetEquippedItem();
    if (equip == ItemFlag_None) return;
    if (this->mEquipLoadTimer > 0) return;

    (*this->mEquipItems)[equip]->vfunc_38(param1);
}

ARM bool ItemManager::EquipItem_vfunc_3c(Vec4p *param1, ItemFlag equipId) {
    Vec4p result;
    if ((*this->mEquipItems)[equipId]->vfunc_3c(&result)) {
        s32 step = (*this->mEquipItems)[equipId]->vfunc_4c();
        if (step > 0) {
            Approach_thunk(&result.y, param1->y, step);
        }
        return func_01ffec34(param1, &result);
    }
    return false;
}

ARM void ItemManager::EquipItem_vfunc_2c(ItemFlag equipId) {
    (*this->mEquipItems)[equipId]->vfunc_2c();
}

ARM EquipItem* ItemManager::GetEquipItem(ItemFlag equipId) {
    if (equipId == ItemFlag_None) {
        return NULL;
    } else {
        return (*this->mEquipItems)[equipId];
    }
}

ARM u16 ItemManager::GetAmmo(ItemFlag equipId) const {
    return (*this->mAmmo)[equipId];
}

ARM void ItemManager::GiveAmmo(ItemFlag equipId, u16 amount) {
    (*this->mAmmo)[equipId] += amount;
    if ((*this->mAmmo)[equipId] <= this->GetMaxAmmo(equipId)) return;
    (*this->mAmmo)[equipId] = this->GetMaxAmmo(equipId);
}

extern "C" unk32 func_ov00_02078b40(UnkStruct_027e0d38 *param1);
extern void *data_027e10a4;
extern "C" bool func_ov15_02136670(void *param1);
extern unk8 data_ov29_0217a4ac[];
extern void *data_027e0e60;
extern "C" bool func_ov00_020849f8(void *param1);
extern unk32 data_027e0fc8;
extern "C" bool func_ov00_020bbd80(unk32 param1, unk32 param2);
extern "C" void _ZN11ItemManager12GetEquipItemEj();
ARM NONMATCH bool ItemManager::func_ov00_020ad790(unk32 param1) {
    #ifndef NONMATCHING
    #include "../asm/ov00/ItemManager/ItemManager_func_ov00_020ad790.inc"
    #else
    unk32 unk1 = func_ov00_02078b40(data_027e0d38);
    if (unk1 == 2) return func_ov15_02136670(data_027e10a4);
    if (data_027e0d38->mUnk_14 == 1) return false;
    // NONMATCH: OverlayId_29 should be in constant pool
    if (gOverlayManager.mLoadedOverlays[OverlayIndex_6] == OverlayId_29 && data_ov29_0217a4ac[0x54] != 0) {
        return false;
    }
    
    ItemFlag equipId = this->mForcedItem;
    bool unk2 = !func_ov00_020849f8(data_027e0e60);
    if (
        this->mEquippedItem != ItemFlag_None &&
        (unk2 || this->mEquippedItem - 9 <= 1) &&
        (data_027e0fc8 == 0 || func_ov00_020bbd80(data_027e0fc8, param1)) &&
        this->HasItem(this->mEquippedItem)
    ) {
        equipId = this->mEquippedItem;
    }
    
    if (equipId != ItemFlag_None) {
        if (this->mEquipLoadTimer != 0) return param1 == 0;
        return this->GetEquipItem(equipId)->IsUsable(param1);
    }
    return false;
    #endif
}

THUMB ShipType ItemManager::GetEquippedShipPart(ShipPart part) const {
    return this->mEquippedShipParts[part];
}

THUMB void ItemManager::EquipShipPart(ShipPart part, ShipType type) {
    this->mEquippedShipParts[part] = type;
}