summaryrefslogtreecommitdiff
path: root/src/fldeff_flash.c
blob: 067e171626b84a17b9da3846090487301f8424c4 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "global.h"
#include "gflib.h"
#include "event_data.h"
#include "event_scripts.h"
#include "fldeff.h"
#include "field_effect.h"
#include "map_preview_screen.h"
#include "overworld.h"
#include "party_menu.h"
#include "script.h"
#include "constants/songs.h"
#include "constants/map_types.h"

struct FlashStruct
{
    u8 fromType;
    u8 toType;
    bool8 isEnter;
    bool8 isExit;
    void (*func1)(void);
    void (*func2)(u8 mapSecId);
};

static void FieldCallback_Flash(void);
static void FldEff_UseFlash(void);
static bool8 TryDoMapTransition(void);
static void FlashTransition_Exit(void);
static void Task_FlashTransition_Exit_0(u8 taskId);
static void Task_FlashTransition_Exit_1(u8 taskId);
static void Task_FlashTransition_Exit_2(u8 taskId);
static void Task_FlashTransition_Exit_3(u8 taskId);
static void Task_FlashTransition_Exit_4(u8 taskId);
static void FlashTransition_Enter(void);
static void Task_FlashTransition_Enter_0(u8 taskId);
static void Task_FlashTransition_Enter_1(u8 taskId);
static void Task_FlashTransition_Enter_2(u8 taskId);
static void Task_FlashTransition_Enter_3(u8 taskId);
static void RunMapPreviewScreen(u8 mapsecId);
static void Task_MapPreviewScreen_0(u8 taskId);

static const struct FlashStruct sTransitionTypes[] = {
    {
        .fromType = MAP_TYPE_TOWN,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_CITY,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_ROUTE,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERWATER,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_OCEAN_ROUTE,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNKNOWN,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_INDOOR,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_SECRET_BASE,
        .toType = MAP_TYPE_UNDERGROUND,
        .isEnter = TRUE,
        .isExit = FALSE,
        .func1 = FlashTransition_Enter,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_TOWN,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_CITY,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_ROUTE,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_UNDERWATER,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_OCEAN_ROUTE,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_UNKNOWN,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_INDOOR,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {
        .fromType = MAP_TYPE_UNDERGROUND,
        .toType = MAP_TYPE_SECRET_BASE,
        .isEnter = FALSE,
        .isExit = TRUE,
        .func1 = FlashTransition_Exit,
        .func2 = RunMapPreviewScreen
    }, {0}
};

static const u16 sCaveTransitionPalette_White[] = INCBIN_U16("graphics/field_effects/flash_white.gbapal");
static const u16 sCaveTransitionPalette_Black[] = INCBIN_U16("graphics/field_effects/flash_black.gbapal");
static const u16 sCaveTransitionPalette_Gradient[] = INCBIN_U16("graphics/field_effects/flash_gradient.gbapal");
static const u32 sCaveTransitionTilemap[] = INCBIN_U32("graphics/field_effects/flash_effect_map.bin.lz");
static const u32 sCaveTransitionTiles[] = INCBIN_U32("graphics/field_effects/flash_effect_tiles.4bpp.lz");

bool8 SetUpFieldMove_Flash(void)
{
    if (gMapHeader.cave != TRUE)
        return FALSE;

    if (FlagGet(FLAG_SYS_FLASH_ACTIVE))
        return FALSE;

    gFieldCallback2 = FieldCallback_PrepareFadeInFromMenu;
    gPostMenuFieldCallback = FieldCallback_Flash;
    return TRUE;
}

static void FieldCallback_Flash(void)
{
    u8 taskId = CreateFieldEffectShowMon();
    gFieldEffectArguments[0] = GetCursorSelectionMonId();
    gTasks[taskId].data[8] = ((uintptr_t)FldEff_UseFlash) >> 16;
    gTasks[taskId].data[9] = ((uintptr_t)FldEff_UseFlash);
}

static void FldEff_UseFlash(void)
{
    PlaySE(SE_M_REFLECT);
    FlagSet(FLAG_SYS_FLASH_ACTIVE);
    ScriptContext1_SetupScript(EventScript_FldEffFlash);
}

// Map transition animatics

static void CB2_ChangeMapMain(void)
{
    RunTasks();
    AnimateSprites();
    BuildOamBuffer();
    UpdatePaletteFade();
}

static void VBC_ChangeMapVBlank(void)
{
    LoadOam();
    ProcessSpriteCopyRequests();
    TransferPlttBuffer();
}

void CB2_DoChangeMap(void)
{
    SetVBlankCallback(NULL);

    SetGpuReg(REG_OFFSET_DISPCNT, 0);
    SetGpuReg(REG_OFFSET_BG2CNT, 0);
    SetGpuReg(REG_OFFSET_BG1CNT, 0);
    SetGpuReg(REG_OFFSET_BG0CNT, 0);
    SetGpuReg(REG_OFFSET_BG2HOFS, 0);
    SetGpuReg(REG_OFFSET_BG2VOFS, 0);
    SetGpuReg(REG_OFFSET_BG1HOFS, 0);
    SetGpuReg(REG_OFFSET_BG1VOFS, 0);
    SetGpuReg(REG_OFFSET_BG0HOFS, 0);
    SetGpuReg(REG_OFFSET_BG0VOFS, 0);

    DmaFill16(3, 0, (void *)VRAM, VRAM_SIZE);
    DmaFill32(3, 0, (void *)OAM, OAM_SIZE);
    DmaFill16(3, 0, (void *)(PLTT + 2), PLTT_SIZE - 2);
    ResetPaletteFade();
    ResetTasks();
    ResetSpriteData();
    EnableInterrupts(INTR_FLAG_VBLANK);
    SetVBlankCallback(VBC_ChangeMapVBlank);
    SetMainCallback2(CB2_ChangeMapMain);
    if (!TryDoMapTransition())
        SetMainCallback2(gMain.savedCallback);
}

static bool8 TryDoMapTransition(void)
{
    u8 fromType = GetLastUsedWarpMapType();
    u8 toType = GetCurrentMapType();
    u8 i = 0;
    if (GetLastUsedWarpMapSectionId() != gMapHeader.regionMapSectionId && MapHasPreviewScreen_HandleQLState2(gMapHeader.regionMapSectionId, MPS_TYPE_CAVE) == TRUE)
    {
        RunMapPreviewScreen(gMapHeader.regionMapSectionId);
        return TRUE;
    }
    for (; sTransitionTypes[i].fromType != 0; i++)
    {
        if (sTransitionTypes[i].fromType == fromType && sTransitionTypes[i].toType == toType)
        {
            sTransitionTypes[i].func1();
            return TRUE;
        }
    }
    return FALSE;
}

bool8 MapTransitionIsEnter(u8 _fromType, u8 _toType)
{
    u8 fromType = _fromType;
    u8 toType = _toType;
    u8 i = 0;
    for (; sTransitionTypes[i].fromType != 0; i++)
    {
        if (sTransitionTypes[i].fromType == fromType && sTransitionTypes[i].toType == toType)
        {
            return sTransitionTypes[i].isEnter;
        }
    }
    return FALSE;
}

bool8 MapTransitionIsExit(u8 _fromType, u8 _toType)
{
    u8 fromType = _fromType;
    u8 toType = _toType;
    u8 i = 0;
    for (; sTransitionTypes[i].fromType != 0; i++)
    {
        if (sTransitionTypes[i].fromType == fromType && sTransitionTypes[i].toType == toType)
        {
            return sTransitionTypes[i].isExit;
        }
    }
    return FALSE;
}

static void FlashTransition_Exit(void)
{
    CreateTask(Task_FlashTransition_Exit_0, 0);
}

static void Task_FlashTransition_Exit_0(u8 taskId)
{
    gTasks[taskId].func = Task_FlashTransition_Exit_1;
}

static void Task_FlashTransition_Exit_1(u8 taskId)
{
    SetGpuReg(REG_OFFSET_DISPCNT, 0);
    LZ77UnCompVram(sCaveTransitionTiles, (void *)BG_CHAR_ADDR(3));
    LZ77UnCompVram(sCaveTransitionTilemap, (void *)BG_SCREEN_ADDR(31));
    LoadPalette(sCaveTransitionPalette_White, 0xE0, 0x20);
    LoadPalette(sCaveTransitionPalette_Gradient + 8, 0xE0, 0x10);
    SetGpuReg(REG_OFFSET_BLDCNT, BLDCNT_TGT1_BG0 | BLDCNT_EFFECT_BLEND | BLDCNT_TGT2_BG1 | BLDCNT_TGT2_BG2 | BLDCNT_TGT2_BG3 | BLDCNT_TGT2_OBJ | BLDCNT_TGT2_BD);
    SetGpuReg(REG_OFFSET_BLDALPHA, 0);
    SetGpuReg(REG_OFFSET_BLDY, 0);
    SetGpuReg(REG_OFFSET_BG0CNT, BGCNT_PRIORITY(0) | BGCNT_CHARBASE(3) | BGCNT_SCREENBASE(31));
    SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_MODE_0 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG0_ON | DISPCNT_OBJ_ON);
    gTasks[taskId].func = Task_FlashTransition_Exit_2;
    gTasks[taskId].data[0] = 16;
    gTasks[taskId].data[1] = 0;
}

static void Task_FlashTransition_Exit_2(u8 taskId)
{
    u16 r4 = gTasks[taskId].data[1];
    SetGpuReg(REG_OFFSET_BLDALPHA, (16 << 8) + r4);
    if (r4 <= 16)
    {
        gTasks[taskId].data[1]++;
    }
    else
    {
        gTasks[taskId].data[2] = 0;
        gTasks[taskId].func = Task_FlashTransition_Exit_3;
    }
}

static void Task_FlashTransition_Exit_3(u8 taskId)
{
    u16 r4;
    SetGpuReg(REG_OFFSET_BLDALPHA, (16 << 8) + 16);
    r4 = gTasks[taskId].data[2];
    if (r4 < 8)
    {
        gTasks[taskId].data[2]++;
        LoadPalette(sCaveTransitionPalette_Gradient + 8 + r4, 0xE0, 0x10 - 2 * r4);
    }
    else
    {
        LoadPalette(sCaveTransitionPalette_White, 0x00, 0x20);
        gTasks[taskId].func = Task_FlashTransition_Exit_4;
        gTasks[taskId].data[2] = 8;
    }
}

static void Task_FlashTransition_Exit_4(u8 taskId)
{
    if (gTasks[taskId].data[2] != 0)
        gTasks[taskId].data[2]--;
    else
        SetMainCallback2(gMain.savedCallback);
}

static void FlashTransition_Enter(void)
{
    CreateTask(Task_FlashTransition_Enter_0, 0);
}

static void Task_FlashTransition_Enter_0(u8 taskId)
{
    gTasks[taskId].func = Task_FlashTransition_Enter_1;
}

static void Task_FlashTransition_Enter_1(u8 taskId)
{
    SetGpuReg(REG_OFFSET_DISPCNT, 0);
    LZ77UnCompVram(sCaveTransitionTiles, (void *)BG_CHAR_ADDR(3));
    LZ77UnCompVram(sCaveTransitionTilemap, (void *)BG_SCREEN_ADDR(31));
    SetGpuReg(REG_OFFSET_BLDCNT, 0);
    SetGpuReg(REG_OFFSET_BLDALPHA, 0);
    SetGpuReg(REG_OFFSET_BLDY, 0);
    SetGpuReg(REG_OFFSET_BG0CNT, BGCNT_PRIORITY(0) | BGCNT_CHARBASE(3) | BGCNT_SCREENBASE(31));
    SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_MODE_0 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG0_ON | DISPCNT_OBJ_ON);
    LoadPalette(sCaveTransitionPalette_White, 0xE0, 0x20);
    LoadPalette(sCaveTransitionPalette_Black, 0, 0x20);
    gTasks[taskId].func = Task_FlashTransition_Enter_2;
    gTasks[taskId].data[0] = 16;
    gTasks[taskId].data[1] = 0;
    gTasks[taskId].data[2] = 0;
}

static void Task_FlashTransition_Enter_2(u8 taskId)
{
    u16 r4;
    r4 = gTasks[taskId].data[2];
    if (r4 < 16)
    {
        gTasks[taskId].data[2]++;
        gTasks[taskId].data[2]++;
        LoadPalette(&sCaveTransitionPalette_Gradient[16 - (r4 + 1)], 0xE0, 2 * (r4 + 1));
    }
    else
    {
        SetGpuReg(REG_OFFSET_BLDALPHA, (16 << 8) + 16);
        SetGpuReg(REG_OFFSET_BLDCNT, BLDCNT_TGT1_BG0 | BLDCNT_EFFECT_BLEND | BLDCNT_TGT2_BG1 | BLDCNT_TGT2_BG2 | BLDCNT_TGT2_BG3 | BLDCNT_TGT2_OBJ | BLDCNT_TGT2_BD);
        gTasks[taskId].func = Task_FlashTransition_Enter_3;
    }
}

static void Task_FlashTransition_Enter_3(u8 taskId)
{
    u16 r4 = 16 - gTasks[taskId].data[1];
    SetGpuReg(REG_OFFSET_BLDALPHA, (16 << 8) + r4);
    if (r4 != 0)
    {
        gTasks[taskId].data[1]++;
    }
    else
    {
        LoadPalette(sCaveTransitionPalette_Black, 0x00, 0x20);
        SetMainCallback2(gMain.savedCallback);
    }
}

static void RunMapPreviewScreen(u8 mapSecId)
{
    u8 taskId = CreateTask(Task_MapPreviewScreen_0, 0);
    gTasks[taskId].data[3] = mapSecId;
}

static void Task_MapPreviewScreen_0(u8 taskId)
{
    s16 *data = gTasks[taskId].data;
    switch (data[0])
    {
    case 0:
        SetWordTaskArg(taskId, 5, (uintptr_t)gMain.vblankCallback);
        SetVBlankCallback(NULL);
        MapPreview_InitBgs();
        MapPreview_LoadGfx(data[3]);
        BlendPalettes(0xFFFFFFFF, 0x10, RGB_BLACK);
        data[0]++;
        break;
    case 1:
        if (!MapPreview_IsGfxLoadFinished())
        {
            data[4] = MapPreview_CreateMapNameWindow(data[3]);
            CopyWindowToVram(data[4], COPYWIN_BOTH);
            data[0]++;
        }
        break;
    case 2:
        if (!IsDma3ManagerBusyWithBgCopy())
        {
            BeginNormalPaletteFade(0xFFFFFFFF, -1, 16, 0, RGB_BLACK);
            SetVBlankCallback((IntrCallback)GetWordTaskArg(taskId, 5));
            data[0]++;
        }
        break;
    case 3:
        if (!UpdatePaletteFade())
        {
            data[2] = MapPreview_GetDuration(data[3]);
            data[0]++;
        }
        break;
    case 4:
        data[1]++;
        if (data[1] > data[2] || JOY_HELD(B_BUTTON))
        {
            BeginNormalPaletteFade(0xFFFFFFFF, -2, 0, 16, RGB_WHITE);
            data[0]++;
        }
        break;
    case 5:
        if (!UpdatePaletteFade())
        {
            int i;
            for (i = 0; i < 16; i++)
            {
                data[i] = 0;
            }
            MapPreview_Unload(data[4]);
            gTasks[taskId].func = Task_FlashTransition_Enter_1;
        }
        break;
    }
}