pokeemerald/src/overworld.c

3225 lines
91 KiB
C
Raw Normal View History

2017-09-30 19:08:28 +02:00
#include "global.h"
2018-02-12 15:01:43 +01:00
#include "overworld.h"
2018-11-18 17:52:22 +01:00
#include "battle_pyramid.h"
2018-02-12 15:01:43 +01:00
#include "battle_setup.h"
#include "berry.h"
#include "bg.h"
2018-12-08 22:05:11 +01:00
#include "cable_club.h"
2018-02-12 15:01:43 +01:00
#include "clock.h"
#include "event_data.h"
#include "event_object_movement.h"
#include "event_scripts.h"
2018-02-12 15:01:43 +01:00
#include "field_camera.h"
2018-02-14 00:58:22 +01:00
#include "field_control_avatar.h"
2018-02-12 15:01:43 +01:00
#include "field_effect.h"
#include "field_message_box.h"
#include "field_player_avatar.h"
#include "field_screen_effect.h"
2018-12-08 22:05:11 +01:00
#include "field_special_scene.h"
2018-02-12 15:01:43 +01:00
#include "field_specials.h"
#include "field_tasks.h"
#include "field_weather.h"
#include "fieldmap.h"
2019-02-08 18:48:51 +01:00
#include "fldeff.h"
#include "gpu_regs.h"
2018-02-12 15:01:43 +01:00
#include "heal_location.h"
#include "io_reg.h"
2018-02-12 15:01:43 +01:00
#include "link.h"
#include "link_rfu.h"
2018-02-12 15:01:43 +01:00
#include "load_save.h"
#include "main.h"
#include "malloc.h"
2018-02-12 15:01:43 +01:00
#include "m4a.h"
#include "map_name_popup.h"
2019-02-08 18:48:51 +01:00
#include "match_call.h"
2018-02-12 15:01:43 +01:00
#include "menu.h"
#include "metatile_behavior.h"
#include "mirage_tower.h"
#include "money.h"
2018-02-12 15:01:43 +01:00
#include "new_game.h"
#include "palette.h"
#include "play_time.h"
#include "random.h"
#include "roamer.h"
2018-12-08 22:05:11 +01:00
#include "rotating_gate.h"
2018-02-12 15:01:43 +01:00
#include "safari_zone.h"
#include "save.h"
#include "save_location.h"
2018-02-12 15:01:43 +01:00
#include "script.h"
#include "script_pokemon_util.h"
2018-02-12 15:01:43 +01:00
#include "secret_base.h"
#include "sound.h"
#include "start_menu.h"
#include "task.h"
2019-02-08 18:48:51 +01:00
#include "tileset_anims.h"
2018-02-12 15:01:43 +01:00
#include "time_events.h"
2019-01-13 20:50:08 +01:00
#include "trainer_hill.h"
2019-02-08 18:48:51 +01:00
#include "trainer_pokemon_sprites.h"
2018-02-12 15:01:43 +01:00
#include "tv.h"
#include "scanline_effect.h"
#include "wild_encounter.h"
2018-10-30 22:17:03 +01:00
#include "frontier_util.h"
2018-02-14 00:58:22 +01:00
#include "constants/abilities.h"
2019-01-31 22:51:20 +01:00
#include "constants/layouts.h"
2018-07-13 05:39:38 +02:00
#include "constants/map_types.h"
2018-12-27 23:30:47 +01:00
#include "constants/region_map_sections.h"
#include "constants/songs.h"
2019-11-13 22:10:05 +01:00
#include "constants/trainer_hill.h"
2018-12-27 23:30:47 +01:00
#include "constants/weather.h"
2018-02-12 15:01:43 +01:00
2021-04-06 22:55:33 +02:00
struct CableClubPlayer
{
u8 playerId;
u8 isLocalPlayer;
u8 movementMode;
u8 facing;
struct MapPosition pos;
u16 metatileBehavior;
};
#define PLAYER_LINK_STATE_IDLE 0x80
#define PLAYER_LINK_STATE_BUSY 0x81
#define PLAYER_LINK_STATE_READY 0x82
#define PLAYER_LINK_STATE_EXITING_ROOM 0x83
#define FACING_NONE 0
#define FACING_UP 1
#define FACING_DOWN 2
#define FACING_LEFT 3
#define FACING_RIGHT 4
#define FACING_FORCED_UP 7
#define FACING_FORCED_DOWN 8
#define FACING_FORCED_LEFT 9
#define FACING_FORCED_RIGHT 10
2018-06-21 00:41:51 +02:00
extern const struct MapLayout *const gMapLayouts[];
2018-02-14 00:58:22 +01:00
extern const struct MapHeader *const *const gMapGroups[];
2018-02-12 15:01:43 +01:00
static void Overworld_ResetStateAfterWhiteOut(void);
static void CB2_ReturnToFieldLocal(void);
static void CB2_ReturnToFieldLink(void);
static void CB2_LoadMapOnReturnToFieldCableClub(void);
2018-02-14 00:58:22 +01:00
static void CB2_LoadMap2(void);
static void VBlankCB_Field(void);
2021-04-06 22:55:33 +02:00
static void SpriteCB_LinkPlayer(struct Sprite *);
2018-02-14 00:58:22 +01:00
static void ChooseAmbientCrySpecies(void);
2021-04-06 22:55:33 +02:00
static void DoMapLoadLoop(u8 *);
static bool32 LoadMapInStepsLocal(u8 *, bool32);
static bool32 LoadMapInStepsLink(u8 *);
static bool32 ReturnToFieldLocal(u8 *);
static bool32 ReturnToFieldLink(u8 *);
static void InitObjectEventsLink(void);
static void InitObjectEventsLocal(void);
static void InitOverworldGraphicsRegisters(void);
static u8 GetSpriteForLinkedPlayer(u8);
2021-04-06 22:55:33 +02:00
static u16 KeyInterCB_SendNothing(u32);
static void ResetMirageTowerAndSaveBlockPtrs(void);
2021-04-06 22:55:33 +02:00
static void ResetScreenForMapLoad(void);
static void OffsetCameraFocusByLinkPlayerId(void);
static void SpawnLinkPlayers(void);
static void SetCameraToTrackGuestPlayer(void);
2021-04-06 22:55:33 +02:00
static void ResumeMap(bool32);
static void SetCameraToTrackPlayer(void);
2021-04-06 11:05:33 +02:00
static void InitObjectEventsReturnToField(void);
static void InitViewGraphics(void);
static void SetCameraToTrackGuestPlayer_2(void);
static void CreateLinkPlayerSprites(void);
static void ClearAllPlayerKeys(void);
2021-04-06 22:55:33 +02:00
static void ResetAllPlayerLinkStates(void);
static void UpdateHeldKeyCode(u16);
static void UpdateAllLinkPlayers(u16*, s32);
2021-04-06 22:55:33 +02:00
static u8 FlipVerticalAndClearForced(u8, u8);
static u8 LinkPlayerDetectCollision(u8, u8, s16, s16);
static void CreateLinkPlayerSprite(u8, u8);
static void GetLinkPlayerCoords(u8, u16 *, u16 *);
static u8 GetLinkPlayerFacingDirection(u8);
static u8 GetLinkPlayerElevation(u8);
static s32 GetLinkPlayerObjectStepTimer(u8);
static u8 GetLinkPlayerIdAt(s16, s16);
static void SetPlayerFacingDirection(u8, u8);
static void ZeroObjectEvent(struct ObjectEvent *);
static void SpawnLinkPlayerObjectEvent(u8, s16, s16, u8);
static void InitLinkPlayerObjectEventPos(struct ObjectEvent *, s16, s16);
static void SetLinkPlayerObjectRange(u8, u8);
static void DestroyLinkPlayerObject(u8);
static u8 GetSpriteForLinkedPlayer(u8);
static void RunTerminateLinkScript(void);
static u32 GetLinkSendQueueLength(void);
2021-04-06 22:55:33 +02:00
static void ZeroLinkPlayerObjectEvent(struct LinkPlayerObjectEvent *);
static const u8 *TryInteractWithPlayer(struct CableClubPlayer *);
static u16 GetDirectionForEventScript(const u8 *);
static void InitLinkPlayerQueueScript(void);
static void InitLinkRoomStartMenuScript(void);
2021-04-06 22:55:33 +02:00
static void RunInteractLocalPlayerScript(const u8 *);
static void RunConfirmLeaveCableClubScript(void);
static void InitMenuBasedScript(const u8 *);
static void LoadCableClubPlayer(s32, s32, struct CableClubPlayer *);
static bool32 IsCableClubPlayerUnfrozen(struct CableClubPlayer *);
static bool32 CanCableClubPlayerPressStart(struct CableClubPlayer *);
static u8 *TryGetTileEventScript(struct CableClubPlayer *);
static bool32 PlayerIsAtSouthExit(struct CableClubPlayer *);
static const u8 *TryInteractWithPlayer(struct CableClubPlayer *);
static u16 KeyInterCB_DeferToRecvQueue(u32);
static u16 KeyInterCB_DeferToSendQueue(u32);
2021-04-06 22:55:33 +02:00
static void ResetPlayerHeldKeys(u16 *);
static u16 KeyInterCB_SelfIdle(u32);
static u16 KeyInterCB_DeferToEventScript(u32);
static u16 GetDirectionForDpadKey(u16);
static void CB1_OverworldLink(void);
static void SetKeyInterceptCallback(u16 (*func)(u32));
2018-02-14 00:58:22 +01:00
static void SetFieldVBlankCallback(void);
static void FieldClearVBlankHBlankCallbacks(void);
static void TransitionMapMusic(void);
2021-04-06 22:55:33 +02:00
static u8 GetAdjustedInitialTransitionFlags(struct InitialPlayerAvatarState *, u16, u8);
static u8 GetAdjustedInitialDirection(struct InitialPlayerAvatarState *, u8, u16, u8);
static u16 GetCenterScreenMetatileBehavior(void);
2017-09-30 19:08:28 +02:00
static void *sUnusedOverworldCallback;
2021-04-06 22:55:33 +02:00
static u8 sPlayerLinkStates[MAX_LINK_PLAYERS];
// This callback is called with a player's key code. It then returns an
// adjusted key code, effectively intercepting the input before anything
// can process it.
static u16 (*sPlayerKeyInterceptCallback)(u32);
2021-03-19 01:35:39 +01:00
static bool8 sReceivingFromLink;
static u8 sRfuKeepAliveTimer;
2017-09-30 19:08:28 +02:00
2018-11-19 01:03:14 +01:00
u16 *gBGTilemapBuffers1;
u16 *gBGTilemapBuffers2;
u16 *gBGTilemapBuffers3;
u16 gHeldKeyCodeToSend;
2018-11-19 01:03:14 +01:00
void (*gFieldCallback)(void);
bool8 (*gFieldCallback2)(void);
2019-02-27 04:04:44 +01:00
u8 gLocalLinkPlayerId; // This is our player id in a multiplayer mode.
2018-11-19 01:03:14 +01:00
u8 gFieldLinkPlayerCount;
EWRAM_DATA static u8 sObjectEventLoadFlag = 0;
EWRAM_DATA struct WarpData gLastUsedWarp = {0};
2018-02-14 00:58:22 +01:00
EWRAM_DATA static struct WarpData sWarpDestination = {0}; // new warp position
EWRAM_DATA static struct WarpData sFixedDiveWarp = {0};
EWRAM_DATA static struct WarpData sFixedHoleWarp = {0};
2018-02-14 00:58:22 +01:00
EWRAM_DATA static u16 sLastMapSectionId = 0;
EWRAM_DATA static struct InitialPlayerAvatarState sInitialPlayerAvatarState = {0};
2018-02-14 00:58:22 +01:00
EWRAM_DATA static u16 sAmbientCrySpecies = 0;
EWRAM_DATA static bool8 sIsAmbientCryWaterMon = FALSE;
EWRAM_DATA struct LinkPlayerObjectEvent gLinkPlayerObjectEvents[4] = {0};
2018-02-12 15:01:43 +01:00
2018-02-14 00:58:22 +01:00
static const struct WarpData sDummyWarpData =
2018-02-12 15:01:43 +01:00
{
2021-11-15 16:57:22 +01:00
.mapGroup = MAP_GROUP(UNDEFINED),
.mapNum = MAP_NUM(UNDEFINED),
.warpId = WARP_ID_NONE,
2018-02-12 15:01:43 +01:00
.x = -1,
.y = -1,
};
static const u32 sUnusedData[] =
2018-02-12 15:01:43 +01:00
{
1200, 3600, 1200, 2400, 50, 80, -44, 44
2018-02-12 15:01:43 +01:00
};
const struct UCoords32 gDirectionToVectors[] =
{
[DIR_NONE] =
{
.x = 0,
.y = 0,
},
[DIR_SOUTH] =
{
.x = 0,
.y = 1,
},
[DIR_NORTH] =
{
.x = 0,
.y = -1,
},
[DIR_WEST] =
{
.x = -1,
.y = 0,
},
[DIR_EAST] =
{
.x = 1,
.y = 0,
},
[DIR_SOUTHWEST] =
{
.x = -1,
.y = 1,
},
[DIR_SOUTHEAST] =
{
.x = 1,
.y = 1,
},
[DIR_NORTHWEST] =
{
.x = -1,
.y = -1,
},
[DIR_NORTHEAST] =
{
.x = 1,
.y = -1,
},
2018-02-12 15:01:43 +01:00
};
2018-12-27 23:30:47 +01:00
static const struct BgTemplate sOverworldBgTemplates[] =
2018-02-12 15:01:43 +01:00
{
{
.bg = 0,
.charBaseIndex = 2,
.mapBaseIndex = 31,
.screenSize = 0,
.paletteMode = 0,
.priority = 0,
.baseTile = 0
},
{
.bg = 1,
.charBaseIndex = 0,
.mapBaseIndex = 29,
.screenSize = 0,
.paletteMode = 0,
.priority = 1,
.baseTile = 0
},
{
.bg = 2,
.charBaseIndex = 0,
.mapBaseIndex = 28,
.screenSize = 0,
.paletteMode = 0,
.priority = 2,
.baseTile = 0
},
{
.bg = 3,
.charBaseIndex = 0,
.mapBaseIndex = 30,
.screenSize = 0,
.paletteMode = 0,
.priority = 3,
.baseTile = 0
}
};
static const struct ScanlineEffectParams sFlashEffectParams =
2018-02-12 15:01:43 +01:00
{
2021-11-10 23:01:21 +01:00
&REG_WIN0H,
2018-02-12 15:01:43 +01:00
((DMA_ENABLE | DMA_START_HBLANK | DMA_REPEAT | DMA_DEST_RELOAD) << 16) | 1,
1,
0,
};
static u8 MovementEventModeCB_Normal(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
static u8 MovementEventModeCB_Ignored(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
2021-04-06 22:55:33 +02:00
static u8 MovementEventModeCB_Scripted(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
2018-02-12 15:01:43 +01:00
static u8 (*const gLinkPlayerMovementModes[])(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8) =
2018-02-12 15:01:43 +01:00
{
2021-04-06 22:55:33 +02:00
[MOVEMENT_MODE_FREE] = MovementEventModeCB_Normal,
[MOVEMENT_MODE_FROZEN] = MovementEventModeCB_Ignored,
[MOVEMENT_MODE_SCRIPTED] = MovementEventModeCB_Scripted,
2018-02-12 15:01:43 +01:00
};
static u8 FacingHandler_DoNothing(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
static u8 FacingHandler_DpadMovement(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
static u8 FacingHandler_ForcedFacingChange(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8);
// These handlers return TRUE if the movement was scripted and successful, and FALSE otherwise.
static bool8 (*const gLinkPlayerFacingHandlers[])(struct LinkPlayerObjectEvent *, struct ObjectEvent *, u8) =
{
FacingHandler_DoNothing,
FacingHandler_DpadMovement,
FacingHandler_DpadMovement,
FacingHandler_DpadMovement,
FacingHandler_DpadMovement,
FacingHandler_DoNothing,
FacingHandler_DoNothing,
FacingHandler_ForcedFacingChange,
FacingHandler_ForcedFacingChange,
FacingHandler_ForcedFacingChange,
FacingHandler_ForcedFacingChange,
2018-02-12 15:01:43 +01:00
};
static void MovementStatusHandler_EnterFreeMode(struct LinkPlayerObjectEvent *, struct ObjectEvent *);
static void MovementStatusHandler_TryAdvanceScript(struct LinkPlayerObjectEvent *, struct ObjectEvent *);
2018-02-12 15:01:43 +01:00
// These handlers are run after an attempted movement.
static void (*const gMovementStatusHandler[])(struct LinkPlayerObjectEvent *, struct ObjectEvent *) =
2018-02-12 15:01:43 +01:00
{
// FALSE:
MovementStatusHandler_EnterFreeMode,
// TRUE:
MovementStatusHandler_TryAdvanceScript,
2018-02-12 15:01:43 +01:00
};
// code
void DoWhiteOut(void)
{
2019-02-18 07:03:44 +01:00
ScriptContext2_RunNewScript(EventScript_WhiteOut);
2018-02-12 15:01:43 +01:00
SetMoney(&gSaveBlock1Ptr->money, GetMoney(&gSaveBlock1Ptr->money) / 2);
HealPlayerParty();
Overworld_ResetStateAfterWhiteOut();
2018-12-27 23:30:47 +01:00
SetWarpDestinationToLastHealLocation();
WarpIntoMap();
2018-02-12 15:01:43 +01:00
}
void Overworld_ResetStateAfterFly(void)
{
ResetInitialPlayerAvatarState();
2018-02-12 15:01:43 +01:00
FlagClear(FLAG_SYS_CYCLING_ROAD);
FlagClear(FLAG_SYS_CRUISE_MODE);
FlagClear(FLAG_SYS_SAFARI_MODE);
FlagClear(FLAG_SYS_USE_STRENGTH);
FlagClear(FLAG_SYS_USE_FLASH);
}
void Overworld_ResetStateAfterTeleport(void)
{
ResetInitialPlayerAvatarState();
2018-02-12 15:01:43 +01:00
FlagClear(FLAG_SYS_CYCLING_ROAD);
FlagClear(FLAG_SYS_CRUISE_MODE);
FlagClear(FLAG_SYS_SAFARI_MODE);
FlagClear(FLAG_SYS_USE_STRENGTH);
FlagClear(FLAG_SYS_USE_FLASH);
2019-02-18 07:03:44 +01:00
ScriptContext2_RunNewScript(EventScript_ResetMrBriney);
2018-02-12 15:01:43 +01:00
}
void Overworld_ResetStateAfterDigEscRope(void)
{
ResetInitialPlayerAvatarState();
2018-02-12 15:01:43 +01:00
FlagClear(FLAG_SYS_CYCLING_ROAD);
FlagClear(FLAG_SYS_CRUISE_MODE);
FlagClear(FLAG_SYS_SAFARI_MODE);
FlagClear(FLAG_SYS_USE_STRENGTH);
FlagClear(FLAG_SYS_USE_FLASH);
}
static void Overworld_ResetStateAfterWhiteOut(void)
{
ResetInitialPlayerAvatarState();
2018-02-12 15:01:43 +01:00
FlagClear(FLAG_SYS_CYCLING_ROAD);
FlagClear(FLAG_SYS_CRUISE_MODE);
FlagClear(FLAG_SYS_SAFARI_MODE);
FlagClear(FLAG_SYS_USE_STRENGTH);
FlagClear(FLAG_SYS_USE_FLASH);
2020-12-14 16:59:57 +01:00
#if VAR_TERRAIN != 0
VarSet(VAR_TERRAIN, 0);
#endif
// If you were defeated by Kyogre/Groudon and the step counter has
2019-12-02 01:19:47 +01:00
// maxed out, end the abnormal weather.
if (VarGet(VAR_SHOULD_END_ABNORMAL_WEATHER) == 1)
2018-02-12 15:01:43 +01:00
{
2019-12-02 01:19:47 +01:00
VarSet(VAR_SHOULD_END_ABNORMAL_WEATHER, 0);
VarSet(VAR_ABNORMAL_WEATHER_LOCATION, ABNORMAL_WEATHER_NONE);
2018-02-12 15:01:43 +01:00
}
}
2021-04-06 22:55:33 +02:00
static void UpdateMiscOverworldStates(void)
2018-02-12 15:01:43 +01:00
{
FlagClear(FLAG_SYS_SAFARI_MODE);
ChooseAmbientCrySpecies();
ResetCyclingRoadChallengeData();
UpdateLocationHistoryForRoamer();
RoamerMoveToOtherLocationSet();
}
void ResetGameStats(void)
{
s32 i;
for (i = 0; i < NUM_GAME_STATS; i++)
SetGameStat(i, 0);
}
void IncrementGameStat(u8 index)
{
if (index < NUM_USED_GAME_STATS)
{
u32 statVal = GetGameStat(index);
if (statVal < 0xFFFFFF)
statVal++;
else
statVal = 0xFFFFFF;
SetGameStat(index, statVal);
}
}
u32 GetGameStat(u8 index)
{
if (index >= NUM_USED_GAME_STATS)
return 0;
return gSaveBlock1Ptr->gameStats[index] ^ gSaveBlock2Ptr->encryptionKey;
}
void SetGameStat(u8 index, u32 value)
{
if (index < NUM_USED_GAME_STATS)
gSaveBlock1Ptr->gameStats[index] = value ^ gSaveBlock2Ptr->encryptionKey;
}
void ApplyNewEncryptionKeyToGameStats(u32 newKey)
{
u8 i;
for (i = 0; i < NUM_GAME_STATS; i++)
ApplyNewEncryptionKeyToWord(&gSaveBlock1Ptr->gameStats[i], newKey);
}
2019-11-21 06:14:40 +01:00
void LoadObjEventTemplatesFromHeader(void)
2018-02-12 15:01:43 +01:00
{
// Clear map object templates
CpuFill32(0, gSaveBlock1Ptr->objectEventTemplates, sizeof(gSaveBlock1Ptr->objectEventTemplates));
2018-02-12 15:01:43 +01:00
// Copy map header events to save block
CpuCopy32(gMapHeader.events->objectEvents,
gSaveBlock1Ptr->objectEventTemplates,
gMapHeader.events->objectEventCount * sizeof(struct ObjectEventTemplate));
2018-02-12 15:01:43 +01:00
}
2019-11-21 06:14:40 +01:00
void LoadSaveblockObjEventScripts(void)
2018-02-12 15:01:43 +01:00
{
struct ObjectEventTemplate *mapHeaderObjTemplates = gMapHeader.events->objectEvents;
struct ObjectEventTemplate *savObjTemplates = gSaveBlock1Ptr->objectEventTemplates;
2018-02-12 15:01:43 +01:00
s32 i;
for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
savObjTemplates[i].script = mapHeaderObjTemplates[i].script;
}
2021-11-15 16:57:22 +01:00
void SetObjEventTemplateCoords(u8 localId, s16 x, s16 y)
2018-02-12 15:01:43 +01:00
{
s32 i;
struct ObjectEventTemplate *savObjTemplates = gSaveBlock1Ptr->objectEventTemplates;
2018-02-12 15:01:43 +01:00
for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
{
struct ObjectEventTemplate *objectEventTemplate = &savObjTemplates[i];
if (objectEventTemplate->localId == localId)
2018-02-12 15:01:43 +01:00
{
objectEventTemplate->x = x;
objectEventTemplate->y = y;
2018-02-12 15:01:43 +01:00
return;
}
}
}
2021-11-15 16:57:22 +01:00
void SetObjEventTemplateMovementType(u8 localId, u8 movementType)
2018-02-12 15:01:43 +01:00
{
s32 i;
struct ObjectEventTemplate *savObjTemplates = gSaveBlock1Ptr->objectEventTemplates;
for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
{
struct ObjectEventTemplate *objectEventTemplate = &savObjTemplates[i];
if (objectEventTemplate->localId == localId)
2018-02-12 15:01:43 +01:00
{
objectEventTemplate->movementType = movementType;
2018-02-12 15:01:43 +01:00
return;
}
}
}
static void InitMapView(void)
2018-02-12 15:01:43 +01:00
{
ResetFieldCamera();
CopyMapTilesetsToVram(gMapHeader.mapLayout);
LoadMapTilesetPalettes(gMapHeader.mapLayout);
2018-02-12 15:01:43 +01:00
DrawWholeMapView();
2019-02-08 22:07:42 +01:00
InitTilesetAnimations();
2018-02-12 15:01:43 +01:00
}
2018-06-21 00:41:51 +02:00
const struct MapLayout *GetMapLayout(void)
2018-02-12 15:01:43 +01:00
{
2018-06-21 00:41:51 +02:00
u16 mapLayoutId = gSaveBlock1Ptr->mapLayoutId;
if (mapLayoutId)
return gMapLayouts[mapLayoutId - 1];
2018-02-12 15:01:43 +01:00
return NULL;
}
void ApplyCurrentWarp(void)
{
gLastUsedWarp = gSaveBlock1Ptr->location;
2018-02-14 00:58:22 +01:00
gSaveBlock1Ptr->location = sWarpDestination;
sFixedDiveWarp = sDummyWarpData;
sFixedHoleWarp = sDummyWarpData;
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
static void ClearDiveAndHoleWarps(void)
2018-02-12 15:01:43 +01:00
{
sFixedDiveWarp = sDummyWarpData;
sFixedHoleWarp = sDummyWarpData;
2018-02-12 15:01:43 +01:00
}
2018-10-17 02:11:44 +02:00
static void SetWarpData(struct WarpData *warp, s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 15:01:43 +01:00
{
warp->mapGroup = mapGroup;
warp->mapNum = mapNum;
warp->warpId = warpId;
warp->x = x;
warp->y = y;
}
2018-10-17 02:11:44 +02:00
static bool32 IsDummyWarp(struct WarpData *warp)
2018-02-12 15:01:43 +01:00
{
2021-11-15 16:57:22 +01:00
if (warp->mapGroup != (s8)MAP_GROUP(UNDEFINED))
2018-02-12 15:01:43 +01:00
return FALSE;
2021-11-15 16:57:22 +01:00
else if (warp->mapNum != (s8)MAP_NUM(UNDEFINED))
2018-02-12 15:01:43 +01:00
return FALSE;
else if (warp->warpId != WARP_ID_NONE)
2018-02-12 15:01:43 +01:00
return FALSE;
2018-02-15 23:09:52 +01:00
else if (warp->x != -1)
2018-02-12 15:01:43 +01:00
return FALSE;
2018-02-15 23:09:52 +01:00
else if (warp->y != -1)
2018-02-12 15:01:43 +01:00
return FALSE;
2018-02-15 23:09:52 +01:00
else
return TRUE;
2018-02-12 15:01:43 +01:00
}
2018-05-09 12:07:56 +02:00
struct MapHeader const *const Overworld_GetMapHeaderByGroupAndId(u16 mapGroup, u16 mapNum)
2018-02-12 15:01:43 +01:00
{
return gMapGroups[mapGroup][mapNum];
}
struct MapHeader const *const GetDestinationWarpMapHeader(void)
2018-02-12 15:01:43 +01:00
{
2018-02-14 00:58:22 +01:00
return Overworld_GetMapHeaderByGroupAndId(sWarpDestination.mapGroup, sWarpDestination.mapNum);
2018-02-12 15:01:43 +01:00
}
2018-10-17 02:11:44 +02:00
static void LoadCurrentMapData(void)
2018-02-12 15:01:43 +01:00
{
2018-02-14 00:58:22 +01:00
sLastMapSectionId = gMapHeader.regionMapSectionId;
2018-02-12 15:01:43 +01:00
gMapHeader = *Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum);
2018-06-21 00:41:51 +02:00
gSaveBlock1Ptr->mapLayoutId = gMapHeader.mapLayoutId;
gMapHeader.mapLayout = GetMapLayout();
2018-02-12 15:01:43 +01:00
}
2018-10-17 02:11:44 +02:00
static void LoadSaveblockMapHeader(void)
2018-02-12 15:01:43 +01:00
{
gMapHeader = *Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum);
2018-06-21 00:41:51 +02:00
gMapHeader.mapLayout = GetMapLayout();
2018-02-12 15:01:43 +01:00
}
2018-10-17 02:11:44 +02:00
static void SetPlayerCoordsFromWarp(void)
2018-02-12 15:01:43 +01:00
{
if (gSaveBlock1Ptr->location.warpId >= 0 && gSaveBlock1Ptr->location.warpId < gMapHeader.events->warpCount)
{
// warpId is a valid warp for this map, use the coords of that warp.
2018-02-12 15:01:43 +01:00
gSaveBlock1Ptr->pos.x = gMapHeader.events->warps[gSaveBlock1Ptr->location.warpId].x;
gSaveBlock1Ptr->pos.y = gMapHeader.events->warps[gSaveBlock1Ptr->location.warpId].y;
}
else if (gSaveBlock1Ptr->location.x >= 0 && gSaveBlock1Ptr->location.y >= 0)
{
// Invalid warpId given. The given coords are valid, use those instead.
// WARP_ID_NONE is used to reach this intentionally.
2018-02-12 15:01:43 +01:00
gSaveBlock1Ptr->pos.x = gSaveBlock1Ptr->location.x;
gSaveBlock1Ptr->pos.y = gSaveBlock1Ptr->location.y;
}
else
{
// Invalid warpId and coords given. Put player in center of map.
2018-06-21 00:41:51 +02:00
gSaveBlock1Ptr->pos.x = gMapHeader.mapLayout->width / 2;
gSaveBlock1Ptr->pos.y = gMapHeader.mapLayout->height / 2;
2018-02-12 15:01:43 +01:00
}
}
void WarpIntoMap(void)
2018-02-12 15:01:43 +01:00
{
ApplyCurrentWarp();
LoadCurrentMapData();
SetPlayerCoordsFromWarp();
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestination(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 15:01:43 +01:00
{
2018-02-14 00:58:22 +01:00
SetWarpData(&sWarpDestination, mapGroup, mapNum, warpId, x, y);
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToMapWarp(s8 mapGroup, s8 mapNum, s8 warpId)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
SetWarpDestination(mapGroup, mapNum, warpId, -1, -1);
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetDynamicWarp(s32 unused, s8 mapGroup, s8 mapNum, s8 warpId)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
SetWarpData(&gSaveBlock1Ptr->dynamicWarp, mapGroup, mapNum, warpId, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y);
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetDynamicWarpWithCoords(s32 unused, s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
SetWarpData(&gSaveBlock1Ptr->dynamicWarp, mapGroup, mapNum, warpId, x, y);
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToDynamicWarp(u8 unusedWarpId)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
sWarpDestination = gSaveBlock1Ptr->dynamicWarp;
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToHealLocation(u8 healLocationId)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
const struct HealLocation *warp = GetHealLocation(healLocationId);
2018-02-12 15:01:43 +01:00
if (warp)
SetWarpDestination(warp->group, warp->map, WARP_ID_NONE, warp->x, warp->y);
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToLastHealLocation(void)
2018-02-12 15:01:43 +01:00
{
2018-02-14 00:58:22 +01:00
sWarpDestination = gSaveBlock1Ptr->lastHealLocation;
2018-02-12 15:01:43 +01:00
}
2018-12-27 23:30:47 +01:00
void SetLastHealLocationWarp(u8 healLocationId)
2018-02-12 15:01:43 +01:00
{
const struct HealLocation *healLocation = GetHealLocation(healLocationId);
2018-12-27 23:30:47 +01:00
if (healLocation)
SetWarpData(&gSaveBlock1Ptr->lastHealLocation, healLocation->group, healLocation->map, WARP_ID_NONE, healLocation->x, healLocation->y);
2018-02-12 15:01:43 +01:00
}
2017-09-30 19:08:28 +02:00
2018-12-28 22:11:15 +01:00
void UpdateEscapeWarp(s16 x, s16 y)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
u8 currMapType = GetCurrentMapType();
2018-02-14 00:58:22 +01:00
u8 destMapType = GetMapTypeByGroupAndId(sWarpDestination.mapGroup, sWarpDestination.mapNum);
2019-03-01 07:49:11 +01:00
if (IsMapTypeOutdoors(currMapType) && IsMapTypeOutdoors(destMapType) != TRUE)
SetEscapeWarp(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, WARP_ID_NONE, x - MAP_OFFSET, y - MAP_OFFSET + 1);
2018-02-12 15:01:43 +01:00
}
2017-09-30 19:08:28 +02:00
2018-12-27 23:30:47 +01:00
void SetEscapeWarp(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 15:01:43 +01:00
{
2018-12-27 23:30:47 +01:00
SetWarpData(&gSaveBlock1Ptr->escapeWarp, mapGroup, mapNum, warpId, x, y);
2018-02-12 15:01:43 +01:00
}
2018-02-12 18:26:26 +01:00
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToEscapeWarp(void)
2018-02-12 18:26:26 +01:00
{
2018-12-27 23:30:47 +01:00
sWarpDestination = gSaveBlock1Ptr->escapeWarp;
2018-02-12 18:26:26 +01:00
}
void SetFixedDiveWarp(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 18:26:26 +01:00
{
SetWarpData(&sFixedDiveWarp, mapGroup, mapNum, warpId, x, y);
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
static void SetWarpDestinationToDiveWarp(void)
2018-02-12 18:26:26 +01:00
{
sWarpDestination = sFixedDiveWarp;
2018-02-12 18:26:26 +01:00
}
void SetFixedHoleWarp(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 18:26:26 +01:00
{
SetWarpData(&sFixedHoleWarp, mapGroup, mapNum, warpId, x, y);
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
void SetWarpDestinationToFixedHoleWarp(s16 x, s16 y)
2018-02-12 18:26:26 +01:00
{
if (IsDummyWarp(&sFixedHoleWarp) == TRUE)
sWarpDestination = gLastUsedWarp;
2018-02-12 18:26:26 +01:00
else
SetWarpDestination(sFixedHoleWarp.mapGroup, sFixedHoleWarp.mapNum, WARP_ID_NONE, x, y);
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
static void SetWarpDestinationToContinueGameWarp(void)
2018-02-12 18:26:26 +01:00
{
2018-12-27 23:30:47 +01:00
sWarpDestination = gSaveBlock1Ptr->continueGameWarp;
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
void SetContinueGameWarp(s8 mapGroup, s8 mapNum, s8 warpId, s8 x, s8 y)
2018-02-12 18:26:26 +01:00
{
2018-12-27 23:30:47 +01:00
SetWarpData(&gSaveBlock1Ptr->continueGameWarp, mapGroup, mapNum, warpId, x, y);
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
void SetContinueGameWarpToHealLocation(u8 healLocationId)
2018-02-12 18:26:26 +01:00
{
2018-12-27 23:30:47 +01:00
const struct HealLocation *warp = GetHealLocation(healLocationId);
2018-02-12 18:26:26 +01:00
if (warp)
SetWarpData(&gSaveBlock1Ptr->continueGameWarp, warp->group, warp->map, WARP_ID_NONE, warp->x, warp->y);
2018-02-12 18:26:26 +01:00
}
2018-12-27 23:30:47 +01:00
void SetContinueGameWarpToDynamicWarp(int unused)
2018-02-12 18:26:26 +01:00
{
2018-12-27 23:30:47 +01:00
gSaveBlock1Ptr->continueGameWarp = gSaveBlock1Ptr->dynamicWarp;
2018-02-12 18:26:26 +01:00
}
2018-02-14 00:58:22 +01:00
const struct MapConnection *GetMapConnection(u8 dir)
2018-02-12 18:26:26 +01:00
{
s32 i;
s32 count = gMapHeader.connections->count;
2018-02-14 00:58:22 +01:00
const struct MapConnection *connection = gMapHeader.connections->connections;
2018-02-12 18:26:26 +01:00
if (connection == NULL)
return NULL;
for(i = 0; i < count; i++, connection++)
if (connection->direction == dir)
return connection;
return NULL;
}
2018-10-17 02:11:44 +02:00
static bool8 SetDiveWarp(u8 dir, u16 x, u16 y)
2018-02-12 18:26:26 +01:00
{
2018-02-14 00:58:22 +01:00
const struct MapConnection *connection = GetMapConnection(dir);
2018-02-12 18:26:26 +01:00
if (connection != NULL)
{
SetWarpDestination(connection->mapGroup, connection->mapNum, WARP_ID_NONE, x, y);
2018-02-12 18:26:26 +01:00
}
else
{
RunOnDiveWarpMapScript();
if (IsDummyWarp(&sFixedDiveWarp))
2018-02-12 18:26:26 +01:00
return FALSE;
2018-12-27 23:30:47 +01:00
SetWarpDestinationToDiveWarp();
2018-02-12 18:26:26 +01:00
}
return TRUE;
}
bool8 SetDiveWarpEmerge(u16 x, u16 y)
2018-02-12 18:26:26 +01:00
{
return SetDiveWarp(CONNECTION_EMERGE, x, y);
2018-02-12 18:26:26 +01:00
}
bool8 SetDiveWarpDive(u16 x, u16 y)
2018-02-12 18:26:26 +01:00
{
return SetDiveWarp(CONNECTION_DIVE, x, y);
2018-02-12 18:26:26 +01:00
}
void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum)
2018-02-12 18:26:26 +01:00
{
s32 paletteIndex;
SetWarpDestination(mapGroup, mapNum, WARP_ID_NONE, -1, -1);
// Dont transition map music between BF Outside West/East
if (gMapHeader.regionMapSectionId != MAPSEC_BATTLE_FRONTIER)
TransitionMapMusic();
2018-02-12 18:26:26 +01:00
ApplyCurrentWarp();
LoadCurrentMapData();
2019-11-21 06:14:40 +01:00
LoadObjEventTemplatesFromHeader();
2018-02-12 18:26:26 +01:00
TrySetMapSaveWarpStatus();
ClearTempFieldEventData();
ResetCyclingRoadChallengeData();
2018-10-17 04:47:08 +02:00
RestartWildEncounterImmunitySteps();
2018-02-12 18:26:26 +01:00
TryUpdateRandomTrainerRematches(mapGroup, mapNum);
DoTimeBasedEvents();
2021-11-16 23:12:16 +01:00
SetSavedWeatherFromCurrMapHeader();
2018-02-12 18:26:26 +01:00
ChooseAmbientCrySpecies();
SetDefaultFlashLevel();
Overworld_ClearSavedMusic();
RunOnTransitionMapScript();
2018-12-28 18:18:23 +01:00
InitMap();
CopySecondaryTilesetToVramUsingHeap(gMapHeader.mapLayout);
LoadSecondaryTilesetPalette(gMapHeader.mapLayout);
2018-02-12 18:26:26 +01:00
for (paletteIndex = 6; paletteIndex < 13; paletteIndex++)
ApplyWeatherGammaShiftToPal(paletteIndex);
2019-02-08 22:07:42 +01:00
InitSecondaryTilesetAnimation();
2018-02-12 18:26:26 +01:00
UpdateLocationHistoryForRoamer();
RoamerMove();
DoCurrentWeather();
ResetFieldTasksArgs();
RunOnResumeMapScript();
2018-02-12 18:26:26 +01:00
if (gMapHeader.regionMapSectionId != MAPSEC_BATTLE_FRONTIER
|| gMapHeader.regionMapSectionId != sLastMapSectionId)
2018-02-12 18:26:26 +01:00
ShowMapNamePopup();
}
static void LoadMapFromWarp(bool32 a1)
2018-02-12 18:26:26 +01:00
{
2019-03-01 07:49:11 +01:00
bool8 isOutdoors;
bool8 isIndoors;
2018-02-12 18:26:26 +01:00
LoadCurrentMapData();
if (!(sObjectEventLoadFlag & SKIP_OBJECT_EVENT_LOAD))
2018-02-12 18:26:26 +01:00
{
2019-12-11 09:28:55 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_FLOOR)
LoadBattlePyramidObjectEventTemplates();
2018-02-12 18:26:26 +01:00
else if (InTrainerHill())
LoadTrainerHillObjectEventTemplates();
2018-02-12 18:26:26 +01:00
else
2019-11-21 06:14:40 +01:00
LoadObjEventTemplatesFromHeader();
2018-02-12 18:26:26 +01:00
}
2019-03-01 07:49:11 +01:00
isOutdoors = IsMapTypeOutdoors(gMapHeader.mapType);
2019-03-02 10:13:27 +01:00
isIndoors = IsMapTypeIndoors(gMapHeader.mapType);
2018-02-12 18:26:26 +01:00
CheckLeftFriendsSecretBase();
2018-02-12 18:26:26 +01:00
TrySetMapSaveWarpStatus();
ClearTempFieldEventData();
ResetCyclingRoadChallengeData();
2018-10-17 04:47:08 +02:00
RestartWildEncounterImmunitySteps();
2018-02-12 18:26:26 +01:00
TryUpdateRandomTrainerRematches(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum);
if (a1 != TRUE)
2018-02-12 18:26:26 +01:00
DoTimeBasedEvents();
2021-11-16 23:12:16 +01:00
SetSavedWeatherFromCurrMapHeader();
2018-02-12 18:26:26 +01:00
ChooseAmbientCrySpecies();
2019-03-01 07:49:11 +01:00
if (isOutdoors)
2018-02-12 18:26:26 +01:00
FlagClear(FLAG_SYS_USE_FLASH);
SetDefaultFlashLevel();
Overworld_ClearSavedMusic();
RunOnTransitionMapScript();
2018-02-12 18:26:26 +01:00
UpdateLocationHistoryForRoamer();
RoamerMoveToOtherLocationSet();
2019-12-11 09:28:55 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_FLOOR)
2019-02-07 03:01:29 +01:00
InitBattlePyramidMap(FALSE);
2018-02-12 18:26:26 +01:00
else if (InTrainerHill())
2018-12-28 18:18:23 +01:00
InitTrainerHillMap();
2018-02-12 18:26:26 +01:00
else
2018-12-28 18:18:23 +01:00
InitMap();
2018-02-12 18:26:26 +01:00
if (a1 != TRUE && isIndoors)
2018-02-12 18:26:26 +01:00
{
2018-12-28 18:18:23 +01:00
UpdateTVScreensOnMap(gBackupMapLayout.width, gBackupMapLayout.height);
2019-04-05 23:11:24 +02:00
InitSecretBaseAppearance(TRUE);
2018-02-12 18:26:26 +01:00
}
}
void ResetInitialPlayerAvatarState(void)
2018-02-12 18:26:26 +01:00
{
sInitialPlayerAvatarState.direction = DIR_SOUTH;
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_ON_FOOT;
2018-02-12 18:26:26 +01:00
}
void StoreInitialPlayerAvatarState(void)
2018-02-12 18:26:26 +01:00
{
sInitialPlayerAvatarState.direction = GetPlayerFacingDirection();
2018-02-12 18:26:26 +01:00
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_MACH_BIKE))
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_MACH_BIKE;
2018-02-12 18:26:26 +01:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_ACRO_BIKE))
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_ACRO_BIKE;
2018-02-12 18:26:26 +01:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_SURFING;
2018-10-17 02:11:44 +02:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_UNDERWATER))
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_UNDERWATER;
2018-02-12 18:26:26 +01:00
else
sInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_ON_FOOT;
2018-02-12 18:26:26 +01:00
}
static struct InitialPlayerAvatarState *GetInitialPlayerAvatarState(void)
2018-02-12 18:26:26 +01:00
{
struct InitialPlayerAvatarState playerStruct;
2018-12-27 23:30:47 +01:00
u8 mapType = GetCurrentMapType();
u16 metatileBehavior = GetCenterScreenMetatileBehavior();
u8 transitionFlags = GetAdjustedInitialTransitionFlags(&sInitialPlayerAvatarState, metatileBehavior, mapType);
playerStruct.transitionFlags = transitionFlags;
playerStruct.direction = GetAdjustedInitialDirection(&sInitialPlayerAvatarState, transitionFlags, metatileBehavior, mapType);
sInitialPlayerAvatarState = playerStruct;
return &sInitialPlayerAvatarState;
2018-02-12 18:26:26 +01:00
}
static u8 GetAdjustedInitialTransitionFlags(struct InitialPlayerAvatarState *playerStruct, u16 metatileBehavior, u8 mapType)
2018-02-12 18:26:26 +01:00
{
if (mapType != MAP_TYPE_INDOOR && FlagGet(FLAG_SYS_CRUISE_MODE))
2018-10-17 02:11:44 +02:00
return PLAYER_AVATAR_FLAG_ON_FOOT;
else if (mapType == MAP_TYPE_UNDERWATER)
2018-10-17 02:11:44 +02:00
return PLAYER_AVATAR_FLAG_UNDERWATER;
else if (MetatileBehavior_IsSurfableWaterOrUnderwater(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return PLAYER_AVATAR_FLAG_SURFING;
2018-02-15 12:36:52 +01:00
else if (Overworld_IsBikingAllowed() != TRUE)
2018-10-17 02:11:44 +02:00
return PLAYER_AVATAR_FLAG_ON_FOOT;
else if (playerStruct->transitionFlags == PLAYER_AVATAR_FLAG_MACH_BIKE)
return PLAYER_AVATAR_FLAG_MACH_BIKE;
else if (playerStruct->transitionFlags != PLAYER_AVATAR_FLAG_ACRO_BIKE)
return PLAYER_AVATAR_FLAG_ON_FOOT;
2018-02-15 12:36:52 +01:00
else
2018-10-17 02:11:44 +02:00
return PLAYER_AVATAR_FLAG_ACRO_BIKE;
2018-02-12 18:26:26 +01:00
}
static u8 GetAdjustedInitialDirection(struct InitialPlayerAvatarState *playerStruct, u8 transitionFlags, u16 metatileBehavior, u8 mapType)
2018-02-12 18:26:26 +01:00
{
2019-02-28 05:54:51 +01:00
if (FlagGet(FLAG_SYS_CRUISE_MODE) && mapType == MAP_TYPE_OCEAN_ROUTE)
2018-10-17 02:11:44 +02:00
return DIR_EAST;
else if (MetatileBehavior_IsDeepSouthWarp(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_NORTH;
else if (MetatileBehavior_IsNonAnimDoor(metatileBehavior) == TRUE || MetatileBehavior_IsDoor(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_SOUTH;
else if (MetatileBehavior_IsSouthArrowWarp(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_NORTH;
else if (MetatileBehavior_IsNorthArrowWarp(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_SOUTH;
else if (MetatileBehavior_IsWestArrowWarp(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_EAST;
else if (MetatileBehavior_IsEastArrowWarp(metatileBehavior) == TRUE)
2018-10-17 02:11:44 +02:00
return DIR_WEST;
else if ((playerStruct->transitionFlags == PLAYER_AVATAR_FLAG_UNDERWATER && transitionFlags == PLAYER_AVATAR_FLAG_SURFING)
|| (playerStruct->transitionFlags == PLAYER_AVATAR_FLAG_SURFING && transitionFlags == PLAYER_AVATAR_FLAG_UNDERWATER))
return playerStruct->direction;
else if (MetatileBehavior_IsLadder(metatileBehavior) == TRUE)
return playerStruct->direction;
2018-02-15 12:36:52 +01:00
else
2018-10-17 02:11:44 +02:00
return DIR_SOUTH;
2018-02-12 18:26:26 +01:00
}
static u16 GetCenterScreenMetatileBehavior(void)
2018-02-12 18:26:26 +01:00
{
2021-10-09 18:12:18 +02:00
return MapGridGetMetatileBehaviorAt(gSaveBlock1Ptr->pos.x + MAP_OFFSET, gSaveBlock1Ptr->pos.y + MAP_OFFSET);
2018-02-12 18:26:26 +01:00
}
bool32 Overworld_IsBikingAllowed(void)
{
if (!gMapHeader.allowCycling)
2018-02-12 18:26:26 +01:00
return FALSE;
else
return TRUE;
}
2021-11-16 23:12:16 +01:00
// Flash level of 0 is fully bright
// Flash level of 1 is the largest flash radius
// Flash level of 7 is the smallest flash radius
// Flash level of 8 is fully black
2018-02-12 18:26:26 +01:00
void SetDefaultFlashLevel(void)
{
if (!gMapHeader.cave)
gSaveBlock1Ptr->flashLevel = 0;
else if (FlagGet(FLAG_SYS_USE_FLASH))
gSaveBlock1Ptr->flashLevel = 1;
else
gSaveBlock1Ptr->flashLevel = gMaxFlashLevel - 1;
}
2021-11-15 16:57:22 +01:00
void SetFlashLevel(s32 flashLevel)
2018-02-12 18:26:26 +01:00
{
if (flashLevel < 0 || flashLevel > gMaxFlashLevel)
flashLevel = 0;
gSaveBlock1Ptr->flashLevel = flashLevel;
}
2021-11-15 16:57:22 +01:00
u8 GetFlashLevel(void)
2018-02-12 18:26:26 +01:00
{
return gSaveBlock1Ptr->flashLevel;
}
2018-12-27 23:30:47 +01:00
void SetCurrentMapLayout(u16 mapLayoutId)
2018-02-12 18:26:26 +01:00
{
2018-06-21 00:41:51 +02:00
gSaveBlock1Ptr->mapLayoutId = mapLayoutId;
gMapHeader.mapLayout = GetMapLayout();
2018-02-12 18:26:26 +01:00
}
void SetObjectEventLoadFlag(u8 flag)
2018-02-12 18:26:26 +01:00
{
sObjectEventLoadFlag = flag;
2018-02-12 18:26:26 +01:00
}
// Unused, sObjectEventLoadFlag is read directly
static u8 GetObjectEventLoadFlag(void)
2018-02-12 18:26:26 +01:00
{
return sObjectEventLoadFlag;
2018-02-12 18:26:26 +01:00
}
2018-02-14 00:58:22 +01:00
static bool16 ShouldLegendaryMusicPlayAtLocation(struct WarpData *warp)
2018-02-12 18:26:26 +01:00
{
if (!FlagGet(FLAG_SYS_WEATHER_CTRL))
return FALSE;
if (warp->mapGroup == 0)
{
switch (warp->mapNum)
{
case MAP_NUM(LILYCOVE_CITY):
case MAP_NUM(MOSSDEEP_CITY):
case MAP_NUM(SOOTOPOLIS_CITY):
case MAP_NUM(EVER_GRANDE_CITY):
case MAP_NUM(ROUTE124):
case MAP_NUM(ROUTE125):
case MAP_NUM(ROUTE126):
case MAP_NUM(ROUTE127):
case MAP_NUM(ROUTE128):
return TRUE;
default:
2019-08-05 17:23:45 +02:00
if (VarGet(VAR_SOOTOPOLIS_CITY_STATE) < 4)
2018-02-12 18:26:26 +01:00
return FALSE;
switch (warp->mapNum)
{
case MAP_NUM(ROUTE129):
case MAP_NUM(ROUTE130):
case MAP_NUM(ROUTE131):
return TRUE;
}
}
}
return FALSE;
}
2018-02-14 00:58:22 +01:00
static bool16 NoMusicInSotopolisWithLegendaries(struct WarpData *warp)
2018-02-12 18:26:26 +01:00
{
if (VarGet(VAR_SKY_PILLAR_STATE) != 1)
2018-02-12 18:26:26 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapGroup != MAP_GROUP(SOOTOPOLIS_CITY))
2018-02-12 18:26:26 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapNum == MAP_NUM(SOOTOPOLIS_CITY))
2018-02-12 18:26:26 +01:00
return TRUE;
2018-02-15 12:36:52 +01:00
else
return FALSE;
2018-02-12 18:26:26 +01:00
}
2018-02-14 00:58:22 +01:00
static bool16 IsInfiltratedWeatherInstitute(struct WarpData *warp)
2018-02-12 18:26:26 +01:00
{
if (VarGet(VAR_WEATHER_INSTITUTE_STATE))
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapGroup != MAP_GROUP(ROUTE119_WEATHER_INSTITUTE_1F))
2018-02-12 18:26:26 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapNum == MAP_NUM(ROUTE119_WEATHER_INSTITUTE_1F)
2018-02-12 18:26:26 +01:00
|| warp->mapNum == MAP_NUM(ROUTE119_WEATHER_INSTITUTE_2F))
return TRUE;
2018-02-15 12:36:52 +01:00
else
return FALSE;
2018-02-12 18:26:26 +01:00
}
2018-02-14 00:58:22 +01:00
static bool16 IsInflitratedSpaceCenter(struct WarpData *warp)
2018-02-12 18:26:26 +01:00
{
2019-08-05 17:23:45 +02:00
if (VarGet(VAR_MOSSDEEP_CITY_STATE) == 0)
2018-02-12 18:26:26 +01:00
return FALSE;
2019-08-05 17:23:45 +02:00
else if (VarGet(VAR_MOSSDEEP_CITY_STATE) > 2)
2018-02-12 18:26:26 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapGroup != MAP_GROUP(MOSSDEEP_CITY_SPACE_CENTER_1F))
2018-02-12 18:26:26 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else if (warp->mapNum == MAP_NUM(MOSSDEEP_CITY_SPACE_CENTER_1F)
2018-02-12 18:26:26 +01:00
|| warp->mapNum == MAP_NUM(MOSSDEEP_CITY_SPACE_CENTER_2F))
return TRUE;
return FALSE;
}
u16 GetLocationMusic(struct WarpData *warp)
{
if (NoMusicInSotopolisWithLegendaries(warp) == TRUE)
2021-09-14 19:14:14 +02:00
return MUS_NONE;
2018-02-12 18:26:26 +01:00
else if (ShouldLegendaryMusicPlayAtLocation(warp) == TRUE)
2020-08-21 00:02:00 +02:00
return MUS_ABNORMAL_WEATHER;
2018-02-12 18:26:26 +01:00
else if (IsInflitratedSpaceCenter(warp) == TRUE)
2020-08-21 00:02:00 +02:00
return MUS_ENCOUNTER_MAGMA;
2018-02-12 18:26:26 +01:00
else if (IsInfiltratedWeatherInstitute(warp) == TRUE)
2020-08-21 00:02:00 +02:00
return MUS_MT_CHIMNEY;
2018-02-12 18:26:26 +01:00
else
return Overworld_GetMapHeaderByGroupAndId(warp->mapGroup, warp->mapNum)->music;
}
u16 GetCurrLocationDefaultMusic(void)
{
u16 music;
// Play the desert music only when the sandstorm is active on Route 111.
if (gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(ROUTE111)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(ROUTE111)
2021-11-16 23:12:16 +01:00
&& GetSavedWeather() == WEATHER_SANDSTORM)
2020-08-21 00:02:00 +02:00
return MUS_ROUTE111;
2018-02-12 18:26:26 +01:00
music = GetLocationMusic(&gSaveBlock1Ptr->location);
2020-08-21 00:02:00 +02:00
if (music != MUS_ROUTE118)
2018-02-12 18:26:26 +01:00
{
return music;
}
else
{
2019-02-18 07:03:44 +01:00
if (gSaveBlock1Ptr->pos.x < 24)
2020-08-21 00:02:00 +02:00
return MUS_ROUTE110;
2018-02-12 18:26:26 +01:00
else
2020-08-21 00:02:00 +02:00
return MUS_ROUTE119;
2018-02-12 18:26:26 +01:00
}
}
u16 GetWarpDestinationMusic(void)
{
2018-02-14 00:58:22 +01:00
u16 music = GetLocationMusic(&sWarpDestination);
2020-08-21 00:02:00 +02:00
if (music != MUS_ROUTE118)
2018-02-12 18:26:26 +01:00
{
return music;
}
else
{
if (gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(MAUVILLE_CITY)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(MAUVILLE_CITY))
2020-08-21 00:02:00 +02:00
return MUS_ROUTE110;
2018-02-12 18:26:26 +01:00
else
2020-08-21 00:02:00 +02:00
return MUS_ROUTE119;
2018-02-12 18:26:26 +01:00
}
}
void Overworld_ResetMapMusic(void)
{
ResetMapMusic();
}
void Overworld_PlaySpecialMapMusic(void)
{
u16 music = GetCurrLocationDefaultMusic();
2021-02-22 18:12:35 +01:00
if (music != MUS_ABNORMAL_WEATHER && music != MUS_NONE)
2018-02-12 18:26:26 +01:00
{
if (gSaveBlock1Ptr->savedMusic)
music = gSaveBlock1Ptr->savedMusic;
2018-12-27 23:30:47 +01:00
else if (GetCurrentMapType() == MAP_TYPE_UNDERWATER)
2020-08-21 00:02:00 +02:00
music = MUS_UNDERWATER;
2018-02-12 18:26:26 +01:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
2020-08-21 00:02:00 +02:00
music = MUS_SURF;
2018-02-12 18:26:26 +01:00
}
if (music != GetCurrentMapMusic())
PlayNewMapMusic(music);
}
void Overworld_SetSavedMusic(u16 songNum)
{
gSaveBlock1Ptr->savedMusic = songNum;
}
void Overworld_ClearSavedMusic(void)
{
gSaveBlock1Ptr->savedMusic = MUS_DUMMY;
2018-02-12 18:26:26 +01:00
}
static void TransitionMapMusic(void)
2018-02-12 18:26:26 +01:00
{
if (FlagGet(FLAG_DONT_TRANSITION_MUSIC) != TRUE)
2018-02-12 18:26:26 +01:00
{
u16 newMusic = GetWarpDestinationMusic();
u16 currentMusic = GetCurrentMapMusic();
2021-02-22 18:12:35 +01:00
if (newMusic != MUS_ABNORMAL_WEATHER && newMusic != MUS_NONE)
2018-02-12 18:26:26 +01:00
{
2020-08-21 00:02:00 +02:00
if (currentMusic == MUS_UNDERWATER || currentMusic == MUS_SURF)
2018-02-12 18:26:26 +01:00
return;
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
2020-08-21 00:02:00 +02:00
newMusic = MUS_SURF;
2018-02-12 18:26:26 +01:00
}
if (newMusic != currentMusic)
{
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_MACH_BIKE | PLAYER_AVATAR_FLAG_ACRO_BIKE))
FadeOutAndFadeInNewMapMusic(newMusic, 4, 4);
else
FadeOutAndPlayNewMapMusic(newMusic, 8);
}
}
}
void Overworld_ChangeMusicToDefault(void)
{
u16 currentMusic = GetCurrentMapMusic();
if (currentMusic != GetCurrLocationDefaultMusic())
FadeOutAndPlayNewMapMusic(GetCurrLocationDefaultMusic(), 8);
}
void Overworld_ChangeMusicTo(u16 newMusic)
{
u16 currentMusic = GetCurrentMapMusic();
2020-08-21 00:02:00 +02:00
if (currentMusic != newMusic && currentMusic != MUS_ABNORMAL_WEATHER)
2018-02-12 18:26:26 +01:00
FadeOutAndPlayNewMapMusic(newMusic, 8);
}
u8 GetMapMusicFadeoutSpeed(void)
{
const struct MapHeader *mapHeader = GetDestinationWarpMapHeader();
2019-03-02 10:13:27 +01:00
if (IsMapTypeIndoors(mapHeader->mapType) == TRUE)
2018-02-12 18:26:26 +01:00
return 2;
else
return 4;
}
2018-12-08 21:42:36 +01:00
void TryFadeOutOldMapMusic(void)
2018-02-12 18:26:26 +01:00
{
u16 currentMusic = GetCurrentMapMusic();
u16 warpMusic = GetWarpDestinationMusic();
if (FlagGet(FLAG_DONT_TRANSITION_MUSIC) != TRUE && warpMusic != GetCurrentMapMusic())
2018-02-12 18:26:26 +01:00
{
2020-08-21 00:02:00 +02:00
if (currentMusic == MUS_SURF
&& VarGet(VAR_SKY_PILLAR_STATE) == 2
2018-02-12 18:26:26 +01:00
&& gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(SOOTOPOLIS_CITY)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(SOOTOPOLIS_CITY)
2018-02-14 00:58:22 +01:00
&& sWarpDestination.mapGroup == MAP_GROUP(SOOTOPOLIS_CITY)
&& sWarpDestination.mapNum == MAP_NUM(SOOTOPOLIS_CITY)
2018-12-27 23:30:47 +01:00
&& sWarpDestination.x == 29
&& sWarpDestination.y == 53)
2018-02-12 18:26:26 +01:00
return;
FadeOutMapMusic(GetMapMusicFadeoutSpeed());
}
}
bool8 BGMusicStopped(void)
2018-02-12 18:26:26 +01:00
{
return IsNotWaitingForBGMStop();
}
void Overworld_FadeOutMapMusic(void)
{
FadeOutMapMusic(4);
}
2018-02-14 00:58:22 +01:00
static void PlayAmbientCry(void)
{
s16 x, y;
s8 pan;
s8 volume;
PlayerGetDestCoords(&x, &y);
if (sIsAmbientCryWaterMon == TRUE
&& !MetatileBehavior_IsSurfableWaterOrUnderwater(MapGridGetMetatileBehaviorAt(x, y)))
return;
pan = (Random() % 88) + 212;
2019-02-18 07:03:44 +01:00
volume = (Random() % 30) + 50;
2021-11-07 19:54:44 +01:00
PlayCry_NormalNoDucking(sAmbientCrySpecies, pan, volume, CRY_PRIORITY_AMBIENT);
2018-02-14 00:58:22 +01:00
}
void UpdateAmbientCry(s16 *state, u16 *delayCounter)
{
u8 i, monsCount, divBy;
switch (*state)
{
case 0:
if (sAmbientCrySpecies == SPECIES_NONE)
*state = 4;
else
*state = 1;
break;
case 1:
*delayCounter = (Random() % 2400) + 1200;
*state = 3;
break;
case 2:
divBy = 1;
monsCount = CalculatePlayerPartyCount();
for (i = 0; i < monsCount; i++)
{
2018-12-15 23:58:47 +01:00
if (!GetMonData(&gPlayerParty[i], MON_DATA_SANITY_IS_EGG)
2018-02-14 00:58:22 +01:00
&& GetMonAbility(&gPlayerParty[0]) == ABILITY_SWARM)
{
divBy = 2;
break;
}
}
*delayCounter = ((Random() % 1200) + 1200) / divBy;
*state = 3;
break;
case 3:
(*delayCounter)--;
if (*delayCounter == 0)
{
PlayAmbientCry();
*state = 2;
}
break;
case 4:
break;
}
}
static void ChooseAmbientCrySpecies(void)
{
if ((gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(ROUTE130)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(ROUTE130))
&& !IsMirageIslandPresent())
{
// Only play water pokemon cries on this route
// when Mirage Island is not present
sIsAmbientCryWaterMon = TRUE;
sAmbientCrySpecies = GetLocalWaterMon();
}
else
{
sAmbientCrySpecies = GetLocalWildMon(&sIsAmbientCryWaterMon);
}
}
u8 GetMapTypeByGroupAndId(s8 mapGroup, s8 mapNum)
{
return Overworld_GetMapHeaderByGroupAndId(mapGroup, mapNum)->mapType;
}
u8 GetMapTypeByWarpData(struct WarpData *warp)
{
return GetMapTypeByGroupAndId(warp->mapGroup, warp->mapNum);
}
2018-12-27 23:30:47 +01:00
u8 GetCurrentMapType(void)
2018-02-14 00:58:22 +01:00
{
return GetMapTypeByWarpData(&gSaveBlock1Ptr->location);
}
2018-10-17 02:11:44 +02:00
u8 GetLastUsedWarpMapType(void)
2018-02-14 00:58:22 +01:00
{
return GetMapTypeByWarpData(&gLastUsedWarp);
2018-02-14 00:58:22 +01:00
}
2019-03-01 07:49:11 +01:00
bool8 IsMapTypeOutdoors(u8 mapType)
2018-02-14 00:58:22 +01:00
{
if (mapType == MAP_TYPE_ROUTE
|| mapType == MAP_TYPE_TOWN
|| mapType == MAP_TYPE_UNDERWATER
|| mapType == MAP_TYPE_CITY
2019-02-28 05:54:51 +01:00
|| mapType == MAP_TYPE_OCEAN_ROUTE)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
bool8 Overworld_MapTypeAllowsTeleportAndFly(u8 mapType)
{
if (mapType == MAP_TYPE_ROUTE
|| mapType == MAP_TYPE_TOWN
2019-02-28 05:54:51 +01:00
|| mapType == MAP_TYPE_OCEAN_ROUTE
2018-02-14 00:58:22 +01:00
|| mapType == MAP_TYPE_CITY)
return TRUE;
else
return FALSE;
}
2019-03-02 10:13:27 +01:00
bool8 IsMapTypeIndoors(u8 mapType)
2018-02-14 00:58:22 +01:00
{
if (mapType == MAP_TYPE_INDOOR
|| mapType == MAP_TYPE_SECRET_BASE)
return TRUE;
else
return FALSE;
}
2018-12-27 23:30:47 +01:00
u8 GetSavedWarpRegionMapSectionId(void)
2018-02-14 00:58:22 +01:00
{
2018-12-27 23:30:47 +01:00
return Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->dynamicWarp.mapGroup, gSaveBlock1Ptr->dynamicWarp.mapNum)->regionMapSectionId;
2018-02-14 00:58:22 +01:00
}
2018-12-27 23:30:47 +01:00
u8 GetCurrentRegionMapSectionId(void)
2018-02-14 00:58:22 +01:00
{
return Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum)->regionMapSectionId;
}
u8 GetCurrentMapBattleScene(void)
{
return Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum)->battleType;
}
2018-12-27 23:30:47 +01:00
static void InitOverworldBgs(void)
2018-02-14 00:58:22 +01:00
{
2018-12-27 23:30:47 +01:00
InitBgsFromTemplates(0, sOverworldBgTemplates, ARRAY_COUNT(sOverworldBgTemplates));
2018-12-26 13:05:02 +01:00
SetBgAttribute(1, BG_ATTR_MOSAIC, 1);
SetBgAttribute(2, BG_ATTR_MOSAIC, 1);
SetBgAttribute(3, BG_ATTR_MOSAIC, 1);
2018-12-27 23:30:47 +01:00
gBGTilemapBuffers2 = AllocZeroed(BG_SCREEN_SIZE);
gBGTilemapBuffers1 = AllocZeroed(BG_SCREEN_SIZE);
gBGTilemapBuffers3 = AllocZeroed(BG_SCREEN_SIZE);
2018-04-18 14:12:41 +02:00
SetBgTilemapBuffer(1, gBGTilemapBuffers2);
SetBgTilemapBuffer(2, gBGTilemapBuffers1);
SetBgTilemapBuffer(3, gBGTilemapBuffers3);
2018-12-27 23:30:47 +01:00
InitStandardTextBoxWindows();
2018-02-14 00:58:22 +01:00
}
2018-12-27 23:30:47 +01:00
void CleanupOverworldWindowsAndTilemaps(void)
2018-02-14 00:58:22 +01:00
{
ClearMirageTowerPulseBlendEffect();
2018-12-27 23:30:47 +01:00
FreeAllOverworldWindowBuffers();
if (gBGTilemapBuffers3)
2018-04-18 14:12:41 +02:00
FREE_AND_SET_NULL(gBGTilemapBuffers3);
2018-12-27 23:30:47 +01:00
if (gBGTilemapBuffers1)
2018-04-18 14:12:41 +02:00
FREE_AND_SET_NULL(gBGTilemapBuffers1);
2018-12-27 23:30:47 +01:00
if (gBGTilemapBuffers2)
2018-04-18 14:12:41 +02:00
FREE_AND_SET_NULL(gBGTilemapBuffers2);
2018-02-14 00:58:22 +01:00
}
static void ResetSafariZoneFlag_(void)
{
ResetSafariZoneFlag();
}
bool32 IsOverworldLinkActive(void)
2018-02-14 00:58:22 +01:00
{
if (gMain.callback1 == CB1_OverworldLink)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
static void DoCB1_Overworld(u16 newKeys, u16 heldKeys)
{
struct FieldInput inputStruct;
UpdatePlayerAvatarTransitionState();
2018-02-14 00:58:22 +01:00
FieldClearPlayerInput(&inputStruct);
FieldGetPlayerInput(&inputStruct, newKeys, heldKeys);
if (!ScriptContext2_IsEnabled())
{
2018-10-17 02:11:44 +02:00
if (ProcessPlayerFieldInput(&inputStruct) == 1)
2018-02-14 00:58:22 +01:00
{
ScriptContext2_Enable();
HideMapNamePopUpWindow();
}
else
{
PlayerStep(inputStruct.dpadDirection, newKeys, heldKeys);
2018-02-14 00:58:22 +01:00
}
}
}
void CB1_Overworld(void)
{
if (gMain.callback2 == CB2_Overworld)
DoCB1_Overworld(gMain.newKeys, gMain.heldKeys);
}
static void OverworldBasic(void)
{
ScriptContext2_RunScript();
RunTasks();
AnimateSprites();
CameraUpdate();
UpdateCameraPanning();
BuildOamBuffer();
UpdatePaletteFade();
2019-02-08 22:07:42 +01:00
UpdateTilesetAnimations();
DoScheduledBgTilemapCopiesToVram();
2018-02-14 00:58:22 +01:00
}
// This CB2 is used when starting
void CB2_OverworldBasic(void)
{
OverworldBasic();
}
void CB2_Overworld(void)
{
bool32 fading = (gPaletteFade.active != 0);
if (fading)
SetVBlankCallback(NULL);
OverworldBasic();
if (fading)
SetFieldVBlankCallback();
}
void SetMainCallback1(MainCallback cb)
{
gMain.callback1 = cb;
}
// This function is never called.
void SetUnusedCallback(void *func)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
sUnusedOverworldCallback = func;
2018-02-14 00:58:22 +01:00
}
static bool8 RunFieldCallback(void)
2018-02-14 00:58:22 +01:00
{
2019-04-05 23:11:24 +02:00
if (gFieldCallback2)
2018-02-14 00:58:22 +01:00
{
2018-06-16 00:45:48 +02:00
if (!gFieldCallback2())
2018-02-14 00:58:22 +01:00
{
return FALSE;
}
else
{
2018-06-16 00:45:48 +02:00
gFieldCallback2 = NULL;
2018-02-14 00:58:22 +01:00
gFieldCallback = NULL;
}
}
else
{
2019-04-05 23:11:24 +02:00
if (gFieldCallback)
2018-02-14 00:58:22 +01:00
gFieldCallback();
else
2019-12-17 09:24:44 +01:00
FieldCB_DefaultWarpExit();
2018-02-14 00:58:22 +01:00
gFieldCallback = NULL;
}
return TRUE;
}
void CB2_NewGame(void)
{
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
NewGameInitData();
ResetInitialPlayerAvatarState();
2018-02-14 00:58:22 +01:00
PlayTimeCounter_Start();
ScriptContext1_Init();
ScriptContext2_Disable();
gFieldCallback = ExecuteTruckSequence;
2018-06-16 00:45:48 +02:00
gFieldCallback2 = NULL;
DoMapLoadLoop(&gMain.state);
2018-02-14 00:58:22 +01:00
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
void CB2_WhiteOut(void)
{
u8 state;
2018-02-14 00:58:22 +01:00
if (++gMain.state >= 120)
{
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
DoWhiteOut();
ResetInitialPlayerAvatarState();
2018-02-14 00:58:22 +01:00
ScriptContext1_Init();
ScriptContext2_Disable();
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_WarpExitFadeFromBlack;
state = 0;
DoMapLoadLoop(&state);
2018-02-14 00:58:22 +01:00
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
}
void CB2_LoadMap(void)
{
FieldClearVBlankHBlankCallbacks();
ScriptContext1_Init();
ScriptContext2_Disable();
SetMainCallback1(NULL);
2019-09-10 19:40:28 +02:00
SetMainCallback2(CB2_DoChangeMap);
2018-02-14 00:58:22 +01:00
gMain.savedCallback = CB2_LoadMap2;
}
static void CB2_LoadMap2(void)
{
DoMapLoadLoop(&gMain.state);
2018-02-14 00:58:22 +01:00
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
void CB2_ReturnToFieldContestHall(void)
2018-02-14 00:58:22 +01:00
{
if (!gMain.state)
{
FieldClearVBlankHBlankCallbacks();
ScriptContext1_Init();
ScriptContext2_Disable();
SetMainCallback1(NULL);
}
if (LoadMapInStepsLocal(&gMain.state, TRUE))
2018-02-14 00:58:22 +01:00
{
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
}
2019-12-17 09:24:44 +01:00
void CB2_ReturnToFieldCableClub(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_ReturnToFieldWirelessLink;
SetMainCallback2(CB2_LoadMapOnReturnToFieldCableClub);
2018-02-14 00:58:22 +01:00
}
static void CB2_LoadMapOnReturnToFieldCableClub(void)
2018-02-14 00:58:22 +01:00
{
if (LoadMapInStepsLink(&gMain.state))
2018-02-14 00:58:22 +01:00
{
SetFieldVBlankCallback();
SetMainCallback1(CB1_OverworldLink);
ResetAllMultiplayerState();
2018-02-14 00:58:22 +01:00
SetMainCallback2(CB2_Overworld);
}
}
void CB2_ReturnToField(void)
{
if (IsOverworldLinkActive() == TRUE)
2018-02-14 00:58:22 +01:00
{
SetMainCallback2(CB2_ReturnToFieldLink);
}
else
{
FieldClearVBlankHBlankCallbacks();
SetMainCallback2(CB2_ReturnToFieldLocal);
}
}
static void CB2_ReturnToFieldLocal(void)
2018-02-14 00:58:22 +01:00
{
if (ReturnToFieldLocal(&gMain.state))
2018-02-14 00:58:22 +01:00
{
SetFieldVBlankCallback();
SetMainCallback2(CB2_Overworld);
}
}
static void CB2_ReturnToFieldLink(void)
2018-02-14 00:58:22 +01:00
{
if (!Overworld_IsRecvQueueAtMax() && ReturnToFieldLink(&gMain.state))
2018-02-14 00:58:22 +01:00
SetMainCallback2(CB2_Overworld);
}
void CB2_ReturnToFieldFromMultiplayer(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
SetMainCallback1(CB1_OverworldLink);
ResetAllMultiplayerState();
2018-02-14 00:58:22 +01:00
2018-02-15 12:36:52 +01:00
if (gWirelessCommType != 0)
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_ReturnToFieldWirelessLink;
2018-02-14 00:58:22 +01:00
else
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_ReturnToFieldCableLink;
2018-02-14 00:58:22 +01:00
ScriptContext1_Init();
ScriptContext2_Disable();
CB2_ReturnToField();
}
void CB2_ReturnToFieldWithOpenMenu(void)
{
FieldClearVBlankHBlankCallbacks();
2019-12-17 09:24:44 +01:00
gFieldCallback2 = FieldCB_ReturnToFieldOpenStartMenu;
2018-02-14 00:58:22 +01:00
CB2_ReturnToField();
}
void CB2_ReturnToFieldContinueScript(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_ContinueScript;
2018-02-14 00:58:22 +01:00
CB2_ReturnToField();
}
void CB2_ReturnToFieldContinueScriptPlayMapMusic(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_ContinueScriptHandleMusic;
2018-02-14 00:58:22 +01:00
CB2_ReturnToField();
}
2021-04-06 22:55:33 +02:00
void CB2_ReturnToFieldFadeFromBlack(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
2019-12-17 09:24:44 +01:00
gFieldCallback = FieldCB_WarpExitFadeFromBlack;
2018-02-14 00:58:22 +01:00
CB2_ReturnToField();
}
2021-04-06 22:55:33 +02:00
static void FieldCB_FadeTryShowMapPopup(void)
2018-02-14 00:58:22 +01:00
{
if (gMapHeader.showMapName == TRUE && SecretBaseMapPopupEnabled() == TRUE)
2018-02-14 00:58:22 +01:00
ShowMapNamePopup();
2019-12-17 09:24:44 +01:00
FieldCB_WarpExitFadeFromBlack();
2018-02-14 00:58:22 +01:00
}
void CB2_ContinueSavedGame(void)
{
u8 trainerHillMapId;
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
2020-01-12 21:27:37 +01:00
if (gSaveFileStatus == SAVE_STATUS_ERROR)
2019-11-24 22:58:40 +01:00
ResetWinStreaks();
2018-02-14 00:58:22 +01:00
LoadSaveblockMapHeader();
2018-12-27 23:30:47 +01:00
ClearDiveAndHoleWarps();
2018-02-14 00:58:22 +01:00
trainerHillMapId = GetCurrentTrainerHillMapId();
2019-12-11 09:28:55 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_FLOOR)
LoadBattlePyramidFloorObjectEventScripts();
2019-11-13 22:10:05 +01:00
else if (trainerHillMapId != 0 && trainerHillMapId != TRAINER_HILL_ENTRANCE)
LoadTrainerHillFloorObjectEventScripts();
2018-02-14 00:58:22 +01:00
else
2019-11-21 06:14:40 +01:00
LoadSaveblockObjEventScripts();
2018-02-14 00:58:22 +01:00
UnfreezeObjectEvents();
2018-02-14 00:58:22 +01:00
DoTimeBasedEvents();
2021-04-06 22:55:33 +02:00
UpdateMiscOverworldStates();
2019-12-11 09:28:55 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_FLOOR)
2019-02-07 03:01:29 +01:00
InitBattlePyramidMap(TRUE);
2018-02-14 00:58:22 +01:00
else if (trainerHillMapId != 0)
2018-12-28 18:18:23 +01:00
InitTrainerHillMap();
2018-02-14 00:58:22 +01:00
else
2018-12-28 18:18:23 +01:00
InitMapFromSavedGame();
2018-02-14 00:58:22 +01:00
PlayTimeCounter_Start();
ScriptContext1_Init();
ScriptContext2_Disable();
2019-01-03 02:07:47 +01:00
InitMatchCallCounters();
2018-12-27 23:30:47 +01:00
if (UseContinueGameWarp() == TRUE)
2018-02-14 00:58:22 +01:00
{
2018-12-27 23:30:47 +01:00
ClearContinueGameWarpStatus();
SetWarpDestinationToContinueGameWarp();
WarpIntoMap();
TryPutTodaysRivalTrainerOnAir();
2018-02-14 00:58:22 +01:00
SetMainCallback2(CB2_LoadMap);
}
else
{
TryPutTodaysRivalTrainerOnAir();
2021-04-06 22:55:33 +02:00
gFieldCallback = FieldCB_FadeTryShowMapPopup;
2018-02-14 00:58:22 +01:00
SetMainCallback1(CB1_Overworld);
CB2_ReturnToField();
}
}
static void FieldClearVBlankHBlankCallbacks(void)
{
2019-09-13 20:24:07 +02:00
if (UsedPokemonCenterWarp() == TRUE)
2018-02-14 00:58:22 +01:00
CloseLink();
if (gWirelessCommType != 0)
{
EnableInterrupts(INTR_FLAG_VBLANK | INTR_FLAG_VCOUNT | INTR_FLAG_TIMER3 | INTR_FLAG_SERIAL);
DisableInterrupts(INTR_FLAG_HBLANK);
}
else
{
u16 savedIme = REG_IME;
REG_IME = 0;
REG_IE &= ~INTR_FLAG_HBLANK;
REG_IE |= INTR_FLAG_VBLANK;
REG_IME = savedIme;
}
SetVBlankCallback(NULL);
SetHBlankCallback(NULL);
}
static void SetFieldVBlankCallback(void)
{
SetVBlankCallback(VBlankCB_Field);
}
static void VBlankCB_Field(void)
{
LoadOam();
ProcessSpriteCopyRequests();
ScanlineEffect_InitHBlankDmaTransfer();
FieldUpdateBgTilemapScroll();
TransferPlttBuffer();
TransferTilesetAnimsBuffer();
}
static void InitCurrentFlashLevelScanlineEffect(void)
2018-02-14 00:58:22 +01:00
{
u8 flashLevel;
2018-02-14 00:58:22 +01:00
2018-11-17 22:55:39 +01:00
if (InBattlePyramid_())
2018-02-14 00:58:22 +01:00
{
2019-02-07 03:01:29 +01:00
WriteBattlePyramidViewScanlineEffectBuffer();
ScanlineEffect_SetParams(sFlashEffectParams);
2018-02-14 00:58:22 +01:00
}
2021-11-15 16:57:22 +01:00
else if ((flashLevel = GetFlashLevel()))
2018-02-14 00:58:22 +01:00
{
2018-12-08 21:42:36 +01:00
WriteFlashScanlineEffectBuffer(flashLevel);
ScanlineEffect_SetParams(sFlashEffectParams);
2018-02-14 00:58:22 +01:00
}
}
static bool32 LoadMapInStepsLink(u8 *state)
2018-02-14 00:58:22 +01:00
{
switch (*state)
{
case 0:
2018-12-27 23:30:47 +01:00
InitOverworldBgs();
2018-02-14 00:58:22 +01:00
ScriptContext1_Init();
ScriptContext2_Disable();
ResetMirageTowerAndSaveBlockPtrs();
2021-04-06 22:55:33 +02:00
ResetScreenForMapLoad();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 1:
LoadMapFromWarp(TRUE);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 2:
ResumeMap(TRUE);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 3:
OffsetCameraFocusByLinkPlayerId();
InitObjectEventsLink();
SpawnLinkPlayers();
SetCameraToTrackGuestPlayer();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 4:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
InitTextBoxGfxAndPrinters();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 5:
ResetFieldCamera();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 6:
CopyPrimaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
CopySecondaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 8:
2020-05-14 10:37:09 +02:00
if (FreeTempTileDataBuffersIfPossible() != TRUE)
2018-02-14 00:58:22 +01:00
{
LoadMapTilesetPalettes(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
}
break;
case 9:
DrawWholeMapView();
(*state)++;
break;
case 10:
2019-02-08 22:07:42 +01:00
InitTilesetAnimations();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 11:
if (gWirelessCommType != 0)
{
2019-04-04 23:05:46 +02:00
LoadWirelessStatusIndicatorSpriteGfx();
2018-02-14 00:58:22 +01:00
CreateWirelessStatusIndicatorSprite(0, 0);
}
(*state)++;
break;
case 12:
if (RunFieldCallback())
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 13:
return TRUE;
}
return FALSE;
}
static bool32 LoadMapInStepsLocal(u8 *state, bool32 a2)
2018-02-14 00:58:22 +01:00
{
switch (*state)
{
case 0:
FieldClearVBlankHBlankCallbacks();
LoadMapFromWarp(a2);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 1:
ResetMirageTowerAndSaveBlockPtrs();
2021-04-06 22:55:33 +02:00
ResetScreenForMapLoad();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 2:
ResumeMap(a2);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 3:
InitObjectEventsLocal();
SetCameraToTrackPlayer();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 4:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
InitTextBoxGfxAndPrinters();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 5:
ResetFieldCamera();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 6:
CopyPrimaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
CopySecondaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 8:
2020-05-14 10:37:09 +02:00
if (FreeTempTileDataBuffersIfPossible() != TRUE)
2018-02-14 00:58:22 +01:00
{
LoadMapTilesetPalettes(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
}
break;
case 9:
DrawWholeMapView();
(*state)++;
break;
case 10:
2019-02-08 22:07:42 +01:00
InitTilesetAnimations();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 11:
if (gMapHeader.showMapName == TRUE && SecretBaseMapPopupEnabled() == TRUE)
2018-02-14 00:58:22 +01:00
ShowMapNamePopup();
(*state)++;
break;
case 12:
if (RunFieldCallback())
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 13:
return TRUE;
}
return FALSE;
}
static bool32 ReturnToFieldLocal(u8 *state)
2018-02-14 00:58:22 +01:00
{
switch (*state)
{
case 0:
ResetMirageTowerAndSaveBlockPtrs();
2021-04-06 22:55:33 +02:00
ResetScreenForMapLoad();
ResumeMap(FALSE);
2021-04-06 11:05:33 +02:00
InitObjectEventsReturnToField();
SetCameraToTrackPlayer();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 1:
InitViewGraphics();
TryLoadTrainerHillEReaderPalette();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 2:
if (RunFieldCallback())
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 3:
return TRUE;
}
return FALSE;
}
static bool32 ReturnToFieldLink(u8 *state)
2018-02-14 00:58:22 +01:00
{
switch (*state)
{
case 0:
FieldClearVBlankHBlankCallbacks();
ResetMirageTowerAndSaveBlockPtrs();
2021-04-06 22:55:33 +02:00
ResetScreenForMapLoad();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 1:
ResumeMap(TRUE);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 2:
CreateLinkPlayerSprites();
2021-04-06 11:05:33 +02:00
InitObjectEventsReturnToField();
SetCameraToTrackGuestPlayer_2();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 3:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
InitTextBoxGfxAndPrinters();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 4:
ResetFieldCamera();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 5:
CopyPrimaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 6:
CopySecondaryTilesetToVram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
2020-05-14 10:37:09 +02:00
if (FreeTempTileDataBuffersIfPossible() != TRUE)
2018-02-14 00:58:22 +01:00
{
LoadMapTilesetPalettes(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
}
break;
case 8:
DrawWholeMapView();
(*state)++;
break;
case 9:
2019-02-08 22:07:42 +01:00
InitTilesetAnimations();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 11:
if (gWirelessCommType != 0)
{
2019-04-04 23:05:46 +02:00
LoadWirelessStatusIndicatorSpriteGfx();
2018-02-14 00:58:22 +01:00
CreateWirelessStatusIndicatorSprite(0, 0);
}
(*state)++;
break;
case 12:
if (RunFieldCallback())
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 10:
(*state)++;
break;
case 13:
SetFieldVBlankCallback();
(*state)++;
return TRUE;
}
return FALSE;
}
static void DoMapLoadLoop(u8 *state)
2018-02-14 00:58:22 +01:00
{
while (!LoadMapInStepsLocal(state, FALSE));
2018-02-14 00:58:22 +01:00
}
static void ResetMirageTowerAndSaveBlockPtrs(void)
2018-02-14 00:58:22 +01:00
{
ClearMirageTowerPulseBlend();
2018-02-14 00:58:22 +01:00
MoveSaveBlocks_ResetHeap();
}
2021-04-06 22:55:33 +02:00
static void ResetScreenForMapLoad(void)
2018-02-14 00:58:22 +01:00
{
SetGpuReg(REG_OFFSET_DISPCNT, 0);
ScanlineEffect_Stop();
DmaClear16(3, PLTT + 2, PLTT_SIZE - 2);
2021-03-30 21:07:04 +02:00
DmaFillLarge16(3, 0, (void *)VRAM, VRAM_SIZE, 0x1000);
2018-02-14 00:58:22 +01:00
ResetOamRange(0, 128);
LoadOam();
}
static void InitViewGraphics(void)
2018-02-14 00:58:22 +01:00
{
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
InitTextBoxGfxAndPrinters();
InitMapView();
2018-02-14 00:58:22 +01:00
}
static void InitOverworldGraphicsRegisters(void)
2018-02-14 00:58:22 +01:00
{
ClearScheduledBgCopiesToVram();
2020-05-14 10:37:09 +02:00
ResetTempTileDataBuffers();
2018-02-14 00:58:22 +01:00
SetGpuReg(REG_OFFSET_MOSAIC, 0);
2018-12-27 23:30:47 +01:00
SetGpuReg(REG_OFFSET_WININ, WININ_WIN0_BG_ALL | WININ_WIN0_OBJ | WININ_WIN1_BG_ALL | WININ_WIN1_OBJ);
SetGpuReg(REG_OFFSET_WINOUT, WINOUT_WIN01_BG0 | WINOUT_WINOBJ_BG0);
2018-02-14 00:58:22 +01:00
SetGpuReg(REG_OFFSET_WIN0H, 0xFF);
SetGpuReg(REG_OFFSET_WIN0V, 0xFF);
SetGpuReg(REG_OFFSET_WIN1H, 0xFFFF);
SetGpuReg(REG_OFFSET_WIN1V, 0xFFFF);
SetGpuReg(REG_OFFSET_BLDCNT, gOverworldBackgroundLayerFlags[1] | gOverworldBackgroundLayerFlags[2] | gOverworldBackgroundLayerFlags[3]
2018-02-15 12:36:52 +01:00
| BLDCNT_TGT2_OBJ | BLDCNT_EFFECT_BLEND);
2018-02-15 23:09:52 +01:00
SetGpuReg(REG_OFFSET_BLDALPHA, BLDALPHA_BLEND(13, 7));
2018-12-27 23:30:47 +01:00
InitOverworldBgs();
2020-05-14 10:37:09 +02:00
ScheduleBgCopyTilemapToVram(1);
ScheduleBgCopyTilemapToVram(2);
ScheduleBgCopyTilemapToVram(3);
2021-11-04 04:02:06 +01:00
ChangeBgX(0, 0, BG_COORD_SET);
ChangeBgY(0, 0, BG_COORD_SET);
ChangeBgX(1, 0, BG_COORD_SET);
ChangeBgY(1, 0, BG_COORD_SET);
ChangeBgX(2, 0, BG_COORD_SET);
ChangeBgY(2, 0, BG_COORD_SET);
ChangeBgX(3, 0, BG_COORD_SET);
ChangeBgY(3, 0, BG_COORD_SET);
2018-02-15 12:36:52 +01:00
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_WIN0_ON | DISPCNT_WIN1_ON
| DISPCNT_OBJ_1D_MAP | DISPCNT_HBLANK_INTERVAL);
2018-02-14 00:58:22 +01:00
ShowBg(0);
ShowBg(1);
ShowBg(2);
ShowBg(3);
2018-12-27 23:30:47 +01:00
InitFieldMessageBox();
2018-02-14 00:58:22 +01:00
}
static void ResumeMap(bool32 a1)
2018-02-14 00:58:22 +01:00
{
ResetTasks();
ResetSpriteData();
ResetPaletteFade();
ScanlineEffect_Clear();
2018-08-19 01:06:10 +02:00
ResetAllPicSprites();
2018-02-14 00:58:22 +01:00
ResetCameraUpdateInfo();
InstallCameraPanAheadCallback();
if (!a1)
InitObjectEventPalettes(0);
2018-02-14 00:58:22 +01:00
else
InitObjectEventPalettes(1);
2018-02-14 00:58:22 +01:00
FieldEffectActiveListClear();
2018-12-08 19:05:03 +01:00
StartWeather();
2019-02-25 08:28:41 +01:00
ResumePausedWeather();
2018-02-14 00:58:22 +01:00
if (!a1)
SetUpFieldTasks();
RunOnResumeMapScript();
TryStartMirageTowerPulseBlendEffect();
2018-02-14 00:58:22 +01:00
}
static void InitObjectEventsLink(void)
2018-02-14 00:58:22 +01:00
{
2018-10-16 15:55:16 +02:00
gTotalCameraPixelOffsetX = 0;
gTotalCameraPixelOffsetY = 0;
ResetObjectEvents();
TrySpawnObjectEvents(0, 0);
TryRunOnWarpIntoMapScript();
2018-02-14 00:58:22 +01:00
}
static void InitObjectEventsLocal(void)
2018-02-14 00:58:22 +01:00
{
s16 x, y;
struct InitialPlayerAvatarState *player;
2018-02-14 00:58:22 +01:00
2018-10-16 15:55:16 +02:00
gTotalCameraPixelOffsetX = 0;
gTotalCameraPixelOffsetY = 0;
ResetObjectEvents();
2018-12-28 18:18:23 +01:00
GetCameraFocusCoords(&x, &y);
player = GetInitialPlayerAvatarState();
InitPlayerAvatar(x, y, player->direction, gSaveBlock2Ptr->playerGender);
SetPlayerAvatarTransitionFlags(player->transitionFlags);
ResetInitialPlayerAvatarState();
TrySpawnObjectEvents(0, 0);
TryRunOnWarpIntoMapScript();
2018-02-14 00:58:22 +01:00
}
2021-04-06 11:05:33 +02:00
static void InitObjectEventsReturnToField(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 11:05:33 +02:00
SpawnObjectEventsOnReturnToField(0, 0);
2018-02-14 00:58:22 +01:00
RotatingGate_InitPuzzleAndGraphics();
RunOnReturnToFieldMapScript();
2018-02-14 00:58:22 +01:00
}
static void SetCameraToTrackPlayer(void)
2018-02-14 00:58:22 +01:00
{
gObjectEvents[gPlayerAvatar.objectEventId].trackedByCamera = TRUE;
2018-02-14 00:58:22 +01:00
InitCameraUpdateCallback(gPlayerAvatar.spriteId);
}
static void SetCameraToTrackGuestPlayer(void)
2018-02-14 00:58:22 +01:00
{
2019-02-27 04:04:44 +01:00
InitCameraUpdateCallback(GetSpriteForLinkedPlayer(gLocalLinkPlayerId));
2018-02-14 00:58:22 +01:00
}
// Duplicate function.
static void SetCameraToTrackGuestPlayer_2(void)
2018-02-14 00:58:22 +01:00
{
2019-02-27 04:04:44 +01:00
InitCameraUpdateCallback(GetSpriteForLinkedPlayer(gLocalLinkPlayerId));
2018-02-14 00:58:22 +01:00
}
static void OffsetCameraFocusByLinkPlayerId(void)
2018-02-14 00:58:22 +01:00
{
u16 x, y;
2018-12-28 18:18:23 +01:00
GetCameraFocusCoords(&x, &y);
// This is a hack of some kind; it's undone in SpawnLinkPlayers, which is called
// soon after this function.
SetCameraFocusCoords(x + gLocalLinkPlayerId, y);
2018-02-14 00:58:22 +01:00
}
static void SpawnLinkPlayers(void)
2018-02-14 00:58:22 +01:00
{
u16 i;
u16 x, y;
2018-12-28 18:18:23 +01:00
GetCameraFocusCoords(&x, &y);
2019-02-27 04:04:44 +01:00
x -= gLocalLinkPlayerId;
2018-02-14 00:58:22 +01:00
for (i = 0; i < gFieldLinkPlayerCount; i++)
{
SpawnLinkPlayerObjectEvent(i, i + x, y, gLinkPlayers[i].gender);
2018-02-14 00:58:22 +01:00
CreateLinkPlayerSprite(i, gLinkPlayers[i].version);
}
ClearAllPlayerKeys();
2018-02-14 00:58:22 +01:00
}
static void CreateLinkPlayerSprites(void)
2018-02-14 00:58:22 +01:00
{
u16 i;
for (i = 0; i < gFieldLinkPlayerCount; i++)
CreateLinkPlayerSprite(i, gLinkPlayers[i].version);
}
static void CB1_OverworldLink(void)
2018-02-14 00:58:22 +01:00
{
if (gWirelessCommType == 0 || !IsRfuRecvQueueEmpty() || !IsSendingKeysToLink())
2018-02-14 00:58:22 +01:00
{
2019-02-27 04:04:44 +01:00
u8 selfId = gLocalLinkPlayerId;
UpdateAllLinkPlayers(gLinkPartnersHeldKeys, selfId);
// Note: Because guestId is between 0 and 4, while the smallest key code is
// LINK_KEY_CODE_EMPTY, this is functionally equivalent to `sPlayerKeyInterceptCallback(0)`.
// It is expecting the callback to be KeyInterCB_SelfIdle, and that will
// completely ignore any input parameters.
//
// UpdateHeldKeyCode performs a sanity check on its input; if
// sPlayerKeyInterceptCallback echoes back the argument, which is selfId, then
// it'll use LINK_KEY_CODE_EMPTY instead.
//
// Note 2: There are some key intercept callbacks that treat the key as a player
// ID. It's so hacky.
UpdateHeldKeyCode(sPlayerKeyInterceptCallback(selfId));
ClearAllPlayerKeys();
2018-02-14 00:58:22 +01:00
}
}
void ResetAllMultiplayerState(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
ResetAllPlayerLinkStates();
SetKeyInterceptCallback(KeyInterCB_SelfIdle);
2018-02-14 00:58:22 +01:00
}
static void ClearAllPlayerKeys(void)
2018-02-14 00:58:22 +01:00
{
ResetPlayerHeldKeys(gLinkPartnersHeldKeys);
2018-02-14 00:58:22 +01:00
}
static void SetKeyInterceptCallback(u16 (*func)(u32))
2018-02-14 00:58:22 +01:00
{
sRfuKeepAliveTimer = 0;
sPlayerKeyInterceptCallback = func;
2018-02-14 00:58:22 +01:00
}
// Once every ~60 frames, if the link state hasn't changed (timer reset by calls
// to SetKeyInterceptCallback), it does a bunch of sanity checks on the connection.
// I'm not sure if sRfuKeepAliveTimer is reset in the process, though; rfu stuff is
// still undocumented.
static void CheckRfuKeepAliveTimer(void)
2018-02-14 00:58:22 +01:00
{
if (gWirelessCommType != 0 && ++sRfuKeepAliveTimer > 60)
LinkRfu_FatalError();
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static void ResetAllPlayerLinkStates(void)
2018-02-14 00:58:22 +01:00
{
s32 i;
for (i = 0; i < MAX_LINK_PLAYERS; i++)
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[i] = PLAYER_LINK_STATE_IDLE;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
// Returns true if all connected players are in state.
static bool32 AreAllPlayersInLinkState(u16 state)
2018-02-14 00:58:22 +01:00
{
s32 i;
s32 count = gFieldLinkPlayerCount;
for (i = 0; i < count; i++)
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[i] != state)
2018-02-14 00:58:22 +01:00
return FALSE;
return TRUE;
}
2021-04-06 22:55:33 +02:00
static bool32 IsAnyPlayerInLinkState(u16 state)
2018-02-14 00:58:22 +01:00
{
s32 i;
s32 count = gFieldLinkPlayerCount;
for (i = 0; i < count; i++)
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[i] == state)
2018-02-14 00:58:22 +01:00
return TRUE;
return FALSE;
}
2021-04-06 22:55:33 +02:00
static void HandleLinkPlayerKeyInput(u32 playerId, u16 key, struct CableClubPlayer *trainer, u16 *forceFacing)
2018-02-14 00:58:22 +01:00
{
const u8 *script;
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[playerId] == PLAYER_LINK_STATE_IDLE)
2018-02-14 00:58:22 +01:00
{
script = TryGetTileEventScript(trainer);
2018-02-14 00:58:22 +01:00
if (script)
{
*forceFacing = GetDirectionForEventScript(script);
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
2021-04-06 22:55:33 +02:00
RunInteractLocalPlayerScript(script);
2018-02-14 00:58:22 +01:00
}
return;
}
2021-04-06 22:55:33 +02:00
if (IsAnyPlayerInLinkState(PLAYER_LINK_STATE_EXITING_ROOM) == TRUE)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
RunTerminateLinkScript();
2018-02-14 00:58:22 +01:00
}
return;
}
switch (key)
2018-02-14 00:58:22 +01:00
{
case LINK_KEY_CODE_START_BUTTON:
2021-04-06 22:55:33 +02:00
if (CanCableClubPlayerPressStart(trainer))
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
InitLinkRoomStartMenuScript();
2018-02-14 00:58:22 +01:00
}
}
break;
case LINK_KEY_CODE_DPAD_DOWN:
if (PlayerIsAtSouthExit(trainer) == TRUE)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
2021-04-06 22:55:33 +02:00
RunConfirmLeaveCableClubScript();
2018-02-14 00:58:22 +01:00
}
}
break;
case LINK_KEY_CODE_A_BUTTON:
script = TryInteractWithPlayer(trainer);
2018-02-14 00:58:22 +01:00
if (script)
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
InitMenuBasedScript(script);
2018-02-14 00:58:22 +01:00
}
}
break;
case LINK_KEY_CODE_HANDLE_RECV_QUEUE:
2021-04-06 22:55:33 +02:00
if (IsCableClubPlayerUnfrozen(trainer))
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToRecvQueue);
2021-04-06 22:55:33 +02:00
InitLinkPlayerQueueScript();
2018-02-14 00:58:22 +01:00
}
}
break;
case LINK_KEY_CODE_HANDLE_SEND_QUEUE:
2021-04-06 22:55:33 +02:00
if (IsCableClubPlayerUnfrozen(trainer))
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToSendQueue);
2021-04-06 22:55:33 +02:00
InitLinkPlayerQueueScript();
2018-02-14 00:58:22 +01:00
}
}
break;
}
}
switch (key)
2018-02-14 00:58:22 +01:00
{
case LINK_KEY_CODE_EXIT_ROOM:
2021-04-06 22:55:33 +02:00
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_EXITING_ROOM;
2018-02-14 00:58:22 +01:00
break;
2021-04-06 22:55:33 +02:00
case LINK_KEY_CODE_READY:
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_READY;
2018-02-14 00:58:22 +01:00
break;
2021-04-06 22:55:33 +02:00
case LINK_KEY_CODE_IDLE:
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_IDLE;
2019-02-18 07:03:44 +01:00
if (trainer->isLocalPlayer)
SetKeyInterceptCallback(KeyInterCB_SelfIdle);
2018-02-14 00:58:22 +01:00
break;
2021-04-06 22:55:33 +02:00
case LINK_KEY_CODE_EXIT_SEAT:
if (sPlayerLinkStates[playerId] == PLAYER_LINK_STATE_READY)
sPlayerLinkStates[playerId] = PLAYER_LINK_STATE_BUSY;
2018-02-14 00:58:22 +01:00
break;
}
}
static void UpdateAllLinkPlayers(u16 *keys, s32 selfId)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
struct CableClubPlayer trainer;
2018-02-14 00:58:22 +01:00
s32 i;
2019-09-08 18:21:24 +02:00
for (i = 0; i < MAX_LINK_PLAYERS; i++)
2018-02-14 00:58:22 +01:00
{
u8 key = keys[i];
u16 setFacing = FACING_NONE;
2021-04-06 22:55:33 +02:00
LoadCableClubPlayer(i, selfId, &trainer);
HandleLinkPlayerKeyInput(i, key, &trainer, &setFacing);
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[i] == PLAYER_LINK_STATE_IDLE)
setFacing = GetDirectionForDpadKey(key);
SetPlayerFacingDirection(i, setFacing);
2018-02-14 00:58:22 +01:00
}
}
static void UpdateHeldKeyCode(u16 key)
2018-02-14 00:58:22 +01:00
{
if (key >= LINK_KEY_CODE_EMPTY && key < LINK_KEY_CODE_UNK_8)
gHeldKeyCodeToSend = key;
2018-02-14 00:58:22 +01:00
else
gHeldKeyCodeToSend = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
if (gWirelessCommType != 0
&& GetLinkSendQueueLength() > 1
&& IsOverworldLinkActive() == TRUE
&& IsSendingKeysToLink() == TRUE)
2018-02-14 00:58:22 +01:00
{
switch (key)
2018-02-14 00:58:22 +01:00
{
case LINK_KEY_CODE_EMPTY:
case LINK_KEY_CODE_DPAD_DOWN:
case LINK_KEY_CODE_DPAD_UP:
case LINK_KEY_CODE_DPAD_LEFT:
case LINK_KEY_CODE_DPAD_RIGHT:
case LINK_KEY_CODE_START_BUTTON:
case LINK_KEY_CODE_A_BUTTON:
gHeldKeyCodeToSend = LINK_KEY_CODE_NULL;
2018-02-14 00:58:22 +01:00
break;
}
}
}
static u16 KeyInterCB_ReadButtons(u32 key)
2018-02-14 00:58:22 +01:00
{
2020-09-05 03:11:55 +02:00
if (JOY_HELD(DPAD_UP))
return LINK_KEY_CODE_DPAD_UP;
2020-09-05 03:11:55 +02:00
if (JOY_HELD(DPAD_DOWN))
return LINK_KEY_CODE_DPAD_DOWN;
2020-09-05 03:11:55 +02:00
if (JOY_HELD(DPAD_LEFT))
return LINK_KEY_CODE_DPAD_LEFT;
2020-09-05 03:11:55 +02:00
if (JOY_HELD(DPAD_RIGHT))
return LINK_KEY_CODE_DPAD_RIGHT;
2020-09-05 03:11:55 +02:00
if (JOY_NEW(START_BUTTON))
return LINK_KEY_CODE_START_BUTTON;
2020-09-05 03:11:55 +02:00
if (JOY_NEW(A_BUTTON))
return LINK_KEY_CODE_A_BUTTON;
2020-09-05 03:11:55 +02:00
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
static u16 GetDirectionForDpadKey(u16 a1)
2018-02-14 00:58:22 +01:00
{
switch (a1)
{
case LINK_KEY_CODE_DPAD_RIGHT:
return FACING_RIGHT;
case LINK_KEY_CODE_DPAD_LEFT:
return FACING_LEFT;
case LINK_KEY_CODE_DPAD_UP:
return FACING_UP;
case LINK_KEY_CODE_DPAD_DOWN:
return FACING_DOWN;
2018-02-14 00:58:22 +01:00
default:
return FACING_NONE;
2018-02-14 00:58:22 +01:00
}
}
// Overwrites the keys with 0x11
static void ResetPlayerHeldKeys(u16 *keys)
2018-02-14 00:58:22 +01:00
{
s32 i;
for (i = 0; i < 4; i++)
keys[i] = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
static u16 KeyInterCB_SelfIdle(u32 key)
2018-02-14 00:58:22 +01:00
{
if (ScriptContext2_IsEnabled() == TRUE)
return LINK_KEY_CODE_EMPTY;
if (GetLinkRecvQueueLength() > 4)
return LINK_KEY_CODE_HANDLE_RECV_QUEUE;
if (GetLinkSendQueueLength() <= 4)
return KeyInterCB_ReadButtons(key);
return LINK_KEY_CODE_HANDLE_SEND_QUEUE;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static u16 KeyInterCB_Idle(u32 key)
2018-02-14 00:58:22 +01:00
{
CheckRfuKeepAliveTimer();
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
// Ignore the player's inputs as long as there is an event script
// in ScriptContext2.
static u16 KeyInterCB_DeferToEventScript(u32 key)
2018-02-14 00:58:22 +01:00
{
u16 retVal;
if (ScriptContext2_IsEnabled() == TRUE)
{
retVal = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
else
{
2021-04-06 22:55:33 +02:00
retVal = LINK_KEY_CODE_IDLE;
SetKeyInterceptCallback(KeyInterCB_Idle);
2018-02-14 00:58:22 +01:00
}
return retVal;
}
// Ignore the player's inputs as long as there are events being recived.
static u16 KeyInterCB_DeferToRecvQueue(u32 key)
2018-02-14 00:58:22 +01:00
{
u16 retVal;
if (GetLinkRecvQueueLength() >= OVERWORLD_RECV_QUEUE_MAX)
2018-02-14 00:58:22 +01:00
{
retVal = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
else
{
2021-04-06 22:55:33 +02:00
retVal = LINK_KEY_CODE_IDLE;
2018-02-14 00:58:22 +01:00
ScriptContext2_Disable();
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_Idle);
2018-02-14 00:58:22 +01:00
}
return retVal;
}
// Ignore the player's inputs as long as there are events being sent.
static u16 KeyInterCB_DeferToSendQueue(u32 key)
2018-02-14 00:58:22 +01:00
{
u16 retVal;
if (GetLinkSendQueueLength() > 2)
2018-02-14 00:58:22 +01:00
{
retVal = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
else
{
2021-04-06 22:55:33 +02:00
retVal = LINK_KEY_CODE_IDLE;
2018-02-14 00:58:22 +01:00
ScriptContext2_Disable();
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_Idle);
2018-02-14 00:58:22 +01:00
}
return retVal;
}
2021-04-06 22:55:33 +02:00
static u16 KeyInterCB_ExitingSeat(u32 key)
2018-02-14 00:58:22 +01:00
{
CheckRfuKeepAliveTimer();
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static u16 KeyInterCB_Ready(u32 keyOrPlayerId)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[keyOrPlayerId] == PLAYER_LINK_STATE_READY)
2018-02-14 00:58:22 +01:00
{
2020-09-05 03:11:55 +02:00
if (JOY_NEW(B_BUTTON))
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_ExitingSeat);
return LINK_KEY_CODE_EXIT_SEAT;
2018-02-14 00:58:22 +01:00
}
else
{
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
}
else
{
CheckRfuKeepAliveTimer();
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
}
2021-04-06 22:55:33 +02:00
static u16 KeyInterCB_SetReady(u32 a1)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_Ready);
return LINK_KEY_CODE_READY;
2018-02-14 00:58:22 +01:00
}
static u16 KeyInterCB_SendNothing(u32 key)
2018-02-14 00:58:22 +01:00
{
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
static u16 KeyInterCB_WaitForPlayersToExit(u32 keyOrPlayerId)
2018-02-14 00:58:22 +01:00
{
// keyOrPlayerId could be any keycode. This callback does no sanity checking
// on the size of the key. It's assuming that it is being called from
// CB1_OverworldLink.
2021-04-06 22:55:33 +02:00
if (sPlayerLinkStates[keyOrPlayerId] != PLAYER_LINK_STATE_EXITING_ROOM)
CheckRfuKeepAliveTimer();
2021-04-06 22:55:33 +02:00
if (AreAllPlayersInLinkState(PLAYER_LINK_STATE_EXITING_ROOM) == TRUE)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
ScriptContext1_SetupScript(EventScript_DoLinkRoomExit);
SetKeyInterceptCallback(KeyInterCB_SendNothing);
2018-02-14 00:58:22 +01:00
}
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
static u16 KeyInterCB_SendExitRoomKey(u32 key)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_WaitForPlayersToExit);
return LINK_KEY_CODE_EXIT_ROOM;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
// Identical to KeyInterCB_SendNothing
static u16 KeyInterCB_InLinkActivity(u32 key)
2018-02-14 00:58:22 +01:00
{
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
u32 GetCableClubPartnersReady(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
if (IsAnyPlayerInLinkState(PLAYER_LINK_STATE_EXITING_ROOM) == TRUE)
return CABLE_SEAT_FAILED;
if (sPlayerKeyInterceptCallback == KeyInterCB_Ready && sPlayerLinkStates[gLocalLinkPlayerId] != PLAYER_LINK_STATE_READY)
return CABLE_SEAT_WAITING;
if (sPlayerKeyInterceptCallback == KeyInterCB_ExitingSeat && sPlayerLinkStates[gLocalLinkPlayerId] == PLAYER_LINK_STATE_BUSY)
return CABLE_SEAT_FAILED;
if (AreAllPlayersInLinkState(PLAYER_LINK_STATE_READY))
return CABLE_SEAT_SUCCESS;
return CABLE_SEAT_WAITING;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
// Unused
static bool32 IsAnyPlayerExitingCableClub(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
return IsAnyPlayerInLinkState(PLAYER_LINK_STATE_EXITING_ROOM);
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
u16 SetInCableClubSeat(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_SetReady);
2018-02-14 00:58:22 +01:00
return 0;
}
2021-04-06 22:55:33 +02:00
u16 SetLinkWaitingForScript(void)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
2018-02-14 00:58:22 +01:00
return 0;
}
// The exit room key will be sent at the next opportunity.
// The return value is meaningless.
u16 QueueExitLinkRoomKey(void)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_SendExitRoomKey);
2018-02-14 00:58:22 +01:00
return 0;
}
2021-04-06 22:55:33 +02:00
u16 SetStartedCableClubActivity(void)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
SetKeyInterceptCallback(KeyInterCB_InLinkActivity);
2018-02-14 00:58:22 +01:00
return 0;
}
2021-04-06 22:55:33 +02:00
static void LoadCableClubPlayer(s32 linkPlayerId, s32 myPlayerId, struct CableClubPlayer *trainer)
2018-02-14 00:58:22 +01:00
{
s16 x, y;
trainer->playerId = linkPlayerId;
2019-02-18 07:03:44 +01:00
trainer->isLocalPlayer = (linkPlayerId == myPlayerId) ? 1 : 0;
2021-04-06 22:55:33 +02:00
trainer->movementMode = gLinkPlayerObjectEvents[linkPlayerId].movementMode;
trainer->facing = GetLinkPlayerFacingDirection(linkPlayerId);
GetLinkPlayerCoords(linkPlayerId, &x, &y);
trainer->pos.x = x;
trainer->pos.y = y;
trainer->pos.height = GetLinkPlayerElevation(linkPlayerId);
2021-04-06 22:55:33 +02:00
trainer->metatileBehavior = MapGridGetMetatileBehaviorAt(x, y);
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static bool32 IsCableClubPlayerUnfrozen(struct CableClubPlayer *player)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
u8 mode = player->movementMode;
if (mode == MOVEMENT_MODE_SCRIPTED || mode == MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
2021-04-06 22:55:33 +02:00
// Identical to IsCableClubPlayerUnfrozen
static bool32 CanCableClubPlayerPressStart(struct CableClubPlayer *player)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
u8 mode = player->movementMode;
if (mode == MOVEMENT_MODE_SCRIPTED || mode == MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
2021-04-06 22:55:33 +02:00
static u8 *TryGetTileEventScript(struct CableClubPlayer *player)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
if (player->movementMode != MOVEMENT_MODE_SCRIPTED)
return FACING_NONE;
return GetCoordEventScriptAtMapPosition(&player->pos);
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static bool32 PlayerIsAtSouthExit(struct CableClubPlayer *player)
2018-02-14 00:58:22 +01:00
{
2021-04-06 22:55:33 +02:00
if (player->movementMode != MOVEMENT_MODE_SCRIPTED && player->movementMode != MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return FALSE;
2021-04-06 22:55:33 +02:00
else if (!MetatileBehavior_IsSouthArrowWarp(player->metatileBehavior))
2018-02-14 00:58:22 +01:00
return FALSE;
2019-10-07 06:14:10 +02:00
else if (player->facing != DIR_SOUTH)
2018-02-14 00:58:22 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else
return TRUE;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static const u8 *TryInteractWithPlayer(struct CableClubPlayer *player)
2018-02-14 00:58:22 +01:00
{
struct MapPosition otherPlayerPos;
2018-02-14 00:58:22 +01:00
u8 linkPlayerId;
2021-04-06 22:55:33 +02:00
if (player->movementMode != MOVEMENT_MODE_FREE && player->movementMode != MOVEMENT_MODE_SCRIPTED)
return FACING_NONE;
2018-02-14 00:58:22 +01:00
otherPlayerPos = player->pos;
otherPlayerPos.x += gDirectionToVectors[player->facing].x;
otherPlayerPos.y += gDirectionToVectors[player->facing].y;
otherPlayerPos.height = 0;
linkPlayerId = GetLinkPlayerIdAt(otherPlayerPos.x, otherPlayerPos.y);
2018-02-14 00:58:22 +01:00
if (linkPlayerId != MAX_LINK_PLAYERS)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
if (!player->isLocalPlayer)
2019-10-11 10:14:09 +02:00
return CableClub_EventScript_TooBusyToNotice;
2021-04-06 22:55:33 +02:00
else if (sPlayerLinkStates[linkPlayerId] != PLAYER_LINK_STATE_IDLE)
2019-10-11 10:14:09 +02:00
return CableClub_EventScript_TooBusyToNotice;
else if (!GetLinkTrainerCardColor(linkPlayerId))
2019-10-11 10:14:09 +02:00
return CableClub_EventScript_ReadTrainerCard;
2018-02-14 00:58:22 +01:00
else
2019-10-11 10:14:09 +02:00
return CableClub_EventScript_ReadTrainerCardColored;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
return GetInteractedLinkPlayerScript(&otherPlayerPos, player->metatileBehavior, player->facing);
2018-02-14 00:58:22 +01:00
}
// This returns which direction to force the player to look when one of
// these event scripts runs.
static u16 GetDirectionForEventScript(const u8 *script)
{
2020-01-14 03:13:33 +01:00
if (script == EventScript_BattleColosseum_4P_PlayerSpot0)
return FACING_FORCED_RIGHT;
2020-01-14 03:13:33 +01:00
else if (script == EventScript_BattleColosseum_4P_PlayerSpot1)
return FACING_FORCED_LEFT;
2020-01-14 03:13:33 +01:00
else if (script == EventScript_BattleColosseum_4P_PlayerSpot2)
return FACING_FORCED_RIGHT;
2020-01-14 03:13:33 +01:00
else if (script == EventScript_BattleColosseum_4P_PlayerSpot3)
return FACING_FORCED_LEFT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_RecordCenter_Spot0)
return FACING_FORCED_RIGHT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_RecordCenter_Spot1)
return FACING_FORCED_LEFT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_RecordCenter_Spot2)
return FACING_FORCED_RIGHT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_RecordCenter_Spot3)
return FACING_FORCED_LEFT;
2020-01-14 03:13:33 +01:00
else if (script == EventScript_BattleColosseum_2P_PlayerSpot0)
return FACING_FORCED_RIGHT;
2020-01-14 03:13:33 +01:00
else if (script == EventScript_BattleColosseum_2P_PlayerSpot1)
return FACING_FORCED_LEFT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_TradeCenter_Chair0)
return FACING_FORCED_RIGHT;
2019-02-18 07:03:44 +01:00
else if (script == EventScript_TradeCenter_Chair1)
return FACING_FORCED_LEFT;
2018-02-15 12:36:52 +01:00
else
return FACING_NONE;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
static void InitLinkPlayerQueueScript(void)
2018-02-14 00:58:22 +01:00
{
ScriptContext2_Enable();
}
static void InitLinkRoomStartMenuScript(void)
2018-02-14 00:58:22 +01:00
{
PlaySE(SE_WIN_OPEN);
2018-03-28 21:09:27 +02:00
ShowStartMenu();
2018-02-14 00:58:22 +01:00
ScriptContext2_Enable();
}
2021-04-06 22:55:33 +02:00
static void RunInteractLocalPlayerScript(const u8 *script)
2018-02-14 00:58:22 +01:00
{
PlaySE(SE_SELECT);
ScriptContext1_SetupScript(script);
ScriptContext2_Enable();
}
2021-04-06 22:55:33 +02:00
static void RunConfirmLeaveCableClubScript(void)
2018-02-14 00:58:22 +01:00
{
PlaySE(SE_WIN_OPEN);
2021-04-06 22:55:33 +02:00
ScriptContext1_SetupScript(EventScript_ConfirmLeaveCableClubRoom);
2018-02-14 00:58:22 +01:00
ScriptContext2_Enable();
}
static void InitMenuBasedScript(const u8 *script)
2018-02-14 00:58:22 +01:00
{
PlaySE(SE_SELECT);
ScriptContext1_SetupScript(script);
ScriptContext2_Enable();
}
static void RunTerminateLinkScript(void)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
ScriptContext1_SetupScript(EventScript_TerminateLink);
2018-02-14 00:58:22 +01:00
ScriptContext2_Enable();
}
bool32 Overworld_IsRecvQueueAtMax(void)
2018-02-14 00:58:22 +01:00
{
if (!IsOverworldLinkActive())
return FALSE;
if (GetLinkRecvQueueLength() >= OVERWORLD_RECV_QUEUE_MAX)
2021-03-19 01:35:39 +01:00
sReceivingFromLink = TRUE;
2018-02-14 00:58:22 +01:00
else
2021-03-19 01:35:39 +01:00
sReceivingFromLink = FALSE;
return sReceivingFromLink;
2018-02-14 00:58:22 +01:00
}
2021-03-19 01:35:39 +01:00
bool32 Overworld_RecvKeysFromLinkIsRunning(void)
2018-02-14 00:58:22 +01:00
{
u8 temp;
if (GetLinkRecvQueueLength() < OVERWORLD_RECV_QUEUE_MAX - 1)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsOverworldLinkActive() != TRUE)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsSendingKeysToLink() != TRUE)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (sPlayerKeyInterceptCallback == KeyInterCB_DeferToRecvQueue)
2018-02-14 00:58:22 +01:00
return TRUE;
else if (sPlayerKeyInterceptCallback != KeyInterCB_DeferToEventScript)
2018-02-14 00:58:22 +01:00
return FALSE;
2021-03-19 01:35:39 +01:00
temp = sReceivingFromLink;
sReceivingFromLink = FALSE;
2018-02-14 00:58:22 +01:00
if (temp == TRUE)
return TRUE;
2018-02-15 12:36:52 +01:00
else if (gPaletteFade.active && gPaletteFade.softwareFadeFinishing)
2018-02-14 00:58:22 +01:00
return TRUE;
2018-02-15 12:36:52 +01:00
else
return FALSE;
2018-02-14 00:58:22 +01:00
}
2021-03-19 01:35:39 +01:00
bool32 Overworld_SendKeysToLinkIsRunning(void)
2018-02-14 00:58:22 +01:00
{
if (GetLinkSendQueueLength() < 2)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsOverworldLinkActive() != TRUE)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsSendingKeysToLink() != TRUE)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (sPlayerKeyInterceptCallback == KeyInterCB_DeferToSendQueue)
2018-02-14 00:58:22 +01:00
return TRUE;
2018-02-15 12:36:52 +01:00
else
return FALSE;
2018-02-14 00:58:22 +01:00
}
2021-03-19 01:35:39 +01:00
bool32 IsSendingKeysOverCable(void)
2018-02-14 00:58:22 +01:00
{
if (gWirelessCommType != 0)
return FALSE;
else if (!IsSendingKeysToLink())
2018-02-14 00:58:22 +01:00
return FALSE;
2018-02-15 12:36:52 +01:00
else
return TRUE;
2018-02-14 00:58:22 +01:00
}
static u32 GetLinkSendQueueLength(void)
2018-02-14 00:58:22 +01:00
{
if (gWirelessCommType != 0)
return gRfu.sendQueue.count;
2018-02-14 00:58:22 +01:00
else
return gLink.sendQueue.count;
}
2019-11-21 06:14:40 +01:00
static void ZeroLinkPlayerObjectEvent(struct LinkPlayerObjectEvent *linkPlayerObjEvent)
2018-02-14 00:58:22 +01:00
{
2019-11-21 06:14:40 +01:00
memset(linkPlayerObjEvent, 0, sizeof(struct LinkPlayerObjectEvent));
2018-02-14 00:58:22 +01:00
}
void ClearLinkPlayerObjectEvents(void)
2018-02-14 00:58:22 +01:00
{
memset(gLinkPlayerObjectEvents, 0, sizeof(gLinkPlayerObjectEvents));
2018-02-14 00:58:22 +01:00
}
static void ZeroObjectEvent(struct ObjectEvent *objEvent)
2018-02-14 00:58:22 +01:00
{
memset(objEvent, 0, sizeof(struct ObjectEvent));
2018-02-14 00:58:22 +01:00
}
// Note: Emerald reuses the direction and range variables during Link mode
// as special gender and direction values. The types and placement
// conflict with the usual Event Object struct, thus the definitions.
#define linkGender(obj) obj->singleMovementActive
// not even one can reference *byte* aligned bitfield members...
#define linkDirection(obj) ((u8*)obj)[offsetof(typeof(*obj), fieldEffectSpriteId) - 1] // -> rangeX
static void SpawnLinkPlayerObjectEvent(u8 linkPlayerId, s16 x, s16 y, u8 gender)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = GetFirstInactiveObjectEventId();
2019-11-21 06:14:40 +01:00
struct LinkPlayerObjectEvent *linkPlayerObjEvent = &gLinkPlayerObjectEvents[linkPlayerId];
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
2018-02-14 00:58:22 +01:00
2019-11-21 06:14:40 +01:00
ZeroLinkPlayerObjectEvent(linkPlayerObjEvent);
ZeroObjectEvent(objEvent);
2018-02-14 00:58:22 +01:00
2021-03-19 01:35:39 +01:00
linkPlayerObjEvent->active = TRUE;
2019-11-21 06:14:40 +01:00
linkPlayerObjEvent->linkPlayerId = linkPlayerId;
linkPlayerObjEvent->objEventId = objEventId;
linkPlayerObjEvent->movementMode = MOVEMENT_MODE_FREE;
2018-02-14 00:58:22 +01:00
2021-03-19 01:35:39 +01:00
objEvent->active = TRUE;
linkGender(objEvent) = gender;
2020-12-15 03:54:19 +01:00
linkDirection(objEvent) = DIR_NORTH;
2021-03-19 01:35:39 +01:00
objEvent->spriteId = MAX_SPRITES;
2018-02-14 00:58:22 +01:00
InitLinkPlayerObjectEventPos(objEvent, x, y);
2018-02-14 00:58:22 +01:00
}
static void InitLinkPlayerObjectEventPos(struct ObjectEvent *objEvent, s16 x, s16 y)
2018-02-14 00:58:22 +01:00
{
objEvent->currentCoords.x = x;
objEvent->currentCoords.y = y;
objEvent->previousCoords.x = x;
objEvent->previousCoords.y = y;
SetSpritePosToMapCoords(x, y, &objEvent->initialCoords.x, &objEvent->initialCoords.y);
objEvent->initialCoords.x += 8;
ObjectEventUpdateZCoord(objEvent);
2018-02-14 00:58:22 +01:00
}
2021-03-19 01:35:39 +01:00
static void SetLinkPlayerObjectRange(u8 linkPlayerId, u8 dir)
2018-02-14 00:58:22 +01:00
{
if (gLinkPlayerObjectEvents[linkPlayerId].active)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
2020-12-15 03:51:38 +01:00
linkDirection(objEvent) = dir;
2018-02-14 00:58:22 +01:00
}
}
2021-03-19 01:35:39 +01:00
static void DestroyLinkPlayerObject(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
2019-11-21 06:14:40 +01:00
struct LinkPlayerObjectEvent *linkPlayerObjEvent = &gLinkPlayerObjectEvents[linkPlayerId];
u8 objEventId = linkPlayerObjEvent->objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
if (objEvent->spriteId != MAX_SPRITES)
DestroySprite(&gSprites[objEvent->spriteId]);
2019-11-21 06:14:40 +01:00
linkPlayerObjEvent->active = 0;
objEvent->active = 0;
2018-02-14 00:58:22 +01:00
}
// Returns the spriteId corresponding to this player.
static u8 GetSpriteForLinkedPlayer(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
return objEvent->spriteId;
2018-02-14 00:58:22 +01:00
}
static void GetLinkPlayerCoords(u8 linkPlayerId, u16 *x, u16 *y)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
*x = objEvent->currentCoords.x;
*y = objEvent->currentCoords.y;
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerFacingDirection(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
return linkDirection(objEvent);
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerElevation(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
return objEvent->currentElevation;
2018-02-14 00:58:22 +01:00
}
2021-03-19 01:35:39 +01:00
static s32 GetLinkPlayerObjectStepTimer(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 objEventId = gLinkPlayerObjectEvents[linkPlayerId].objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
return 16 - (s8)objEvent->directionSequenceIndex;
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerIdAt(s16 x, s16 y)
{
u8 i;
2019-09-08 18:21:24 +02:00
for (i = 0; i < MAX_LINK_PLAYERS; i++)
2018-02-14 00:58:22 +01:00
{
if (gLinkPlayerObjectEvents[i].active
&& (gLinkPlayerObjectEvents[i].movementMode == 0 || gLinkPlayerObjectEvents[i].movementMode == 2))
2018-02-14 00:58:22 +01:00
{
struct ObjectEvent *objEvent = &gObjectEvents[gLinkPlayerObjectEvents[i].objEventId];
if (objEvent->currentCoords.x == x && objEvent->currentCoords.y == y)
2018-02-14 00:58:22 +01:00
return i;
}
}
return 4;
}
static void SetPlayerFacingDirection(u8 linkPlayerId, u8 facing)
2018-02-14 00:58:22 +01:00
{
2019-11-21 06:14:40 +01:00
struct LinkPlayerObjectEvent *linkPlayerObjEvent = &gLinkPlayerObjectEvents[linkPlayerId];
u8 objEventId = linkPlayerObjEvent->objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
2018-02-14 00:58:22 +01:00
2019-11-21 06:14:40 +01:00
if (linkPlayerObjEvent->active)
2018-02-14 00:58:22 +01:00
{
if (facing > FACING_FORCED_RIGHT)
2019-02-18 07:03:44 +01:00
{
objEvent->triggerGroundEffectsOnMove = 1;
2019-02-18 07:03:44 +01:00
}
else
{
// This is a hack to split this code onto two separate lines, without declaring a local variable.
// C++ style inline variables would be nice here.
2019-11-21 06:14:40 +01:00
#define TEMP gLinkPlayerMovementModes[linkPlayerObjEvent->movementMode](linkPlayerObjEvent, objEvent, facing)
2019-11-21 06:14:40 +01:00
gMovementStatusHandler[TEMP](linkPlayerObjEvent, objEvent);
// Clean up the hack.
#undef TEMP
}
2018-02-14 00:58:22 +01:00
}
}
2021-03-19 23:49:19 +01:00
static u8 MovementEventModeCB_Normal(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
2021-03-19 23:49:19 +01:00
return gLinkPlayerFacingHandlers[dir](linkPlayerObjEvent, objEvent, dir);
2018-02-14 00:58:22 +01:00
}
2021-03-19 23:49:19 +01:00
static u8 MovementEventModeCB_Ignored(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
return FACING_UP;
2018-02-14 00:58:22 +01:00
}
2021-04-06 22:55:33 +02:00
// Identical to MovementEventModeCB_Normal
static u8 MovementEventModeCB_Scripted(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
2021-03-19 23:49:19 +01:00
return gLinkPlayerFacingHandlers[dir](linkPlayerObjEvent, objEvent, dir);
2018-02-14 00:58:22 +01:00
}
2021-03-19 23:49:19 +01:00
static bool8 FacingHandler_DoNothing(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
return FALSE;
2018-02-14 00:58:22 +01:00
}
2021-03-19 23:49:19 +01:00
static bool8 FacingHandler_DpadMovement(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
s16 x, y;
2021-03-19 23:49:19 +01:00
linkDirection(objEvent) = FlipVerticalAndClearForced(dir, linkDirection(objEvent));
ObjectEventMoveDestCoords(objEvent, linkDirection(objEvent), &x, &y);
2018-02-14 00:58:22 +01:00
if (LinkPlayerDetectCollision(linkPlayerObjEvent->objEventId, linkDirection(objEvent), x, y))
2018-02-14 00:58:22 +01:00
{
return FALSE;
2018-02-14 00:58:22 +01:00
}
else
{
objEvent->directionSequenceIndex = 16;
ShiftObjectEventCoords(objEvent, x, y);
ObjectEventUpdateZCoord(objEvent);
return TRUE;
2018-02-14 00:58:22 +01:00
}
}
2021-03-19 23:49:19 +01:00
static bool8 FacingHandler_ForcedFacingChange(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent, u8 dir)
2018-02-14 00:58:22 +01:00
{
2021-03-19 23:49:19 +01:00
linkDirection(objEvent) = FlipVerticalAndClearForced(dir, linkDirection(objEvent));
return FALSE;
2018-02-14 00:58:22 +01:00
}
// This is called every time a free movement happens. Most of the time it's a No-Op.
2019-11-21 06:14:40 +01:00
static void MovementStatusHandler_EnterFreeMode(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent)
2018-02-14 00:58:22 +01:00
{
2019-11-21 06:14:40 +01:00
linkPlayerObjEvent->movementMode = MOVEMENT_MODE_FREE;
2018-02-14 00:58:22 +01:00
}
2019-11-21 06:14:40 +01:00
static void MovementStatusHandler_TryAdvanceScript(struct LinkPlayerObjectEvent *linkPlayerObjEvent, struct ObjectEvent *objEvent)
2018-02-14 00:58:22 +01:00
{
objEvent->directionSequenceIndex--;
2019-11-21 06:14:40 +01:00
linkPlayerObjEvent->movementMode = MOVEMENT_MODE_FROZEN;
MoveCoords(linkDirection(objEvent), &objEvent->initialCoords.x, &objEvent->initialCoords.y);
if (!objEvent->directionSequenceIndex)
2018-02-14 00:58:22 +01:00
{
ShiftStillObjectEventCoords(objEvent);
2019-11-21 06:14:40 +01:00
linkPlayerObjEvent->movementMode = MOVEMENT_MODE_SCRIPTED;
2018-02-14 00:58:22 +01:00
}
}
// Flip Up/Down facing codes. If newFacing doesn't specify a direction, default
// to oldFacing. Note that this clears also the "FORCED" part of the facing code,
// even for Left/Right codes.
static u8 FlipVerticalAndClearForced(u8 newFacing, u8 oldFacing)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
switch (newFacing)
2018-02-14 00:58:22 +01:00
{
2019-02-18 07:03:44 +01:00
case FACING_UP:
case FACING_FORCED_UP:
return DIR_NORTH;
2019-02-18 07:03:44 +01:00
case FACING_DOWN:
case FACING_FORCED_DOWN:
return DIR_SOUTH;
2019-02-18 07:03:44 +01:00
case FACING_LEFT:
case FACING_FORCED_LEFT:
return DIR_WEST;
2019-02-18 07:03:44 +01:00
case FACING_RIGHT:
case FACING_FORCED_RIGHT:
return DIR_EAST;
2018-02-14 00:58:22 +01:00
}
return oldFacing;
2018-02-14 00:58:22 +01:00
}
static bool8 LinkPlayerDetectCollision(u8 selfObjEventId, u8 direction, s16 x, s16 y)
2018-02-14 00:58:22 +01:00
{
u8 i;
for (i = 0; i < OBJECT_EVENTS_COUNT; i++)
2018-02-14 00:58:22 +01:00
{
2019-11-21 06:14:40 +01:00
if (i != selfObjEventId)
2018-02-14 00:58:22 +01:00
{
if ((gObjectEvents[i].currentCoords.x == x && gObjectEvents[i].currentCoords.y == y)
|| (gObjectEvents[i].previousCoords.x == x && gObjectEvents[i].previousCoords.y == y))
2018-02-14 00:58:22 +01:00
{
return TRUE;
2018-02-14 00:58:22 +01:00
}
}
}
return MapGridIsImpassableAt(x, y);
}
static void CreateLinkPlayerSprite(u8 linkPlayerId, u8 gameVersion)
{
2019-11-21 06:14:40 +01:00
struct LinkPlayerObjectEvent *linkPlayerObjEvent = &gLinkPlayerObjectEvents[linkPlayerId];
u8 objEventId = linkPlayerObjEvent->objEventId;
struct ObjectEvent *objEvent = &gObjectEvents[objEventId];
2018-02-14 00:58:22 +01:00
struct Sprite *sprite;
2019-11-21 06:14:40 +01:00
if (linkPlayerObjEvent->active)
2018-02-14 00:58:22 +01:00
{
switch (gameVersion)
{
case VERSION_FIRE_RED:
case VERSION_LEAF_GREEN:
objEvent->spriteId = CreateObjectGraphicsSprite(GetFRLGAvatarGraphicsIdByGender(linkGender(objEvent)), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
case VERSION_RUBY:
case VERSION_SAPPHIRE:
objEvent->spriteId = CreateObjectGraphicsSprite(GetRSAvatarGraphicsIdByGender(linkGender(objEvent)), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
case VERSION_EMERALD:
objEvent->spriteId = CreateObjectGraphicsSprite(GetRivalAvatarGraphicsIdByStateIdAndGender(PLAYER_AVATAR_STATE_NORMAL, linkGender(objEvent)), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
}
sprite = &gSprites[objEvent->spriteId];
2018-02-14 00:58:22 +01:00
sprite->coordOffsetEnabled = TRUE;
sprite->data[0] = linkPlayerId;
objEvent->triggerGroundEffectsOnMove = 0;
2018-02-14 00:58:22 +01:00
}
}
static void SpriteCB_LinkPlayer(struct Sprite *sprite)
{
2019-11-21 06:14:40 +01:00
struct LinkPlayerObjectEvent *linkPlayerObjEvent = &gLinkPlayerObjectEvents[sprite->data[0]];
struct ObjectEvent *objEvent = &gObjectEvents[linkPlayerObjEvent->objEventId];
2021-07-07 15:11:52 +02:00
sprite->x = objEvent->initialCoords.x;
sprite->y = objEvent->initialCoords.y;
SetObjectSubpriorityByZCoord(objEvent->previousElevation, sprite, 1);
sprite->oam.priority = ZCoordToPriority(objEvent->previousElevation);
2018-02-14 00:58:22 +01:00
if (linkPlayerObjEvent->movementMode == MOVEMENT_MODE_FREE)
StartSpriteAnim(sprite, GetFaceDirectionAnimNum(linkDirection(objEvent)));
2018-02-14 00:58:22 +01:00
else
StartSpriteAnimIfDifferent(sprite, GetMoveDirectionAnimNum(linkDirection(objEvent)));
2018-02-14 00:58:22 +01:00
2021-04-04 23:52:07 +02:00
UpdateObjectEventSpriteInvisibility(sprite, 0);
if (objEvent->triggerGroundEffectsOnMove)
2018-02-14 00:58:22 +01:00
{
sprite->invisible = ((sprite->data[7] & 4) >> 2);
sprite->data[7]++;
}
}