pokeemerald/src/field_tasks.c

761 lines
21 KiB
C
Raw Normal View History

2018-05-19 18:02:43 +02:00
#include "global.h"
#include "bike.h"
2018-11-14 01:01:50 +01:00
#include "clock.h"
2018-05-19 18:02:43 +02:00
#include "event_data.h"
2018-11-14 01:01:50 +01:00
#include "field_camera.h"
#include "field_effect_helpers.h"
#include "field_player_avatar.h"
#include "field_special_scene.h"
#include "field_tasks.h"
#include "fieldmap.h"
#include "item.h"
#include "main.h"
#include "metatile_behavior.h"
2018-05-19 18:02:43 +02:00
#include "overworld.h"
#include "script.h"
#include "secret_base.h"
#include "sound.h"
2018-11-14 01:01:50 +01:00
#include "task.h"
#include "constants/items.h"
#include "constants/songs.h"
#include "constants/vars.h"
#include "constants/metatile_labels.h"
2018-05-19 18:02:43 +02:00
2018-12-31 19:22:27 +01:00
struct PacifidlogMetatileOffsets
2018-05-19 18:02:43 +02:00
{
s8 x;
s8 y;
u16 tileId;
};
static void DummyPerStepCallback(u8 taskId);
2018-12-31 19:22:27 +01:00
static void AshGrassPerStepCallback(u8 taskId);
static void FortreeBridgePerStepCallback(u8 taskId);
static void PacifidlogBridgePerStepCallback(u8 taskId);
static void SootopolisGymIcePerStepCallback(u8 taskId);
static void CrackedFloorPerStepCallback(u8 taskId);
2018-05-19 18:02:43 +02:00
static void Task_MuddySlope(u8 taskId);
2018-12-31 19:22:27 +01:00
static const TaskFunc sPerStepCallbacks[] =
2018-05-19 18:02:43 +02:00
{
DummyPerStepCallback,
2018-12-31 19:22:27 +01:00
AshGrassPerStepCallback,
FortreeBridgePerStepCallback,
PacifidlogBridgePerStepCallback,
SootopolisGymIcePerStepCallback,
2018-05-19 18:02:43 +02:00
EndTruckSequence,
2018-12-31 19:22:27 +01:00
SecretBasePerStepCallback,
CrackedFloorPerStepCallback
2018-05-19 18:02:43 +02:00
};
// they are in pairs but declared as 1D array
2018-12-31 19:22:27 +01:00
static const struct PacifidlogMetatileOffsets sHalfSubmergedBridgeMetatileOffsets[] =
2018-05-19 18:02:43 +02:00
{
{ 0, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Vertical0)}, {0, 1, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Vertical1)},
{ 0, -1, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Vertical0)}, {0, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Vertical1)},
{ 0, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Horizontal0)}, {1, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Horizontal1)},
{-1, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Horizontal0)}, {0, 0, METATILE_ID(Pacifidlog, HalfSubmergedLogs_Horizontal1)}
2018-05-19 18:02:43 +02:00
};
2018-12-31 19:22:27 +01:00
static const struct PacifidlogMetatileOffsets sFullySubmergedBridgeMetatileOffsets[] =
2018-05-19 18:02:43 +02:00
{
{ 0, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Vertical0)}, {0, 1, METATILE_ID(Pacifidlog, SubmergedLogs_Vertical1)},
{ 0, -1, METATILE_ID(Pacifidlog, SubmergedLogs_Vertical0)}, {0, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Vertical1)},
{ 0, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Horizontal0)}, {1, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Horizontal1)},
{-1, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Horizontal0)}, {0, 0, METATILE_ID(Pacifidlog, SubmergedLogs_Horizontal1)}
2018-05-19 18:02:43 +02:00
};
2018-12-31 19:22:27 +01:00
static const struct PacifidlogMetatileOffsets sFloatingBridgeMetatileOffsets[] =
2018-05-19 18:02:43 +02:00
{
{ 0, 0, METATILE_ID(Pacifidlog, FloatingLogs_Vertical0)}, {0, 1, METATILE_ID(Pacifidlog, FloatingLogs_Vertical1)},
{ 0, -1, METATILE_ID(Pacifidlog, FloatingLogs_Vertical0)}, {0, 0, METATILE_ID(Pacifidlog, FloatingLogs_Vertical1)},
{ 0, 0, METATILE_ID(Pacifidlog, FloatingLogs_Horizontal0)}, {1, 0, METATILE_ID(Pacifidlog, FloatingLogs_Horizontal1)},
{-1, 0, METATILE_ID(Pacifidlog, FloatingLogs_Horizontal0)}, {0, 0, METATILE_ID(Pacifidlog, FloatingLogs_Horizontal1)}
2018-05-19 18:02:43 +02:00
};
2018-12-31 19:22:27 +01:00
// Each element corresponds to a y coordinate row in the sootopolis gym 1F map.
static const u16 sSootopolisGymIceRowVars[] =
2018-05-19 18:02:43 +02:00
{
0,
0,
0,
0,
0,
0,
VAR_TEMP_1,
VAR_TEMP_2,
VAR_TEMP_3,
VAR_TEMP_4,
0,
0,
VAR_TEMP_5,
VAR_TEMP_6,
VAR_TEMP_7,
0,
0,
VAR_TEMP_8,
VAR_TEMP_9,
VAR_TEMP_A,
0,
0,
0,
0,
0,
0
};
static const u16 sMuddySlopeMetatiles[] = {
METATILE_ID(General, MuddySlope_Frame0),
METATILE_ID(General, MuddySlope_Frame3),
METATILE_ID(General, MuddySlope_Frame2),
METATILE_ID(General, MuddySlope_Frame1)
};
2018-05-19 18:02:43 +02:00
static void Task_RunPerStepCallback(u8 taskId)
{
int idx = gTasks[taskId].data[0];
2018-12-31 19:22:27 +01:00
sPerStepCallbacks[idx](taskId);
2018-05-19 18:02:43 +02:00
}
#define tState data[0]
#define tAmbientCryState data[1]
#define tAmbientCryDelay data[2]
static void RunTimeBasedEvents(s16 *data)
{
switch (tState)
{
case 0:
if (gMain.vblankCounter1 & 0x1000)
{
DoTimeBasedEvents();
tState++;
}
break;
case 1:
if (!(gMain.vblankCounter1 & 0x1000))
{
tState--;
}
break;
}
}
static void Task_RunTimeBasedEvents(u8 taskId)
{
s16 *data = gTasks[taskId].data;
if (!ScriptContext2_IsEnabled())
{
RunTimeBasedEvents(data);
UpdateAmbientCry(&tAmbientCryState, &tAmbientCryDelay);
}
}
#undef tState
#undef tAmbientCryState
#undef tAmbientCryDelay
void SetUpFieldTasks(void)
{
if (!FuncIsActiveTask(Task_RunPerStepCallback))
{
u8 taskId = CreateTask(Task_RunPerStepCallback, 0x50);
gTasks[taskId].data[0] = 0;
}
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (!FuncIsActiveTask(Task_MuddySlope))
CreateTask(Task_MuddySlope, 0x50);
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (!FuncIsActiveTask(Task_RunTimeBasedEvents))
CreateTask(Task_RunTimeBasedEvents, 0x50);
}
void ActivatePerStepCallback(u8 callbackId)
{
u8 taskId = FindTaskIdByFunc(Task_RunPerStepCallback);
if (taskId != 0xff)
{
s32 i;
s16 *data = gTasks[taskId].data;
for (i = 0; i < 16; i++)
data[i] = 0;
2018-12-31 19:22:27 +01:00
if (callbackId >= ARRAY_COUNT(sPerStepCallbacks))
2018-05-19 18:02:43 +02:00
{
data[0] = 0;
}
else
{
data[0] = callbackId;
}
}
}
void ResetFieldTasksArgs(void)
{
u8 taskId;
s16 *data;
taskId = FindTaskIdByFunc(Task_RunPerStepCallback);
if (taskId != 0xff)
{
data = gTasks[taskId].data;
}
taskId = FindTaskIdByFunc(Task_RunTimeBasedEvents);
if (taskId != 0xff)
{
data = gTasks[taskId].data;
data[1] = 0;
data[2] = 0;
}
}
static void DummyPerStepCallback(u8 taskId)
{
}
2018-12-31 19:22:27 +01:00
static const struct PacifidlogMetatileOffsets *GetPacifidlogBridgeMetatileOffsets(const struct PacifidlogMetatileOffsets *offsets, u16 metatileBehavior)
2018-05-19 18:02:43 +02:00
{
if (MetatileBehavior_IsPacifilogVerticalLog1(metatileBehavior))
return &offsets[0 * 2];
else if (MetatileBehavior_IsPacifilogVerticalLog2(metatileBehavior))
return &offsets[1 * 2];
else if (MetatileBehavior_IsPacifilogHorizontalLog1(metatileBehavior))
return &offsets[2 * 2];
else if (MetatileBehavior_IsPacifilogHorizontalLog2(metatileBehavior))
return &offsets[3 * 2];
else
return NULL;
}
2018-12-31 19:22:27 +01:00
static void SetPacifidlogBridgeMetatiles(const struct PacifidlogMetatileOffsets *offsets, s16 x, s16 y, bool32 redrawMap)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
offsets = GetPacifidlogBridgeMetatileOffsets(offsets, MapGridGetMetatileBehaviorAt(x, y));
if (offsets)
2018-05-19 18:02:43 +02:00
{
MapGridSetMetatileIdAt(x + offsets[0].x, y + offsets[0].y, offsets[0].tileId);
2018-12-31 19:22:27 +01:00
if (redrawMap)
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x + offsets[0].x, y + offsets[0].y);
MapGridSetMetatileIdAt(x + offsets[1].x, y + offsets[1].y, offsets[1].tileId);
2018-12-31 19:22:27 +01:00
if (redrawMap)
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x + offsets[1].x, y + offsets[1].y);
}
}
2018-12-31 19:22:27 +01:00
static void UpdateHalfSubmergedBridgeMetatiles(s16 x, s16 y, bool32 redrawMap)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
SetPacifidlogBridgeMetatiles(sHalfSubmergedBridgeMetatileOffsets, x, y, redrawMap);
2018-05-19 18:02:43 +02:00
}
2018-12-31 19:22:27 +01:00
static void UpdateFullySubmergedBridgeMetatiles(s16 x, s16 y, bool32 redrawMap)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
SetPacifidlogBridgeMetatiles(sFullySubmergedBridgeMetatileOffsets, x, y, redrawMap);
2018-05-19 18:02:43 +02:00
}
2018-12-31 19:22:27 +01:00
static void UpdateFloatingBridgeMetatiles(s16 x, s16 y, bool32 redrawMap)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
SetPacifidlogBridgeMetatiles(sFloatingBridgeMetatileOffsets, x, y, redrawMap);
2018-05-19 18:02:43 +02:00
}
2018-12-31 19:22:27 +01:00
static bool32 StandingOnNewPacifidlogBridge(s16 x1, s16 y1, s16 x2, s16 y2)
2018-05-19 18:02:43 +02:00
{
u16 metatileBehavior = MapGridGetMetatileBehaviorAt(x2, y2);
if (MetatileBehavior_IsPacifilogVerticalLog1(metatileBehavior))
{
if (y1 > y2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogVerticalLog2(metatileBehavior))
{
if (y1 < y2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogHorizontalLog1(metatileBehavior))
{
if (x1 > x2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogHorizontalLog2(metatileBehavior))
{
if (x1 < x2)
return FALSE;
}
return TRUE;
}
2018-12-31 19:22:27 +01:00
static bool32 StandingOnSamePacifidlogBridge(s16 x1, s16 y1, s16 x2, s16 y2)
2018-05-19 18:02:43 +02:00
{
u16 metatileBehavior = MapGridGetMetatileBehaviorAt(x1, y1);
if (MetatileBehavior_IsPacifilogVerticalLog1(metatileBehavior))
{
if (y1 < y2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogVerticalLog2(metatileBehavior))
{
if (y1 > y2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogHorizontalLog1(metatileBehavior))
{
if (x1 < x2)
return FALSE;
}
else if (MetatileBehavior_IsPacifilogHorizontalLog2(metatileBehavior))
{
if (x1 > x2)
return FALSE;
}
return TRUE;
}
2018-12-31 19:22:27 +01:00
static void PacifidlogBridgePerStepCallback(u8 taskId)
2018-05-19 18:02:43 +02:00
{
s16 *data;
s16 x, y;
data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
switch (data[1])
{
case 0:
data[2] = x;
data[3] = y;
2018-12-31 19:22:27 +01:00
UpdateFullySubmergedBridgeMetatiles(x, y, TRUE);
2018-05-19 18:02:43 +02:00
data[1] = 1;
break;
case 1:
if (x != data[2] || y != data[3])
{
2018-12-31 19:22:27 +01:00
if (StandingOnNewPacifidlogBridge(x, y, data[2], data[3]))
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
UpdateHalfSubmergedBridgeMetatiles(data[2], data[3], TRUE);
UpdateFloatingBridgeMetatiles(data[2], data[3], FALSE);
2018-05-19 18:02:43 +02:00
data[4] = data[2];
data[5] = data[3];
data[1] = 2;
data[6] = 8;
}
else
{
data[4] = -1;
data[5] = -1;
}
2018-12-31 19:22:27 +01:00
if (StandingOnSamePacifidlogBridge(x, y, data[2], data[3]))
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
UpdateHalfSubmergedBridgeMetatiles(x, y, TRUE);
2018-05-19 18:02:43 +02:00
data[1] = 2;
data[6] = 8;
}
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
data[2] = x;
data[3] = y;
if (MetatileBehavior_IsPacifidlogLog(MapGridGetMetatileBehaviorAt(x, y)))
PlaySE(SE_MIZU);
}
break;
case 2:
if ((--data[6]) == 0)
{
2018-12-31 19:22:27 +01:00
UpdateFullySubmergedBridgeMetatiles(x, y, TRUE);
2018-05-19 18:02:43 +02:00
if (data[4] != -1 && data[5] != -1)
2018-12-31 19:22:27 +01:00
UpdateFloatingBridgeMetatiles(data[4], data[5], TRUE);
2018-05-19 18:02:43 +02:00
data[1] = 1;
}
break;
}
}
2018-12-31 19:22:27 +01:00
static void SetLoweredForetreeBridgeMetatile(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
u8 z = PlayerGetZCoord();
2018-12-31 19:22:27 +01:00
if (!(z & 1))
2018-05-19 18:02:43 +02:00
{
switch (MapGridGetMetatileIdAt(x, y))
{
case METATILE_ID(Fortree, BridgeOverGrass_Raised):
MapGridSetMetatileIdAt(x, y, METATILE_ID(Fortree, BridgeOverGrass_Lowered));
2018-05-19 18:02:43 +02:00
break;
case METATILE_ID(Fortree, BridgeOverTrees_Raised):
MapGridSetMetatileIdAt(x, y, METATILE_ID(Fortree, BridgeOverTrees_Lowered));
2018-05-19 18:02:43 +02:00
break;
}
}
}
2018-12-31 19:22:27 +01:00
static void SetNormalFortreeBridgeMetatile(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
u8 z = PlayerGetZCoord();
2018-12-31 19:22:27 +01:00
if (!(z & 1))
2018-05-19 18:02:43 +02:00
{
switch (MapGridGetMetatileIdAt(x, y))
{
case METATILE_ID(Fortree, BridgeOverGrass_Lowered):
MapGridSetMetatileIdAt(x, y, METATILE_ID(Fortree, BridgeOverGrass_Raised));
2018-05-19 18:02:43 +02:00
break;
case METATILE_ID(Fortree, BridgeOverTrees_Lowered):
MapGridSetMetatileIdAt(x, y, METATILE_ID(Fortree, BridgeOverTrees_Raised));
2018-05-19 18:02:43 +02:00
break;
}
}
}
2018-12-31 19:22:27 +01:00
static void FortreeBridgePerStepCallback(u8 taskId)
2018-05-19 18:02:43 +02:00
{
bool8 isFortreeBridgeCur;
bool8 isFortreeBridgePrev;
u8 z, flag;
s16 x, y, x2, y2;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
switch (data[1])
{
default:
break;
case 0:
data[2] = x;
data[3] = y;
if (MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(x, y)))
{
2018-12-31 19:22:27 +01:00
SetLoweredForetreeBridgeMetatile(x, y);
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x, y);
}
data[1] = 1;
break;
case 1:
x2 = data[2];
y2 = data[3];
if (x == x2 && y == y2)
break;
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
isFortreeBridgeCur = MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(x, y));
isFortreeBridgePrev = MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(x2, y2));
z = PlayerGetZCoord();
flag = 0;
if ((u8)(z & 1) == 0)
flag = 1;
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (flag && (isFortreeBridgeCur == 1 || isFortreeBridgePrev == 1))
PlaySE(SE_HASHI);
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (isFortreeBridgePrev)
{
2018-12-31 19:22:27 +01:00
SetNormalFortreeBridgeMetatile(x2, y2);
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x2, y2);
2018-12-31 19:22:27 +01:00
SetLoweredForetreeBridgeMetatile(x, y);
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x, y);
}
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
data[4] = x2;
data[5] = y2;
data[2] = x;
data[3] = y;
if (!isFortreeBridgePrev)
break;
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
data[6] = 16;
data[1] = 2;
// fallthrough
case 2:
data[6]--;
x2 = data[4];
y2 = data[5];
switch (data[6] % 7)
{
case 0:
CurrentMapDrawMetatileAt(x2, y2);
case 1:
case 2:
case 3:
break;
case 4:
2018-12-31 19:22:27 +01:00
SetLoweredForetreeBridgeMetatile(x2, y2);
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x2, y2);
2018-12-31 19:22:27 +01:00
SetNormalFortreeBridgeMetatile(x2, y2);
2018-05-19 18:02:43 +02:00
case 5:
case 6:
case 7:
break;
}
if (data[6] == 0)
{
data[1] = 1;
}
break;
}
}
2018-12-31 19:22:27 +01:00
static bool32 CoordInIcePuzzleRegion(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
if ((u16)(x - 3) < 11 && (u16)(y - 6) < 14 && sSootopolisGymIceRowVars[y])
2018-05-19 18:02:43 +02:00
return TRUE;
else
return FALSE;
}
2018-12-31 19:22:27 +01:00
static void MarkIcePuzzleCoordVisited(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
if (CoordInIcePuzzleRegion(x, y))
*GetVarPointer(sSootopolisGymIceRowVars[y]) |= (1 << (x - 3));
2018-05-19 18:02:43 +02:00
}
2018-12-31 19:22:27 +01:00
static bool32 IsIcePuzzleCoordVisited(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
u32 var;
2018-12-31 19:22:27 +01:00
if (!CoordInIcePuzzleRegion(x, y))
2018-05-19 18:02:43 +02:00
return FALSE;
2018-12-31 19:22:27 +01:00
var = VarGet(sSootopolisGymIceRowVars[y]) << 16;
if ((0x10000 << (x - 3)) & var) // TODO: fix that if
2018-05-19 18:02:43 +02:00
return TRUE;
else
return FALSE;
}
void SetSootopolisGymCrackedIceMetatiles(void)
{
s32 x, y;
2018-06-21 00:41:51 +02:00
s32 width = gMapHeader.mapLayout->width;
s32 height = gMapHeader.mapLayout->height;
2018-05-19 18:02:43 +02:00
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
2018-12-31 19:22:27 +01:00
if (IsIcePuzzleCoordVisited(x, y) == TRUE)
MapGridSetMetatileIdAt(x + 7, y + 7, METATILE_ID(SootopolisGym, Ice_Cracked));
2018-05-19 18:02:43 +02:00
}
}
}
2018-12-31 19:22:27 +01:00
static void SootopolisGymIcePerStepCallback(u8 taskId)
2018-05-19 18:02:43 +02:00
{
s16 x, y;
u16 tileBehavior;
2018-12-31 19:22:27 +01:00
u16 *iceStepCount;
2018-05-19 18:02:43 +02:00
s16 *data = gTasks[taskId].data;
switch (data[1])
{
case 0:
PlayerGetDestCoords(&x, &y);
data[2] = x;
data[3] = y;
data[1] = 1;
break;
case 1:
PlayerGetDestCoords(&x, &y);
if (x != data[2] || y != data[3])
{
data[2] = x;
data[3] = y;
tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
2018-12-31 19:22:27 +01:00
iceStepCount = GetVarPointer(VAR_ICE_STEP_COUNT);
2018-05-19 18:02:43 +02:00
if (MetatileBehavior_IsThinIce(tileBehavior) == TRUE)
{
2018-12-31 19:22:27 +01:00
(*iceStepCount)++;
2018-05-19 18:02:43 +02:00
data[6] = 4;
data[1] = 2;
data[4] = x;
data[5] = y;
}
else if (MetatileBehavior_IsCrackedIce(tileBehavior) == TRUE)
{
2018-12-31 19:22:27 +01:00
*iceStepCount = 0;
2018-05-19 18:02:43 +02:00
data[6] = 4;
data[1] = 3;
data[4] = x;
data[5] = y;
}
}
break;
case 2:
if (data[6] != 0)
{
data[6]--;
}
else
{
x = data[4];
y = data[5];
PlaySE(SE_RU_BARI);
MapGridSetMetatileIdAt(x, y, METATILE_ID(SootopolisGym, Ice_Cracked));
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x, y);
2018-12-31 19:22:27 +01:00
MarkIcePuzzleCoordVisited(x - 7, y - 7);
2018-05-19 18:02:43 +02:00
data[1] = 1;
}
break;
case 3:
if (data[6] != 0)
{
data[6]--;
}
else
{
x = data[4];
y = data[5];
PlaySE(SE_RU_GASYAN);
MapGridSetMetatileIdAt(x, y, METATILE_ID(SootopolisGym, Ice_Broken));
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x, y);
data[1] = 1;
}
break;
}
}
2018-12-31 19:22:27 +01:00
static void AshGrassPerStepCallback(u8 taskId)
2018-05-19 18:02:43 +02:00
{
s16 x, y;
2018-12-31 19:22:27 +01:00
u16 *ashGatherCount;
2018-05-19 18:02:43 +02:00
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
if (x != data[1] || y != data[2])
{
data[1] = x;
data[2] = y;
2018-06-16 00:45:48 +02:00
if (MetatileBehavior_IsAshGrass(MapGridGetMetatileBehaviorAt(x, y)))
2018-05-19 18:02:43 +02:00
{
if (MapGridGetMetatileIdAt(x, y) == METATILE_ID(Fallarbor, AshGrass))
StartAshFieldEffect(x, y, METATILE_ID(Fallarbor, NormalGrass), 4);
2018-05-19 18:02:43 +02:00
else
2019-06-12 04:22:50 +02:00
StartAshFieldEffect(x, y, METATILE_ID(Lavaridge, NormalGrass), 4);
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (CheckBagHasItem(ITEM_SOOT_SACK, 1))
{
2018-12-31 19:22:27 +01:00
ashGatherCount = GetVarPointer(VAR_ASH_GATHER_COUNT);
if (*ashGatherCount < 9999)
(*ashGatherCount)++;
2018-05-19 18:02:43 +02:00
}
}
}
}
2018-12-31 19:22:27 +01:00
static void SetCrackedFloorHoleMetatile(s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
2019-06-12 04:22:50 +02:00
MapGridSetMetatileIdAt(x, y, MapGridGetMetatileIdAt(x, y) == 0x22f ? 0x206 : 0x237);// unsure what these are referring to
2018-05-19 18:02:43 +02:00
CurrentMapDrawMetatileAt(x, y);
}
2018-12-31 19:22:27 +01:00
static void CrackedFloorPerStepCallback(u8 taskId)
2018-05-19 18:02:43 +02:00
{
s16 x, y;
u16 behavior;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
behavior = MapGridGetMetatileBehaviorAt(x, y);
if (data[4] != 0 && (--data[4]) == 0)
2018-12-31 19:22:27 +01:00
SetCrackedFloorHoleMetatile(data[5], data[6]);
2018-05-19 18:02:43 +02:00
if (data[7] != 0 && (--data[7]) == 0)
2018-12-31 19:22:27 +01:00
SetCrackedFloorHoleMetatile(data[8], data[9]);
2018-05-19 18:02:43 +02:00
if (MetatileBehavior_IsCrackedFloorHole(behavior))
VarSet(VAR_ICE_STEP_COUNT, 0); // this var does double duty
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if ((x != data[2] || y != data[3]))
{
data[2] = x;
data[3] = y;
if (MetatileBehavior_IsCrackedFloor(behavior))
{
if (GetPlayerSpeed() != 4)
VarSet(VAR_ICE_STEP_COUNT, 0); // this var does double duty
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
if (data[4] == 0)
{
data[4] = 3;
data[5] = x;
data[6] = y;
}
else if (data[7] == 0)
{
data[7] = 3;
data[8] = x;
data[9] = y;
}
}
}
}
2018-12-31 19:22:27 +01:00
static void SetMuddySlopeMetatile(s16 *data, s16 x, s16 y)
2018-05-19 18:02:43 +02:00
{
u16 tile;
if ((--data[0]) == 0)
tile = 0xe8;
else
2018-12-31 19:22:27 +01:00
tile = sMuddySlopeMetatiles[data[0] / 8];
2018-05-19 18:02:43 +02:00
MapGridSetMetatileIdAt(x, y, tile);
CurrentMapDrawMetatileAt(x, y);
MapGridSetMetatileIdAt(x, y, METATILE_ID(General, MuddySlope_Frame0));
2018-05-19 18:02:43 +02:00
}
static void Task_MuddySlope(u8 taskId)
{
s16 x, y, x2, y2;
int i;
2018-12-31 19:22:27 +01:00
u16 mapId;
2018-05-19 18:02:43 +02:00
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
2018-12-31 19:22:27 +01:00
mapId = (gSaveBlock1Ptr->location.mapGroup << 8) | gSaveBlock1Ptr->location.mapNum;
2018-05-19 18:02:43 +02:00
switch (data[1])
{
case 0:
2018-12-31 19:22:27 +01:00
data[0] = mapId;
2018-05-19 18:02:43 +02:00
data[2] = x;
data[3] = y;
data[1] = 1;
data[4] = 0;
data[7] = 0;
data[10] = 0;
data[13] = 0;
break;
case 1:
if (data[2] != x || data[3] != y)
{
data[2] = x;
data[3] = y;
if (MetatileBehavior_IsMuddySlope(MapGridGetMetatileBehaviorAt(x, y)))
{
2018-12-31 19:22:27 +01:00
for (i = 4; i < 14; i += 3)
2018-05-19 18:02:43 +02:00
{
if (data[i] == 0)
{
data[i] = 32;
data[i + 1] = x;
data[i + 2] = y;
break;
}
}
}
}
break;
}
2018-12-31 19:22:27 +01:00
if (gCamera.active && mapId != data[0])
2018-05-19 18:02:43 +02:00
{
2018-12-31 19:22:27 +01:00
data[0] = mapId;
2018-05-19 18:02:43 +02:00
x2 = gCamera.x;
y2 = gCamera.y;
}
else
{
x2 = 0;
y2 = 0;
}
2018-12-31 19:22:27 +01:00
2018-05-19 18:02:43 +02:00
for (i = 4; i < 14; i += 3)
{
if (data[i])
{
data[i + 1] -= x2;
data[i + 2] -= y2;
2018-12-31 19:22:27 +01:00
SetMuddySlopeMetatile(&data[i], data[i + 1], data[i + 2]);
2018-05-19 18:02:43 +02:00
}
}
}