pokeemerald/src/overworld.c

3221 lines
89 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 "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 "event_object_movement.h"
2018-02-12 15:01:43 +01:00
#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 "link.h"
#include "link_rfu.h"
2018-02-12 15:01:43 +01:00
#include "load_save.h"
#include "main.h"
#include "alloc.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"
2019-02-08 18:48:51 +01:00
#include "script_pokemon_util_80F87D8.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"
#include "constants/maps.h"
2018-12-27 23:30:47 +01:00
#include "constants/region_map_sections.h"
#include "constants/songs.h"
#include "constants/species.h"
2018-12-27 23:30:47 +01:00
#include "constants/weather.h"
2018-02-12 15:01:43 +01:00
#define PLAYER_TRADING_STATE_IDLE 0x80
#define PLAYER_TRADING_STATE_BUSY 0x81
#define PLAYER_TRADING_STATE_UNK_2 0x82
#define PLAYER_TRADING_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-02-12 15:01:43 +01:00
// event scripts
extern const u8 gEventScript_WhiteOut[];
extern const u8 gEventScript_ResetMrBriney[];
extern const u8 gEventScript_DoLinkRoomExit[];
extern const u8 gEventScript_TradeRoom_TooBusyToNotice[];
extern const u8 gEventScript_TradeRoom_ReadTrainerCard_NoColor[];
extern const u8 gEventScript_TradeRoom_ReadTrainerCard_Normal[];
extern const u8 gEventScript_DoubleBattleColosseum_PlayerSpot0[];
extern const u8 gEventScript_DoubleBattleColosseum_PlayerSpot1[];
extern const u8 gEventScript_DoubleBattleColosseum_PlayerSpot2[];
extern const u8 gEventScript_DoubleBattleColosseum_PlayerSpot3[];
extern const u8 gEventScript_RecordCenter_Spot0[];
extern const u8 gEventScript_RecordCenter_Spot1[];
extern const u8 gEventScript_RecordCenter_Spot2[];
extern const u8 gEventScript_RecordCenter_Spot3[];
extern const u8 gEventScript_SingleBattleColosseum_PlayerSpot0[];
extern const u8 gEventScript_SingleBattleColosseum_PlayerSpot1[];
extern const u8 gEventScript_TradeCenter_Chair1[];
extern const u8 gEventScript_TradeCenter_Chair0[];
extern const u8 gEventScript_ConfirmLeaveTradeRoom[];
extern const u8 gEventScript_TerminateLink[];
2018-02-12 15:01:43 +01:00
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[];
2019-02-08 18:48:51 +01:00
extern const int gMaxFlashLevel;
extern const u16 gOverworldBackgroundLayerFlags[];
2018-02-14 00:58:22 +01:00
2018-02-12 15:01:43 +01:00
static void Overworld_ResetStateAfterWhiteOut(void);
2018-02-14 00:58:22 +01:00
static void c2_80567AC(void);
static void CB2_LoadMap2(void);
static void VBlankCB_Field(void);
static void SpriteCB_LinkPlayer(struct Sprite *sprite);
static void ChooseAmbientCrySpecies(void);
static void do_load_map_stuff_loop(u8 *state);
static bool32 map_loading_iteration_3(u8 *state);
static bool32 sub_8086638(u8 *state);
static bool32 load_map_stuff(u8 *state, u32);
static bool32 map_loading_iteration_2_link(u8 *state);
static void mli4_mapscripts_and_other(void);
static void InitOverworldGraphicsRegisters(void);
static u8 GetSpriteForLinkedPlayer(u8);
static u16 KeyInterCB_SendNothing(u32 a1);
2018-02-14 00:58:22 +01:00
static void sub_80867C8(void);
static void sub_80867D8(void);
static void sub_8086AE4(void);
static void sub_80869DC(void);
static void sub_8086B14(void);
static void SetCameraToTrackGuestPlayer(void);
2018-02-14 00:58:22 +01:00
static void sub_8086988(bool32 arg0);
static void sub_8086A80(void);
static void sub_8086A68(void);
static void sub_8086860(void);
static void SetCameraToTrackGuestPlayer_2(void);
static void CreateLinkPlayerSprites(void);
static void ClearAllPlayerKeys(void);
static void ResetAllTradingStates(void);
static void UpdateHeldKeyCode(u16);
static void UpdateAllLinkPlayers(u16*, s32);
static u8 FlipVerticalAndClearForced(u8 a1, u8 a2);
static u8 LinkPlayerDetectCollision(u8 selfEventObjId, u8 a2, s16 x, s16 y);
2018-02-14 00:58:22 +01:00
static void CreateLinkPlayerSprite(u8 linkPlayerId, u8 gameVersion);
static void GetLinkPlayerCoords(u8 linkPlayerId, u16 *x, u16 *y);
static u8 GetLinkPlayerFacingDirection(u8 linkPlayerId);
static u8 GetLinkPlayerElevation(u8 linkPlayerId);
2018-02-14 00:58:22 +01:00
static s32 sub_80878E4(u8 linkPlayerId);
static u8 GetLinkPlayerIdAt(s16 x, s16 y);
static void SetPlayerFacingDirection(u8 linkPlayerId, u8 a2);
static void ZeroEventObject(struct EventObject *eventObj);
static void SpawnLinkPlayerEventObject(u8 linkPlayerId, s16 x, s16 y, u8 a4);
static void InitLinkPlayerEventObjectPos(struct EventObject *eventObj, s16 x, s16 y);
2018-02-14 00:58:22 +01:00
static void sub_80877DC(u8 linkPlayerId, u8 a2);
static void sub_808780C(u8 linkPlayerId);
static u8 GetSpriteForLinkedPlayer(u8 linkPlayerId);
2018-02-14 00:58:22 +01:00
static void sub_8087584(void);
static u32 GetLinkSendQueueLength(void);
static void ZeroLinkPlayerEventObject(struct LinkPlayerEventObject *linkPlayerEventObj);
static const u8 *TryInteractWithPlayer(struct TradeRoomPlayer *a1);
static u16 GetDirectionForEventScript(const u8 *script);
2018-02-14 00:58:22 +01:00
static void sub_8087510(void);
static void InitLinkRoomStartMenuScript(void);
2018-02-14 00:58:22 +01:00
static void sub_8087530(const u8 *script);
static void CreateConfirmLeaveTradeRoomPrompt(void);
static void InitMenuBasedScript(const u8 *script);
static void LoadTradeRoomPlayer(s32 linkPlayerId, s32 a2, struct TradeRoomPlayer *a3);
static bool32 sub_8087340(struct TradeRoomPlayer *a1);
static bool32 sub_8087340_2(struct TradeRoomPlayer *a1);
static u8 *TryGetTileEventScript(struct TradeRoomPlayer *a1);
static bool32 PlayerIsAtSouthExit(struct TradeRoomPlayer *a1);
static const u8 *TryInteractWithPlayer(struct TradeRoomPlayer *a1);
static u16 KeyInterCB_DeferToRecvQueue(u32);
static u16 KeyInterCB_DeferToSendQueue(u32);
static void ResetPlayerHeldKeys(u16 *a1);
static u16 KeyInterCB_SelfIdle(u32 a1);
static u16 KeyInterCB_DeferToEventScript(u32 a1);
static u16 GetDirectionForDpadKey(u16 a1);
static void CB1_UpdateLinkState(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 sub_8085810(void);
static u8 GetAdjustedInitialTransitionFlags(struct InitialPlayerAvatarState *playerStruct, u16 a2, u8 a3);
static u8 GetAdjustedInitialDirection(struct InitialPlayerAvatarState *playerStruct, u8 a2, u16 a3, u8 a4);
static u16 GetCenterScreenMetatileBehavior(void);
2017-09-30 19:08:28 +02:00
2018-02-12 15:01:43 +01:00
// IWRAM bss vars
IWRAM_DATA static void *sUnusedCallback;
IWRAM_DATA static u8 sPlayerTradingStates[4];
// 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.
IWRAM_DATA static u16 (*sPlayerKeyInterceptCallback)(u32);
IWRAM_DATA static bool8 sUnknown_03000E18;
IWRAM_DATA static u8 sRfuKeepAliveTimer;
2018-02-14 00:58:22 +01:00
IWRAM_DATA static u32 sUnusedVar;
2017-09-30 19:08:28 +02:00
2018-11-19 01:03:14 +01:00
// IWRAM common
u16 *gBGTilemapBuffers1;
u16 *gBGTilemapBuffers2;
u16 *gBGTilemapBuffers3;
u16 gHeldKeyCodeToSend;
2018-11-19 01:03:14 +01:00
void (*gFieldCallback)(void);
bool8 (*gFieldCallback2)(void);
u8 gLinkSelfPlayerId; // This is our player id in a multiplayer mode.
2018-11-19 01:03:14 +01:00
u8 gFieldLinkPlayerCount;
2018-02-12 15:01:43 +01:00
// EWRAM vars
2018-02-14 00:58:22 +01:00
EWRAM_DATA static u8 sUnknown_020322D8 = 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 gFixedDiveWarp = {0};
EWRAM_DATA static struct WarpData gFixedHoleWarp = {0};
2018-02-14 00:58:22 +01:00
EWRAM_DATA static u16 sLastMapSectionId = 0;
EWRAM_DATA static struct InitialPlayerAvatarState gInitialPlayerAvatarState = {0};
2018-02-14 00:58:22 +01:00
EWRAM_DATA static u16 sAmbientCrySpecies = 0;
EWRAM_DATA static bool8 sIsAmbientCryWaterMon = FALSE;
EWRAM_DATA struct LinkPlayerEventObject gLinkPlayerEventObjects[4] = {0};
2018-02-12 15:01:43 +01:00
// const rom data
2018-02-14 00:58:22 +01:00
static const struct WarpData sDummyWarpData =
2018-02-12 15:01:43 +01:00
{
.mapGroup = -1,
.mapNum = -1,
.warpId = -1,
.x = -1,
.y = -1,
};
2018-02-14 00:58:22 +01:00
static const u8 sUnusedData[] =
2018-02-12 15:01:43 +01:00
{
0xB0, 0x04, 0x00, 0x00,
0x10, 0x0E, 0x00, 0x00,
0xB0, 0x04, 0x00, 0x00,
0x60, 0x09, 0x00, 0x00,
0x32, 0x00, 0x00, 0x00,
0x50, 0x00, 0x00, 0x00,
0xD4, 0xFF, 0xFF, 0xFF,
0x2C, 0x00, 0x00, 0x00,
};
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
{
(void *)REG_ADDR_WIN0H,
((DMA_ENABLE | DMA_START_HBLANK | DMA_REPEAT | DMA_DEST_RELOAD) << 16) | 1,
1,
0,
};
static u8 MovementEventModeCB_Normal(struct LinkPlayerEventObject *, struct EventObject *, u8);
static u8 MovementEventModeCB_Ignored(struct LinkPlayerEventObject *, struct EventObject *, u8);
static u8 MovementEventModeCB_Normal_2(struct LinkPlayerEventObject *, struct EventObject *, u8);
2018-02-12 15:01:43 +01:00
static u8 (*const gLinkPlayerMovementModes[])(struct LinkPlayerEventObject *, struct EventObject *, u8) =
2018-02-12 15:01:43 +01:00
{
MovementEventModeCB_Normal, // MOVEMENT_MODE_FREE
MovementEventModeCB_Ignored, // MOVEMENT_MODE_FROZEN
MovementEventModeCB_Normal_2, // MOVEMENT_MODE_SCRIPTED
2018-02-12 15:01:43 +01:00
};
static u8 FacingHandler_DoNothing(struct LinkPlayerEventObject *, struct EventObject *, u8);
static u8 FacingHandler_DpadMovement(struct LinkPlayerEventObject *, struct EventObject *, u8);
static u8 FacingHandler_ForcedFacingChange(struct LinkPlayerEventObject *, struct EventObject *, u8);
// These handlers return TRUE if the movement was scripted and successful, and FALSE otherwise.
static bool8 (*const gLinkPlayerFacingHandlers[])(struct LinkPlayerEventObject *, struct EventObject *, 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 LinkPlayerEventObject *, struct EventObject *);
static void MovementStatusHandler_TryAdvanceScript(struct LinkPlayerEventObject *, struct EventObject *);
2018-02-12 15:01:43 +01:00
// These handlers are run after an attempted movement.
static void (*const gMovementStatusHandler[])(struct LinkPlayerEventObject *, struct EventObject *) =
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)
{
ScriptContext2_RunNewScript(gEventScript_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);
ScriptContext2_RunNewScript(gEventScript_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);
if (VarGet(VAR_0x4039) == 1)
{
VarSet(VAR_0x4039, 0);
VarSet(VAR_0x4037, 0);
}
}
2018-02-14 00:58:22 +01:00
static void sub_8084788(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);
}
void LoadEventObjTemplatesFromHeader(void)
2018-02-12 15:01:43 +01:00
{
// Clear map object templates
CpuFill32(0, gSaveBlock1Ptr->eventObjectTemplates, sizeof(gSaveBlock1Ptr->eventObjectTemplates));
2018-02-12 15:01:43 +01:00
// Copy map header events to save block
CpuCopy32(gMapHeader.events->eventObjects,
gSaveBlock1Ptr->eventObjectTemplates,
gMapHeader.events->eventObjectCount * sizeof(struct EventObjectTemplate));
2018-02-12 15:01:43 +01:00
}
void LoadSaveblockEventObjScripts(void)
2018-02-12 15:01:43 +01:00
{
struct EventObjectTemplate *mapHeaderObjTemplates = gMapHeader.events->eventObjects;
struct EventObjectTemplate *savObjTemplates = gSaveBlock1Ptr->eventObjectTemplates;
2018-02-12 15:01:43 +01:00
s32 i;
for (i = 0; i < EVENT_OBJECT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
savObjTemplates[i].script = mapHeaderObjTemplates[i].script;
}
void Overworld_SetEventObjTemplateCoords(u8 localId, s16 x, s16 y)
2018-02-12 15:01:43 +01:00
{
s32 i;
struct EventObjectTemplate *savObjTemplates = gSaveBlock1Ptr->eventObjectTemplates;
2018-02-12 15:01:43 +01:00
for (i = 0; i < EVENT_OBJECT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
{
struct EventObjectTemplate *eventObjectTemplate = &savObjTemplates[i];
if (eventObjectTemplate->localId == localId)
2018-02-12 15:01:43 +01:00
{
eventObjectTemplate->x = x;
eventObjectTemplate->y = y;
2018-02-12 15:01:43 +01:00
return;
}
}
}
void Overworld_SetEventObjTemplateMovementType(u8 localId, u8 movementType)
2018-02-12 15:01:43 +01:00
{
s32 i;
struct EventObjectTemplate *savObjTemplates = gSaveBlock1Ptr->eventObjectTemplates;
for (i = 0; i < EVENT_OBJECT_TEMPLATES_COUNT; i++)
2018-02-12 15:01:43 +01:00
{
struct EventObjectTemplate *eventObjectTemplate = &savObjTemplates[i];
if (eventObjectTemplate->localId == localId)
2018-02-12 15:01:43 +01:00
{
eventObjectTemplate->movementType = movementType;
2018-02-12 15:01:43 +01:00
return;
}
}
}
2018-02-14 00:58:22 +01:00
static void mapdata_load_assets_to_gpu_and_full_redraw(void)
2018-02-12 15:01:43 +01:00
{
move_tilemap_camera_to_upper_left_corner();
2018-06-21 00:41:51 +02:00
copy_map_tileset1_tileset2_to_vram(gMapHeader.mapLayout);
apply_map_tileset1_tileset2_palette(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;
gFixedDiveWarp = sDummyWarpData;
gFixedHoleWarp = 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
{
gFixedDiveWarp = sDummyWarpData;
gFixedHoleWarp = 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
{
if (warp->mapGroup != -1)
return FALSE;
2018-02-15 23:09:52 +01:00
else if (warp->mapNum != -1)
2018-02-12 15:01:43 +01:00
return FALSE;
2018-02-15 23:09:52 +01:00
else if (warp->warpId != -1)
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)
{
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)
{
gSaveBlock1Ptr->pos.x = gSaveBlock1Ptr->location.x;
gSaveBlock1Ptr->pos.y = gSaveBlock1Ptr->location.y;
}
else
{
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)
2018-12-27 23:30:47 +01:00
SetWarpDestination(warp->group, warp->map, -1, 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)
2018-02-12 15:01:43 +01:00
SetWarpData(&gSaveBlock1Ptr->lastHealLocation, healLocation->group, healLocation->map, -1, healLocation->x, healLocation->y);
}
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);
if (is_map_type_1_2_3_5_or_6(currMapType) && is_map_type_1_2_3_5_or_6(destMapType) != TRUE)
2018-12-27 23:30:47 +01:00
SetEscapeWarp(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, -1, x - 7, y - 6);
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(&gFixedDiveWarp, 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 = gFixedDiveWarp;
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(&gFixedHoleWarp, 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(&gFixedHoleWarp) == TRUE)
sWarpDestination = gLastUsedWarp;
2018-02-12 18:26:26 +01:00
else
2018-12-27 23:30:47 +01:00
SetWarpDestination(gFixedHoleWarp.mapGroup, gFixedHoleWarp.mapNum, -1, 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)
2018-12-27 23:30:47 +01:00
SetWarpData(&gSaveBlock1Ptr->continueGameWarp, warp->group, warp->map, -1, 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)
{
2018-12-27 23:30:47 +01:00
SetWarpDestination(connection->mapGroup, connection->mapNum, -1, x, y);
2018-02-12 18:26:26 +01:00
}
else
{
mapheader_run_script_with_tag_x6();
if (IsDummyWarp(&gFixedDiveWarp))
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 mliX_load_map(u8 mapGroup, u8 mapNum)
{
s32 paletteIndex;
2018-12-27 23:30:47 +01:00
SetWarpDestination(mapGroup, mapNum, -1, -1, -1);
2018-02-12 18:26:26 +01:00
if (gMapHeader.regionMapSectionId != 0x3A)
sub_8085810();
ApplyCurrentWarp();
LoadCurrentMapData();
LoadEventObjTemplatesFromHeader();
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();
SetSav1WeatherFromCurrMapHeader();
ChooseAmbientCrySpecies();
SetDefaultFlashLevel();
Overworld_ClearSavedMusic();
mapheader_run_script_with_tag_x3();
2018-12-28 18:18:23 +01:00
InitMap();
2018-06-21 00:41:51 +02:00
copy_map_tileset2_to_vram_2(gMapHeader.mapLayout);
apply_map_tileset2_palette(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();
mapheader_run_script_with_tag_x5();
2018-12-27 23:30:47 +01:00
if (gMapHeader.regionMapSectionId != MAPSEC_BATTLE_FRONTIER || gMapHeader.regionMapSectionId != sLastMapSectionId)
2018-02-12 18:26:26 +01:00
ShowMapNamePopup();
}
2018-02-14 00:58:22 +01:00
static void mli0_load_map(u32 a1)
2018-02-12 18:26:26 +01:00
{
bool8 v2;
bool8 indoors;
LoadCurrentMapData();
2018-02-14 00:58:22 +01:00
if (!(sUnknown_020322D8 & 1))
2018-02-12 18:26:26 +01:00
{
2019-01-31 22:51:20 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_EMPTY_SQUARE)
2019-02-07 18:37:28 +01:00
LoadBattlePyramidEventObjectTemplates();
2018-02-12 18:26:26 +01:00
else if (InTrainerHill())
sub_81D5DF8();
else
LoadEventObjTemplatesFromHeader();
2018-02-12 18:26:26 +01:00
}
v2 = is_map_type_1_2_3_5_or_6(gMapHeader.mapType);
2018-02-12 18:26:26 +01:00
indoors = Overworld_MapTypeIsIndoors(gMapHeader.mapType);
sub_80EB218();
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 != 1)
DoTimeBasedEvents();
SetSav1WeatherFromCurrMapHeader();
ChooseAmbientCrySpecies();
if (v2)
FlagClear(FLAG_SYS_USE_FLASH);
SetDefaultFlashLevel();
Overworld_ClearSavedMusic();
mapheader_run_script_with_tag_x3();
UpdateLocationHistoryForRoamer();
RoamerMoveToOtherLocationSet();
2019-01-31 22:51:20 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_EMPTY_SQUARE)
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 != 1 && indoors)
{
2018-12-28 18:18:23 +01:00
UpdateTVScreensOnMap(gBackupMapLayout.width, gBackupMapLayout.height);
2018-02-12 18:26:26 +01:00
sub_80E9238(1);
}
}
void ResetInitialPlayerAvatarState(void)
2018-02-12 18:26:26 +01:00
{
2018-10-17 02:11:44 +02:00
gInitialPlayerAvatarState.direction = DIR_SOUTH;
gInitialPlayerAvatarState.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
{
gInitialPlayerAvatarState.direction = GetPlayerFacingDirection();
2018-02-12 18:26:26 +01:00
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_MACH_BIKE))
2018-10-17 02:11:44 +02:00
gInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_MACH_BIKE;
2018-02-12 18:26:26 +01:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_ACRO_BIKE))
2018-10-17 02:11:44 +02:00
gInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_ACRO_BIKE;
2018-02-12 18:26:26 +01:00
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
2018-10-17 02:11:44 +02:00
gInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_SURFING;
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_UNDERWATER))
gInitialPlayerAvatarState.transitionFlags = PLAYER_AVATAR_FLAG_UNDERWATER;
2018-02-12 18:26:26 +01:00
else
2018-10-17 02:11:44 +02:00
gInitialPlayerAvatarState.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(&gInitialPlayerAvatarState, metatileBehavior, mapType);
playerStruct.transitionFlags = transitionFlags;
playerStruct.direction = GetAdjustedInitialDirection(&gInitialPlayerAvatarState, transitionFlags, metatileBehavior, mapType);
gInitialPlayerAvatarState = playerStruct;
return &gInitialPlayerAvatarState;
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
{
if (FlagGet(FLAG_SYS_CRUISE_MODE) && mapType == MAP_TYPE_6)
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
{
return MapGridGetMetatileBehaviorAt(gSaveBlock1Ptr->pos.x + 7, gSaveBlock1Ptr->pos.y + 7);
}
bool32 Overworld_IsBikingAllowed(void)
{
if (!(gMapHeader.flags & 1))
return FALSE;
else
return TRUE;
}
void SetDefaultFlashLevel(void)
{
if (!gMapHeader.cave)
gSaveBlock1Ptr->flashLevel = 0;
else if (FlagGet(FLAG_SYS_USE_FLASH))
gSaveBlock1Ptr->flashLevel = 1;
else
gSaveBlock1Ptr->flashLevel = gMaxFlashLevel - 1;
}
void Overworld_SetFlashLevel(s32 flashLevel)
{
if (flashLevel < 0 || flashLevel > gMaxFlashLevel)
flashLevel = 0;
gSaveBlock1Ptr->flashLevel = flashLevel;
}
u8 Overworld_GetFlashLevel(void)
{
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 sub_8085540(u8 var)
{
2018-02-14 00:58:22 +01:00
sUnknown_020322D8 = var;
2018-02-12 18:26:26 +01:00
}
u8 sub_808554C(void)
{
2018-02-14 00:58:22 +01:00
return sUnknown_020322D8;
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:
if (VarGet(VAR_0x405E) < 4)
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_0x40CA) != 1)
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
{
if (VarGet(VAR_0x405D) == 0)
return FALSE;
2018-02-15 12:36:52 +01:00
else if (VarGet(VAR_0x405D) > 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)
return 0xFFFF;
2018-02-12 18:26:26 +01:00
else if (ShouldLegendaryMusicPlayAtLocation(warp) == TRUE)
return MUS_OOAME;
else if (IsInflitratedSpaceCenter(warp) == TRUE)
return MUS_MGM0;
else if (IsInfiltratedWeatherInstitute(warp) == TRUE)
return MUS_TOZAN;
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)
2018-12-27 23:30:47 +01:00
&& GetSav1Weather() == WEATHER_SANDSTORM)
2018-02-12 18:26:26 +01:00
return MUS_ASHROAD;
music = GetLocationMusic(&gSaveBlock1Ptr->location);
2018-12-27 23:30:47 +01:00
if (music != MUS_ROUTE_118)
2018-02-12 18:26:26 +01:00
{
return music;
}
else
{
if (gSaveBlock1Ptr->pos.x < LINK_KEY_CODE_START_BUTTON)
2018-02-12 18:26:26 +01:00
return MUS_DOORO_X1;
else
return MUS_GRANROAD;
}
}
u16 GetWarpDestinationMusic(void)
{
2018-02-14 00:58:22 +01:00
u16 music = GetLocationMusic(&sWarpDestination);
2018-12-27 23:30:47 +01:00
if (music != MUS_ROUTE_118)
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))
return MUS_DOORO_X1;
else
return MUS_GRANROAD;
}
}
void Overworld_ResetMapMusic(void)
{
ResetMapMusic();
}
void Overworld_PlaySpecialMapMusic(void)
{
u16 music = GetCurrLocationDefaultMusic();
if (music != MUS_OOAME && music != 0xFFFF)
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)
2018-02-12 18:26:26 +01:00
music = MUS_DEEPDEEP;
else if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
music = MUS_NAMINORI;
}
if (music != GetCurrentMapMusic())
PlayNewMapMusic(music);
}
void Overworld_SetSavedMusic(u16 songNum)
{
gSaveBlock1Ptr->savedMusic = songNum;
}
void Overworld_ClearSavedMusic(void)
{
gSaveBlock1Ptr->savedMusic = 0;
}
2018-02-14 00:58:22 +01:00
static void sub_8085810(void)
2018-02-12 18:26:26 +01:00
{
if (FlagGet(FLAG_SPECIAL_FLAG_0x4001) != TRUE)
{
u16 newMusic = GetWarpDestinationMusic();
u16 currentMusic = GetCurrentMapMusic();
if (newMusic != MUS_OOAME && newMusic != 0xFFFF)
2018-02-12 18:26:26 +01:00
{
if (currentMusic == MUS_DEEPDEEP || currentMusic == MUS_NAMINORI)
return;
if (TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_SURFING))
newMusic = MUS_NAMINORI;
}
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();
if (currentMusic != newMusic && currentMusic != MUS_OOAME)
FadeOutAndPlayNewMapMusic(newMusic, 8);
}
u8 GetMapMusicFadeoutSpeed(void)
{
const struct MapHeader *mapHeader = GetDestinationWarpMapHeader();
2018-02-12 18:26:26 +01:00
if (Overworld_MapTypeIsIndoors(mapHeader->mapType) == TRUE)
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_SPECIAL_FLAG_0x4001) != TRUE && warpMusic != GetCurrentMapMusic())
{
if (currentMusic == MUS_NAMINORI
&& VarGet(VAR_0x40CA) == 2
&& 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;
volume = (Random() % LINK_KEY_CODE_UNK_8) + 50;
2018-02-14 00:58:22 +01:00
PlayCry2(sAmbientCrySpecies, pan, volume, 1);
}
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
}
bool8 is_map_type_1_2_3_5_or_6(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
|| mapType == MAP_TYPE_6)
return TRUE;
else
return FALSE;
}
bool8 Overworld_MapTypeAllowsTeleportAndFly(u8 mapType)
{
if (mapType == MAP_TYPE_ROUTE
|| mapType == MAP_TYPE_TOWN
|| mapType == MAP_TYPE_6
|| mapType == MAP_TYPE_CITY)
return TRUE;
else
return FALSE;
}
bool8 Overworld_MapTypeIsIndoors(u8 mapType)
{
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 IsUpdateLinkStateCBActive(void)
2018-02-14 00:58:22 +01:00
{
if (gMain.callback1 == CB1_UpdateLinkState)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
static void DoCB1_Overworld(u16 newKeys, u16 heldKeys)
{
struct FieldInput inputStruct;
sub_808B578();
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
{
player_step(inputStruct.dpadDirection, newKeys, heldKeys);
}
}
}
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();
2018-02-14 00:58:22 +01:00
do_scheduled_bg_tilemap_copies_to_vram();
}
// 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
{
sUnusedCallback = func;
2018-02-14 00:58:22 +01:00
}
static bool8 map_post_load_hook_exec(void)
{
2018-06-16 00:45:48 +02:00
if (gFieldCallback2 != NULL)
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
{
if (gFieldCallback != NULL)
gFieldCallback();
else
mapldr_default();
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;
2018-02-14 00:58:22 +01:00
do_load_map_stuff_loop(&gMain.state);
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
void CB2_WhiteOut(void)
{
u8 val;
if (++gMain.state >= 120)
{
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
DoWhiteOut();
ResetInitialPlayerAvatarState();
2018-02-14 00:58:22 +01:00
ScriptContext1_Init();
ScriptContext2_Disable();
gFieldCallback = sub_80AF3C8;
val = 0;
do_load_map_stuff_loop(&val);
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
}
void CB2_LoadMap(void)
{
FieldClearVBlankHBlankCallbacks();
ScriptContext1_Init();
ScriptContext2_Disable();
SetMainCallback1(NULL);
SetMainCallback2(c2_change_map);
gMain.savedCallback = CB2_LoadMap2;
}
static void CB2_LoadMap2(void)
{
do_load_map_stuff_loop(&gMain.state);
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
void sub_8086024(void)
{
if (!gMain.state)
{
FieldClearVBlankHBlankCallbacks();
ScriptContext1_Init();
ScriptContext2_Disable();
SetMainCallback1(NULL);
}
if (load_map_stuff(&gMain.state, 1))
{
SetFieldVBlankCallback();
SetMainCallback1(CB1_Overworld);
SetMainCallback2(CB2_Overworld);
}
}
void sub_8086074(void)
{
FieldClearVBlankHBlankCallbacks();
gFieldCallback = sub_80AF314;
SetMainCallback2(c2_80567AC);
}
static void c2_80567AC(void)
{
if (map_loading_iteration_3(&gMain.state))
{
SetFieldVBlankCallback();
SetMainCallback1(CB1_UpdateLinkState);
ResetAllMultiplayerState();
2018-02-14 00:58:22 +01:00
SetMainCallback2(CB2_Overworld);
}
}
void CB2_ReturnToField(void)
{
if (IsUpdateLinkStateCBActive() == TRUE)
2018-02-14 00:58:22 +01:00
{
SetMainCallback2(CB2_ReturnToFieldLink);
}
else
{
FieldClearVBlankHBlankCallbacks();
SetMainCallback2(CB2_ReturnToFieldLocal);
}
}
void CB2_ReturnToFieldLocal(void)
{
if (sub_8086638(&gMain.state))
{
SetFieldVBlankCallback();
SetMainCallback2(CB2_Overworld);
}
}
void CB2_ReturnToFieldLink(void)
{
if (!sub_8087598() && map_loading_iteration_2_link(&gMain.state))
SetMainCallback2(CB2_Overworld);
}
void CB2_ReturnToFieldFromMultiplayer(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
SetMainCallback1(CB1_UpdateLinkState);
ResetAllMultiplayerState();
2018-02-14 00:58:22 +01:00
2018-02-15 12:36:52 +01:00
if (gWirelessCommType != 0)
2018-02-14 00:58:22 +01:00
gFieldCallback = sub_80AF314;
else
gFieldCallback = sub_80AF214;
ScriptContext1_Init();
ScriptContext2_Disable();
CB2_ReturnToField();
}
void CB2_ReturnToFieldWithOpenMenu(void)
{
FieldClearVBlankHBlankCallbacks();
2018-06-16 00:45:48 +02:00
gFieldCallback2 = sub_80AF6A4;
2018-02-14 00:58:22 +01:00
CB2_ReturnToField();
}
void CB2_ReturnToFieldContinueScript(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
gFieldCallback = sub_80AF188;
CB2_ReturnToField();
}
void CB2_ReturnToFieldContinueScriptPlayMapMusic(void)
2018-02-14 00:58:22 +01:00
{
FieldClearVBlankHBlankCallbacks();
gFieldCallback = sub_80AF168;
CB2_ReturnToField();
}
void sub_80861E8(void)
{
FieldClearVBlankHBlankCallbacks();
gFieldCallback = sub_80AF3C8;
CB2_ReturnToField();
}
static void sub_8086204(void)
{
if ((gMapHeader.flags & 0xF8) == 8 && sub_80E909C() == TRUE)
ShowMapNamePopup();
sub_80AF3C8();
}
void CB2_ContinueSavedGame(void)
{
u8 trainerHillMapId;
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
if (gSaveFileStatus == 0xFF)
sub_81A3908();
LoadSaveblockMapHeader();
2018-12-27 23:30:47 +01:00
ClearDiveAndHoleWarps();
2018-02-14 00:58:22 +01:00
trainerHillMapId = GetCurrentTrainerHillMapId();
2019-01-31 22:51:20 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_EMPTY_SQUARE)
2019-02-07 18:37:28 +01:00
LoadBattlePyramidFloorEventObjectScripts();
2018-02-14 00:58:22 +01:00
else if (trainerHillMapId != 0 && trainerHillMapId != 6)
sub_81D5F48();
else
LoadSaveblockEventObjScripts();
2018-02-14 00:58:22 +01:00
UnfreezeEventObjects();
2018-02-14 00:58:22 +01:00
DoTimeBasedEvents();
sub_8084788();
2019-01-31 22:51:20 +01:00
if (gMapHeader.mapLayoutId == LAYOUT_BATTLE_FRONTIER_BATTLE_PYRAMID_EMPTY_SQUARE)
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();
2018-02-14 00:58:22 +01:00
sub_80EDB44();
SetMainCallback2(CB2_LoadMap);
}
else
{
sub_80EDB44();
gFieldCallback = sub_8086204;
SetMainCallback1(CB1_Overworld);
CB2_ReturnToField();
}
}
static void FieldClearVBlankHBlankCallbacks(void)
{
if (warp0_in_pokecenter() == TRUE)
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
}
else if ((flashLevel = Overworld_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 map_loading_iteration_3(u8 *state)
{
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();
sub_80867C8();
sub_80867D8();
(*state)++;
break;
case 1:
mli0_load_map(1);
(*state)++;
break;
case 2:
sub_8086988(TRUE);
(*state)++;
break;
case 3:
sub_8086AE4();
sub_80869DC();
sub_8086B14();
SetCameraToTrackGuestPlayer();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 4:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
2018-02-14 00:58:22 +01:00
sub_8197200();
(*state)++;
break;
case 5:
move_tilemap_camera_to_upper_left_corner();
(*state)++;
break;
case 6:
2018-06-21 00:41:51 +02:00
copy_map_tileset1_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
2018-06-21 00:41:51 +02:00
copy_map_tileset2_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 8:
if (free_temp_tile_data_buffers_if_possible() != TRUE)
{
2018-06-21 00:41:51 +02:00
apply_map_tileset1_tileset2_palette(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)
{
sub_800E0E8();
CreateWirelessStatusIndicatorSprite(0, 0);
}
(*state)++;
break;
case 12:
if (map_post_load_hook_exec())
(*state)++;
break;
case 13:
return TRUE;
}
return FALSE;
}
static bool32 load_map_stuff(u8 *state, u32 a2)
{
switch (*state)
{
case 0:
FieldClearVBlankHBlankCallbacks();
mli0_load_map(a2);
(*state)++;
break;
case 1:
sub_80867C8();
sub_80867D8();
(*state)++;
break;
case 2:
sub_8086988(a2);
(*state)++;
break;
case 3:
mli4_mapscripts_and_other();
sub_8086A80();
(*state)++;
break;
case 4:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
2018-02-14 00:58:22 +01:00
sub_8197200();
(*state)++;
break;
case 5:
move_tilemap_camera_to_upper_left_corner();
(*state)++;
break;
case 6:
2018-06-21 00:41:51 +02:00
copy_map_tileset1_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
2018-06-21 00:41:51 +02:00
copy_map_tileset2_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 8:
if (free_temp_tile_data_buffers_if_possible() != TRUE)
{
2018-06-21 00:41:51 +02:00
apply_map_tileset1_tileset2_palette(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.flags & 0xF8) == 8 && sub_80E909C() == 1)
ShowMapNamePopup();
(*state)++;
break;
case 12:
if (map_post_load_hook_exec())
(*state)++;
break;
case 13:
return TRUE;
}
return FALSE;
}
static bool32 sub_8086638(u8 *state)
{
switch (*state)
{
case 0:
sub_80867C8();
sub_80867D8();
sub_8086988(0);
sub_8086A68();
sub_8086A80();
(*state)++;
break;
case 1:
sub_8086860();
sub_81D64C0();
(*state)++;
break;
case 2:
if (map_post_load_hook_exec())
(*state)++;
break;
case 3:
return TRUE;
}
return FALSE;
}
static bool32 map_loading_iteration_2_link(u8 *state)
{
switch (*state)
{
case 0:
FieldClearVBlankHBlankCallbacks();
sub_80867C8();
sub_80867D8();
(*state)++;
break;
case 1:
sub_8086988(1);
(*state)++;
break;
case 2:
CreateLinkPlayerSprites();
2018-02-14 00:58:22 +01:00
sub_8086A68();
SetCameraToTrackGuestPlayer_2();
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 3:
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
2018-02-14 00:58:22 +01:00
sub_8197200();
(*state)++;
break;
case 4:
move_tilemap_camera_to_upper_left_corner();
(*state)++;
break;
case 5:
2018-06-21 00:41:51 +02:00
copy_map_tileset1_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 6:
2018-06-21 00:41:51 +02:00
copy_map_tileset2_to_vram(gMapHeader.mapLayout);
2018-02-14 00:58:22 +01:00
(*state)++;
break;
case 7:
if (free_temp_tile_data_buffers_if_possible() != TRUE)
{
2018-06-21 00:41:51 +02:00
apply_map_tileset1_tileset2_palette(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)
{
sub_800E0E8();
CreateWirelessStatusIndicatorSprite(0, 0);
}
(*state)++;
break;
case 12:
if (map_post_load_hook_exec())
(*state)++;
break;
case 10:
(*state)++;
break;
case 13:
SetFieldVBlankCallback();
(*state)++;
return TRUE;
}
return FALSE;
}
static void do_load_map_stuff_loop(u8 *state)
{
while (!load_map_stuff(state, 0));
}
static void sub_80867C8(void)
{
ClearMirageTowerPulseBlend();
2018-02-14 00:58:22 +01:00
MoveSaveBlocks_ResetHeap();
}
static void sub_80867D8(void)
{
SetGpuReg(REG_OFFSET_DISPCNT, 0);
ScanlineEffect_Stop();
DmaClear16(3, PLTT + 2, PLTT_SIZE - 2);
DmaFillLarge16(3, 0, (void *)(VRAM + 0x0), 0x18000, 0x1000);
ResetOamRange(0, 128);
LoadOam();
}
static void sub_8086860(void)
{
InitCurrentFlashLevelScanlineEffect();
InitOverworldGraphicsRegisters();
2018-02-14 00:58:22 +01:00
sub_8197200();
mapdata_load_assets_to_gpu_and_full_redraw();
}
static void InitOverworldGraphicsRegisters(void)
2018-02-14 00:58:22 +01:00
{
clear_scheduled_bg_copies_to_vram();
reset_temp_tile_data_buffers();
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();
2018-02-14 00:58:22 +01:00
schedule_bg_copy_tilemap_to_vram(1);
schedule_bg_copy_tilemap_to_vram(2);
schedule_bg_copy_tilemap_to_vram(3);
ChangeBgX(0, 0, 0);
ChangeBgY(0, 0, 0);
ChangeBgX(1, 0, 0);
ChangeBgY(1, 0, 0);
ChangeBgX(2, 0, 0);
ChangeBgY(2, 0, 0);
ChangeBgX(3, 0, 0);
ChangeBgY(3, 0, 0);
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 sub_8086988(u32 a1)
{
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)
2018-09-03 00:20:45 +02:00
InitEventObjectPalettes(0);
2018-02-14 00:58:22 +01:00
else
2018-09-03 00:20:45 +02:00
InitEventObjectPalettes(1);
2018-02-14 00:58:22 +01:00
FieldEffectActiveListClear();
2018-12-08 19:05:03 +01:00
StartWeather();
2018-02-14 00:58:22 +01:00
sub_80AEE84();
if (!a1)
SetUpFieldTasks();
mapheader_run_script_with_tag_x5();
TryStartMirageTowerPulseBlendEffect();
2018-02-14 00:58:22 +01:00
}
static void sub_80869DC(void)
{
2018-10-16 15:55:16 +02:00
gTotalCameraPixelOffsetX = 0;
gTotalCameraPixelOffsetY = 0;
2018-10-17 02:11:44 +02:00
ResetEventObjects();
TrySpawnEventObjects(0, 0);
2018-02-14 00:58:22 +01:00
mapheader_run_first_tag4_script_list_match();
}
static void mli4_mapscripts_and_other(void)
{
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;
2018-10-17 02:11:44 +02:00
ResetEventObjects();
2018-12-28 18:18:23 +01:00
GetCameraFocusCoords(&x, &y);
player = GetInitialPlayerAvatarState();
InitPlayerAvatar(x, y, player->direction, gSaveBlock2Ptr->playerGender);
SetPlayerAvatarTransitionFlags(player->transitionFlags);
ResetInitialPlayerAvatarState();
TrySpawnEventObjects(0, 0);
2018-02-14 00:58:22 +01:00
mapheader_run_first_tag4_script_list_match();
}
static void sub_8086A68(void)
{
sub_808E16C(0, 0);
RotatingGate_InitPuzzleAndGraphics();
mapheader_run_script_with_tag_x7();
}
static void sub_8086A80(void)
{
gEventObjects[gPlayerAvatar.eventObjectId].trackedByCamera = 1;
2018-02-14 00:58:22 +01:00
InitCameraUpdateCallback(gPlayerAvatar.spriteId);
}
static void SetCameraToTrackGuestPlayer(void)
2018-02-14 00:58:22 +01:00
{
InitCameraUpdateCallback(GetSpriteForLinkedPlayer(gLinkSelfPlayerId));
2018-02-14 00:58:22 +01:00
}
// Duplicate function.
static void SetCameraToTrackGuestPlayer_2(void)
2018-02-14 00:58:22 +01:00
{
InitCameraUpdateCallback(GetSpriteForLinkedPlayer(gLinkSelfPlayerId));
2018-02-14 00:58:22 +01:00
}
static void sub_8086AE4(void)
{
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 sub_8086B14, which is called
// soon after this function.
sub_8088B3C(x + gLinkSelfPlayerId, y);
2018-02-14 00:58:22 +01:00
}
static void sub_8086B14(void)
{
u16 i;
u16 x, y;
2018-12-28 18:18:23 +01:00
GetCameraFocusCoords(&x, &y);
x -= gLinkSelfPlayerId;
2018-02-14 00:58:22 +01:00
for (i = 0; i < gFieldLinkPlayerCount; i++)
{
SpawnLinkPlayerEventObject(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_UpdateLinkState(void)
2018-02-14 00:58:22 +01:00
{
if (gWirelessCommType == 0 || !IsRfuRecvQueueEmpty() || !IsSendingKeysToLink())
2018-02-14 00:58:22 +01:00
{
u8 selfId = gLinkSelfPlayerId;
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
{
ResetAllTradingStates();
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)
2018-02-15 12:36:52 +01:00
sub_8010198();
2018-02-14 00:58:22 +01:00
}
static void ResetAllTradingStates(void)
2018-02-14 00:58:22 +01:00
{
s32 i;
for (i = 0; i < 4; i++)
sPlayerTradingStates[i] = PLAYER_TRADING_STATE_IDLE;
2018-02-14 00:58:22 +01:00
}
// Returns true if all connected players are in tradingState.
static bool32 AreAllPlayersInState(u16 tradingState)
2018-02-14 00:58:22 +01:00
{
s32 i;
s32 count = gFieldLinkPlayerCount;
for (i = 0; i < count; i++)
if (sPlayerTradingStates[i] != tradingState)
2018-02-14 00:58:22 +01:00
return FALSE;
return TRUE;
}
static bool32 AreAnyPlayersInState(u16 tradingState)
2018-02-14 00:58:22 +01:00
{
s32 i;
s32 count = gFieldLinkPlayerCount;
for (i = 0; i < count; i++)
if (sPlayerTradingStates[i] == tradingState)
2018-02-14 00:58:22 +01:00
return TRUE;
return FALSE;
}
static void HandleLinkPlayerKeyInput(u32 playerId, u16 key, struct TradeRoomPlayer *trainer, u16 *forceFacing)
2018-02-14 00:58:22 +01:00
{
const u8 *script;
if (sPlayerTradingStates[playerId] == PLAYER_TRADING_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);
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
2018-02-14 00:58:22 +01:00
sub_8087530(script);
}
return;
}
if (AreAnyPlayersInState(PLAYER_TRADING_STATE_EXITING_ROOM) == TRUE)
2018-02-14 00:58:22 +01:00
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
2018-02-14 00:58:22 +01:00
sub_8087584();
}
return;
}
switch (key)
2018-02-14 00:58:22 +01:00
{
case LINK_KEY_CODE_START_BUTTON:
if (sub_8087340_2(trainer))
2018-02-14 00:58:22 +01:00
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
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
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToEventScript);
CreateConfirmLeaveTradeRoomPrompt();
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)
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
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:
if (sub_8087340(trainer))
2018-02-14 00:58:22 +01:00
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToRecvQueue);
2018-02-14 00:58:22 +01:00
sub_8087510();
}
}
break;
case LINK_KEY_CODE_HANDLE_SEND_QUEUE:
if (sub_8087340(trainer))
2018-02-14 00:58:22 +01:00
{
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_BUSY;
if (trainer->isSelf)
2018-02-14 00:58:22 +01:00
{
SetKeyInterceptCallback(KeyInterCB_DeferToSendQueue);
2018-02-14 00:58:22 +01:00
sub_8087510();
}
}
break;
}
}
switch (key)
2018-02-14 00:58:22 +01:00
{
case LINK_KEY_CODE_EXIT_ROOM:
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_EXITING_ROOM;
2018-02-14 00:58:22 +01:00
break;
case LINK_KEY_CODE_UNK_2:
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_UNK_2;
2018-02-14 00:58:22 +01:00
break;
case LINK_KEY_CODE_UNK_4:
sPlayerTradingStates[playerId] = PLAYER_TRADING_STATE_IDLE;
if (trainer->isSelf)
SetKeyInterceptCallback(KeyInterCB_SelfIdle);
2018-02-14 00:58:22 +01:00
break;
case LINK_KEY_CODE_UNK_7:
if (sPlayerTradingStates[playerId] == PLAYER_TRADING_STATE_UNK_2)
sPlayerTradingStates[playerId] = PLAYER_TRADING_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
{
struct TradeRoomPlayer trainer;
2018-02-14 00:58:22 +01:00
s32 i;
for (i = 0; i < 4; i++)
{
u8 key = keys[i];
u16 setFacing = FACING_NONE;
LoadTradeRoomPlayer(i, selfId, &trainer);
HandleLinkPlayerKeyInput(i, key, &trainer, &setFacing);
if (sPlayerTradingStates[i] == PLAYER_TRADING_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
&& IsUpdateLinkStateCBActive() == 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
{
if (gMain.heldKeys & DPAD_UP)
return LINK_KEY_CODE_DPAD_UP;
2018-02-14 00:58:22 +01:00
else if (gMain.heldKeys & DPAD_DOWN)
return LINK_KEY_CODE_DPAD_DOWN;
2018-02-14 00:58:22 +01:00
else if (gMain.heldKeys & DPAD_LEFT)
return LINK_KEY_CODE_DPAD_LEFT;
2018-02-14 00:58:22 +01:00
else if (gMain.heldKeys & DPAD_RIGHT)
return LINK_KEY_CODE_DPAD_RIGHT;
2018-02-14 00:58:22 +01:00
else if (gMain.newKeys & START_BUTTON)
return LINK_KEY_CODE_START_BUTTON;
2018-02-14 00:58:22 +01:00
else if (gMain.newKeys & A_BUTTON)
return LINK_KEY_CODE_A_BUTTON;
2018-02-14 00:58:22 +01:00
else
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
}
static u16 sub_80870EC(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
{
retVal = LINK_KEY_CODE_UNK_4;
SetKeyInterceptCallback(sub_80870EC);
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() > 2)
2018-02-14 00:58:22 +01:00
{
retVal = LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
else
{
retVal = LINK_KEY_CODE_UNK_4;
2018-02-14 00:58:22 +01:00
ScriptContext2_Disable();
SetKeyInterceptCallback(sub_80870EC);
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
{
retVal = LINK_KEY_CODE_UNK_4;
2018-02-14 00:58:22 +01:00
ScriptContext2_Disable();
SetKeyInterceptCallback(sub_80870EC);
2018-02-14 00:58:22 +01:00
}
return retVal;
}
static u16 KeyInterCB_DoNothingAndKeepAlive(u32 key)
2018-02-14 00:58:22 +01:00
{
CheckRfuKeepAliveTimer();
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
static u16 sub_8087170(u32 keyOrPlayerId)
2018-02-14 00:58:22 +01:00
{
if (sPlayerTradingStates[keyOrPlayerId] == PLAYER_TRADING_STATE_UNK_2)
2018-02-14 00:58:22 +01:00
{
if (gMain.newKeys & B_BUTTON)
{
SetKeyInterceptCallback(KeyInterCB_DoNothingAndKeepAlive);
return LINK_KEY_CODE_UNK_7;
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
}
}
static u16 sub_80871AC(u32 a1)
{
SetKeyInterceptCallback(sub_8087170);
return LINK_KEY_CODE_UNK_2;
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_UpdateLinkState.
if (sPlayerTradingStates[keyOrPlayerId] != PLAYER_TRADING_STATE_EXITING_ROOM)
CheckRfuKeepAliveTimer();
if (AreAllPlayersInState(PLAYER_TRADING_STATE_EXITING_ROOM) == TRUE)
2018-02-14 00:58:22 +01:00
{
ScriptContext1_SetupScript(gEventScript_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
}
// Duplicate function.
static u16 KeyInterCB_SendNothing_2(u32 key)
2018-02-14 00:58:22 +01:00
{
return LINK_KEY_CODE_EMPTY;
2018-02-14 00:58:22 +01:00
}
u32 sub_8087214(void)
{
if (AreAnyPlayersInState(PLAYER_TRADING_STATE_EXITING_ROOM) == TRUE)
2018-02-14 00:58:22 +01:00
return 2;
if (sPlayerKeyInterceptCallback == sub_8087170 && sPlayerTradingStates[gLinkSelfPlayerId] != PLAYER_TRADING_STATE_UNK_2)
2018-02-14 00:58:22 +01:00
return 0;
if (sPlayerKeyInterceptCallback == KeyInterCB_DoNothingAndKeepAlive && sPlayerTradingStates[gLinkSelfPlayerId] == PLAYER_TRADING_STATE_BUSY)
2018-02-14 00:58:22 +01:00
return 2;
if (AreAllPlayersInState(PLAYER_TRADING_STATE_UNK_2) != FALSE)
2018-02-14 00:58:22 +01:00
return 1;
return 0;
}
bool32 sub_808727C(void)
{
return AreAnyPlayersInState(PLAYER_TRADING_STATE_EXITING_ROOM);
2018-02-14 00:58:22 +01:00
}
u16 sub_8087288(void)
{
SetKeyInterceptCallback(sub_80871AC);
2018-02-14 00:58:22 +01:00
return 0;
}
u16 sub_808729C(void)
{
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;
}
u16 sub_80872C4(void)
{
SetKeyInterceptCallback(KeyInterCB_SendNothing_2);
2018-02-14 00:58:22 +01:00
return 0;
}
static void LoadTradeRoomPlayer(s32 linkPlayerId, s32 myPlayerId, struct TradeRoomPlayer *trainer)
2018-02-14 00:58:22 +01:00
{
s16 x, y;
trainer->playerId = linkPlayerId;
trainer->isSelf = (linkPlayerId == myPlayerId) ? 1 : 0;
trainer->c = gLinkPlayerEventObjects[linkPlayerId].movementMode;
trainer->facing = GetLinkPlayerFacingDirection(linkPlayerId);
GetLinkPlayerCoords(linkPlayerId, &x, &y);
trainer->pos.x = x;
trainer->pos.y = y;
trainer->pos.height = GetLinkPlayerElevation(linkPlayerId);
trainer->field_C = MapGridGetMetatileBehaviorAt(x, y);
2018-02-14 00:58:22 +01:00
}
static bool32 sub_8087340(struct TradeRoomPlayer *player)
2018-02-14 00:58:22 +01:00
{
u8 v1 = player->c;
if (v1 == MOVEMENT_MODE_SCRIPTED || v1 == MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
// Duplicate function.
static bool32 sub_8087340_2(struct TradeRoomPlayer *player)
2018-02-14 00:58:22 +01:00
{
u8 v1 = player->c;
if (v1 == MOVEMENT_MODE_SCRIPTED || v1 == MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return TRUE;
else
return FALSE;
}
static u8 *TryGetTileEventScript(struct TradeRoomPlayer *player)
2018-02-14 00:58:22 +01:00
{
if (player->c != MOVEMENT_MODE_SCRIPTED)
return FACING_NONE;
return GetCoordEventScriptAtMapPosition(&player->pos);
2018-02-14 00:58:22 +01:00
}
static bool32 PlayerIsAtSouthExit(struct TradeRoomPlayer *player)
2018-02-14 00:58:22 +01:00
{
if (player->c != MOVEMENT_MODE_SCRIPTED && player->c != MOVEMENT_MODE_FREE)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (!MetatileBehavior_IsSouthArrowWarp(player->field_C))
2018-02-14 00:58:22 +01:00
return FALSE;
else if (player->facing != 1)
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 const u8 *TryInteractWithPlayer(struct TradeRoomPlayer *player)
2018-02-14 00:58:22 +01:00
{
struct MapPosition otherPlayerPos;
2018-02-14 00:58:22 +01:00
u8 linkPlayerId;
if (player->c != MOVEMENT_MODE_FREE && player->c != 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 != 4)
{
if (!player->isSelf)
return gEventScript_TradeRoom_TooBusyToNotice;
else if (sPlayerTradingStates[linkPlayerId] != PLAYER_TRADING_STATE_IDLE)
return gEventScript_TradeRoom_TooBusyToNotice;
else if (!GetLinkTrainerCardColor(linkPlayerId))
return gEventScript_TradeRoom_ReadTrainerCard_NoColor;
2018-02-14 00:58:22 +01:00
else
return gEventScript_TradeRoom_ReadTrainerCard_Normal;
2018-02-14 00:58:22 +01:00
}
return GetInteractedLinkPlayerScript(&otherPlayerPos, player->field_C, 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)
{
if (script == gEventScript_DoubleBattleColosseum_PlayerSpot0)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_DoubleBattleColosseum_PlayerSpot1)
return FACING_FORCED_LEFT;
else if (script == gEventScript_DoubleBattleColosseum_PlayerSpot2)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_DoubleBattleColosseum_PlayerSpot3)
return FACING_FORCED_LEFT;
else if (script == gEventScript_RecordCenter_Spot0)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_RecordCenter_Spot1)
return FACING_FORCED_LEFT;
else if (script == gEventScript_RecordCenter_Spot2)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_RecordCenter_Spot3)
return FACING_FORCED_LEFT;
else if (script == gEventScript_SingleBattleColosseum_PlayerSpot0)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_SingleBattleColosseum_PlayerSpot1)
return FACING_FORCED_LEFT;
else if (script == gEventScript_TradeCenter_Chair0)
return FACING_FORCED_RIGHT;
else if (script == gEventScript_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
}
static void sub_8087510(void)
{
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();
}
static void sub_8087530(const u8 *script)
{
PlaySE(SE_SELECT);
ScriptContext1_SetupScript(script);
ScriptContext2_Enable();
}
static void CreateConfirmLeaveTradeRoomPrompt(void)
2018-02-14 00:58:22 +01:00
{
PlaySE(SE_WIN_OPEN);
ScriptContext1_SetupScript(gEventScript_ConfirmLeaveTradeRoom);
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 sub_8087584(void)
{
ScriptContext1_SetupScript(gEventScript_TerminateLink);
2018-02-14 00:58:22 +01:00
ScriptContext2_Enable();
}
bool32 sub_8087598(void)
{
if (!IsUpdateLinkStateCBActive())
return FALSE;
if (GetLinkRecvQueueLength() >= 3)
sUnknown_03000E18 = TRUE;
2018-02-14 00:58:22 +01:00
else
sUnknown_03000E18 = FALSE;
2018-02-14 00:58:22 +01:00
return sUnknown_03000E18;
}
bool32 sub_80875C8(void)
{
u8 temp;
if (GetLinkRecvQueueLength() < 2)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsUpdateLinkStateCBActive() != 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;
temp = sUnknown_03000E18;
sUnknown_03000E18 = 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
}
bool32 sub_8087634(void)
{
if (GetLinkSendQueueLength() < 2)
2018-02-14 00:58:22 +01:00
return FALSE;
else if (IsUpdateLinkStateCBActive() != 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
}
bool32 sub_808766C(void)
{
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 gUnknown_03005000.unk_9e8.unk_232;
else
return gLink.sendQueue.count;
}
static void ZeroLinkPlayerEventObject(struct LinkPlayerEventObject *linkPlayerEventObj)
2018-02-14 00:58:22 +01:00
{
memset(linkPlayerEventObj, 0, sizeof(struct LinkPlayerEventObject));
2018-02-14 00:58:22 +01:00
}
void ClearLinkPlayerEventObjects(void)
2018-02-14 00:58:22 +01:00
{
memset(gLinkPlayerEventObjects, 0, sizeof(gLinkPlayerEventObjects));
2018-02-14 00:58:22 +01:00
}
static void ZeroEventObject(struct EventObject *eventObj)
2018-02-14 00:58:22 +01:00
{
memset(eventObj, 0, sizeof(struct EventObject));
2018-02-14 00:58:22 +01:00
}
static void SpawnLinkPlayerEventObject(u8 linkPlayerId, s16 x, s16 y, u8 a4)
2018-02-14 00:58:22 +01:00
{
u8 eventObjId = GetFirstInactiveEventObjectId();
struct LinkPlayerEventObject *linkPlayerEventObj = &gLinkPlayerEventObjects[linkPlayerId];
struct EventObject *eventObj = &gEventObjects[eventObjId];
2018-02-14 00:58:22 +01:00
ZeroLinkPlayerEventObject(linkPlayerEventObj);
ZeroEventObject(eventObj);
2018-02-14 00:58:22 +01:00
linkPlayerEventObj->active = 1;
linkPlayerEventObj->linkPlayerId = linkPlayerId;
linkPlayerEventObj->eventObjId = eventObjId;
linkPlayerEventObj->movementMode = MOVEMENT_MODE_FREE;
2018-02-14 00:58:22 +01:00
eventObj->active = 1;
eventObj->singleMovementActive = a4;
eventObj->range.as_byte = 2;
eventObj->spriteId = 64;
2018-02-14 00:58:22 +01:00
InitLinkPlayerEventObjectPos(eventObj, x, y);
2018-02-14 00:58:22 +01:00
}
static void InitLinkPlayerEventObjectPos(struct EventObject *eventObj, s16 x, s16 y)
2018-02-14 00:58:22 +01:00
{
eventObj->currentCoords.x = x;
eventObj->currentCoords.y = y;
eventObj->previousCoords.x = x;
eventObj->previousCoords.y = y;
sub_8093038(x, y, &eventObj->initialCoords.x, &eventObj->initialCoords.y);
eventObj->initialCoords.x += 8;
EventObjectUpdateZCoord(eventObj);
2018-02-14 00:58:22 +01:00
}
static void sub_80877DC(u8 linkPlayerId, u8 a2)
{
if (gLinkPlayerEventObjects[linkPlayerId].active)
2018-02-14 00:58:22 +01:00
{
u8 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
eventObj->range.as_byte = a2;
2018-02-14 00:58:22 +01:00
}
}
static void sub_808780C(u8 linkPlayerId)
{
struct LinkPlayerEventObject *linkPlayerEventObj = &gLinkPlayerEventObjects[linkPlayerId];
u8 eventObjId = linkPlayerEventObj->eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
2018-11-28 21:08:22 +01:00
if (eventObj->spriteId != MAX_SPRITES)
DestroySprite(&gSprites[eventObj->spriteId]);
linkPlayerEventObj->active = 0;
eventObj->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 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
return eventObj->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 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
*x = eventObj->currentCoords.x;
*y = eventObj->currentCoords.y;
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerFacingDirection(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
return eventObj->range.as_byte;
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerElevation(u8 linkPlayerId)
2018-02-14 00:58:22 +01:00
{
u8 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
return eventObj->currentElevation;
2018-02-14 00:58:22 +01:00
}
static s32 sub_80878E4(u8 linkPlayerId)
{
u8 eventObjId = gLinkPlayerEventObjects[linkPlayerId].eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
return 16 - (s8)eventObj->directionSequenceIndex;
2018-02-14 00:58:22 +01:00
}
static u8 GetLinkPlayerIdAt(s16 x, s16 y)
{
u8 i;
for (i = 0; i < 4; i++)
{
if (gLinkPlayerEventObjects[i].active
&& (gLinkPlayerEventObjects[i].movementMode == 0 || gLinkPlayerEventObjects[i].movementMode == 2))
2018-02-14 00:58:22 +01:00
{
struct EventObject *eventObj = &gEventObjects[gLinkPlayerEventObjects[i].eventObjId];
if (eventObj->currentCoords.x == x && eventObj->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
{
struct LinkPlayerEventObject *linkPlayerEventObj = &gLinkPlayerEventObjects[linkPlayerId];
u8 eventObjId = linkPlayerEventObj->eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
2018-02-14 00:58:22 +01:00
if (linkPlayerEventObj->active)
2018-02-14 00:58:22 +01:00
{
if (facing > FACING_FORCED_RIGHT)
eventObj->triggerGroundEffectsOnMove = 1;
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.
#define TEMP gLinkPlayerMovementModes[linkPlayerEventObj->movementMode](linkPlayerEventObj, eventObj, facing)
gMovementStatusHandler[TEMP](linkPlayerEventObj, eventObj);
// Clean up the hack.
#undef TEMP
}
2018-02-14 00:58:22 +01:00
}
}
static u8 MovementEventModeCB_Normal(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
return gLinkPlayerFacingHandlers[a3](linkPlayerEventObj, eventObj, a3);
2018-02-14 00:58:22 +01:00
}
static u8 MovementEventModeCB_Ignored(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
return FACING_UP;
2018-02-14 00:58:22 +01:00
}
// Duplicate Function
static u8 MovementEventModeCB_Normal_2(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
return gLinkPlayerFacingHandlers[a3](linkPlayerEventObj, eventObj, a3);
2018-02-14 00:58:22 +01:00
}
static bool8 FacingHandler_DoNothing(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
return FALSE;
2018-02-14 00:58:22 +01:00
}
static bool8 FacingHandler_DpadMovement(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
s16 x, y;
eventObj->range.as_byte = FlipVerticalAndClearForced(a3, eventObj->range.as_byte);
EventObjectMoveDestCoords(eventObj, eventObj->range.as_byte, &x, &y);
2018-02-14 00:58:22 +01:00
if (LinkPlayerDetectCollision(linkPlayerEventObj->eventObjId, eventObj->range.as_byte, x, y))
2018-02-14 00:58:22 +01:00
{
return FALSE;
2018-02-14 00:58:22 +01:00
}
else
{
eventObj->directionSequenceIndex = 16;
ShiftEventObjectCoords(eventObj, x, y);
EventObjectUpdateZCoord(eventObj);
return TRUE;
2018-02-14 00:58:22 +01:00
}
}
static bool8 FacingHandler_ForcedFacingChange(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj, u8 a3)
2018-02-14 00:58:22 +01:00
{
eventObj->range.as_byte = FlipVerticalAndClearForced(a3, eventObj->range.as_byte);
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.
static void MovementStatusHandler_EnterFreeMode(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj)
2018-02-14 00:58:22 +01:00
{
linkPlayerEventObj->movementMode = MOVEMENT_MODE_FREE;
2018-02-14 00:58:22 +01:00
}
static void MovementStatusHandler_TryAdvanceScript(struct LinkPlayerEventObject *linkPlayerEventObj, struct EventObject *eventObj)
2018-02-14 00:58:22 +01:00
{
eventObj->directionSequenceIndex--;
linkPlayerEventObj->movementMode = MOVEMENT_MODE_FROZEN;
MoveCoords(eventObj->range.as_byte, &eventObj->initialCoords.x, &eventObj->initialCoords.y);
if (!eventObj->directionSequenceIndex)
2018-02-14 00:58:22 +01:00
{
ShiftStillEventObjectCoords(eventObj);
linkPlayerEventObj->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
{
switch (newFacing - 1)
2018-02-14 00:58:22 +01:00
{
case (FACING_UP - 1):
case (FACING_FORCED_UP - 1):
return DIR_NORTH;
case (FACING_DOWN - 1):
case (FACING_FORCED_DOWN - 1):
return DIR_SOUTH;
case (FACING_LEFT - 1):
case (FACING_FORCED_LEFT - 1):
return DIR_WEST;
case (FACING_RIGHT - 1):
case (FACING_FORCED_RIGHT - 1):
return DIR_EAST;
2018-02-14 00:58:22 +01:00
}
return oldFacing;
2018-02-14 00:58:22 +01:00
}
static u8 LinkPlayerDetectCollision(u8 selfEventObjId, u8 a2, s16 x, s16 y)
2018-02-14 00:58:22 +01:00
{
u8 i;
for (i = 0; i < 16; i++)
{
if (i != selfEventObjId)
2018-02-14 00:58:22 +01:00
{
if ((gEventObjects[i].currentCoords.x == x && gEventObjects[i].currentCoords.y == y)
|| (gEventObjects[i].previousCoords.x == x && gEventObjects[i].previousCoords.y == y))
2018-02-14 00:58:22 +01:00
{
return 1;
}
}
}
return MapGridIsImpassableAt(x, y);
}
static void CreateLinkPlayerSprite(u8 linkPlayerId, u8 gameVersion)
{
struct LinkPlayerEventObject *linkPlayerEventObj = &gLinkPlayerEventObjects[linkPlayerId];
u8 eventObjId = linkPlayerEventObj->eventObjId;
struct EventObject *eventObj = &gEventObjects[eventObjId];
2018-02-14 00:58:22 +01:00
struct Sprite *sprite;
if (linkPlayerEventObj->active)
2018-02-14 00:58:22 +01:00
{
switch (gameVersion)
{
case VERSION_FIRE_RED:
case VERSION_LEAF_GREEN:
eventObj->spriteId = AddPseudoEventObject(GetFRLGAvatarGraphicsIdByGender(eventObj->singleMovementActive), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
case VERSION_RUBY:
case VERSION_SAPPHIRE:
eventObj->spriteId = AddPseudoEventObject(GetRSAvatarGraphicsIdByGender(eventObj->singleMovementActive), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
case VERSION_EMERALD:
2018-11-01 05:03:41 +01:00
eventObj->spriteId = AddPseudoEventObject(GetRivalAvatarGraphicsIdByStateIdAndGender(PLAYER_AVATAR_STATE_NORMAL, eventObj->singleMovementActive), SpriteCB_LinkPlayer, 0, 0, 0);
2018-02-14 00:58:22 +01:00
break;
}
sprite = &gSprites[eventObj->spriteId];
2018-02-14 00:58:22 +01:00
sprite->coordOffsetEnabled = TRUE;
sprite->data[0] = linkPlayerId;
eventObj->triggerGroundEffectsOnMove = 0;
2018-02-14 00:58:22 +01:00
}
}
static void SpriteCB_LinkPlayer(struct Sprite *sprite)
{
struct LinkPlayerEventObject *linkPlayerEventObj = &gLinkPlayerEventObjects[sprite->data[0]];
struct EventObject *eventObj = &gEventObjects[linkPlayerEventObj->eventObjId];
sprite->pos1.x = eventObj->initialCoords.x;
sprite->pos1.y = eventObj->initialCoords.y;
SetObjectSubpriorityByZCoord(eventObj->previousElevation, sprite, 1);
sprite->oam.priority = ZCoordToPriority(eventObj->previousElevation);
2018-02-14 00:58:22 +01:00
if (!linkPlayerEventObj->movementMode != MOVEMENT_MODE_FREE)
StartSpriteAnim(sprite, GetFaceDirectionAnimNum(eventObj->range.as_byte));
2018-02-14 00:58:22 +01:00
else
StartSpriteAnimIfDifferent(sprite, GetMoveDirectionAnimNum(eventObj->range.as_byte));
2018-02-14 00:58:22 +01:00
UpdateEventObjectSpriteVisibility(sprite, 0);
if (eventObj->triggerGroundEffectsOnMove)
2018-02-14 00:58:22 +01:00
{
sprite->invisible = ((sprite->data[7] & 4) >> 2);
sprite->data[7]++;
}
}