summaryrefslogtreecommitdiff
path: root/src/ItemManager.cpp
blob: 19a7c594cf149c9deb77969209c9321c2786c48e (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#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 void ItemManager::Save(SaveItemManager *save) {
    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];
    }
    for (s32 i = 0; i < ShipPart_COUNT; ++i) {
        save->equippedShipParts[i] = this->mEquippedShipParts[i];
        for (s32 j = 0; j < ShipType_COUNT; ++j) {
            save->shipParts[i][j] = this->mShipParts[i][j];
        }
    }
    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;
}

THUMB void ItemManager::Load(const SaveItemManager *save) {
    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];
    }
    for (s32 i = 0; i < ShipPart_COUNT; ++i) {
        this->mEquippedShipParts[i] = save->equippedShipParts[i];
        for (s32 j = 0; j < ShipType_COUNT; ++j) {
            this->mShipParts[i][j] = save->shipParts[i][j];
        }
    }
    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 ((u32) 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;
        }
    }
}

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 _ZN14OverlayManager13LoadEquipItemEi();
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" bool _ZNK11ItemManager7HasItemEi();
extern "C" void _ZN11ItemManager12GetEquipItemEi();
ARM bool NONMATCH(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 || (u32) 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;
}

THUMB s8 ItemManager::GetShipPartCount(ShipPart part, ShipType type) const {
    return this->mShipParts[part][type];
}

THUMB void ItemManager::SetShipPartCount(ShipPart part, ShipType type, s8 count) {
    if (count > 99) count = 99;
    this->mShipParts[part][type] = count;
}

THUMB s8 ItemManager::GetTreasureCount(Treasure treasure) const {
    return this->mTreasure[treasure];
}

THUMB void ItemManager::SetTreasureCount(Treasure treasure, s8 count) {
    if (count > 99) count = 99;
    this->mTreasure[treasure] = count;
}

THUMB u8 ItemManager::GetUnk_098(u32 index) const {
    return this->mUnk_098[index];
}

THUMB u16 ItemManager::GetUnk_09e(u32 index) const {
    return this->mUnk_09e[index];
}

THUMB s32 ItemManager::GetUnk_09e_Divided(u32 index) const {
    q20 quotient = Divide(INT_TO_Q20(this->mUnk_09e[index]), FLOAT_TO_Q20(2.54));
    s32 result = ROUND_Q20(quotient);
    if (result < 1) result = 1;
    return result;
}

THUMB void ItemManager::SetUnk_09e(u32 index, u16 value) {
    u8 count = this->mUnk_098[index] + 1;
    if (count > 99) count = 99;
    this->mUnk_098[index] = count;
    if (value > this->mUnk_09e[index]) {
        if (value > 9999) value = 9999;
        this->mUnk_09e[index] = value;
    }
}

const u16 sQuiverSizes[] = {20, 20, 30, 50};
const u16 sBombBagSizes[] = {10, 20, 30};

THUMB u16 ItemManager::GetMaxAmmo(ItemFlag equipId) const {
    switch (equipId) {
        case ItemFlag_Bow: return (sQuiverSizes + 1)[this->mQuiverSize];
        case ItemFlag_BombBag: return sBombBagSizes[this->mBombBagSize];
        case ItemFlag_BombchuBag: return sBombBagSizes[this->mBombchuBagSize];
        default: return 1;
    }
}

THUMB u8 ItemManager::GetMaxShipPartCount() const {
    return 99;
}

THUMB u8 ItemManager::GetMaxTreasureCount() const {
    return 99;
}

ARM u32 ItemManager::GetActiveFairyLevel(FairyId id) const {
    if (this->mEquippedFairy == FairyId_None) return 0;
    FairyId fairy = this->GetEquippedFairy();
    if (fairy == FairyId_Courage && this->mUnk_14d != 0) return 0;
    fairy = this->GetEquippedFairy();
    if (id == fairy) return this->GetFairyLevel(id);
    return 0;
}

ARM u32 ItemManager::GetFairyLevel(FairyId id) const {
    if (this->HasItem(ItemFlag_FAIRY_LV2 + id)) return 2;
    if (this->HasItem(ItemFlag_FAIRY_LV1 + id)) return 1;
    return 0;
}

ARM bool ItemManager::HasItem(ItemFlag item) const {
    return GET_FLAG(this->mItemFlags.flags, item);
}

THUMB void ItemManager::AddItem(ItemFlag item) {
    SET_FLAG(this->mItemFlags.flags, item);
}

ARM bool ItemManager::IsTreasureSalvaged(u32 index) const {
    return GET_FLAG(&this->mSalvagedTreasureFlags, index);
}

THUMB void ItemManager::SetTreasureSalvaged(u32 index) {
    SET_FLAG(&this->mSalvagedTreasureFlags, index);
}

THUMB void ItemManager::RemoveItem(ItemFlag item) {
    RESET_FLAG(this->mItemFlags.flags, item);
    if (item >= ItemFlag_EQUIP_START && item <= ItemFlag_EQUIP_END) {
        (*this->mAmmo)[item] = 0;
    }
}