summaryrefslogtreecommitdiff
path: root/src/object/linkEmptyingBottle.c
blob: 84d1da82bf3e1512a681e4f238f44eb9d3fc16e8 (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
/**
 * @file linkEmptyingBottle.c
 * @ingroup Objects
 *
 * @brief Link Emptying Bottle object
 * Handles effects of using water, mineral water or a fairy in a bottle in PlayerItemBottle_UseOther.
 */
#include "functions.h"
#include "item.h"
#include "object.h"
#include "tiles.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unk_68;
} LinkEmptyingBottleEntity;

extern u32 sub_080B1A0C(Entity*, s32, s32);

void LinkEmptyingBottle_Init(LinkEmptyingBottleEntity*);
void LinkEmptyingBottle_Action1(LinkEmptyingBottleEntity*);
void sub_080948E8(LinkEmptyingBottleEntity*);
void sub_08094980(LinkEmptyingBottleEntity*, u32, u32);

void LinkEmptyingBottle(LinkEmptyingBottleEntity* this) {
    static void (*const LinkEmptyingBottle_Actions[])(LinkEmptyingBottleEntity*) = {
        LinkEmptyingBottle_Init,
        LinkEmptyingBottle_Action1,
    };
    LinkEmptyingBottle_Actions[super->action](this);
}

void LinkEmptyingBottle_Init(LinkEmptyingBottleEntity* this) {
    static const s8 gUnk_08122A18[] = { 0, -8, 8, 1, 2, 8, -8, 1 };
    Entity* effect;
    Entity* child;
    super->action = 1;
    switch (super->type) {
        case 0:
            effect = CreateFx(super, FX_WATER_SPLASH, 0);
            super->child = effect;
            if (effect != NULL) {
                PositionRelative(&gPlayerEntity.base, effect,
                                 gUnk_08122A18[gPlayerEntity.base.animationState & 6] << 0x10,
                                 gUnk_08122A18[gPlayerEntity.base.animationState | 1] << 0x10);
            }
            CopyPosition(super->child, super);
            sub_08094980(this, SPECIAL_TILE_50, SPECIAL_TILE_51);
            sub_08094980(this, SPECIAL_TILE_97, SPECIAL_TILE_34);
            break;
        case 1:
            effect = CreateFx(super, FX_GREEN_SPLASH2, 0);
            super->child = effect;
            if (effect != NULL) {
                PositionRelative(&gPlayerEntity.base, effect,
                                 gUnk_08122A18[gPlayerEntity.base.animationState & 6] << 0x10,
                                 gUnk_08122A18[gPlayerEntity.base.animationState | 1] << 0x10);
            }
            if (super->type2 == 0) {
                CopyPosition(super->child, super);
                super->timer = 0;
                super->spritePriority.b0 = 0;
                super->spriteRendering.b3 = 0;
                sub_08094980(this, SPECIAL_TILE_59, SPECIAL_TILE_60);
                sub_08094980(this, SPECIAL_TILE_97, SPECIAL_TILE_34);
                return;
            }
            break;
        case 2:
            child = CreateGroundItem(&gPlayerEntity.base, ITEM_FAIRY, 0);
            super->child = child;
            if (child != NULL) {
                child->timer = 1;
            }
            break;
        case 3 ... 8:
            break;
    }
    DeleteThisEntity();
}

void LinkEmptyingBottle_Action1(LinkEmptyingBottleEntity* this) {
    static void (*const gUnk_08122A20[])(LinkEmptyingBottleEntity*) = {
        NULL,
        sub_080948E8,
    };
    gUnk_08122A20[super->type](this);
}

void sub_080948E8(LinkEmptyingBottleEntity* this) {
    if (super->timer < 0x3c) {
        super->z.WORD -= Q_16_16(0.25);
        if ((super->timer & 3) == 0) {
            super->child = CreateFx(super, FX_DASH, 0x40);
            if (super->child != NULL) {
                super->child->spriteRendering.b3 = super->spriteRendering.b3;
                super->child->spritePriority.b0 = super->spritePriority.b0;
                if ((Random() & 1) != 0) {
                    super->child->x.HALF.HI += (Random() & 0xf);
                } else {
                    super->child->x.HALF.HI -= (Random() & 0xf);
                }
            }
        }
        super->timer++;
    } else {
        DeleteThisEntity();
    }
}

void sub_08094980(LinkEmptyingBottleEntity* this, u32 searchTileIndex, u32 replaceTileIndex) {
    static const s8 gUnk_08122A28[] = {
        0, 0, -8, 0, 8, 0, 0, 8, 0, -8, 0, 0,
    };
    s32 xOffset;
    s32 yOffset;
    u32 index = 0;
    while (index < 10) {
        xOffset = gUnk_08122A28[index];
        yOffset = gUnk_08122A28[index + 1];
        if (searchTileIndex == sub_080B1A0C(super, xOffset, yOffset)) {
            SetTile(replaceTileIndex, TILE(super->x.HALF.HI + xOffset, super->y.HALF.HI + yOffset),
                        super->collisionLayer);
        }
        index += 2;
    }
}