From 43af7d46ca500de5244a94f1dab51988f833dce5 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 1 Feb 2023 10:37:11 -0500 Subject: [PATCH] Label data for elevator effects --- src/field_specials.c | 124 +++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 45 deletions(-) diff --git a/src/field_specials.c b/src/field_specials.c index dd03358ebf..cfa12a4181 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -1664,7 +1664,7 @@ void OffsetCameraForBattle(void) SetCameraPanning(8, 0); } -const struct WindowTemplate gElevatorFloor_WindowTemplate = +static const struct WindowTemplate sWindowTemplate_ElevatorFloor = { .bg = 0, .tilemapLeft = 21, @@ -1675,7 +1675,7 @@ const struct WindowTemplate gElevatorFloor_WindowTemplate = .baseBlock = 8, }; -const u8 *const gDeptStoreFloorNames[] = +static const u8 *const sDeptStoreFloorNames[] = { [DEPT_STORE_FLOORNUM_B4F] = gText_B4F, [DEPT_STORE_FLOORNUM_B3F] = gText_B3F, @@ -1695,7 +1695,11 @@ const u8 *const gDeptStoreFloorNames[] = [DEPT_STORE_FLOORNUM_ROOFTOP] = gText_Rooftop }; -static const u16 sElevatorWindowTiles_Ascending[][3] = +#define ELEVATOR_WINDOW_WIDTH 3 +#define ELEVATOR_WINDOW_HEIGHT 3 +#define ELEVATOR_LIGHT_STAGES 3 + +static const u16 sElevatorWindowTiles_Ascending[ELEVATOR_WINDOW_HEIGHT][ELEVATOR_LIGHT_STAGES] = { { METATILE_BattleFrontier_Elevator_Top0, @@ -1714,7 +1718,7 @@ static const u16 sElevatorWindowTiles_Ascending[][3] = }, }; -static const u16 sElevatorWindowTiles_Descending[][3] = +static const u16 sElevatorWindowTiles_Descending[ELEVATOR_WINDOW_HEIGHT][ELEVATOR_LIGHT_STAGES] = { { METATILE_BattleFrontier_Elevator_Top0, @@ -1798,53 +1802,66 @@ u16 GetDeptStoreDefaultFloorChoice(void) return sLilycoveDeptStore_DefaultFloorChoice; } +// Task data for Task_MoveElevator +#define tTimer data[1] +#define tMoveCounter data[2] +#define tVerticalPan data[4] +#define tTotalMoves data[5] +#define tDescending data[6] + +// The maximum considered difference between floors. +// Elevator trips with a larger difference are treated the same +// (i.e. traveling 9 floors and 200 floors would take the same amount of time). +#define MAX_ELEVATOR_TRIP 9 + +// gSpecialVar_0x8005 here is expected to be the current floor number, and +// gSpecialVar_0x8006 is expected to be the destination floor number. void MoveElevator(void) { - static const u8 sElevatorTripLength[] = { 8, 16, 24, 32, 38, 46, 52, 56, 57 }; + static const u8 sElevatorTripLength[MAX_ELEVATOR_TRIP] = { 8, 16, 24, 32, 38, 46, 52, 56, 57 }; s16 *data = gTasks[CreateTask(Task_MoveElevator, 9)].data; u16 floorDelta; - data[1] = 0; - data[2] = 0; - data[4] = 1; + tTimer = 0; + tMoveCounter = 0; + tVerticalPan = 1; - // descending if (gSpecialVar_0x8005 > gSpecialVar_0x8006) { floorDelta = gSpecialVar_0x8005 - gSpecialVar_0x8006; - data[6] = TRUE; + tDescending = TRUE; } else { floorDelta = gSpecialVar_0x8006 - gSpecialVar_0x8005; - data[6] = FALSE; + tDescending = FALSE; } - if (floorDelta > 8) - floorDelta = 8; + if (floorDelta > MAX_ELEVATOR_TRIP - 1) + floorDelta = MAX_ELEVATOR_TRIP - 1; - data[5] = sElevatorTripLength[floorDelta]; + tTotalMoves = sElevatorTripLength[floorDelta]; SetCameraPanningCallback(NULL); - MoveElevatorWindowLights(floorDelta, data[6]); + MoveElevatorWindowLights(floorDelta, tDescending); PlaySE(SE_ELEVATOR); } static void Task_MoveElevator(u8 taskId) { s16 *data = gTasks[taskId].data; - data[1]++; - if (data[1] % 3 == 0) + tTimer++; + if (tTimer % 3 == 0) { - data[1] = 0; - data[2]++; - data[4] = -data[4]; - SetCameraPanning(0, data[4]); + tTimer = 0; + tMoveCounter++; + tVerticalPan = -tVerticalPan; + SetCameraPanning(0, tVerticalPan); - // arrived at floor - if (data[2] == data[5]) + if (tMoveCounter == tTotalMoves) { + // Arrived at floor PlaySE(SE_DING_DONG); DestroyTask(taskId); ScriptContext_Enable(); @@ -1853,18 +1870,24 @@ static void Task_MoveElevator(u8 taskId) } } +#undef tTimer +#undef tMoveCounter +#undef tVerticalPan +#undef tTotalMoves +#undef tDescending + void ShowDeptStoreElevatorFloorSelect(void) { int xPos; - sTutorMoveAndElevatorWindowId = AddWindow(&gElevatorFloor_WindowTemplate); + sTutorMoveAndElevatorWindowId = AddWindow(&sWindowTemplate_ElevatorFloor); SetStandardWindowBorderStyle(sTutorMoveAndElevatorWindowId, FALSE); xPos = GetStringCenterAlignXOffset(FONT_NORMAL, gText_ElevatorNowOn, 64); AddTextPrinterParameterized(sTutorMoveAndElevatorWindowId, FONT_NORMAL, gText_ElevatorNowOn, xPos, 1, TEXT_SKIP_DRAW, NULL); - xPos = GetStringCenterAlignXOffset(FONT_NORMAL, gDeptStoreFloorNames[gSpecialVar_0x8005], 64); - AddTextPrinterParameterized(sTutorMoveAndElevatorWindowId, FONT_NORMAL, gDeptStoreFloorNames[gSpecialVar_0x8005], xPos, 17, TEXT_SKIP_DRAW, NULL); + xPos = GetStringCenterAlignXOffset(FONT_NORMAL, sDeptStoreFloorNames[gSpecialVar_0x8005], 64); + AddTextPrinterParameterized(sTutorMoveAndElevatorWindowId, FONT_NORMAL, sDeptStoreFloorNames[gSpecialVar_0x8005], xPos, 17, TEXT_SKIP_DRAW, NULL); PutWindowTilemap(sTutorMoveAndElevatorWindowId); CopyWindowToVram(sTutorMoveAndElevatorWindowId, COPYWIN_FULL); @@ -1876,17 +1899,23 @@ void CloseDeptStoreElevatorWindow(void) RemoveWindow(sTutorMoveAndElevatorWindowId); } +// Task data for Task_MoveElevatorWindowLights +#define tMoveCounter data[0] +#define tTimer data[1] +#define tDescending data[2] +#define tTotalMoves data[3] + static void MoveElevatorWindowLights(u16 floorDelta, bool8 descending) { - static const u8 sElevatorLightCycles[] = { 3, 6, 9, 12, 15, 18, 21, 24, 27 }; + static const u8 sElevatorLightCycles[MAX_ELEVATOR_TRIP] = { 3, 6, 9, 12, 15, 18, 21, 24, 27 }; if (FuncIsActiveTask(Task_MoveElevatorWindowLights) != TRUE) { u8 taskId = CreateTask(Task_MoveElevatorWindowLights, 8); - gTasks[taskId].data[0] = 0; - gTasks[taskId].data[1] = 0; - gTasks[taskId].data[2] = descending; - gTasks[taskId].data[3] = sElevatorLightCycles[floorDelta]; + gTasks[taskId].tMoveCounter = 0; + gTasks[taskId].tTimer = 0; + gTasks[taskId].tDescending = descending; + gTasks[taskId].tTotalMoves = sElevatorLightCycles[floorDelta]; } } @@ -1895,36 +1924,41 @@ static void Task_MoveElevatorWindowLights(u8 taskId) u8 x, y; s16 *data = gTasks[taskId].data; - if (data[1] == 6) + if (tTimer == 6) { - data[0]++; + tMoveCounter++; - // ascending - if (data[2] == FALSE) + if (!tDescending) { - for (y = 0; y < 3; y++) + // Ascending + for (y = 0; y < ELEVATOR_WINDOW_HEIGHT; y++) { - for (x = 0; x < 3; x++) - MapGridSetMetatileIdAt(x + MAP_OFFSET + 1, y + MAP_OFFSET, sElevatorWindowTiles_Ascending[y][data[0] % 3] | MAPGRID_COLLISION_MASK); + for (x = 0; x < ELEVATOR_WINDOW_WIDTH; x++) + MapGridSetMetatileIdAt(x + MAP_OFFSET + 1, y + MAP_OFFSET, sElevatorWindowTiles_Ascending[y][tMoveCounter % ELEVATOR_LIGHT_STAGES] | MAPGRID_COLLISION_MASK); } } - // descending else { - for (y = 0; y < 3; y++) + // Descending + for (y = 0; y < ELEVATOR_WINDOW_HEIGHT; y++) { - for (x = 0; x < 3; x++) - MapGridSetMetatileIdAt(x + MAP_OFFSET + 1, y + MAP_OFFSET, sElevatorWindowTiles_Descending[y][data[0] % 3] | MAPGRID_COLLISION_MASK); + for (x = 0; x < ELEVATOR_WINDOW_WIDTH; x++) + MapGridSetMetatileIdAt(x + MAP_OFFSET + 1, y + MAP_OFFSET, sElevatorWindowTiles_Descending[y][tMoveCounter % ELEVATOR_LIGHT_STAGES] | MAPGRID_COLLISION_MASK); } } DrawWholeMapView(); - data[1] = 0; - if (data[0] == data[3]) + tTimer = 0; + if (tMoveCounter == tTotalMoves) DestroyTask(taskId); } - data[1]++; + tTimer++; } +#undef tMoveCounter +#undef tTimer +#undef tDescending +#undef tTotalMoves + void BufferVarsForIVRater(void) { u8 i;