summaryrefslogtreecommitdiff
path: root/src/manager/lightRayManager.c
blob: 1a75f63b773717985ff8ab42a37bc08ff7ee7f6f (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
/**
 * @file lightRayManager.c
 * @ingroup Managers
 *
 * @brief Creates and updates light rays.
 *
 * Light rays at the left of minish woods
 * Light rays in the barrel minish house
 */
#include "manager/lightRayManager.h"
#include "common.h"
#include "main.h"
#include "screen.h"
#include "game.h"
#include "player.h"
#include "physics.h"

extern void DisableVBlankDMA(void);

void (*const LightRayManager_Actions[])(LightRayManager*);
void (*const gUnk_08107C48[])(LightRayManager*);
const u16 gUnk_08107C1C[];
const u16 gUnk_08107C30[];
const u8 gLightRayManagerGfxGroups[];

extern u16 gUnk_02017AA0[];
extern u16 gUnk_085B4180[];

typedef struct {
    u8 unk0;
    u8 unk1;
    u8 unk2;
    u8 unk3;
    u8 unk4;
} PACKED LightRayManagerProp;

#define ZS(this) ((u8*)&this->speed)

void LightRayManager_Main(LightRayManager* this) {
    u8 gfxGroup;
    u8* pbVar2;

    LightRayManager_Actions[super->action](this);
    gUnk_08107C48[this->unk_21](this);
    gfxGroup = gLightRayManagerGfxGroups[this->unk_21];
    if ((gfxGroup != 0) && (this->gfxGroup != gfxGroup)) {
        this->gfxGroup = gfxGroup;
        LoadGfxGroup(gfxGroup);
    }
}

static void LightRayManager_OnEnterRoom(LightRayManager* this) {
    u8* pbVar1;

    LoadGfxGroup(this->gfxGroup);
    this->unk_34 = NULL;
    pbVar1 = &this->unk_21;
    if (*pbVar1 == 3) {
        gScreen.bg3.updated = 1;
    } else {
        gUnk_08107C48[*pbVar1](this);
    }
}

static void LightRayManager_OnExitRoom(void) {
    gScreen.lcd.displayControl &= ~DISPCNT_BG3_ON;
    gScreen.controls.layerFXControl = 0;
    DisableVBlankDMA();
}

void LightRayManager_Init(LightRayManager* this) {
    super->timer = 0;
    this->gfxGroup = 0;
    this->unk_21 = 0;
    this->unk_22 = 0;
    super->action = 1;
    gScreen.bg3.control = 0x1e04;
    gScreen.lcd.displayControl |= DISPCNT_BG3_ON;
    gScreen.controls.layerFXControl = 0x3648;
    gScreen.controls.alphaBlend = 0x1000;
    RegisterTransitionHandler(this, LightRayManager_OnEnterRoom, LightRayManager_OnExitRoom);
}

void LightRayManager_Action1(LightRayManager* this) {
    LightRayManagerProp* prop = GetCurrentRoomProperty(super->type);
    s32 temp;
    s32 x;
    s32 y;

    if (prop->unk0 == 0xff)
        return;

    x = gPlayerEntity.base.x.HALF.HI / 16;
    y = gPlayerEntity.base.y.HALF.HI / 16;

    for (; prop->unk0 != 0xff; prop++) {
        if (prop->unk0 != this->unk_21) {
            u32 x2 = (gRoomControls.origin_x / 16) + prop->unk1;
            u32 y2 = (gRoomControls.origin_y / 16) + prop->unk2;

            if (y - y2 < prop->unk4 && x - x2 < prop->unk3) {
                switch (prop->unk0) {
                    case 1:
                        if (this->unk_21 == 2) {
                            super->action = 3;
                            super->timer = 9;
                        }
                        break;
                    case 5:
                        if (this->unk_21 == 6) {
                            super->action = 3;
                            super->timer = 9;
                        }
                        break;
                    case 2:
                        if (this->unk_21 == 0) {
                            super->action = 2;
                            super->timer = 0;
                            this->gfxGroup = 0;
                        }
                        break;
                    case 6:
                        if (this->unk_21 == 0) {
                            super->action = 2;
                            super->timer = 0;
                        }
                        break;
                    case 4:
                        if (this->unk_21 == 0) {
                            super->action = 2;
                            super->timer = 0;
                            this->unk_34 = NULL;
                        }
                        break;
                    case 3:
                        if (this->unk_21 == 4) {
                            super->action = 3;
                            super->timer = 9;
                        }
                        break;
                }

                if (super->action != 1) {
                    super->subtimer = 8;
                    this->unk_21 = prop->unk0;
                    this->unk_22 = 1;
                    return;
                }
            }
        }
    }
}

void LightRayManager_Action2(LightRayManager* this) {
    if (--super->subtimer == 0) {
        super->subtimer = 8;
        gScreen.controls.alphaBlend = gUnk_08107C1C[super->timer++];

        if (super->timer == 10) {
            super->action = 1;
            this->unk_22 = 0;
        }
    }
}

void LightRayManager_Action3(LightRayManager* this) {
    if (--super->subtimer == 0) {
        super->subtimer = 8;
        gScreen.controls.alphaBlend = gUnk_08107C1C[super->timer--];

        if (super->timer == 0xff) {
            super->action = 1;
            this->unk_22 = 0;
            this->unk_21 = 0;
            gScreen.vBlankDMA.ready = FALSE;
        }
    }
}

void sub_0805732C(u32 param_1, u32 param_2) {
    u32 index;
    u16* ptr = &gUnk_02017AA0[gUnk_03003DE4[0] * 0x500];

    for (index = 0; index < 0xa0; ptr++, index++) {
        *ptr = gSineTable[(param_2 + index) & 0xff] * param_1 / 0x100 + gScreen.bg3.xOffset;
    }

    SetVBlankDMA(&gUnk_02017AA0[gUnk_03003DE4[0] * 0x500], (u16*)REG_ADDR_BG3HOFS,
                 ((DMA_ENABLE | DMA_START_HBLANK | DMA_16BIT | DMA_REPEAT | DMA_SRC_INC | DMA_DEST_RELOAD) << 16) +
                     0x1);
}

void nullsub_494() {
}

void sub_080573AC(LightRayManager* this) {
    s32 sin, frameCount;
    gRoomControls.bg3OffsetX.WORD -= 0x2000;
    gRoomControls.bg3OffsetY.WORD -= 0x1000;
    gScreen.bg3.xOffset = ((gRoomControls.scroll_x - gRoomControls.origin_x) >> 1) + gRoomControls.bg3OffsetX.HALF.HI;
    gScreen.bg3.yOffset = ((gRoomControls.scroll_y - gRoomControls.origin_y) >> 1) + gRoomControls.bg3OffsetY.HALF.HI;
    sin = gSineTable[(gRoomTransition.frameCount & 0xff) + 0x40];
    sub_0805732C((sin >> 5) + 0x10, gRoomTransition.frameCount);
    if (this->unk_22 == 0) {
        if ((gRoomTransition.frameCount & 0x1f) == 0) {
            this->unk_24 = (this->unk_24 + 1) & 7;
            gScreen.controls.alphaBlend = gUnk_08107C30[this->unk_24];
        }
    }
}

void sub_08057450(LightRayManager* this) {
    s32 y;
    gScreen.bg3.xOffset = 0x10;
    y = gRoomControls.scroll_y;
    y -= gRoomControls.origin_y;
    y >>= 2;

    gScreen.bg3.yOffset = y & 0x3f;
    gScreen.bg3.subTileMap = &gBG3Buffer[(y / 0x40) << 8];
    if (this->unk_34 != gScreen.bg3.subTileMap) {
        this->unk_34 = gScreen.bg3.subTileMap;
        gScreen.bg3.updated = 1;
    }

    if (this->unk_22 == 0 && (gRoomTransition.frameCount & 0x1f) == 0) {
        this->unk_24 = (this->unk_24 + 1) & 7;
        gScreen.controls.alphaBlend = gUnk_08107C30[this->unk_24];
    }

    if ((gRoomTransition.frameCount & 0x7) == 0) {
        u32 index;
        u16* ptr = &gUnk_085B4180[(this->unk_23 << 4)];

        for (index = 0, ptr = ptr + 7; index <= 3; index++) {
            SetColor(index + 0x87, *(ptr + index));
            SetColor(index + 0x8c, *(ptr + index + 5));
        }

        this->unk_23 = (this->unk_23 + 1) & 3;
    }
}

const u16 gUnk_08107C1C[] = {
    0x1000, 0xF01, 0xE02, 0xD03, 0xC04, 0xB05, 0xA06, 0x907, 0x908, 0x909,
};

const u16 gUnk_08107C30[] = {
    0x909, 0xA08, 0xB07, 0xC06, 0xD05, 0xC06, 0xB07, 0xA08,
};

const u8 gLightRayManagerGfxGroups[] = {
    0x25, 0x00, 0x24, 0x00, 0x25, 0x00, 0x00, 0x00,
};

void (*const gUnk_08107C48[])(LightRayManager*) = {
    nullsub_494, nullsub_494, sub_080573AC, nullsub_494, sub_08057450,
};

void (*const LightRayManager_Actions[])(LightRayManager*) = {
    LightRayManager_Init,
    LightRayManager_Action1,
    LightRayManager_Action2,
    LightRayManager_Action3,
};