summaryrefslogtreecommitdiff
path: root/src/manager/lightManager.c
blob: a27d3b5a78cf13197e60a9c2a442939976df363b (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
/**
 * @file lightManager.c
 * @ingroup Managers
 *
 * @brief Manages the light in dark rooms.
 */
#include "manager/lightManager.h"
#include "area.h"
#include "common.h"
#include "game.h"
#include "main.h"
#include "physics.h"
#include "player.h"
#include "room.h"
#include "screen.h"
#include "asm.h"

extern void sub_0801E120(void);
extern void sub_0801E154(u32);
extern void sub_0801E160(u32, u32, u32);

bool32 LerpLightLevel();
void UpdateLightAlpha();

void LightManager_Main(LightManager* this) {
    s32 sVar1;
    u32 uVar3;

    if (gArea.lightType == 0) {
        sub_0801E104();
        gScreen.lcd.displayControl &= ~(DISPCNT_BG3_ON | DISPCNT_WIN0_ON);
        DeleteThisEntity();
    }
    if (super->action == 0) {
        super->action = 1;
        super->flags |= ENT_PERSIST;
        super->timer = 17;
        this->unk20 = 0;
        SetEntityPriority((Entity*)this, 6);
        sub_0801E120();
        sub_0801E154(super->timer);
    }
    if (gMain.substate == GAMEMAIN_UPDATE) {
        gScreen.lcd.displayControl |= DISPCNT_BG3_ON;
        LerpLightLevel();
        UpdateLightAlpha();
    }
    if (gArea.lightType == 2) {
        gScreen.lcd.displayControl &= ~DISPCNT_WIN0_ON;
    } else {
        if (CheckRectOnScreen(gPlayerEntity.base.x.HALF.HI - gRoomControls.origin_x,
                              gPlayerEntity.base.y.HALF.HI - gRoomControls.origin_y, 0, 0)) {
            gScreen.lcd.displayControl |= DISPCNT_WIN0_ON;
        } else {
            gScreen.lcd.displayControl &= ~DISPCNT_WIN0_ON;
        }
        uVar3 = super->timer;
        if (((gPlayerState.flags & PL_USE_LANTERN)) && (gArea.lightType)) {
            if (uVar3 < 0x48) {
                uVar3 += 4;
                sub_0801E154(uVar3);
                this->unk20 = 0;
            } else if ((gRoomTransition.frameCount & 3) == 0) {
                super->subtimer += 16;
                sVar1 = gSineTable[super->subtimer];
                this->unk20 = sVar1 >> 7;
                sub_0801E154(uVar3 + this->unk20);
            }
        } else {
            this->unk20 = 0;
            if (0x11 < uVar3) {
                uVar3 -= 4;
                if (uVar3 < 0x11) {
                    uVar3 = 0x11;
                }
                sub_0801E154(uVar3);
            }
        }

        super->timer = uVar3;
        sub_0801E160(gPlayerEntity.base.x.HALF.HI - gRoomControls.scroll_x,
                     gPlayerEntity.base.y.HALF.HI - gRoomControls.scroll_y - 9 + gPlayerEntity.base.z.HALF.HI,
                     uVar3 + this->unk20);
    }
}

#define ABS(x) ((unsigned)(x < 0 ? -(x) : x))

bool32 LerpLightLevel() {
    s32 tgt;
    s32 cur;

    cur = (short)gArea.lightLevel;
    tgt = gRoomVars.lightLevel;

    tgt = max(tgt, 0);
    tgt = min(tgt, 0x100);

    if (cur != tgt) {
        if (ABS(tgt - cur) <= 4) {
            cur = tgt;
        } else if (tgt < cur) {
            cur -= 4;
        } else if (tgt > cur) {
            cur += 4;
        }
        gArea.lightLevel = cur;
        return TRUE;
    }
    return FALSE;
}

extern u16 gUnk_08108CA8[];

void UpdateLightAlpha() {
    static const u16 gUnk_08108CA8[] = { 0x10,  0x10f, 0x20e, 0x30d, 0x40c, 0x50b, 0x60a, 0x709,  0x808,
                                         0x907, 0xa06, 0xb05, 0xc04, 0xd03, 0xe02, 0xf01, 0x1000, 0x00 };
    if (gArea.lightType != 0) {
        gScreen.controls.alphaBlend = gUnk_08108CA8[(gArea.lightLevel << 0x10) >> 0x14];
    }
}

void sub_0805BB00(s32 lightLevel, s32 param_2) {
    if (gArea.lightType == 0) {
        Manager* pManager = GetEmptyManager();
        if (pManager != 0) {
            pManager->kind = MANAGER;
            pManager->id = LIGHT_MANAGER;
            AppendEntityToList((Entity*)pManager, 0);
        }
    }
    MemFill16(0xf, BG_SCREEN_ADDR(0x1E), BG_SCREEN_SIZE);
    gScreen.bg3.control = 0x1e0c;
    gScreen.controls.layerFXControl = 0x3e48;
    gRoomVars.lightLevel = lightLevel;
    if (param_2) {
        gArea.lightType = 1;
    } else {
        gArea.lightType = 2;
    }
}

void sub_0805BB74(s32 lightLevel) {
    LightManager* manager;

    if (lightLevel < 0) {
        lightLevel = gRoomVars.lightLevel;
    }
    gArea.lightLevel = lightLevel;
    gRoomVars.lightLevel = gArea.lightLevel;
    manager = (LightManager*)DeepFindEntityByID(MANAGER, LIGHT_MANAGER);
    if (manager) {
        LightManager_Main(manager);
        gScreen.lcd.displayControl |= DISPCNT_BG3_ON;
        LerpLightLevel();
        UpdateLightAlpha();
    }
}

bool32 UpdateLightLevel() {
    bool32 iVar1;

    iVar1 = FALSE;
    if (gArea.lightType && gRoomVars.lightLevel < (s16)gArea.lightLevel) {
        gScreen.lcd.displayControl |= DISPCNT_BG3_ON;
        iVar1 = LerpLightLevel();
        if (iVar1) {
            UpdateLightAlpha();
        }
    }
    return iVar1;
}

s32 sub_0805BC04(void) {
    s32 iVar1;

    iVar1 = 0;
    if (gArea.lightType && gRoomVars.lightLevel > (short)gArea.lightLevel) {
        gScreen.lcd.displayControl |= DISPCNT_BG3_ON;
        iVar1 = LerpLightLevel();
        if (iVar1) {
            UpdateLightAlpha();
        }
    }
    return iVar1;
}

void UnDarkRoom() {
    if (gArea.lightType != 0) {
        gArea.lightType = 0;
        gScreen.lcd.displayControl &= ~(DISPCNT_BG3_ON | DISPCNT_WIN0_ON);
    }
}