diff options
author | Diegoisawesome <diego@domoreaweso.me> | 2018-10-16 21:47:08 -0500 |
---|---|---|
committer | Diegoisawesome <diego@domoreaweso.me> | 2018-10-16 21:47:08 -0500 |
commit | e75f0b4f80670bd3b6a28257f10770911869ea08 (patch) | |
tree | d22ea566cd8e0fd4b8abd5b4dd36c339f98fea85 /src | |
parent | ed1bb3030bae8b0d00a1fbb68ebd50f6376938ac (diff) |
Port/decompile field_control_avatar
Diffstat (limited to 'src')
-rw-r--r-- | src/battle_controller_player.c | 2 | ||||
-rw-r--r-- | src/battle_controller_player_partner.c | 2 | ||||
-rw-r--r-- | src/battle_interface.c | 2 | ||||
-rw-r--r-- | src/battle_script_commands.c | 4 | ||||
-rw-r--r-- | src/battle_setup.c | 12 | ||||
-rw-r--r-- | src/berry.c | 6 | ||||
-rw-r--r-- | src/data/pokemon/experience_tables.h | 2 | ||||
-rw-r--r-- | src/daycare.c | 6 | ||||
-rw-r--r-- | src/event_object_movement.c | 5 | ||||
-rw-r--r-- | src/field_control_avatar.c | 1058 | ||||
-rw-r--r-- | src/field_poison.c | 2 | ||||
-rw-r--r-- | src/field_specials.c | 2 | ||||
-rw-r--r-- | src/metatile_behavior.c | 8 | ||||
-rw-r--r-- | src/overworld.c | 14 | ||||
-rw-r--r-- | src/pokemon.c | 12 | ||||
-rw-r--r-- | src/pokemon_summary_screen.c | 4 | ||||
-rw-r--r-- | src/rom_8011DC0.c | 2 | ||||
-rw-r--r-- | src/scrcmd.c | 6 | ||||
-rw-r--r-- | src/script.c | 6 | ||||
-rw-r--r-- | src/secret_base.c | 8 |
20 files changed, 1110 insertions, 53 deletions
diff --git a/src/battle_controller_player.c b/src/battle_controller_player.c index 57a4efa9f..02479a1be 100644 --- a/src/battle_controller_player.c +++ b/src/battle_controller_player.c @@ -2710,7 +2710,7 @@ static void PlayerHandleExpUpdate(void) { u8 monId = gBattleBufferA[gActiveBattler][1]; - if (GetMonData(&gPlayerParty[monId], MON_DATA_LEVEL) >= MAX_MON_LEVEL) + if (GetMonData(&gPlayerParty[monId], MON_DATA_LEVEL) >= MAX_LEVEL) { PlayerBufferExecCompleted(); } diff --git a/src/battle_controller_player_partner.c b/src/battle_controller_player_partner.c index 7795c5656..c72847bd2 100644 --- a/src/battle_controller_player_partner.c +++ b/src/battle_controller_player_partner.c @@ -1597,7 +1597,7 @@ static void PlayerPartnerHandleExpUpdate(void) { u8 monId = gBattleBufferA[gActiveBattler][1]; - if (GetMonData(&gPlayerParty[monId], MON_DATA_LEVEL) >= MAX_MON_LEVEL) + if (GetMonData(&gPlayerParty[monId], MON_DATA_LEVEL) >= MAX_LEVEL) { PlayerPartnerBufferExecCompleted(); } diff --git a/src/battle_interface.c b/src/battle_interface.c index d2750bf9d..691e760a8 100644 --- a/src/battle_interface.c +++ b/src/battle_interface.c @@ -2341,7 +2341,7 @@ static void MoveBattleBarGraphically(u8 battlerId, u8 whichBar) &gBattleSpritesDataPtr->battleBars[battlerId].currValue, array, B_EXPBAR_PIXELS / 8); level = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_LEVEL); - if (level == MAX_MON_LEVEL) + if (level == MAX_LEVEL) { for (i = 0; i < 8; i++) array[i] = 0; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 685f0db7a..cdc996a4d 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -3347,7 +3347,7 @@ static void atk23_getexp(void) gBattleScripting.atk23_state = 5; gBattleMoveDamage = 0; // used for exp } - else if (GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_LEVEL) == MAX_MON_LEVEL) + else if (GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_LEVEL) == MAX_LEVEL) { *(&gBattleStruct->sentInPokes) >>= 1; gBattleScripting.atk23_state = 5; @@ -3430,7 +3430,7 @@ static void atk23_getexp(void) if (gBattleControllerExecFlags == 0) { gBattleBufferB[gBattleStruct->expGetterBattlerId][0] = 0; - if (GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_HP) && GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_LEVEL) != MAX_MON_LEVEL) + if (GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_HP) && GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_LEVEL) != MAX_LEVEL) { gBattleResources->statsBeforeLvlUp->hp = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_MAX_HP); gBattleResources->statsBeforeLvlUp->atk = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_ATK); diff --git a/src/battle_setup.c b/src/battle_setup.c index f9fab3d17..db173a38e 100644 --- a/src/battle_setup.c +++ b/src/battle_setup.c @@ -56,8 +56,8 @@ extern bool8 InBattlePyramid(void); extern bool8 InBattlePike(void); extern bool32 InTrainerHill(void); extern bool32 FieldPoisonEffectIsRunning(void); -extern void prev_quest_postbuffer_cursor_backup_reset(void); -extern void ResetPoisonStepCounter(void); +extern void RestartWildEncounterImmunitySteps(void); +extern void ClearPoisonStepCounter(void); extern void sub_81BE72C(void); extern void sub_808BCF4(void); extern void sub_80EECC8(void); @@ -354,8 +354,8 @@ static void Task_BattleStart(u8 taskId) { overworld_free_bg_tilemaps(); SetMainCallback2(CB2_InitBattle); - prev_quest_postbuffer_cursor_backup_reset(); - ResetPoisonStepCounter(); + RestartWildEncounterImmunitySteps(); + ClearPoisonStepCounter(); DestroyTask(taskId); } break; @@ -921,8 +921,8 @@ static void CB2_StartFirstBattle(void) gMain.savedCallback = CB2_EndFirstBattle; FreeAllWindowBuffers(); SetMainCallback2(CB2_InitBattle); - prev_quest_postbuffer_cursor_backup_reset(); - ResetPoisonStepCounter(); + RestartWildEncounterImmunitySteps(); + ClearPoisonStepCounter(); IncrementGameStat(GAME_STAT_TOTAL_BATTLES); IncrementGameStat(GAME_STAT_WILD_BATTLES); sub_80EECC8(); diff --git a/src/berry.c b/src/berry.c index ea216b61c..66cf91409 100644 --- a/src/berry.c +++ b/src/berry.c @@ -11,7 +11,7 @@ extern u8 EventObjectGetBerryTreeId(u8 eventObjectId); extern void sub_8092EF0(u8 mapId, u8 mapNumber, u8 mapGroup); extern void CB2_ChooseBerry(void); -extern const u8* GetEventObjectScriptPointerForComparison(void); +extern const u8* GetEventObjectScriptPointerPlayerFacing(void); extern bool8 IsBerryTreeSparkling(u8, u8, u8); extern u16 gSpecialVar_ItemId; @@ -930,7 +930,7 @@ bool32 EventObjectInteractionWaterBerryTree(void) bool8 IsPlayerFacingPlantedBerryTree(void) { - if (GetEventObjectScriptPointerForComparison() == BerryTreeScript + if (GetEventObjectScriptPointerPlayerFacing() == BerryTreeScript && GetStageByBerryTreeId(EventObjectGetBerryTreeId(gSelectedEventObject)) == 0) return TRUE; else @@ -939,7 +939,7 @@ bool8 IsPlayerFacingPlantedBerryTree(void) bool8 TryToWaterBerryTree(void) { - if (GetEventObjectScriptPointerForComparison() != BerryTreeScript) + if (GetEventObjectScriptPointerPlayerFacing() != BerryTreeScript) return FALSE; else return EventObjectInteractionWaterBerryTree(); diff --git a/src/data/pokemon/experience_tables.h b/src/data/pokemon/experience_tables.h index 58474b6f1..9fd5a2fae 100644 --- a/src/data/pokemon/experience_tables.h +++ b/src/data/pokemon/experience_tables.h @@ -18,7 +18,7 @@ :(n <= 36) ? ((n + 14) * CUBE(n) / 50) \ : (((n / 2) + 32) * CUBE(n) / 50) -const u32 gExperienceTables[][MAX_MON_LEVEL + 1] = +const u32 gExperienceTables[][MAX_LEVEL + 1] = { { // Medium Fast 0, // 0 diff --git a/src/daycare.c b/src/daycare.c index 934cdea43..c80a8b743 100644 --- a/src/daycare.c +++ b/src/daycare.c @@ -241,7 +241,7 @@ static void ApplyDaycareExperience(struct Pokemon *mon) bool8 firstMove; u16 learnedMove; - for (i = 0; i < MAX_MON_LEVEL; i++) + for (i = 0; i < MAX_LEVEL; i++) { // Add the mon's gained daycare experience level by level until it can't level up anymore. if (TryIncrementMonLevel(mon)) @@ -278,7 +278,7 @@ static u16 TakeSelectedPokemonFromDaycare(struct DaycareMon *daycareMon) species = GetBoxMonData(&daycareMon->mon, MON_DATA_SPECIES); BoxMonToMon(&daycareMon->mon, &pokemon); - if (GetMonData(&pokemon, MON_DATA_LEVEL) != MAX_MON_LEVEL) + if (GetMonData(&pokemon, MON_DATA_LEVEL) != MAX_LEVEL) { experience = GetMonData(&pokemon, MON_DATA_EXP) + daycareMon->steps; SetMonData(&pokemon, MON_DATA_EXP, &experience); @@ -949,7 +949,7 @@ static bool8 _DoEggActions_CheckHatch(struct DayCare *daycare) return FALSE; // no hatching } -bool8 DoEggActions_CheckHatch(void) +bool8 ShouldEggHatch(void) { return _DoEggActions_CheckHatch(&gSaveBlock1Ptr->daycare); } diff --git a/src/event_object_movement.c b/src/event_object_movement.c index 8c5c92473..57f72b284 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -17,7 +17,6 @@ #include "pokenav.h" #include "random.h" #include "rom_818CFC8.h" -#include "rom_81BE66C.h" #include "sprite.h" #include "trainer_see.h" #include "util.h" @@ -2638,10 +2637,10 @@ void sub_808F28C(u8 localId, u8 mapNum, u8 mapGroup, u8 decorCat) switch (decorCat) { case DECORCAT_DOLL: - OverrideMovementTypeForEventObject(&gEventObjects[eventObjectId], EventScript_2766A2); + OverrideMovementTypeForEventObject(&gEventObjects[eventObjectId], EventScript_SecretPower1); break; case DECORCAT_CUSHION: - OverrideMovementTypeForEventObject(&gEventObjects[eventObjectId], EventScript_2766A6); + OverrideMovementTypeForEventObject(&gEventObjects[eventObjectId], EventScript_SecretPower2); break; } } diff --git a/src/field_control_avatar.c b/src/field_control_avatar.c new file mode 100644 index 000000000..fef403c04 --- /dev/null +++ b/src/field_control_avatar.c @@ -0,0 +1,1058 @@ +#include "global.h" +#include "battle_setup.h" +#include "battle_frontier_1.h" +#include "bike.h" +#include "coord_event_weather.h" +#include "daycare.h" +#include "event_data.h" +#include "event_object_movement.h" +#include "fieldmap.h" +#include "field_control_avatar.h" +#include "field_fadetransition.h" +#include "field_player_avatar.h" +#include "field_poison.h" +#include "field_screen.h" +#include "field_specials.h" +#include "fldeff_80F9BCC.h" +#include "item_menu.h" +#include "link.h" +#include "metatile_behavior.h" +#include "overworld.h" +#include "pokemon.h" +#include "pokenav.h" +#include "safari_zone.h" +#include "script.h" +#include "secret_base.h" +#include "sound.h" +#include "start_menu.h" +#include "trainer_see.h" +#include "wild_encounter.h" +#include "constants/bg_event_constants.h" +#include "constants/map_types.h" +#include "constants/songs.h" + +static EWRAM_DATA u8 sWildEncounterImmunitySteps = 0; +static EWRAM_DATA u16 sPreviousPlayerMetatileBehavior = 0; + +u8 gSelectedEventObject; + +//scripts +extern const u8 EventScript_PlayerPCMale[]; +extern const u8 EventScript_PlayerPCFemale[]; +extern const u8 EventScript_SecretBasePC[]; +extern const u8 EventScript_RecordMixingSecretBasePC[]; +extern const u8 EventScript_SecretPower1[]; +extern const u8 EventScript_SecretPower2[]; +extern const u8 EventScript_PC[]; +extern const u8 EventScript_TestSignpostMsg[]; +extern const u8 EventScript_HiddenItemScript[]; +extern const u8 EventScript_2759F1[]; +extern const u8 EventScript_TV[]; +extern const u8 EventScript_ClosedSootopolisDoor[]; +extern const u8 SkyPillar_Outside_EventScript_2393F9[]; +extern const u8 EventScript_CableBoxResults[]; +extern const u8 EventScript_2A4BAC[]; +extern const u8 Route110_TrickHouseEntrance_EventScript_26A22A[]; +extern const u8 EventScript_RegionMap[]; +extern const u8 EventScript_RunningShoesManual[]; +extern const u8 EventScript_PictureBookShelf[]; +extern const u8 EventScript_BookShelf[]; +extern const u8 EventScript_PokemonCenterBookshelf[]; +extern const u8 EventScript_Vase[]; +extern const u8 EventScript_TrashCan[]; +extern const u8 EventScript_ShopShelf[]; +extern const u8 EventScript_Blueprint[]; +extern const u8 EventScript_WirelessBoxResults[]; +extern const u8 EventScript_CableBoxResults[]; +extern const u8 EventScript_Questionnaire[]; +extern const u8 EventScript_TrainerHillTimer[]; +extern const u8 EventScript_SecretBaseSandOrnament[]; +extern const u8 EventScript_SecretBaseShieldOrToyTV[]; +extern const u8 EventScript_UseSurf[]; +extern const u8 EventScript_UseWaterfall[]; +extern const u8 EventScript_CannotUseWaterfall[]; +extern const u8 EventScript_UseDive[]; +extern const u8 EventScript_UseDiveUnderwater[]; +extern const u8 EventScript_FallDownHole[]; +extern const u8 BattleFrontier_BattlePyramidEmptySquare_EventScript_252BE8[]; +extern const u8 EventScript_Poison[]; +extern const u8 EventScript_EggHatch[]; +extern const u8 gUnknown_08273D1F[]; +extern const u8 IslandCave_EventScript_238EAF[]; +extern const u8 MauvilleCity_EventScript_1DF7BA[]; +extern const u8 Route119_EventScript_1F49EC[]; +extern const u8 LittlerootTown_ProfessorBirchsLab_EventScript_1FA4D6[]; +extern const u8 RustboroCity_Gym_EventScript_21307B[]; +extern const u8 MossdeepCity_SpaceCenter_2F_EventScript_224175[]; +extern const u8 SSTidalCorridor_EventScript_23C050[]; +extern const u8 gUnknown_082A8350[]; + +static void GetPlayerPosition(struct MapPosition *); +static void GetInFrontOfPlayerPosition(struct MapPosition *); +static u16 GetPlayerCurMetatileBehavior(int); +static bool8 TryStartInteractionScript(struct MapPosition*, u16, u8); +static const u8 *GetInteractionScript(struct MapPosition*, u8, u8); +static const u8 *GetInteractedEventObjectScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedBackgroundEventScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedMetatileScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedWaterScript(struct MapPosition *, u8, u8); +static bool32 TrySetupDiveDownScript(void); +static bool32 TrySetupDiveEmergeScript(void); +static bool8 TryStartStepBasedScript(struct MapPosition *, u16, u16); +static bool8 CheckStandardWildEncounter(u16); +static bool8 mapheader_run_first_tag2_script_list_match_conditionally(struct MapPosition *, u16, u8); +static bool8 IsWarpMetatileBehavior(u16); +static bool8 IsArrowWarpMetatileBehavior(u16, u8); +static s8 GetWarpEventAtMapPosition(struct MapHeader *, struct MapPosition *); +static void sub_809CEB0(struct MapHeader *, s8, struct MapPosition *); +static bool8 map_warp_consider_2_to_inside(struct MapPosition *, u16, u8); +static s8 GetWarpEventAtPosition(struct MapHeader *, u16, u16, u8); +static u8 *GetCoordEventScriptAtPosition(struct MapHeader *, u16, u16, u8); +static struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *, u16, u16, u8); +static bool8 TryStartCoordEventScript(struct MapPosition *); +static bool8 TryStartWarpEventScript(struct MapPosition *, u16); +static bool8 TryStartMiscWalkingScripts(u16); +static bool8 TryStartStepCountScript(u16); +static void UpdateHappinessStepCounter(void); +static bool8 UpdatePoisonStepCounter(void); + +void FieldClearPlayerInput(struct FieldInput *input) +{ + input->pressedAButton = FALSE; + input->checkStandardWildEncounter = FALSE; + input->pressedStartButton = FALSE; + input->pressedSelectButton = FALSE; + input->input_field_0_4 = FALSE; + input->input_field_0_5 = FALSE; + input->tookStep = FALSE; + input->pressedBButton = FALSE; + input->input_field_1_0 = FALSE; + input->input_field_1_1 = FALSE; + input->input_field_1_2 = FALSE; + input->input_field_1_3 = FALSE; + input->dpadDirection = 0; +} + +void FieldGetPlayerInput(struct FieldInput *input, u16 newKeys, u16 heldKeys) +{ + u8 tileTransitionState = gPlayerAvatar.tileTransitionState; + u8 runningState = gPlayerAvatar.runningState; + bool8 forcedMove = MetatileBehavior_IsForcedMovementTile(GetPlayerCurMetatileBehavior(runningState)); + + if ((tileTransitionState == T_TILE_CENTER && forcedMove == FALSE) || tileTransitionState == T_NOT_MOVING) + { + if (GetPlayerSpeed() != 4) + { + if (newKeys & START_BUTTON) + input->pressedStartButton = TRUE; + if (newKeys & SELECT_BUTTON) + input->pressedSelectButton = TRUE; + if (newKeys & A_BUTTON) + input->pressedAButton = TRUE; + if (newKeys & B_BUTTON) + input->pressedBButton = TRUE; + } + + if (heldKeys & (DPAD_UP | DPAD_DOWN | DPAD_LEFT | DPAD_RIGHT)) + { + input->input_field_0_4 = TRUE; + input->input_field_0_5 = TRUE; + } + } + + if (forcedMove == FALSE) + { + if (tileTransitionState == T_TILE_CENTER && runningState == MOVING) + input->tookStep = TRUE; + if (forcedMove == FALSE && tileTransitionState == T_TILE_CENTER) + input->checkStandardWildEncounter = TRUE; + } + + if (heldKeys & DPAD_UP) + input->dpadDirection = DIR_NORTH; + else if (heldKeys & DPAD_DOWN) + input->dpadDirection = DIR_SOUTH; + else if (heldKeys & DPAD_LEFT) + input->dpadDirection = DIR_WEST; + else if (heldKeys & DPAD_RIGHT) + input->dpadDirection = DIR_EAST; +} + +int sub_809C014(struct FieldInput *input) +{ + struct MapPosition position; + u8 playerDirection; + u16 metatileBehavior; + + gSpecialVar_LastTalked = 0; + gSelectedEventObject = 0; + + playerDirection = GetPlayerFacingDirection(); + GetPlayerPosition(&position); + metatileBehavior = MapGridGetMetatileBehaviorAt(position.x, position.y); + + if (CheckForTrainersWantingBattle() == TRUE) + return TRUE; + + if (mapheader_run_first_tag2_script_list_match() == 1) + return TRUE; + + if (input->pressedBButton && TrySetupDiveEmergeScript() == TRUE) + return TRUE; + if (input->tookStep) + { + IncrementGameStat(GAME_STAT_STEPS); + increment_var_x4026_on_birth_island_modulo_100(); + if (TryStartStepBasedScript(&position, metatileBehavior, playerDirection) == TRUE) + return TRUE; + } + if (input->checkStandardWildEncounter && CheckStandardWildEncounter(metatileBehavior) == TRUE) + return TRUE; + if (input->input_field_0_4 && input->dpadDirection == playerDirection) + { + if (mapheader_run_first_tag2_script_list_match_conditionally(&position, metatileBehavior, playerDirection) == TRUE) + return TRUE; + } + + GetInFrontOfPlayerPosition(&position); + metatileBehavior = MapGridGetMetatileBehaviorAt(position.x, position.y); + if (input->pressedAButton && TryStartInteractionScript(&position, metatileBehavior, playerDirection) == TRUE) + return TRUE; + + if (input->input_field_0_5 && input->dpadDirection == playerDirection) + { + if (map_warp_consider_2_to_inside(&position, metatileBehavior, playerDirection) == TRUE) + return TRUE; + } + if (input->pressedAButton && TrySetupDiveDownScript() == TRUE) + return TRUE; + if (input->pressedStartButton) + { + PlaySE(SE_WIN_OPEN); + ShowStartMenu(); + return TRUE; + } + if (input->pressedSelectButton && UseRegisteredKeyItemOnField() == TRUE) + return TRUE; + + return FALSE; +} + +static void GetPlayerPosition(struct MapPosition *position) +{ + PlayerGetDestCoords(&position->x, &position->y); + position->height = PlayerGetZCoord(); +} + +static void GetInFrontOfPlayerPosition(struct MapPosition *position) +{ + s16 x, y; + + GetXYCoordsOneStepInFrontOfPlayer(&position->x, &position->y); + PlayerGetDestCoords(&x, &y); + if (MapGridGetZCoordAt(x, y) != 0) + position->height = PlayerGetZCoord(); + else + position->height = 0; +} + +static u16 GetPlayerCurMetatileBehavior(int runningState) +{ + s16 x, y; + + PlayerGetDestCoords(&x, &y); + return MapGridGetMetatileBehaviorAt(x, y); +} + +static bool8 TryStartInteractionScript(struct MapPosition *position, u16 metatileBehavior, u8 direction) +{ + const u8 *script = GetInteractionScript(position, metatileBehavior, direction); + if (script == NULL) + return FALSE; + + // Don't play interaction sound for certain scripts. + if (script != EventScript_PlayerPCMale + && script != EventScript_PlayerPCFemale + && script != EventScript_SecretBasePC + && script != EventScript_RecordMixingSecretBasePC + && script != EventScript_SecretPower1 + && script != EventScript_SecretPower2 + && script != EventScript_PC) + PlaySE(SE_SELECT); + + ScriptContext1_SetupScript(script); + return TRUE; +} + +static const u8 *GetInteractionScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +{ + const u8 *script = GetInteractedEventObjectScript(position, metatileBehavior, direction); + if (script != NULL) + return script; + + script = GetInteractedBackgroundEventScript(position, metatileBehavior, direction); + if (script != NULL) + return script; + + script = GetInteractedMetatileScript(position, metatileBehavior, direction); + if (script != NULL) + return script; + + script = GetInteractedWaterScript(position, metatileBehavior, direction); + if (script != NULL) + return script; + + return NULL; +} + +const u8 *GetInteractedLinkPlayerScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +{ + u8 eventObjectId; + s32 i; + + if (!MetatileBehavior_IsCounter(MapGridGetMetatileBehaviorAt(position->x, position->y))) + eventObjectId = GetEventObjectIdByXYZ(position->x, position->y, position->height); + else + eventObjectId = GetEventObjectIdByXYZ(position->x + gDirectionToVectors[direction].x, position->y + gDirectionToVectors[direction].y, position->height); + + if (eventObjectId == 16 || gEventObjects[eventObjectId].localId == 0xFF) + return NULL; + + for (i = 0; i < 4; i++) + { + if (gLinkPlayerEventObjects[i].active == TRUE && gLinkPlayerEventObjects[i].eventObjId == eventObjectId) + return NULL; + } + + gSelectedEventObject = eventObjectId; + gSpecialVar_LastTalked = gEventObjects[eventObjectId].localId; + gSpecialVar_Facing = direction; + return GetEventObjectScriptPointerByEventObjectId(eventObjectId); +} + +static const u8 *GetInteractedEventObjectScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +{ + u8 eventObjectId; + const u8 *script; + + eventObjectId = GetEventObjectIdByXYZ(position->x, position->y, position->height); + if (eventObjectId == 16 || gEventObjects[eventObjectId].localId == 0xFF) + { + if (MetatileBehavior_IsCounter(metatileBehavior) != TRUE) + return NULL; + + // Look for an event object on the other side of the counter. + eventObjectId = GetEventObjectIdByXYZ(position->x + gDirectionToVectors[direction].x, position->y + gDirectionToVectors[direction].y, position->height); + if (eventObjectId == 16 || gEventObjects[eventObjectId].localId == 0xFF) + return NULL; + } + + gSelectedEventObject = eventObjectId; + gSpecialVar_LastTalked = gEventObjects[eventObjectId].localId; + gSpecialVar_Facing = direction; + + if (InTrainerHill() == TRUE) + { + script = sub_81D62AC(); + } + else + { + script = GetEventObjectScriptPointerByEventObjectId(eventObjectId); + } + script = GetRamScript(gSpecialVar_LastTalked, script); + return script; +} + +static const u8 *GetInteractedBackgroundEventScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +{ + struct BgEvent *bgEvent = GetBackgroundEventAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); + + if (bgEvent == NULL) + return NULL; + if (bgEvent->bgUnion.script == NULL) + return EventScript_TestSignpostMsg; + + switch (bgEvent->kind) + { + case BG_EVENT_PLAYER_FACING_ANY: + default: + return bgEvent->bgUnion.script; + case BG_EVENT_PLAYER_FACING_NORTH: + if (direction != DIR_NORTH) + return NULL; + break; + case BG_EVENT_PLAYER_FACING_SOUTH: + if (direction != DIR_SOUTH) + return NULL; + break; + case BG_EVENT_PLAYER_FACING_EAST: + if (direction != DIR_EAST) + return NULL; + break; + case BG_EVENT_PLAYER_FACING_WEST: + if (direction != DIR_WEST) + return NULL; + break; + case 5: + case 6: + case BG_EVENT_HIDDEN_ITEM: + gSpecialVar_0x8004 = ((u32)bgEvent->bgUnion.script >> 16) + FLAG_HIDDEN_ITEMS_START; + gSpecialVar_0x8005 = (u32)bgEvent->bgUnion.script; + if (FlagGet(gSpecialVar_0x8004) == TRUE) + return NULL; + return EventScript_HiddenItemScript; + case BG_EVENT_SECRET_BASE: + if (direction == DIR_NORTH) + { + gSpecialVar_0x8004 = bgEvent->bgUnion.secretBaseId; + if (sub_80E9680()) + return EventScript_2759F1; + } + return NULL; + } + + return bgEvent->bgUnion.script; +} + +static const u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +{ + s8 height; + + if (MetatileBehavior_IsPlayerFacingTVScreen(metatileBehavior, direction) == TRUE) + return EventScript_TV; + if (MetatileBehavior_IsPC(metatileBehavior) == TRUE) + return EventScript_PC; + if (MetatileBehavior_IsClosedSootopolisDoor(metatileBehavior) == TRUE) + return EventScript_ClosedSootopolisDoor; + if (MetatileBehavior_IsUnknownClosedDoor(metatileBehavior) == TRUE) + return SkyPillar_Outside_EventScript_2393F9; + if (MetatileBehavior_IsCableBoxResults1(metatileBehavior) == TRUE) + return EventScript_CableBoxResults; + if (MetatileBehavior_IsPokeblockFeeder(metatileBehavior) == TRUE) + return EventScript_2A4BAC; + if (MetatileBehavior_IsTrickHousePuzzleDoor(metatileBehavior) == TRUE) + return Route110_TrickHouseEntrance_EventScript_26A22A; + if (MetatileBehavior_IsRegionMap(metatileBehavior) == TRUE) + return EventScript_RegionMap; + if (MetatileBehavior_IsRunningShoesManual(metatileBehavior) == TRUE) + return EventScript_RunningShoesManual; + if (MetatileBehavior_IsPictureBookShelf(metatileBehavior) == TRUE) + return EventScript_PictureBookShelf; + if (MetatileBehavior_IsBookShelf(metatileBehavior) == TRUE) + return EventScript_BookShelf; + if (MetatileBehavior_IsPokeCenterBookShelf(metatileBehavior) == TRUE) + return EventScript_PokemonCenterBookshelf; + if (MetatileBehavior_IsVase(metatileBehavior) == TRUE) + return EventScript_Vase; + if (MetatileBehavior_IsTrashCan(metatileBehavior) == TRUE) + return EventScript_TrashCan; + if (MetatileBehavior_IsShopShelf(metatileBehavior) == TRUE) + return EventScript_ShopShelf; + if (MetatileBehavior_IsBlueprint(metatileBehavior) == TRUE) + return EventScript_Blueprint; + if (MetatileBehavior_IsPlayerFacingWirelessBoxResults(metatileBehavior, direction) == TRUE) + return EventScript_WirelessBoxResults; + if (MetatileBehavior_IsCableBoxResults2(metatileBehavior, direction) == TRUE) + return EventScript_CableBoxResults; + if (MetatileBehavior_IsQuestionnaire(metatileBehavior) == TRUE) + return EventScript_Questionnaire; + if (MetatileBehavior_IsTrainerHillTimer(metatileBehavior) == TRUE) + return EventScript_TrainerHillTimer; + + height = position->height; + if (height == MapGridGetZCoordAt(position->x, position->y)) + { + if (MetatileBehavior_IsSecretBasePC(metatileBehavior) == TRUE) + return EventScript_SecretBasePC; + if (MetatileBehavior_IsRecordMixingSecretBasePC(metatileBehavior) == TRUE) + return EventScript_RecordMixingSecretBasePC; + if (MetatileBehavior_IsSecretBaseSandOrnament(metatileBehavior) == TRUE) + return EventScript_SecretBaseSandOrnament; + if (MetatileBehavior_IsSecretBaseShieldOrToyTV(metatileBehavior) == TRUE) + return EventScript_SecretBaseShieldOrToyTV; + if (MetatileBehavior_IsMB_C6(metatileBehavior) == TRUE) + { + sub_80EB56C(); + return NULL; + } + if (MetatileBehavior_IsLargeMatCenter(metatileBehavior) == TRUE) + { + sub_80EB9E0(); + return NULL; + } + if (MetatileBehavior_IsSecretBaseLargeMatEdge(metatileBehavior) == TRUE) + { + sub_80EBB28(); + return NULL; + } + } + else if (MetatileBehavior_IsSecretBasePoster(metatileBehavior) == TRUE) + { + sub_80EB498(); + return NULL; + } + + return NULL; +} + +static const u8 *GetInteractedWaterScript(struct MapPosition *unused1, u8 metatileBehavior, u8 direction) +{ + if (FlagGet(FLAG_BADGE05_GET) == TRUE && PartyHasMonWithSurf() == TRUE && IsPlayerFacingSurfableFishableWater() == TRUE) + return EventScript_UseSurf; + + if (MetatileBehavior_IsWaterfall(metatileBehavior) == TRUE) + { + if (FlagGet(FLAG_BADGE08_GET) == TRUE && IsPlayerSurfingNorth() == TRUE) + return EventScript_UseWaterfall; + else + return EventScript_CannotUseWaterfall; + } + return NULL; +} + +static bool32 TrySetupDiveDownScript(void) +{ + if (FlagGet(FLAG_BADGE07_GET) && TrySetDiveWarp() == 2) + { + ScriptContext1_SetupScript(EventScript_UseDive); + return TRUE; + } + return FALSE; +} + +static bool32 TrySetupDiveEmergeScript(void) +{ + if (FlagGet(FLAG_BADGE07_GET) && gMapHeader.mapType == MAP_TYPE_UNDERWATER && TrySetDiveWarp() == 1) + { + ScriptContext1_SetupScript(EventScript_UseDiveUnderwater); + return TRUE; + } + return FALSE; +} + +static bool8 TryStartStepBasedScript(struct MapPosition *position, u16 metatileBehavior, u16 direction) +{ + if (TryStartCoordEventScript(position) == TRUE) + return TRUE; + if (TryStartWarpEventScript(position, metatileBehavior) == TRUE) + return TRUE; + if (TryStartMiscWalkingScripts(metatileBehavior) == TRUE) + return TRUE; + if (TryStartStepCountScript(metatileBehavior) == TRUE) + return TRUE; + if (UpdateRepelCounter() == TRUE) + return TRUE; + return FALSE; +} + +static bool8 TryStartCoordEventScript(struct MapPosition *position) +{ + u8 *script = GetCoordEventScriptAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); + + if (script == NULL) + return FALSE; + ScriptContext1_SetupScript(script); + return TRUE; +} + +static bool8 TryStartMiscWalkingScripts(u16 metatileBehavior) +{ + s16 x; + s16 y; + + if (MetatileBehavior_IsCrackedFloorHole(metatileBehavior)) + { + ScriptContext1_SetupScript(EventScript_FallDownHole); + return TRUE; + } + else if (MetatileBehavior_IsBattlePyramidWarp(metatileBehavior)) + { + ScriptContext1_SetupScript(BattleFrontier_BattlePyramidEmptySquare_EventScript_252BE8); + return TRUE; + } + else if (MetatileBehavior_IsSecretBaseGlitterMat(metatileBehavior) == TRUE) + { + sub_80FA9D0(); + return FALSE; + } + else if (MetatileBehavior_IsSecretBaseSoundMat(metatileBehavior) == TRUE) + { + PlayerGetDestCoords(&x, &y); + sub_80FA970(MapGridGetMetatileIdAt(x, y)); + return FALSE; + } + return FALSE; +} + +static bool8 TryStartStepCountScript(u16 metatileBehavior) +{ + if (InUnionRoom() == TRUE) + { + return FALSE; + } + + IncrementRematchStepCounter(); + UpdateHappinessStepCounter(); + sub_81D4998(); + + if (!(gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_6) && !MetatileBehavior_IsForcedMovementTile(metatileBehavior)) + { + if (UpdatePoisonStepCounter() == TRUE) + { + ScriptContext1_SetupScript(EventScript_Poison); + return TRUE; + } + if (ShouldEggHatch()) + { + IncrementGameStat(GAME_STAT_HATCHED_EGGS); + ScriptContext1_SetupScript(EventScript_EggHatch); + return TRUE; + } + if (sub_813B3B0() == TRUE) + { + ScriptContext1_SetupScript(gUnknown_08273D1F); + return TRUE; + } + if (ShouldDoBrailleRegicePuzzle() == TRUE) + { + ScriptContext1_SetupScript(IslandCave_EventScript_238EAF); + return TRUE; + } + if (is_tile_that_overrides_player_control() == TRUE) + { + ScriptContext1_SetupScript(MauvilleCity_EventScript_1DF7BA); + return TRUE; + } + if (sub_8138120() == TRUE) + { + ScriptContext1_SetupScript(Route119_EventScript_1F49EC); + return TRUE; + } + if (sub_8138168() == TRUE) + { + ScriptContext1_SetupScript(LittlerootTown_ProfessorBirchsLab_EventScript_1FA4D6); + return TRUE; + } + if (sub_81381B0() == TRUE) + { + ScriptContext1_SetupScript(RustboroCity_Gym_EventScript_21307B); + return TRUE; + } + if (sub_81381F8() == TRUE) + { + ScriptContext1_SetupScript(MossdeepCity_SpaceCenter_2F_EventScript_224175); + return TRUE; + } + } + + if (SafariZoneTakeStep() == TRUE) + return TRUE; + if (CountSSTidalStep(1) == TRUE) + { + ScriptContext1_SetupScript(SSTidalCorridor_EventScript_23C050); + return TRUE; + } + if (sub_8196034()) + return TRUE; + return FALSE; +} + +void Unref_ClearHappinessStepCounter(void) +{ + VarSet(VAR_HAPPINESS_STEP_COUNTER, 0); +} + +static void UpdateHappinessStepCounter(void) +{ + u16 *ptr = GetVarPointer(VAR_HAPPINESS_STEP_COUNTER); + int i; + + (*ptr)++; + (*ptr) %= 128; + if (*ptr == 0) + { + struct Pokemon *mon = gPlayerParty; + for (i = 0; i < PARTY_SIZE; i++) + { + AdjustFriendship(mon, FRIENDSHIP_EVENT_WALKING); + mon++; + } + } +} + +void ClearPoisonStepCounter(void) +{ + VarSet(VAR_POISON_STEP_COUNTER, 0); +} + +static bool8 UpdatePoisonStepCounter(void) +{ + u16 *ptr; + + if (gMapHeader.mapType != MAP_TYPE_SECRET_BASE) + { + ptr = GetVarPointer(VAR_POISON_STEP_COUNTER); + (*ptr)++; + (*ptr) %= 4; + if (*ptr == 0) + { + switch (DoPoisonFieldEffect()) + { + case 0: + return FALSE; + case 1: + return FALSE; + case 2: + return TRUE; + } + } + } + return FALSE; +} + +void RestartWildEncounterImmunitySteps(void) +{ + // Starts at 0 and counts up to 4 steps. + sWildEncounterImmunitySteps = 0; +} + +static bool8 CheckStandardWildEncounter(u16 metatileBehavior) +{ + if (sWildEncounterImmunitySteps < 4) + { + sWildEncounterImmunitySteps++; + sPreviousPlayerMetatileBehavior = metatileBehavior; + return FALSE; + } + + if (StandardWildEncounter(metatileBehavior, sPreviousPlayerMetatileBehavior) == TRUE) + { + sWildEncounterImmunitySteps = 0; + sPreviousPlayerMetatileBehavior = metatileBehavior; + return TRUE; + } + + sPreviousPlayerMetatileBehavior = metatileBehavior; + return FALSE; +} + +static bool8 mapheader_run_first_tag2_script_list_match_conditionally(struct MapPosition *position, u16 metatileBehavior, u8 direction) +{ + s8 warpEventId = GetWarpEventAtMapPosition(&gMapHeader, position); + + if (IsArrowWarpMetatileBehavior(metatileBehavior, direction) == TRUE && warpEventId != -1) + { + StoreInitialPlayerAvatarState(); + sub_809CEB0(&gMapHeader, warpEventId, position); + sub_80AF734(); + return TRUE; + } + return FALSE; +} + +static bool8 TryStartWarpEventScript(struct MapPosition *position, u16 metatileBehavior) +{ + s8 warpEventId = GetWarpEventAtMapPosition(&gMapHeader, position); + + if (warpEventId != -1 && IsWarpMetatileBehavior(metatileBehavior) == TRUE) + { + StoreInitialPlayerAvatarState(); + sub_809CEB0(&gMapHeader, warpEventId, position); + if (MetatileBehavior_IsEscalator(metatileBehavior) == TRUE) + { + sub_80AF80C(metatileBehavior); + return TRUE; + } + if (MetatileBehavior_IsLavaridgeB1FWarp(metatileBehavior) == TRUE) + { + sub_80AF828(); + return TRUE; + } + if (MetatileBehavior_IsLavaridge1FWarp(metatileBehavior) == TRUE) + { + sub_80AF838(); + return TRUE; + } + if (MetatileBehavior_IsAquaHideoutWarp(metatileBehavior) == TRUE) + { + sub_80AF848(); + return TRUE; + } + if (MetatileBehavior_IsWarpOrBridge(metatileBehavior) == TRUE) + { + sub_80B0268(); + return TRUE; + } + if (MetatileBehavior_IsMtPyreHole(metatileBehavior) == TRUE) + { + ScriptContext1_SetupScript(gUnknown_082A8350); + return TRUE; + } + if (MetatileBehavior_IsMossdeepGymWarp(metatileBehavior) == TRUE) + { + sub_80AF87C(); + return TRUE; + } + sub_80AF734(); + return TRUE; + } + return FALSE; +} + +static bool8 IsWarpMetatileBehavior(u16 metatileBehavior) +{ + if (MetatileBehavior_IsWarpDoor(metatileBehavior) != TRUE + && MetatileBehavior_IsLadder(metatileBehavior) != TRUE + && MetatileBehavior_IsEscalator(metatileBehavior) != TRUE + && MetatileBehavior_IsNonAnimDoor(metatileBehavior) != TRUE + && MetatileBehavior_IsLavaridgeB1FWarp(metatileBehavior) != TRUE + && MetatileBehavior_IsLavaridge1FWarp(metatileBehavior) != TRUE + && MetatileBehavior_IsAquaHideoutWarp(metatileBehavior) != TRUE + && MetatileBehavior_IsMtPyreHole(metatileBehavior) != TRUE + && MetatileBehavior_IsMossdeepGymWarp(metatileBehavior) != TRUE + && MetatileBehavior_IsWarpOrBridge(metatileBehavior) != TRUE) + return FALSE; + return TRUE; +} + +static bool8 IsArrowWarpMetatileBehavior(u16 metatileBehavior, u8 direction) +{ + switch (direction) + { + case DIR_NORTH: + return MetatileBehavior_IsNorthArrowWarp(metatileBehavior); + case DIR_SOUTH: + return MetatileBehavior_IsSouthArrowWarp(metatileBehavior); + case DIR_WEST: + return MetatileBehavior_IsWestArrowWarp(metatileBehavior); + case DIR_EAST: + return MetatileBehavior_IsEastArrowWarp(metatileBehavior); + } + return FALSE; +} + +static s8 GetWarpEventAtMapPosition(struct MapHeader *mapHeader, struct MapPosition *position) +{ + return GetWarpEventAtPosition(mapHeader, position->x - 7, position->y - 7, position->height); +} + +static void sub_809CEB0(struct MapHeader *unused, s8 warpEventId, struct MapPosition *position) +{ + const struct WarpEvent *warpEvent; + + u8 trainerHillMapId = GetCurrentTrainerHillMapId(); + + if (trainerHillMapId) + { + if (trainerHillMapId == sub_81D6490()) + { + if (warpEventId == 0) + { + warpEvent = &gMapHeader.events->warps[0]; + } + else + { + warpEvent = sub_81D6120(); + } + } + else if (trainerHillMapId == 5) + { + warpEvent = sub_81D6134(warpEventId); + } + else + { + warpEvent = &gMapHeader.events->warps[warpEventId]; + } + } + else + { + warpEvent = &gMapHeader.events->warps[warpEventId]; + } + + if (warpEvent->mapNum == 0x7F) + { + copy_saved_warp2_bank_and_enter_x_to_warp1(warpEvent->warpId); + } + else + { + const struct MapHeader *mapHeader; + + warp1_set_2(warpEvent->mapGroup, warpEvent->mapNum, warpEvent->warpId); + sub_8084D5C(position->x, position->y); + mapHeader = Overworld_GetMapHeaderByGroupAndId(warpEvent->mapGroup, warpEvent->mapNum); + if (mapHeader->events->warps[warpEvent->warpId].mapNum == 0x7F) + saved_warp2_set(mapHeader->events->warps[warpEventId].warpId, gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, warpEventId); + } +} + +static bool8 map_warp_consider_2_to_inside(struct MapPosition *position, u16 metatileBehavior, u8 direction) +{ + s8 warpEventId; + + if (direction == DIR_NORTH) + { + if (MetatileBehavior_IsOpenSecretBaseDoor(metatileBehavior) == TRUE) + { + sub_80E9668(position, gMapHeader.events); + return TRUE; + } + if (MetatileBehavior_IsWarpDoor(metatileBehavior) == TRUE) + { + warpEventId = GetWarpEventAtMapPosition(&gMapHeader, position); + if (warpEventId != -1 && IsWarpMetatileBehavior(metatileBehavior) == TRUE) + { + StoreInitialPlayerAvatarState(); + sub_809CEB0(&gMapHeader, warpEventId, position); + sub_80AF7D0(); + return TRUE; + } + } + } + return FALSE; +} + +static s8 GetWarpEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) +{ + s32 i; + struct WarpEvent *warpEvent = mapHeader->events->warps; + u8 warpCount = mapHeader->events->warpCount; + + for (i = 0; i < warpCount; i++, warpEvent++) + { + if ((u16)warpEvent->x == x && (u16)warpEvent->y == y) + { + if (warpEvent->elevation == elevation || warpEvent->elevation == 0) + return i; + } + } + return -1; +} + +static u8 *TryRunCoordEventScript(struct CoordEvent *coordEvent) +{ + if (coordEvent != NULL) + { + if (coordEvent->script == NULL) + { + DoCoordEventWeather(coordEvent->trigger); + return NULL; + } + if (coordEvent->trigger == 0) + { + ScriptContext2_RunNewScript(coordEvent->script); + return NULL; + } + if (VarGet(coordEvent->trigger) == (u8)coordEvent->index) + return coordEvent->script; + } + return NULL; +} + +static u8 *GetCoordEventScriptAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) +{ + s32 i; + struct CoordEvent *coordEvents = mapHeader->events->coordEvents; + u8 coordEventCount = mapHeader->events->coordEventCount; + + for (i = 0; i < coordEventCount; i++) + { + if ((u16)coordEvents[i].x == x && (u16)coordEvents[i].y == y) + { + if (coordEvents[i].elevation == elevation || coordEvents[i].elevation == 0) + { + u8 *script = TryRunCoordEventScript(&coordEvents[i]); + if (script != NULL) + return script; + } + } + } + return NULL; +} + +u8 *GetCoordEventScriptAtMapPosition(struct MapPosition *position) +{ + return GetCoordEventScriptAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); +} + +static struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) +{ + u8 i; + struct BgEvent *bgEvents = mapHeader->events->bgEvents; + u8 bgEventCount = mapHeader->events->bgEventCount; + + for (i = 0; i < bgEventCount; i++) + { + if ((u16)bgEvents[i].x == x && (u16)bgEvents[i].y == y) + { + if (bgEvents[i].elevation == elevation || bgEvents[i].elevation == 0) + return &bgEvents[i]; + } + } + return NULL; +} + +bool8 dive_warp(struct MapPosition *position, u16 metatileBehavior) +{ + if (gMapHeader.mapType == MAP_TYPE_UNDERWATER && !MetatileBehavior_IsUnableToEmerge(metatileBehavior)) + { + if (SetDiveWarpEmerge(position->x - 7, position->y - 7)) + { + StoreInitialPlayerAvatarState(); + sp13E_warp_to_last_warp(); + PlaySE(SE_W291); + return TRUE; + } + } + else if (MetatileBehavior_IsDiveable(metatileBehavior) == TRUE) + { + if (SetDiveWarpDive(position->x - 7, position->y - 7)) + { + StoreInitialPlayerAvatarState(); + sp13E_warp_to_last_warp(); + PlaySE(SE_W291); + return TRUE; + } + } + return FALSE; +} + +u8 TrySetDiveWarp(void) +{ + s16 x, y; + u8 metatileBehavior; + + PlayerGetDestCoords(&x, &y); + metatileBehavior = MapGridGetMetatileBehaviorAt(x, y); + if (gMapHeader.mapType == MAP_TYPE_UNDERWATER && !MetatileBehavior_IsUnableToEmerge(metatileBehavior)) + { + if (SetDiveWarpEmerge(x - 7, y - 7) == TRUE) + return 1; + } + else if (MetatileBehavior_IsDiveable(metatileBehavior) == TRUE) + { + if (SetDiveWarpDive(x - 7, y - 7) == TRUE) + return 2; + } + return 0; +} + +const u8 *GetEventObjectScriptPointerPlayerFacing(void) +{ + u8 direction; + struct MapPosition position; + + direction = GetPlayerMovementDirection(); + GetInFrontOfPlayerPosition(&position); + return GetInteractedEventObjectScript(&position, MapGridGetMetatileBehaviorAt(position.x, position.y), direction); +} + +int SetCableClubWarp(void) +{ + struct MapPosition position; + + GetPlayerMovementDirection(); //unnecessary + GetPlayerPosition(&position); + MapGridGetMetatileBehaviorAt(position.x, position.y); //unnecessary + sub_809CEB0(&gMapHeader, GetWarpEventAtMapPosition(&gMapHeader, &position), &position); + return 0; +} diff --git a/src/field_poison.c b/src/field_poison.c index 98819c450..6a519d90c 100644 --- a/src/field_poison.c +++ b/src/field_poison.c @@ -111,7 +111,7 @@ void sub_80F972C(void) ScriptContext1_Stop(); } -unsigned int overworld_poison(void) +int DoPoisonFieldEffect(void) { int i; unsigned int hp; diff --git a/src/field_specials.c b/src/field_specials.c index 113979375..fcef56f14 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -3668,7 +3668,7 @@ bool32 sub_813B374(void) } } -bool32 sub_813B3B0(void) +bool8 sub_813B3B0(void) { static const u8 gUnknown_085B3410[] = { 0x1d, 0x1d, 0x1e, 0x1e, 0x1f, 0x1f, 0x21, 0x21, 0x14, 0x14, 0x28, 0x28, 0x2a, 0x2a, 0x2c, 0x2c }; diff --git a/src/metatile_behavior.c b/src/metatile_behavior.c index 5a7d4ae83..dd594f075 100644 --- a/src/metatile_behavior.c +++ b/src/metatile_behavior.c @@ -679,7 +679,7 @@ bool8 MetatileBehavior_IsSecretBasePC(u8 metatileBehavior) return FALSE; } -bool8 MetatileBehavior_IsSecretBaseRegisterPC(u8 metatileBehavior) +bool8 MetatileBehavior_IsRecordMixingSecretBasePC(u8 metatileBehavior) { if (metatileBehavior == MB_SECRET_BASE_REGISTER_PC) return TRUE; @@ -815,7 +815,7 @@ bool8 MetatileBehavior_IsSecretBaseSandOrnament(u8 metatileBehavior) return FALSE; } -bool8 MetatileBehavior_IsSecretBaseTvOrShield(u8 metatileBehavior) +bool8 MetatileBehavior_IsSecretBaseShieldOrToyTV(u8 metatileBehavior) { if (metatileBehavior == MB_SECRET_BASE_TV_SHIELD) return TRUE; @@ -1164,7 +1164,7 @@ bool8 MetatileBehavior_IsRegionMap(u8 metatileBehavior) return FALSE; } -bool8 MetatileBehavior_IsClosedSootopolisGymDoor(u8 metatileBehavior) +bool8 MetatileBehavior_IsClosedSootopolisDoor(u8 metatileBehavior) { if (metatileBehavior == MB_CLOSED_SOOTOPOLIS_DOOR) return TRUE; @@ -1365,7 +1365,7 @@ bool8 MetatileBehavior_IsCuttableGrass(u8 metatileBehavior) return FALSE; } -bool8 MetatileBehavior_IsRunningShoesInstruction(u8 metatileBehavior) +bool8 MetatileBehavior_IsRunningShoesManual(u8 metatileBehavior) { if (metatileBehavior == MB_RUNNING_SHOES_INSTRUCTION) return TRUE; diff --git a/src/overworld.c b/src/overworld.c index 683679218..94c3f8328 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -107,7 +107,7 @@ extern void sub_80A0A2C(void); extern void not_trainer_hill_battle_pyramid(void); extern void apply_map_tileset2_palette(const struct MapLayout *); extern void copy_map_tileset2_to_vram_2(const struct MapLayout *); -extern void prev_quest_postbuffer_cursor_backup_reset(void); +extern void RestartWildEncounterImmunitySteps(void); extern void ShowMapNamePopup(void); extern bool32 InTrainerHill(void); extern bool32 sub_808651C(void); @@ -164,8 +164,8 @@ extern bool32 sub_8009F3C(void); extern void sub_8010198(void); extern u32 sub_800B4DC(void); extern bool32 sub_80B39D4(u8); -extern const u8* sub_809C2C8(struct MapPosition *a1, u8, u8); -extern u8 *sub_809D0F4(void*); +extern const u8* GetInteractedLinkPlayerScript(struct MapPosition *a1, u8, u8); +extern u8 *GetCoordEventScriptAtMapPosition(void*); extern u8 sub_808BD6C(u8); extern u8 sub_808BD7C(u8); extern void UpdateEventObjectSpriteVisibility(struct Sprite*, u8); @@ -827,7 +827,7 @@ void mliX_load_map(u8 mapGroup, u8 mapNum) TrySetMapSaveWarpStatus(); ClearTempFieldEventData(); ResetCyclingRoadChallengeData(); - prev_quest_postbuffer_cursor_backup_reset(); + RestartWildEncounterImmunitySteps(); TryUpdateRandomTrainerRematches(mapGroup, mapNum); DoTimeBasedEvents(); SetSav1WeatherFromCurrMapHeader(); @@ -876,7 +876,7 @@ static void mli0_load_map(u32 a1) TrySetMapSaveWarpStatus(); ClearTempFieldEventData(); ResetCyclingRoadChallengeData(); - prev_quest_postbuffer_cursor_backup_reset(); + RestartWildEncounterImmunitySteps(); TryUpdateRandomTrainerRematches(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum); if (a1 != 1) DoTimeBasedEvents(); @@ -2706,7 +2706,7 @@ static u8 *sub_8087370(struct UnkStruct_8054FF8 *a1) { if (a1->c != 2) return 0; - return sub_809D0F4(&a1->sub); + return GetCoordEventScriptAtMapPosition(&a1->sub); } static bool32 sub_8087388(struct UnkStruct_8054FF8 *a1) @@ -2747,7 +2747,7 @@ static const u8 *sub_80873B4(struct UnkStruct_8054FF8 *a1) return EventScript_TradeRoom_ReadTrainerCard2; } - return sub_809C2C8(&unkStruct, a1->field_C, a1->d); + return GetInteractedLinkPlayerScript(&unkStruct, a1->field_C, a1->d); } static u16 sub_8087480(const u8 *script) diff --git a/src/pokemon.c b/src/pokemon.c index 40f7e94b2..7227d28cb 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -3251,7 +3251,7 @@ u8 GetLevelFromMonExp(struct Pokemon *mon) u32 exp = GetMonData(mon, MON_DATA_EXP, NULL); s32 level = 1; - while (level <= MAX_MON_LEVEL && gExperienceTables[gBaseStats[species].growthRate][level] <= exp) + while (level <= MAX_LEVEL && gExperienceTables[gBaseStats[species].growthRate][level] <= exp) level++; return level - 1; @@ -3263,7 +3263,7 @@ u8 GetLevelFromBoxMonExp(struct BoxPokemon *boxMon) u32 exp = GetBoxMonData(boxMon, MON_DATA_EXP, NULL); s32 level = 1; - while (level <= MAX_MON_LEVEL && gExperienceTables[gBaseStats[species].growthRate][level] <= exp) + while (level <= MAX_LEVEL && gExperienceTables[gBaseStats[species].growthRate][level] <= exp) level++; return level - 1; @@ -5145,7 +5145,7 @@ bool8 PokemonUseItemEffects(struct Pokemon *mon, u16 item, u8 partyIndex, u8 mov retVal = FALSE; } if ((itemEffect[cmdIndex] & 0x40) // raise level - && GetMonData(mon, MON_DATA_LEVEL, NULL) != MAX_MON_LEVEL) + && GetMonData(mon, MON_DATA_LEVEL, NULL) != MAX_LEVEL) { dataUnsigned = gExperienceTables[gBaseStats[GetMonData(mon, MON_DATA_SPECIES, NULL)].growthRate][GetMonData(mon, MON_DATA_LEVEL, NULL) + 1]; SetMonData(mon, MON_DATA_EXP, &dataUnsigned); @@ -6430,12 +6430,12 @@ bool8 TryIncrementMonLevel(struct Pokemon *mon) u16 species = GetMonData(mon, MON_DATA_SPECIES, 0); u8 nextLevel = GetMonData(mon, MON_DATA_LEVEL, 0) + 1; u32 expPoints = GetMonData(mon, MON_DATA_EXP, 0); - if (expPoints > gExperienceTables[gBaseStats[species].growthRate][MAX_MON_LEVEL]) + if (expPoints > gExperienceTables[gBaseStats[species].growthRate][MAX_LEVEL]) { - expPoints = gExperienceTables[gBaseStats[species].growthRate][MAX_MON_LEVEL]; + expPoints = gExperienceTables[gBaseStats[species].growthRate][MAX_LEVEL]; SetMonData(mon, MON_DATA_EXP, &expPoints); } - if (nextLevel > MAX_MON_LEVEL || expPoints < gExperienceTables[gBaseStats[species].growthRate][nextLevel]) + if (nextLevel > MAX_LEVEL || expPoints < gExperienceTables[gBaseStats[species].growthRate][nextLevel]) { return FALSE; } diff --git a/src/pokemon_summary_screen.c b/src/pokemon_summary_screen.c index df596749e..db2a07909 100644 --- a/src/pokemon_summary_screen.c +++ b/src/pokemon_summary_screen.c @@ -2533,7 +2533,7 @@ static void DrawExperienceProgressBar(struct Pokemon *unused) u16 *r9; u8 i; - if (summary->level < MAX_MON_LEVEL) + if (summary->level < MAX_LEVEL) { u32 expBetweenLevels = gExperienceTables[gBaseStats[summary->species].growthRate][summary->level + 1] - gExperienceTables[gBaseStats[summary->species].growthRate][summary->level]; u32 expSinceLastLevel = summary->exp - gExperienceTables[gBaseStats[summary->species].growthRate][summary->level]; @@ -3349,7 +3349,7 @@ static void PrintExpPointsNextLevel(void) offset = GetStringRightAlignXOffset(1, gStringVar1, 42) + 2; SummaryScreen_PrintTextOnWindow(windowId, gStringVar1, offset, 1, 0, 0); - if (sum->level < MAX_MON_LEVEL) + if (sum->level < MAX_LEVEL) expToNextLevel = gExperienceTables[gBaseStats[sum->species].growthRate][sum->level + 1] - sum->exp; else expToNextLevel = 0; diff --git a/src/rom_8011DC0.c b/src/rom_8011DC0.c index dc94cb9d5..bf16a20fc 100644 --- a/src/rom_8011DC0.c +++ b/src/rom_8011DC0.c @@ -1646,7 +1646,7 @@ void sub_8014304(s8 mapGroup, s8 mapNum, s32 x, s32 y, u16 arg4) VarSet(VAR_0x4087, arg4); gFieldLinkPlayerCount = GetLinkPlayerCount(); gUnknown_03005DB4 = GetMultiplayerId(); - sub_809D2BC(); + SetCableClubWarp(); Overworld_SetWarpDestination(mapGroup, mapNum, -1, x, y); WarpIntoMap(); } diff --git a/src/scrcmd.c b/src/scrcmd.c index acaf78576..fa3e21996 100644 --- a/src/scrcmd.c +++ b/src/scrcmd.c @@ -52,7 +52,7 @@ typedef u16 (*SpecialFunc)(void); typedef void (*NativeFunc)(void); -extern u32 gUnknown_020375C0; +extern const u8 *gUnknown_020375C0; static EWRAM_DATA u32 gUnknown_020375C4 = 0; static EWRAM_DATA u16 sPauseCounter = 0; @@ -284,7 +284,7 @@ bool8 ScrCmd_callstd_if(struct ScriptContext *ctx) bool8 ScrCmd_gotoram(struct ScriptContext *ctx) { - ScriptJump(ctx, (const u8 *)gUnknown_020375C0); + ScriptJump(ctx, gUnknown_020375C0); return FALSE; } @@ -2223,7 +2223,7 @@ bool8 ScrCmd_cmdCF(struct ScriptContext *ctx) if (v1) { - ((u8*)gUnknown_020375C0) = ctx->scriptPtr; + gUnknown_020375C0 = ctx->scriptPtr; ScriptJump(ctx, v1); } return FALSE; diff --git a/src/script.c b/src/script.c index 8c868128b..c61ae7183 100644 --- a/src/script.c +++ b/src/script.c @@ -5,7 +5,7 @@ #define RAM_SCRIPT_MAGIC 51 -extern u8* gUnknown_020375C0; +extern const u8* gUnknown_020375C0; extern bool32 sub_801B27C(void); @@ -344,7 +344,7 @@ void ClearRamScript(void) CpuFill32(0, &gSaveBlock1Ptr->ramScript, sizeof(struct RamScript)); } -bool8 InitRamScript(u8 *script, u16 scriptSize, u8 mapGroup, u8 mapNum, u8 objectId) +bool8 InitRamScript(const u8 *script, u16 scriptSize, u8 mapGroup, u8 mapNum, u8 objectId) { struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data; @@ -362,7 +362,7 @@ bool8 InitRamScript(u8 *script, u16 scriptSize, u8 mapGroup, u8 mapNum, u8 objec return TRUE; } -u8 *GetRamScript(u8 objectId, u8 *script) +const u8 *GetRamScript(u8 objectId, const u8 *script) { struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data; gUnknown_020375C0 = NULL; diff --git a/src/secret_base.c b/src/secret_base.c index 441bdef04..a684f3982 100644 --- a/src/secret_base.c +++ b/src/secret_base.c @@ -596,13 +596,13 @@ void sub_80E95D4(void) VarSet(VAR_OBJ_GFX_ID_F, gUnknown_0858D060[sub_80EA20C(VarGet(VAR_0x4054))]); } -void sub_80E9608(struct Coords16 *coords, struct MapEvents *events) +void sub_80E9608(const struct MapPosition *position, const struct MapEvents *events) { s16 bgEventIdx; for (bgEventIdx = 0; bgEventIdx < events->bgEventCount; bgEventIdx ++) { - if (events->bgEvents[bgEventIdx].kind == BG_EVENT_SECRET_BASE && coords->x == events->bgEvents[bgEventIdx].x + 7 && coords->y == events->bgEvents[bgEventIdx].y + 7) + if (events->bgEvents[bgEventIdx].kind == BG_EVENT_SECRET_BASE && position->x == events->bgEvents[bgEventIdx].x + 7 && position->y == events->bgEvents[bgEventIdx].y + 7) { sCurSecretBaseId = events->bgEvents[bgEventIdx].bgUnion.secretBaseId; break; @@ -610,9 +610,9 @@ void sub_80E9608(struct Coords16 *coords, struct MapEvents *events) } } -void sub_80E9668(struct Coords16 *coords, struct MapEvents *events) +void sub_80E9668(const struct MapPosition *position, const struct MapEvents *events) { - sub_80E9608(coords, events); + sub_80E9608(position, events); sub_80E8B6C(); ScriptContext1_SetupScript(EventScript_275BB7); } |