summaryrefslogtreecommitdiff
path: root/src/object/linkHoldingItem.c
blob: c0920698ede12b03a158ecf28cf65b07dd118e36 (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
/**
 * @file linkHoldingItem.c
 * @ingroup Objects
 *
 * @brief Link Holding Item object
 */
#include "scroll.h"
#include "game.h"
#include "item.h"
#include "itemMetaData.h"
#include "message.h"
#include "object.h"
#include "sound.h"
#include "player.h"
#include "save.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u16 unk_68;
} LinkHoldingItemEntity;

void LinkHoldingItem_Init(LinkHoldingItemEntity*);
void LinkHoldingItem_Action1(LinkHoldingItemEntity*);
void LinkHoldingItem_Action2(LinkHoldingItemEntity*);
void LinkHoldingItem_Action3(LinkHoldingItemEntity*);

void LinkHoldingItem(LinkHoldingItemEntity* this) {
    static void (*const LinkHoldingItem_Actions[])(LinkHoldingItemEntity*) = {
        LinkHoldingItem_Init, LinkHoldingItem_Action1, LinkHoldingItem_Action2, LinkHoldingItem_Action3
    };
    u32 tmp;
    LinkHoldingItem_Actions[super->action](this);
    tmp = (super->parent->frame >> 6);
    super->spriteSettings.draw = tmp & 1;
    sub_08080CB4(super);
    super->spritePriority.b1 = 0;
    super->spriteOrientation.flipY = 1;
    super->spriteRendering.b3 = 0;
}

void LinkHoldingItem_Init(LinkHoldingItemEntity* this) {
    SetEntityPriority(super, 6);
    super->action = 1;
}

void LinkHoldingItem_Action1(LinkHoldingItemEntity* this) {
    u32 tmp;
    const ItemMetaData* ptr;

    if ((super->parent)->frame != 0x41) {
        return;
    }
    super->action = 2;
    switch (super->timer) {
        case 0:
        case 1:
            this->unk_68 = GiveItem(super->type, super->type2);
            switch (super->type) {
                case ITEM_EARTH_ELEMENT:
                case ITEM_FIRE_ELEMENT:
                case ITEM_WATER_ELEMENT:
                case ITEM_WIND_ELEMENT:
                    break;
                default:
                    SoundReq(SFX_ITEM_GET);
                    break;
            }
            break;
        case 2:
            GiveItem(super->type, super->type2);
            this->unk_68 = gUnk_080FD964[super->type].gotItemMessageId;
            SoundReq(SFX_ITEM_GET);
            break;
        case 5:
            tmp = GetInventoryValue(super->type);
            ptr = &gItemMetaData[super->type];
            if (tmp == 0) {
                this->unk_68 = *(u16*)&ptr->textId;
            } else {
                this->unk_68 = *(u16*)&ptr->unk6;
            }
            return;
        case 3:
        case 4:
        default:
            return;
    }
}

void LinkHoldingItem_Action2(LinkHoldingItemEntity* this) {
    u32 messageIndex;
    if ((super->parent)->frame == 0x42) {
        super->action = 3;
        switch (super->timer) {
            case 1:
            case 3:
            case 4:
            default:
                break;
            case 0:
            case 2:
            case 5:
                messageIndex = this->unk_68;
                if (messageIndex != 0) {
                    switch (super->type) {
                        case ITEM_HEART_PIECE:
                            messageIndex += gSave.stats.heartPieces;
                            break;
                        case ITEM_SHELLS:
                            if (super->type2 > 1) {
                                if (messageIndex == TEXT_INDEX(TEXT_ITEM_GET, 0x77)) {
                                    messageIndex--;
                                } else {
                                    messageIndex++;
                                }
                            }
                            break;
                    }
                    MessageFromTarget(messageIndex);
                    gMessage.rupees = super->type2;
                }
        }
    }
}

void LinkHoldingItem_Action3(LinkHoldingItemEntity* this) {
    u32 bottle_no;
    if (super->parent->action == PLAYER_ITEMGET) {
        return;
    }
    switch (super->timer) {
        case 0:
        case 1:
        case 2:
            if (super->type != ITEM_HEART_CONTAINER) {
                if (super->type != ITEM_HEART_PIECE) {
                    break;
                }
                ModHealth(160);
                gSave.stats.heartPieces++;
                if (gSave.stats.heartPieces < 4) {
                    break;
                }
                gSave.stats.heartPieces = 0;
            }
            gSave.stats.maxHealth += 8;
            if (gSave.stats.maxHealth > 160) {
                gSave.stats.maxHealth = 160;
            }
            ModHealth(160);
            break;
        case 3:
            bottle_no = GetBottleContaining(super->type);
            if (bottle_no != 0) {
                gSave.stats.bottles[bottle_no - 1] = ITEM_BOTTLE_EMPTY;
            } else {
                SetInventoryValue(super->type, ITEM_GREEN_SWORD);
            }
            break;
        case 4:
        case 5:
            break;
    }
    DeleteThisEntity();
}