From afb9ff3a40dcfb2681ef274752bceb726d14b783 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 1 Nov 2021 18:06:15 -0400 Subject: [PATCH 1/2] Document files with a few remaining symbols --- gflib/sprite.c | 103 ++- gflib/sprite.h | 7 +- include/constants/event_object_movement.h | 15 + include/constants/metatile_behaviors.h | 6 +- include/event_object_movement.h | 12 +- include/field_weather.h | 2 +- include/global.fieldmap.h | 2 +- include/international_string_util.h | 2 +- include/link.h | 3 +- include/menu_helpers.h | 4 +- include/metatile_behavior.h | 9 +- include/overworld.h | 4 +- include/palette_util.h | 2 +- include/text_window.h | 4 +- src/battle_dome.c | 18 +- src/battle_pyramid_bag.c | 14 +- src/berry_tag_screen.c | 6 +- src/bike.c | 2 +- src/confetti_util.c | 37 - .../object_events/movement_type_func_tables.h | 22 +- src/decompress.c | 2 +- src/easy_chat.c | 2 +- src/event_object_movement.c | 632 +++++++----------- src/field_control_avatar.c | 7 +- src/field_player_avatar.c | 97 +-- src/field_weather.c | 4 +- src/field_weather_effect.c | 2 +- src/international_string_util.c | 9 +- src/item_menu.c | 16 +- src/item_use.c | 2 +- src/link.c | 4 +- src/mail.c | 6 +- src/menu_helpers.c | 46 +- src/metatile_behavior.c | 23 +- src/minigame_countdown.c | 2 +- src/mystery_event_menu.c | 7 +- src/mystery_gift_menu.c | 2 +- src/overworld.c | 34 +- src/palette_util.c | 41 +- src/party_menu.c | 16 +- src/pokeblock.c | 10 +- src/pokeblock_feed.c | 4 +- src/pokemon_jump.c | 2 +- src/pokemon_summary_screen.c | 14 +- src/pokenav.c | 4 +- src/pokenav_match_call_ui.c | 5 +- src/roulette.c | 8 +- src/scrcmd.c | 6 +- src/start_menu.c | 6 +- src/text_window.c | 4 +- src/trainer_card.c | 6 +- src/union_room_chat.c | 14 +- 52 files changed, 582 insertions(+), 729 deletions(-) diff --git a/gflib/sprite.c b/gflib/sprite.c index 9629fc31b..f05fe76f2 100644 --- a/gflib/sprite.c +++ b/gflib/sprite.c @@ -7,6 +7,9 @@ #define OAM_MATRIX_COUNT 32 +#define sAnchorX data[6] +#define sAnchorY data[7] + #define SET_SPRITE_TILE_RANGE(index, start, count) \ { \ sSpriteTileRanges[index * 2] = start; \ @@ -91,7 +94,7 @@ static void ApplyAffineAnimFrame(u8 matrixNum, struct AffineAnimFrameCmd *frameC static u8 IndexOfSpriteTileTag(u16 tag); static void AllocSpriteTileRange(u16 tag, u16 start, u16 count); static void DoLoadSpritePalette(const u16 *src, u16 paletteOffset); -static void obj_update_pos2(struct Sprite* sprite, s32 a1, s32 a2); +static void UpdateSpriteMatrixAnchorPos(struct Sprite* sprite, s32 a1, s32 a2); typedef void (*AnimFunc)(struct Sprite *); typedef void (*AnimCmdFunc)(struct Sprite *); @@ -99,13 +102,13 @@ typedef void (*AffineAnimCmdFunc)(u8 matrixNum, struct Sprite *); #define DUMMY_OAM_DATA \ { \ - .y = 160, \ + .y = DISPLAY_HEIGHT, \ .affineMode = 0, \ .objMode = 0, \ .mosaic = 0, \ .bpp = 0, \ .shape = SPRITE_SHAPE(8x8), \ - .x = 304, \ + .x = DISPLAY_WIDTH + 64, \ .matrixNum = 0, \ .size = SPRITE_SIZE(8x8), \ .tileNum = 0, \ @@ -159,41 +162,11 @@ static const struct Sprite sDummySprite = { .oam = DUMMY_OAM_DATA, .anims = gDummySpriteAnimTable, - .images = NULL, .affineAnims = gDummySpriteAffineAnimTable, .template = &gDummySpriteTemplate, - .subspriteTables = NULL, .callback = SpriteCallbackDummy, - .x = 304, .y = 160, - .x2 = 0, .y2 = 0, - .centerToCornerVecX = 0, - .centerToCornerVecY = 0, - .animNum = 0, - .animCmdIndex = 0, - .animDelayCounter = 0, - .animPaused = 0, - .affineAnimPaused = 0, - .animLoopCounter = 0, - .data = {0, 0, 0, 0, 0, 0, 0}, - .inUse = 0, - .coordOffsetEnabled = 0, - .invisible = FALSE, - .flags_3 = 0, - .flags_4 = 0, - .flags_5 = 0, - .flags_6 = 0, - .flags_7 = 0, - .hFlip = 0, - .vFlip = 0, - .animBeginning = 0, - .affineAnimBeginning = 0, - .animEnded = 0, - .affineAnimEnded = 0, - .usingSheet = 0, - .flags_f = 0, - .sheetTileStart = 0, - .subspriteTableNum = 0, - .subspriteMode = 0, + .x = DISPLAY_WIDTH + 64, + .y = DISPLAY_HEIGHT, .subpriority = 0xFF }; @@ -890,16 +863,26 @@ void ResetAllSprites(void) ResetSprite(&gSprites[i]); } -// UB: template pointer may point to freed temporary storage void FreeSpriteTiles(struct Sprite *sprite) { +// UB: template pointer may point to freed temporary storage +#ifdef UBFIX + if (!sprite || !sprite->template) + return; +#endif + if (sprite->template->tileTag != TAG_NONE) FreeSpriteTilesByTag(sprite->template->tileTag); } -// UB: template pointer may point to freed temporary storage void FreeSpritePalette(struct Sprite *sprite) { +// UB: template pointer may point to freed temporary storage +#ifdef UBFIX + if (!sprite || !sprite->template) + return; +#endif + FreeSpritePaletteByTag(sprite->template->paletteTag); } @@ -1098,8 +1081,8 @@ void BeginAffineAnim(struct Sprite *sprite) sprite->affineAnimEnded = FALSE; ApplyAffineAnimFrame(matrixNum, &frameCmd); sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; - if (sprite->flags_f) - obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); + if (sprite->anchored) + UpdateSpriteMatrixAnchorPos(sprite, sprite->sAnchorX, sprite->sAnchorY); } } @@ -1124,8 +1107,8 @@ void ContinueAffineAnim(struct Sprite *sprite) funcIndex = type - 32765; sAffineAnimCmdFuncs[funcIndex](matrixNum, sprite); } - if (sprite->flags_f) - obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); + if (sprite->anchored) + UpdateSpriteMatrixAnchorPos(sprite, sprite->sAnchorX, sprite->sAnchorY); } } @@ -1219,14 +1202,16 @@ u8 GetSpriteMatrixNum(struct Sprite *sprite) return matrixNum; } -void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3) +// Used to shift a sprite's position as it scales. +// Only used by the minigame countdown, so that for instance the numbers don't slide up as they squish down before jumping. +void SetSpriteMatrixAnchor(struct Sprite* sprite, s16 x, s16 y) { - sprite->data[6] = a2; - sprite->data[7] = a3; - sprite->flags_f = 1; + sprite->sAnchorX = x; + sprite->sAnchorY = y; + sprite->anchored = TRUE; } -s32 sub_8007E28(s32 a0, s32 a1, s32 a2) +static s32 GetAnchorCoord(s32 a0, s32 a1, s32 coord) { s32 subResult, var1; @@ -1235,27 +1220,27 @@ s32 sub_8007E28(s32 a0, s32 a1, s32 a2) var1 = -(subResult) >> 9; else var1 = -(subResult >> 9); - return a2 - ((u32)(a2 * a1) / (u32)(a0) + var1); + return coord - ((u32)(coord * a1) / (u32)(a0) + var1); } -void obj_update_pos2(struct Sprite *sprite, s32 a1, s32 a2) +static void UpdateSpriteMatrixAnchorPos(struct Sprite *sprite, s32 x, s32 y) { - s32 var0, var1, var2; + s32 dimension, var1, var2; u32 matrixNum = sprite->oam.matrixNum; - if (a1 != 0x800) + if (x != NO_ANCHOR) { - var0 = sOamDimensions32[sprite->oam.shape][sprite->oam.size].width; - var1 = var0 << 8; - var2 = (var0 << 16) / gOamMatrices[matrixNum].a; - sprite->x2 = sub_8007E28(var1, var2, a1); + dimension = sOamDimensions32[sprite->oam.shape][sprite->oam.size].width; + var1 = dimension << 8; + var2 = (dimension << 16) / gOamMatrices[matrixNum].a; + sprite->x2 = GetAnchorCoord(var1, var2, x); } - if (a2 != 0x800) + if (y != NO_ANCHOR) { - var0 = sOamDimensions32[sprite->oam.shape][sprite->oam.size].height; - var1 = var0 << 8; - var2 = (var0 << 16) / gOamMatrices[matrixNum].d; - sprite->y2 = sub_8007E28(var1, var2, a2); + dimension = sOamDimensions32[sprite->oam.shape][sprite->oam.size].height; + var1 = dimension << 8; + var2 = (dimension << 16) / gOamMatrices[matrixNum].d; + sprite->y2 = GetAnchorCoord(var1, var2, y); } } diff --git a/gflib/sprite.h b/gflib/sprite.h index 89299d43e..595ecd7db 100644 --- a/gflib/sprite.h +++ b/gflib/sprite.h @@ -5,6 +5,9 @@ #define SPRITE_NONE 0xFF #define TAG_NONE 0xFFFF +// Given to SetSpriteMatrixAnchor to skip anchoring one of the coords. +#define NO_ANCHOR 0x800 + struct SpriteSheet { const void *data; // Raw uncompressed pixel data @@ -227,7 +230,7 @@ struct Sprite bool16 animEnded:1; //0x10 bool16 affineAnimEnded:1; //0x20 bool16 usingSheet:1; //0x40 - bool16 flags_f:1; //0x80 + bool16 anchored:1; //0x80 /*0x40*/ u16 sheetTileStart; @@ -280,7 +283,7 @@ void FreeSpriteOamMatrix(struct Sprite *sprite); void DestroySpriteAndFreeResources(struct Sprite *sprite); void sub_800142C(u32 a1, u32 a2, u16 *a3, u16 a4, u32 a5); void AnimateSprite(struct Sprite *sprite); -void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3); +void SetSpriteMatrixAnchor(struct Sprite* sprite, s16 x, s16 y); void StartSpriteAnim(struct Sprite *sprite, u8 animNum); void StartSpriteAnimIfDifferent(struct Sprite *sprite, u8 animNum); void SeekSpriteAnim(struct Sprite *sprite, u8 animCmdIndex); diff --git a/include/constants/event_object_movement.h b/include/constants/event_object_movement.h index e7238b56e..9611d008b 100755 --- a/include/constants/event_object_movement.h +++ b/include/constants/event_object_movement.h @@ -82,6 +82,7 @@ #define MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_UP 0x4E #define MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_LEFT 0x4F #define MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_RIGHT 0x50 +#define NUM_MOVEMENT_TYPES 0x51 #define MOVEMENT_ACTION_FACE_DOWN 0x0 #define MOVEMENT_ACTION_FACE_UP 0x1 @@ -318,4 +319,18 @@ #define ANIM_HOOKED_POKEMON_WEST 10 #define ANIM_HOOKED_POKEMON_EAST 11 +// IDs for how NPCs that copy player movement should respond. +// Most go unused. +#define COPY_MOVE_NONE 0 +#define COPY_MOVE_FACE 1 +#define COPY_MOVE_WALK 2 +#define COPY_MOVE_WALK_FAST 3 +#define COPY_MOVE_WALK_FASTER 4 +#define COPY_MOVE_SLIDE 5 +#define COPY_MOVE_JUMP_IN_PLACE 6 +#define COPY_MOVE_JUMP 7 +#define COPY_MOVE_JUMP2 8 +#define COPY_MOVE_EMPTY_1 9 +#define COPY_MOVE_EMPTY_2 10 + #endif // GUARD_CONSTANTS_EVENT_OBJECT_MOVEMENT_H diff --git a/include/constants/metatile_behaviors.h b/include/constants/metatile_behaviors.h index df2d28c7d..dde821358 100755 --- a/include/constants/metatile_behaviors.h +++ b/include/constants/metatile_behaviors.h @@ -127,8 +127,8 @@ #define MB_BRIDGE_OVER_POND_MED_EDGE_2 0x7B #define MB_BRIDGE_OVER_POND_HIGH_EDGE_1 0x7C #define MB_BRIDGE_OVER_POND_HIGH_EDGE_2 0x7D -#define MB_UNUSED_BRIDGE_1 0x7E -#define MB_UNUSED_BRIDGE_2 0x7F +#define MB_UNUSED_BRIDGE 0x7E +#define MB_BIKE_BRIDGE_OVER_BARRIER 0x7F #define MB_COUNTER 0x80 #define MB_UNUSED_81 0x81 #define MB_UNUSED_82 0x82 @@ -199,7 +199,7 @@ #define MB_HOLDS_LARGE_DECORATION 0xC3 #define MB_SECRET_BASE_TV_SHIELD 0xC4 #define MB_PLAYER_ROOM_PC_ON 0xC5 -#define MB_C6 0xC6 +#define MB_SECRET_BASE_DECORATION_BASE 0xC6 #define MB_SECRET_BASE_POSTER 0xC7 #define MB_UNUSED_C8 0xC8 #define MB_UNUSED_C9 0xC9 diff --git a/include/event_object_movement.h b/include/event_object_movement.h index 11dac813d..9ed524e66 100644 --- a/include/event_object_movement.h +++ b/include/event_object_movement.h @@ -1,6 +1,8 @@ #ifndef GUARD_EVENT_OBJECT_MOVEMENT_H #define GUARD_EVENT_OBJECT_MOVEMENT_H +#include "constants/event_object_movement.h" + enum SpinnerRunnerFollowPatterns { RUNFOLLOW_ANY, @@ -395,13 +397,13 @@ u8 MovementType_CopyPlayer_Step1(struct ObjectEvent *, struct Sprite *); u8 MovementType_CopyPlayer_Step2(struct ObjectEvent *, struct Sprite *); bool8 CopyablePlayerMovement_None(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); bool8 CopyablePlayerMovement_FaceDirection(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); -bool8 CopyablePlayerMovement_GoSpeed0(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); -bool8 CopyablePlayerMovement_GoSpeed1(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); -bool8 CopyablePlayerMovement_GoSpeed2(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); +bool8 CopyablePlayerMovement_WalkNormal(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); +bool8 CopyablePlayerMovement_WalkFast(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); +bool8 CopyablePlayerMovement_WalkFaster(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); bool8 CopyablePlayerMovement_Slide(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); -bool8 cph_IM_DIFFERENT(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); -bool8 CopyablePlayerMovement_GoSpeed4(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); +bool8 CopyablePlayerMovement_JumpInPlace(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); bool8 CopyablePlayerMovement_Jump(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); +bool8 CopyablePlayerMovement_Jump2(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)); u8 MovementType_CopyPlayerInGrass_Step1(struct ObjectEvent *, struct Sprite *); u8 MovementType_Buried_Step0(struct ObjectEvent *, struct Sprite *); u8 MovementType_WalkInPlace_Step0(struct ObjectEvent *, struct Sprite *); diff --git a/include/field_weather.h b/include/field_weather.h index 9c6a4ab7a..72a56ab6e 100644 --- a/include/field_weather.h +++ b/include/field_weather.h @@ -147,7 +147,7 @@ void SetNextWeather(u8 weather); void SetCurrentAndNextWeather(u8 weather); void SetCurrentAndNextWeatherNoDelay(u8 weather); void ApplyWeatherGammaShiftIfIdle(s8 gammaIndex); -void sub_80ABC7C(u8 gammaIndex, u8 gammaTargetIndex, u8 gammaStepDelay); +void ApplyWeatherGammaShiftIfIdle_Gradual(u8 gammaIndex, u8 gammaTargetIndex, u8 gammaStepDelay); void FadeScreen(u8 mode, s8 delay); bool8 IsWeatherNotFadingIn(void); void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex); diff --git a/include/global.fieldmap.h b/include/global.fieldmap.h index 71ccd7068..33be942c8 100644 --- a/include/global.fieldmap.h +++ b/include/global.fieldmap.h @@ -211,7 +211,7 @@ struct ObjectEvent /*0x1F*/ u8 previousMetatileBehavior; /*0x20*/ u8 previousMovementDirection; /*0x21*/ u8 directionSequenceIndex; - /*0x22*/ u8 playerCopyableMovement; + /*0x22*/ u8 playerCopyableMovement; // COPY_MOVE_* /*size = 0x24*/ }; diff --git a/include/international_string_util.h b/include/international_string_util.h index 54f4c008f..dd5c6ac5f 100644 --- a/include/international_string_util.h +++ b/include/international_string_util.h @@ -19,6 +19,6 @@ void PadNameString(u8 *dest, u8 padChar); void ConvertInternationalPlayerNameStripChar(u8 *, u8); void ConvertInternationalContestantName(u8 *); int GetNicknameLanguage(u8 *); -void sub_81DB620(int windowId, int columnStart, int rowStart, int numFillTiles, int numRows); +void FillWindowTilesByRow(int windowId, int columnStart, int rowStart, int numFillTiles, int numRows); #endif // GUARD_INTERNATIONAL_STRING_UTIL_H diff --git a/include/link.h b/include/link.h index 6061745af..96a76e120 100644 --- a/include/link.h +++ b/include/link.h @@ -5,6 +5,7 @@ #define MAX_RFU_PLAYERS 5 #define CMD_LENGTH 8 #define QUEUE_CAPACITY 50 +#define OVERWORLD_RECV_QUEUE_MAX 3 #define BLOCK_BUFFER_SIZE 0x100 #define LINK_SLAVE 0 @@ -301,7 +302,7 @@ bool32 Link_AnyPartnersPlayingFRLG_JP(void); void ResetLinkPlayerCount(void); void SaveLinkPlayers(u8 a0); void SetWirelessCommType0(void); -bool32 IsLinkRecvQueueLengthAtLeast3(void); +bool32 IsLinkRecvQueueAtOverworldMax(void); extern u16 gLinkPartnersHeldKeys[6]; extern u32 gLinkDebugSeed; diff --git a/include/menu_helpers.h b/include/menu_helpers.h index df71ce252..0e063e5c1 100644 --- a/include/menu_helpers.h +++ b/include/menu_helpers.h @@ -27,8 +27,8 @@ u8 GetLRKeysPressed(void); u8 GetLRKeysPressedAndHeld(void); bool8 IsHoldingItemAllowed(u16 itemId); bool8 IsWritingMailAllowed(u16 itemId); -bool8 MenuHelpers_LinkSomething(void); -bool8 MenuHelpers_CallLinkSomething(void); +bool8 MenuHelpers_IsLinkActive(void); +bool8 MenuHelpers_ShouldWaitForLinkRecv(void); void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage); void SetCursorWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 totalItems); void SetCursorScrollWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 shownItems, u8 totalItems, u8 maxShownItems); diff --git a/include/metatile_behavior.h b/include/metatile_behavior.h index 87a9ebcc5..23c84dffb 100644 --- a/include/metatile_behavior.h +++ b/include/metatile_behavior.h @@ -23,7 +23,6 @@ bool8 MetatileBehavior_IsIce(u8); bool8 MetatileBehavior_IsWarpDoor(u8); bool8 MetatileBehavior_IsDoor(u8); bool8 MetatileBehavior_IsEscalator(u8); -bool8 MetatileBehavior_IsMB_04(u8); bool8 MetatileBehavior_IsLadder(u8); bool8 MetatileBehavior_IsNonAnimDoor(u8); bool8 MetatileBehavior_IsDeepSouthWarp(u8); @@ -36,7 +35,6 @@ bool8 MetatileBehavior_IsArrowWarp(u8); bool8 MetatileBehavior_IsForcedMovementTile(u8); bool8 MetatileBehavior_IsIce_2(u8); bool8 MetatileBehavior_IsTrickHouseSlipperyFloor(u8); -bool8 MetatileBehavior_IsMB_05(u8); bool8 MetatileBehavior_IsWalkNorth(u8); bool8 MetatileBehavior_IsWalkSouth(u8); bool8 MetatileBehavior_IsWalkWest(u8); @@ -59,14 +57,12 @@ bool8 MetatileBehavior_IsSecretBaseTree(u8); bool8 MetatileBehavior_IsSecretBaseShrub(u8); bool8 MetatileBehavior_IsSecretBasePC(u8); bool8 MetatileBehavior_IsRecordMixingSecretBasePC(u8); -bool8 MetatileBehavior_IsMB_B2(u8); bool8 MetatileBehavior_IsBlockDecoration(u8); bool8 MetatileBehavior_IsSecretBaseImpassable(u8); -bool8 MetatileBehavior_IsMB_C6(u8); +bool8 MetatileBehavior_IsSecretBaseDecorationBase(u8); bool8 MetatileBehavior_IsSecretBasePoster(u8); bool8 MetatileBehavior_IsNormal(u8); bool8 MetatileBehavior_IsSecretBaseNorthWall(u8); -bool8 MetatileBehavior_IsMB_B2_Duplicate(u8); bool8 MetatileBehavior_HoldsSmallDecoration(u8); bool8 MetatileBehavior_HoldsLargeDecoration(u8); bool8 MetatileBehavior_IsSecretBaseHole(u8); @@ -97,7 +93,6 @@ bool8 MetatileBehavior_IsShallowFlowingWater(u8); bool8 MetatileBehavior_IsThinIce(u8); bool8 MetatileBehavior_IsCrackedIce(u8); bool8 MetatileBehavior_IsDeepOrOceanWater(u8); -bool8 MetatileBehavior_IsMB_18_OrMB_1A(u8); bool8 MetatileBehavior_IsSurfableAndNotWaterfall(u8); bool8 MetatileBehavior_IsEastBlocked(u8); bool8 MetatileBehavior_IsWestBlocked(u8); @@ -123,7 +118,7 @@ bool8 MetatileBehavior_IsSecretBaseSpinMat(u8); bool8 MetatileBehavior_IsLavaridgeB1FWarp(u8); bool8 MetatileBehavior_IsLavaridge1FWarp(u8); bool8 MetatileBehavior_IsAquaHideoutWarp(u8); -bool8 MetatileBehavior_IsBridgeOverOcean(u8); +bool8 MetatileBehavior_IsUnionRoomWarp(u8); bool8 MetatileBehavior_IsMossdeepGymWarp(u8); bool8 MetatileBehavior_IsSurfableFishableWater(u8); bool8 MetatileBehavior_IsMtPyreHole(u8); diff --git a/include/overworld.h b/include/overworld.h index 8005a2222..e1cf100db 100644 --- a/include/overworld.h +++ b/include/overworld.h @@ -127,7 +127,7 @@ u8 GetSavedWarpRegionMapSectionId(void); u8 GetCurrentRegionMapSectionId(void); u8 GetCurrentMapBattleScene(void); void CleanupOverworldWindowsAndTilemaps(void); -bool32 IsUpdateLinkStateCBActive(void); +bool32 IsOverworldLinkActive(void); void CB1_Overworld(void); void CB2_OverworldBasic(void); void CB2_Overworld(void); @@ -151,7 +151,7 @@ u16 SetInCableClubSeat(void); u16 SetLinkWaitingForScript(void); u16 QueueExitLinkRoomKey(void); u16 SetStartedCableClubActivity(void); -bool32 Overworld_LinkRecvQueueLengthMoreThan2(void); +bool32 Overworld_IsRecvQueueAtMax(void); bool32 Overworld_RecvKeysFromLinkIsRunning(void); bool32 Overworld_SendKeysToLinkIsRunning(void); bool32 IsSendingKeysOverCable(void); diff --git a/include/palette_util.h b/include/palette_util.h index 46468c0a6..12ffc145e 100644 --- a/include/palette_util.h +++ b/include/palette_util.h @@ -72,7 +72,7 @@ void MarkUsedPulseBlendPalettes(struct PulseBlend *, u16, u8); void UnloadUsedPulseBlendPalettes(struct PulseBlend *, u16, u8); void UnmarkUsedPulseBlendPalettes(struct PulseBlend *, u16, u8); void UpdatePulseBlend(struct PulseBlend *); -void ClearTilemapRect(u16 *dest, u16 src, u8 left, u8 top, u8 width, u8 height); +void FillTilemapRect(u16 *dest, u16 src, u8 left, u8 top, u8 width, u8 height); void SetTilemapRect(u16 *dest, u16 *src, u8 left, u8 top, u8 width, u8 height); void RouletteFlash_Run(struct RouletteFlashUtil *r0); void RouletteFlash_Reset(struct RouletteFlashUtil *r0); diff --git a/include/text_window.h b/include/text_window.h index 7bdcacd17..aa7798c35 100644 --- a/include/text_window.h +++ b/include/text_window.h @@ -14,14 +14,14 @@ extern const u16 gTextWindowFrame1_Pal[]; const struct TilesPal *GetWindowFrameTilesPal(u8 id); void LoadMessageBoxGfx(u8 windowId, u16 destOffset, u8 palOffset); -void LoadUserWindowBorderGfx(u8 windowId, u16 destOffset, u8 palOffset); void LoadWindowGfx(u8 windowId, u8 frameId, u16 destOffset, u8 palOffset); +void LoadUserWindowBorderGfx(u8 windowId, u16 destOffset, u8 palOffset); void LoadUserWindowBorderGfx_(u8 windowId, u16 destOffset, u8 palOffset); +void LoadUserWindowBorderGfxOnBg(u8 bg, u16 destOffset, u8 palOffset); void DrawTextBorderOuter(u8 windowId, u16 tileNum, u8 palNum); void DrawTextBorderInner(u8 windowId, u16 tileNum, u8 palNum); void rbox_fill_rectangle(u8 windowId); const u16 *GetTextWindowPalette(u8 id); const u16 *GetOverworldTextboxPalettePtr(void); -void sub_8098C6C(u8 bg, u16 destOffset, u8 palOffset); #endif // GUARD_TEXT_WINDOW_H diff --git a/src/battle_dome.c b/src/battle_dome.c index 944918631..84bc774df 100644 --- a/src/battle_dome.c +++ b/src/battle_dome.c @@ -104,7 +104,7 @@ static void HblankCb_TourneyTree(void); static void VblankCb_TourneyTree(void); static u8 UpdateTourneyTreeCursor(u8 taskId); static void DecideRoundWinners(u8 roundId); -static u8 sub_81953E8(u8 tournamentId, u8); +static u8 GetOpposingNPCTournamentIdByRound(u8 tournamentId, u8); static void DrawTourneyAdvancementLine(u8, u8); static void SpriteCb_HorizontalScrollArrow(struct Sprite *sprite); static void SpriteCb_VerticalScrollArrow(struct Sprite *sprite); @@ -1197,7 +1197,7 @@ static const u8 sLastMatchCardNum[DOME_ROUNDS_COUNT] = [DOME_FINAL] = 30 }; -static const u8 gUnknown_0860D1A0[DOME_TOURNAMENT_TRAINERS_COUNT / 2][DOME_ROUNDS_COUNT] = +static const u8 sTrainerAndRoundToLastMatchCardNum[DOME_TOURNAMENT_TRAINERS_COUNT / 2][DOME_ROUNDS_COUNT] = { {16, 24, 28, 30}, {17, 24, 28, 30}, @@ -1209,7 +1209,7 @@ static const u8 gUnknown_0860D1A0[DOME_TOURNAMENT_TRAINERS_COUNT / 2][DOME_ROUND {23, 27, 29, 30}, }; -static const u8 gUnknown_0860D1C0[DOME_TOURNAMENT_TRAINERS_COUNT] = {0, 15, 8, 7, 3, 12, 11, 4, 1, 14, 9, 6, 2, 13, 10, 5}; +static const u8 sTournamentIdToPairedTrainerIds[DOME_TOURNAMENT_TRAINERS_COUNT] = {0, 15, 8, 7, 3, 12, 11, 4, 1, 14, 9, 6, 2, 13, 10, 5}; // The first line of text on a trainers info card. It describes their potential to win, based on their seed in the tournament tree. // Dome Ace Tucker has their own separate potential text. @@ -4178,7 +4178,7 @@ static u8 Task_GetInfoCardInput(u8 taskId) if (input == INFOCARD_INPUT_AB) { if (sInfoCard->pos != 0) - gTasks[taskId2].data[1] = gUnknown_0860D1A0[position / 2][sInfoCard->pos - 1]; + gTasks[taskId2].data[1] = sTrainerAndRoundToLastMatchCardNum[position / 2][sInfoCard->pos - 1]; else gTasks[taskId2].data[1] = position; } @@ -4218,9 +4218,9 @@ static u8 Task_GetInfoCardInput(u8 taskId) if (input == INFOCARD_INPUT_AB) { if (sInfoCard->pos == 0) // On left trainer info card - gTasks[taskId2].data[1] = gUnknown_0860D1C0[sInfoCard->tournamentIds[0]]; + gTasks[taskId2].data[1] = sTournamentIdToPairedTrainerIds[sInfoCard->tournamentIds[0]]; else if (sInfoCard->pos == 2) // On right trainer info card - gTasks[taskId2].data[1] = gUnknown_0860D1C0[sInfoCard->tournamentIds[1]]; + gTasks[taskId2].data[1] = sTournamentIdToPairedTrainerIds[sInfoCard->tournamentIds[1]]; else // On match info card gTasks[taskId2].data[1] = position; } @@ -5224,7 +5224,7 @@ static u16 GetWinningMove(int winnerTournamentId, int loserTournamentId, u8 roun { for (i = 0; i < roundId - 1; i++) { - if (gSaveBlock2Ptr->frontier.domeWinningMoves[sub_81953E8(winnerTournamentId, i)] == moveIds[j]) + if (gSaveBlock2Ptr->frontier.domeWinningMoves[GetOpposingNPCTournamentIdByRound(winnerTournamentId, i)] == moveIds[j]) break; } if (i != roundId - 1) @@ -5934,10 +5934,10 @@ int TrainerIdToDomeTournamentId(u16 trainerId) return i; } -static u8 sub_81953E8(u8 tournamentId, u8 round) +static u8 GetOpposingNPCTournamentIdByRound(u8 tournamentId, u8 round) { u8 tournamentIds[2]; - BufferDomeWinString(gUnknown_0860D1A0[gUnknown_0860D1C0[tournamentId] / 2][round] - 16, tournamentIds); + BufferDomeWinString(sTrainerAndRoundToLastMatchCardNum[sTournamentIdToPairedTrainerIds[tournamentId] / 2][round] - 16, tournamentIds); if (tournamentId == tournamentIds[0]) return tournamentIds[1]; else diff --git a/src/battle_pyramid_bag.c b/src/battle_pyramid_bag.c index d45770f74..286049e8b 100644 --- a/src/battle_pyramid_bag.c +++ b/src/battle_pyramid_bag.c @@ -452,9 +452,9 @@ static void VBlankCB_PyramidBag(void) static void CB2_LoadPyramidBagMenu(void) { - while (MenuHelpers_CallLinkSomething() != TRUE + while (MenuHelpers_ShouldWaitForLinkRecv() != TRUE && LoadPyramidBagMenu() != TRUE - && MenuHelpers_LinkSomething() != TRUE); + && MenuHelpers_IsLinkActive() != TRUE); } static bool8 LoadPyramidBagMenu(void) @@ -484,7 +484,7 @@ static bool8 LoadPyramidBagMenu(void) gMain.state++; break; case 5: - if (!MenuHelpers_LinkSomething()) + if (!MenuHelpers_IsLinkActive()) ResetTasks(); gMain.state++; break; @@ -885,7 +885,7 @@ static void Task_ClosePyramidBag(u8 taskId) static void Task_HandlePyramidBagInput(u8 taskId) { s16 *data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() == TRUE || gPaletteFade.active) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE || gPaletteFade.active) return; if (JOY_NEW(SELECT_BUTTON)) @@ -990,7 +990,7 @@ static void PrintMenuActionText_MultiRow(u8 windowId, u8 horizontalCount, u8 ver static void HandleMenuActionInput_SingleRow(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s32 id = Menu_ProcessInputNoWrap(); switch (id) @@ -1012,7 +1012,7 @@ static void HandleMenuActionInput_SingleRow(u8 taskId) static void HandleMenuActionInput_2x2(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s8 id = Menu_GetCursorPos(); if (JOY_NEW(DPAD_UP)) @@ -1325,7 +1325,7 @@ static void Task_BeginItemSwap(u8 taskId) static void Task_ItemSwapHandleInput(u8 taskId) { s16 *data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (JOY_NEW(SELECT_BUTTON)) { diff --git a/src/berry_tag_screen.c b/src/berry_tag_screen.c index babbbb3eb..5a6f6f694 100644 --- a/src/berry_tag_screen.c +++ b/src/berry_tag_screen.c @@ -200,11 +200,11 @@ static void CB2_InitBerryTagScreen(void) { while (1) { - if (MenuHelpers_CallLinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE) break; if (InitBerryTagScreen() == TRUE) break; - if (MenuHelpers_LinkSomething() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE) break; } } @@ -237,7 +237,7 @@ static bool8 InitBerryTagScreen(void) gMain.state++; break; case 5: - if (!MenuHelpers_LinkSomething()) + if (!MenuHelpers_IsLinkActive()) ResetTasks(); gMain.state++; break; diff --git a/src/bike.c b/src/bike.c index fd794a3c7..20166392e 100644 --- a/src/bike.c +++ b/src/bike.c @@ -658,7 +658,7 @@ static void AcroBikeTransition_SideJump(u8 direction) playerObjEvent = &gObjectEvents[gPlayerAvatar.objectEventId]; PlaySE(SE_BIKE_HOP); playerObjEvent->facingDirectionLocked = 1; - PlayerSetAnimId(GetJumpMovementAction(direction), 2); + PlayerSetAnimId(GetJumpMovementAction(direction), COPY_MOVE_WALK); } static void AcroBikeTransition_TurnJump(u8 direction) diff --git a/src/confetti_util.c b/src/confetti_util.c index bcf19f705..9ede3088a 100644 --- a/src/confetti_util.c +++ b/src/confetti_util.c @@ -10,43 +10,6 @@ static EWRAM_DATA struct struct ConfettiUtil *array; } *sWork = NULL; -static void sub_81520A8(void *dest, u16 value, u8 left, u8 top, u8 width, u8 height) // Unused. -{ - u8 i; - u8 j; - u8 x; - u8 y; - - for (i = 0, y = top; i < height; i++) - { - for (x = left, j = 0; j < width; j++) - { - *(u16 *)((dest) + (y * 64 + x * 2)) = value; - x = (x + 1) % 32; - } - y = (y + 1) % 32; - } -} - -static void sub_8152134(void *dest, const u16 *src, u8 left, u8 top, u8 width, u8 height) // Unused. -{ - u8 i; - u8 j; - u8 x; - u8 y; - const u16 *_src; - - for (i = 0, _src = src, y = top; i < height; i++) - { - for (x = left, j = 0; j < width; j++) - { - *(u16 *)((dest) + (y * 64 + x * 2)) = *(_src++); - x = (x + 1) % 32; - } - y = (y + 1) % 32; - } -} - bool32 ConfettiUtil_Init(u8 count) { u8 i = 0; diff --git a/src/data/object_events/movement_type_func_tables.h b/src/data/object_events/movement_type_func_tables.h index f4890d684..f5189d838 100755 --- a/src/data/object_events/movement_type_func_tables.h +++ b/src/data/object_events/movement_type_func_tables.h @@ -388,17 +388,17 @@ u8 (*const gMovementTypeFuncs_CopyPlayer[])(struct ObjectEvent *, struct Sprite }; bool8 (*const gCopyPlayerMovementFuncs[])(struct ObjectEvent *, struct Sprite *, u8, bool8(u8)) = { - CopyablePlayerMovement_None, - CopyablePlayerMovement_FaceDirection, - CopyablePlayerMovement_GoSpeed0, - CopyablePlayerMovement_GoSpeed1, - CopyablePlayerMovement_GoSpeed2, - CopyablePlayerMovement_Slide, - cph_IM_DIFFERENT, - CopyablePlayerMovement_GoSpeed4, - CopyablePlayerMovement_Jump, - CopyablePlayerMovement_None, - CopyablePlayerMovement_None, + [COPY_MOVE_NONE] = CopyablePlayerMovement_None, + [COPY_MOVE_FACE] = CopyablePlayerMovement_FaceDirection, + [COPY_MOVE_WALK] = CopyablePlayerMovement_WalkNormal, + [COPY_MOVE_WALK_FAST] = CopyablePlayerMovement_WalkFast, + [COPY_MOVE_WALK_FASTER] = CopyablePlayerMovement_WalkFaster, + [COPY_MOVE_SLIDE] = CopyablePlayerMovement_Slide, + [COPY_MOVE_JUMP_IN_PLACE] = CopyablePlayerMovement_JumpInPlace, + [COPY_MOVE_JUMP] = CopyablePlayerMovement_Jump, + [COPY_MOVE_JUMP2] = CopyablePlayerMovement_Jump2, + [COPY_MOVE_EMPTY_1] = CopyablePlayerMovement_None, + [COPY_MOVE_EMPTY_2] = CopyablePlayerMovement_None, }; u8 (*const gMovementTypeFuncs_CopyPlayerInGrass[])(struct ObjectEvent *, struct Sprite *) = { diff --git a/src/decompress.c b/src/decompress.c index a65f38c2d..b74d4e814 100644 --- a/src/decompress.c +++ b/src/decompress.c @@ -113,7 +113,7 @@ void Unused_LZDecompressWramIndirect(const void **src, void *dest) LZ77UnCompWram(*src, dest); } -void sub_803471C(s32 object_size, s32 object_count, u8 *src_tiles, u8 *dest_tiles) +static void StitchObjectsOn8x8Canvas(s32 object_size, s32 object_count, u8 *src_tiles, u8 *dest_tiles) { /* This function appears to emulate behaviour found in the GB(C) versions regarding how the Pokemon images diff --git a/src/easy_chat.c b/src/easy_chat.c index b18d09b83..625e8cd9e 100644 --- a/src/easy_chat.c +++ b/src/easy_chat.c @@ -1310,7 +1310,7 @@ static void StartEasyChatScreen(u8 taskId, TaskFunc taskFunc) static void Task_InitEasyChatScreen(u8 taskId) { - if (!IsUpdateLinkStateCBActive()) + if (!IsOverworldLinkActive()) { while (InitEasyChatScreen(taskId)); } diff --git a/src/event_object_movement.c b/src/event_object_movement.c index 1bb4991ab..5b1857297 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -26,6 +26,7 @@ #include "constants/event_objects.h" #include "constants/field_effects.h" #include "constants/items.h" +#include "constants/maps.h" #include "constants/mauville_old_man.h" #include "constants/trainer_types.h" #include "constants/union_room.h" @@ -79,7 +80,7 @@ static bool8 ObjectEventExecSingleMovementAction(struct ObjectEvent *, struct Sp static void SetMovementDelay(struct Sprite *, s16); static bool8 WaitForMovementDelay(struct Sprite *); static u8 GetCollisionInDirection(struct ObjectEvent *, u8); -static u32 state_to_direction(u8, u32, u32); +static u32 GetCopyDirection(u8, u32, u32); static void TryEnableObjectEventAnim(struct ObjectEvent *, struct Sprite *); static void ObjectEventExecHeldMovementAction(struct ObjectEvent *, struct Sprite *); static void UpdateObjectEventSpriteAnimPause(struct ObjectEvent *, struct Sprite *); @@ -267,88 +268,48 @@ static void (*const sMovementTypeCallbacks[])(struct Sprite *) = [MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_RIGHT] = MovementType_WalkSlowlyInPlace, }; -const u8 gRangedMovementTypes[] = { - [MOVEMENT_TYPE_NONE] = 0, - [MOVEMENT_TYPE_LOOK_AROUND] = 0, - [MOVEMENT_TYPE_WANDER_AROUND] = 1, - [MOVEMENT_TYPE_WANDER_UP_AND_DOWN] = 1, - [MOVEMENT_TYPE_WANDER_DOWN_AND_UP] = 1, - [MOVEMENT_TYPE_WANDER_LEFT_AND_RIGHT] = 1, - [MOVEMENT_TYPE_WANDER_RIGHT_AND_LEFT] = 1, - [MOVEMENT_TYPE_FACE_UP] = 0, - [MOVEMENT_TYPE_FACE_DOWN] = 0, - [MOVEMENT_TYPE_FACE_LEFT] = 0, - [MOVEMENT_TYPE_FACE_RIGHT] = 0, - [MOVEMENT_TYPE_PLAYER] = 0, - [MOVEMENT_TYPE_BERRY_TREE_GROWTH] = 0, - [MOVEMENT_TYPE_FACE_DOWN_AND_UP] = 0, - [MOVEMENT_TYPE_FACE_LEFT_AND_RIGHT] = 0, - [MOVEMENT_TYPE_FACE_UP_AND_LEFT] = 0, - [MOVEMENT_TYPE_FACE_UP_AND_RIGHT] = 0, - [MOVEMENT_TYPE_FACE_DOWN_AND_LEFT] = 0, - [MOVEMENT_TYPE_FACE_DOWN_AND_RIGHT] = 0, - [MOVEMENT_TYPE_FACE_DOWN_UP_AND_LEFT] = 0, - [MOVEMENT_TYPE_FACE_DOWN_UP_AND_RIGHT] = 0, - [MOVEMENT_TYPE_FACE_UP_LEFT_AND_RIGHT] = 0, - [MOVEMENT_TYPE_FACE_DOWN_LEFT_AND_RIGHT] = 0, - [MOVEMENT_TYPE_ROTATE_COUNTERCLOCKWISE] = 0, - [MOVEMENT_TYPE_ROTATE_CLOCKWISE] = 0, - [MOVEMENT_TYPE_WALK_UP_AND_DOWN] = 1, - [MOVEMENT_TYPE_WALK_DOWN_AND_UP] = 1, - [MOVEMENT_TYPE_WALK_LEFT_AND_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_RIGHT_AND_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_RIGHT_LEFT_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_LEFT_DOWN_UP] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_UP_RIGHT_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_DOWN_UP_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_LEFT_RIGHT_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_RIGHT_DOWN_UP] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_UP_LEFT_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_DOWN_UP_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_UP_DOWN_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_DOWN_RIGHT_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_LEFT_UP_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_RIGHT_LEFT_UP] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_UP_DOWN_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_DOWN_LEFT_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_RIGHT_UP_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_LEFT_RIGHT_UP] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_LEFT_DOWN_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_RIGHT_UP_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_DOWN_RIGHT_UP] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_UP_LEFT_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_UP_RIGHT_DOWN_LEFT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_LEFT_UP_RIGHT] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_UP_RIGHT_DOWN] = 1, - [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_DOWN_LEFT_UP] = 1, - [MOVEMENT_TYPE_COPY_PLAYER] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_OPPOSITE] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_COUNTERCLOCKWISE] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_CLOCKWISE] = 1, - [MOVEMENT_TYPE_TREE_DISGUISE] = 0, - [MOVEMENT_TYPE_MOUNTAIN_DISGUISE] = 0, - [MOVEMENT_TYPE_COPY_PLAYER_IN_GRASS] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_OPPOSITE_IN_GRASS] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_COUNTERCLOCKWISE_IN_GRASS] = 1, - [MOVEMENT_TYPE_COPY_PLAYER_CLOCKWISE_IN_GRASS] = 1, - [MOVEMENT_TYPE_BURIED] = 0, - [MOVEMENT_TYPE_WALK_IN_PLACE_DOWN] = 0, - [MOVEMENT_TYPE_WALK_IN_PLACE_UP] = 0, - [MOVEMENT_TYPE_WALK_IN_PLACE_LEFT] = 0, - [MOVEMENT_TYPE_WALK_IN_PLACE_RIGHT] = 0, - [MOVEMENT_TYPE_JOG_IN_PLACE_DOWN] = 0, - [MOVEMENT_TYPE_JOG_IN_PLACE_UP] = 0, - [MOVEMENT_TYPE_JOG_IN_PLACE_LEFT] = 0, - [MOVEMENT_TYPE_JOG_IN_PLACE_RIGHT] = 0, - [MOVEMENT_TYPE_RUN_IN_PLACE_DOWN] = 0, - [MOVEMENT_TYPE_RUN_IN_PLACE_UP] = 0, - [MOVEMENT_TYPE_RUN_IN_PLACE_LEFT] = 0, - [MOVEMENT_TYPE_RUN_IN_PLACE_RIGHT] = 0, - [MOVEMENT_TYPE_INVISIBLE] = 0, - [MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_DOWN] = 0, - [MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_UP] = 0, - [MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_LEFT] = 0, - [MOVEMENT_TYPE_WALK_SLOWLY_IN_PLACE_RIGHT] = 0, +static const bool8 sMovementTypeHasRange[NUM_MOVEMENT_TYPES] = { + [MOVEMENT_TYPE_WANDER_AROUND] = TRUE, + [MOVEMENT_TYPE_WANDER_UP_AND_DOWN] = TRUE, + [MOVEMENT_TYPE_WANDER_DOWN_AND_UP] = TRUE, + [MOVEMENT_TYPE_WANDER_LEFT_AND_RIGHT] = TRUE, + [MOVEMENT_TYPE_WANDER_RIGHT_AND_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_UP_AND_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_DOWN_AND_UP] = TRUE, + [MOVEMENT_TYPE_WALK_LEFT_AND_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_RIGHT_AND_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_RIGHT_LEFT_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_LEFT_DOWN_UP] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_UP_RIGHT_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_DOWN_UP_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_LEFT_RIGHT_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_RIGHT_DOWN_UP] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_UP_LEFT_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_DOWN_UP_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_UP_DOWN_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_DOWN_RIGHT_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_LEFT_UP_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_RIGHT_LEFT_UP] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_UP_DOWN_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_DOWN_LEFT_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_RIGHT_UP_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_LEFT_RIGHT_UP] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_LEFT_DOWN_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_RIGHT_UP_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_DOWN_RIGHT_UP] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_UP_LEFT_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_UP_RIGHT_DOWN_LEFT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_DOWN_LEFT_UP_RIGHT] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_LEFT_UP_RIGHT_DOWN] = TRUE, + [MOVEMENT_TYPE_WALK_SEQUENCE_RIGHT_DOWN_LEFT_UP] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_OPPOSITE] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_COUNTERCLOCKWISE] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_CLOCKWISE] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_IN_GRASS] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_OPPOSITE_IN_GRASS] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_COUNTERCLOCKWISE_IN_GRASS] = TRUE, + [MOVEMENT_TYPE_COPY_PLAYER_CLOCKWISE_IN_GRASS] = TRUE, }; const u8 gInitialMovementTypeFacingDirections[] = { @@ -517,7 +478,7 @@ static const struct SpritePalette sObjectEventSpritePalettes[] = { {gObjectEventPal_Lugia, OBJ_EVENT_PAL_TAG_LUGIA}, {gObjectEventPal_RubySapphireBrendan, OBJ_EVENT_PAL_TAG_RS_BRENDAN}, {gObjectEventPal_RubySapphireMay, OBJ_EVENT_PAL_TAG_RS_MAY}, - {NULL, 0x0000}, + {}, }; static const u16 sReflectionPaletteTags_Brendan[] = { @@ -1115,31 +1076,75 @@ static const u8 sOppositeDirections[] = { DIR_SOUTHWEST, }; -const u8 gUnknown_0850DC2F[][4] = { - {2, 1, 4, 3}, - {1, 2, 3, 4}, - {3, 4, 2, 1}, - {4, 3, 1, 2} +// Takes the player's original and current facing direction to get the direction that should be considered to copy. +// Note that this means an NPC who copies the player's movement changes how they copy them based on how +// the player entered the area. For instance an NPC who does the same movements as the player when they +// entered the area facing South will do the opposite movements as the player if they enter facing North. +static const u8 sPlayerDirectionsForCopy[][4] = { + [DIR_SOUTH - 1] = { + [DIR_SOUTH - 1] = DIR_NORTH, + [DIR_NORTH - 1] = DIR_SOUTH, + [DIR_WEST - 1] = DIR_EAST, + [DIR_EAST - 1] = DIR_WEST + }, + [DIR_NORTH - 1] = { + [DIR_SOUTH - 1] = DIR_SOUTH, + [DIR_NORTH - 1] = DIR_NORTH, + [DIR_WEST - 1] = DIR_WEST, + [DIR_EAST - 1] = DIR_EAST + }, + [DIR_WEST - 1] = { + [DIR_SOUTH - 1] = DIR_WEST, + [DIR_NORTH - 1] = DIR_EAST, + [DIR_WEST - 1] = DIR_NORTH, + [DIR_EAST - 1] = DIR_SOUTH + }, + [DIR_EAST - 1] = { + [DIR_SOUTH - 1] = DIR_EAST, + [DIR_NORTH - 1] = DIR_WEST, + [DIR_WEST - 1] = DIR_SOUTH, + [DIR_EAST - 1] = DIR_NORTH + } }; -const u8 gUnknown_0850DC3F[][4] = { - {2, 1, 4, 3}, - {1, 2, 3, 4}, - {4, 3, 1, 2}, - {3, 4, 2, 1} +// Indexed first with the NPC's initial facing direction based on movement type, and secondly with the player direction to copy. +// Returns the direction the copy NPC should travel in. +static const u8 sPlayerDirectionToCopyDirection[][4] = { + [DIR_SOUTH - 1] = { // MOVEMENT_TYPE_COPY_PLAYER_OPPOSITE(_IN_GRASS) + [DIR_SOUTH - 1] = DIR_NORTH, + [DIR_NORTH - 1] = DIR_SOUTH, + [DIR_WEST - 1] = DIR_EAST, + [DIR_EAST - 1] = DIR_WEST + }, + [DIR_NORTH - 1] = { // MOVEMENT_TYPE_COPY_PLAYER(_IN_GRASS) + [DIR_SOUTH - 1] = DIR_SOUTH, + [DIR_NORTH - 1] = DIR_NORTH, + [DIR_WEST - 1] = DIR_WEST, + [DIR_EAST - 1] = DIR_EAST + }, + [DIR_WEST - 1] = { // MOVEMENT_TYPE_COPY_PLAYER_COUNTERCLOCKWISE(_IN_GRASS) + [DIR_SOUTH - 1] = DIR_EAST, + [DIR_NORTH - 1] = DIR_WEST, + [DIR_WEST - 1] = DIR_SOUTH, + [DIR_EAST - 1] = DIR_NORTH + }, + [DIR_EAST - 1] = { // MOVEMENT_TYPE_COPY_PLAYER_CLOCKWISE(_IN_GRASS) + [DIR_SOUTH - 1] = DIR_WEST, + [DIR_NORTH - 1] = DIR_EAST, + [DIR_WEST - 1] = DIR_NORTH, + [DIR_EAST - 1] = DIR_SOUTH + } }; #include "data/object_events/movement_action_func_tables.h" -// Code - static void ClearObjectEvent(struct ObjectEvent *objectEvent) { *objectEvent = (struct ObjectEvent){}; objectEvent->localId = 0xFF; - objectEvent->mapNum = 0xFF; - objectEvent->mapGroup = 0xFF; - objectEvent->movementActionId = 0xFF; + objectEvent->mapNum = MAP_NUM(UNDEFINED); + objectEvent->mapGroup = MAP_GROUP(UNDEFINED); + objectEvent->movementActionId = MOVEMENT_ACTION_NONE; } static void ClearAllObjectEvents(void) @@ -1188,9 +1193,8 @@ u8 GetFirstInactiveObjectEventId(void) u8 GetObjectEventIdByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroupId) { if (localId < OBJ_EVENT_ID_PLAYER) - { return GetObjectEventIdByLocalIdAndMapInternal(localId, mapNum, mapGroupId); - } + return GetObjectEventIdByLocalId(localId); } @@ -1275,16 +1279,12 @@ static u8 InitObjectEventStateFromTemplate(struct ObjectEventTemplate *template, objectEvent->previousMovementDirection = gInitialMovementTypeFacingDirections[template->movementType]; SetObjectEventDirection(objectEvent, objectEvent->previousMovementDirection); SetObjectEventDynamicGraphicsId(objectEvent); - if (gRangedMovementTypes[objectEvent->movementType]) + if (sMovementTypeHasRange[objectEvent->movementType]) { if (objectEvent->rangeX == 0) - { objectEvent->rangeX++; - } if (objectEvent->rangeY == 0) - { objectEvent->rangeY++; - } } return objectEventId; } @@ -1298,24 +1298,17 @@ u8 Unref_TryInitLocalObjectEvent(u8 localId) if (gMapHeader.events != NULL) { if (InBattlePyramid()) - { objectEventCount = GetNumBattlePyramidObjectEvents(); - } else if (InTrainerHill()) - { objectEventCount = 2; - } else - { objectEventCount = gMapHeader.events->objectEventCount; - } + for (i = 0; i < objectEventCount; i++) { template = &gSaveBlock1Ptr->objectEventTemplates[i]; if (template->localId == localId && !FlagGet(template->flagId)) - { return InitObjectEventStateFromTemplate(template, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup); - } } } return OBJECT_EVENTS_COUNT; @@ -1540,9 +1533,8 @@ u8 AddPseudoObjectEvent(u16 graphicsId, void (*callback)(struct Sprite *), s16 x spriteTemplate = malloc(sizeof(struct SpriteTemplate)); MakeObjectTemplateFromObjectEventGraphicsInfo(graphicsId, callback, spriteTemplate, &subspriteTables); if (spriteTemplate->paletteTag != TAG_NONE) - { LoadObjectEventPalette(spriteTemplate->paletteTag); - } + spriteId = CreateSprite(spriteTemplate, x, y, subpriority); free(spriteTemplate); @@ -1580,20 +1572,16 @@ u8 CreateObjectSprite(u8 graphicsId, u8 objectEventId, s16 x, s16 y, u8 z, u8 di sprite->y += sprite->centerToCornerVecY; sprite->oam.paletteNum = graphicsInfo->paletteSlot; if (sprite->oam.paletteNum >= 16) - { sprite->oam.paletteNum -= 16; - } + sprite->coordOffsetEnabled = TRUE; sprite->sObjEventId = objectEventId; sprite->data[1] = z; if (graphicsInfo->paletteSlot == 10) - { LoadSpecialObjectReflectionPalette(graphicsInfo->paletteTag, graphicsInfo->paletteSlot); - } else if (graphicsInfo->paletteSlot >= 16) - { _PatchObjectPalette(graphicsInfo->paletteTag, graphicsInfo->paletteSlot | 0xf0); - } + if (subspriteTables != NULL) { SetSubspriteTables(sprite, subspriteTables); @@ -1817,9 +1805,7 @@ void ObjectEventSetGraphicsId(struct ObjectEvent *objectEvent, u8 graphicsId) sprite->x += 8; sprite->y += 16 + sprite->centerToCornerVecY; if (objectEvent->trackedByCamera) - { CameraObjectReset1(); - } } void ObjectEventSetGraphicsIdByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup, u8 graphicsId) @@ -1827,9 +1813,7 @@ void ObjectEventSetGraphicsIdByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup, u8 objectEventId; if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { ObjectEventSetGraphicsId(&gObjectEvents[objectEventId], graphicsId); - } } void ObjectEventTurn(struct ObjectEvent *objectEvent, u8 direction) @@ -1847,9 +1831,7 @@ void ObjectEventTurnByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup, u8 direc u8 objectEventId; if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { ObjectEventTurn(&gObjectEvents[objectEventId], direction); - } } void PlayerObjectTurn(struct PlayerAvatar *playerAvatar, u8 direction) @@ -1886,9 +1868,8 @@ const struct ObjectEventGraphicsInfo *GetObjectEventGraphicsInfo(u8 graphicsId) u8 bard; if (graphicsId >= OBJ_EVENT_GFX_VARS) - { graphicsId = VarGetObjectEventGraphicsId(graphicsId - OBJ_EVENT_GFX_VARS); - } + if (graphicsId == OBJ_EVENT_GFX_BARD) { bard = GetCurrentMauvilleOldMan(); @@ -1896,9 +1877,7 @@ const struct ObjectEventGraphicsInfo *GetObjectEventGraphicsInfo(u8 graphicsId) } if (graphicsId >= NUM_OBJ_EVENT_GFX) - { graphicsId = OBJ_EVENT_GFX_NINJA_BOY; - } return gObjectEventGraphicsInfoPointers[graphicsId]; } @@ -1906,9 +1885,7 @@ const struct ObjectEventGraphicsInfo *GetObjectEventGraphicsInfo(u8 graphicsId) static void SetObjectEventDynamicGraphicsId(struct ObjectEvent *objectEvent) { if (objectEvent->graphicsId >= OBJ_EVENT_GFX_VARS) - { objectEvent->graphicsId = VarGetObjectEventGraphicsId(objectEvent->graphicsId - OBJ_EVENT_GFX_VARS); - } } void SetObjectInvisibility(u8 localId, u8 mapNum, u8 mapGroup, bool8 invisible) @@ -1916,9 +1893,7 @@ void SetObjectInvisibility(u8 localId, u8 mapNum, u8 mapGroup, bool8 invisible) u8 objectEventId; if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { gObjectEvents[objectEventId].invisible = invisible; - } } void ObjectEventGetLocalIdAndMap(struct ObjectEvent *objectEvent, void *localId, void *mapNum, void *mapGroup) @@ -2037,9 +2012,7 @@ static u8 FindObjectEventPaletteIndexByTag(u16 tag) for (i = 0; sObjectEventSpritePalettes[i].tag != OBJ_EVENT_PAL_TAG_NONE; i++) { if (sObjectEventSpritePalettes[i].tag == tag) - { return i; - } } return 0xFF; } @@ -2173,9 +2146,7 @@ u8 GetObjectEventIdByXYZ(u16 x, u16 y, u8 z) if (gObjectEvents[i].active) { if (gObjectEvents[i].currentCoords.x == x && gObjectEvents[i].currentCoords.y == y && ObjectEventDoesZCoordMatch(&gObjectEvents[i], z)) - { return i; - } } } return OBJECT_EVENTS_COUNT; @@ -2184,9 +2155,8 @@ u8 GetObjectEventIdByXYZ(u16 x, u16 y, u8 z) static bool8 ObjectEventDoesZCoordMatch(struct ObjectEvent *objectEvent, u8 z) { if (objectEvent->currentElevation != 0 && z != 0 && objectEvent->currentElevation != z) - { return FALSE; - } + return TRUE; } @@ -2288,9 +2258,8 @@ static u8 CameraObjectGetFollowedSpriteId(void) camera = FindCameraSprite(); if (camera == NULL) - { return MAX_SPRITES; - } + return camera->sLinkedSpriteId; } @@ -2298,13 +2267,9 @@ void CameraObjectReset2(void) { // UB: Possible null dereference #ifdef UBFIX - struct Sprite *camera; - - camera = FindCameraSprite(); - if (camera != NULL) - { + struct Sprite *camera = FindCameraSprite(); + if (camera) camera->sState = 2; - } #else FindCameraSprite()->sState = 2; #endif // UBFIX @@ -2390,9 +2355,8 @@ static u8 GetObjectTrainerTypeByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup u8 objectEventId; if (TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { return 0xFF; - } + return gObjectEvents[objectEventId].trainerType; } @@ -2408,9 +2372,8 @@ u8 GetObjectEventBerryTreeIdByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup) u8 objectEventId; if (TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { return 0xFF; - } + return gObjectEvents[objectEventId].trainerRange_berryTreeId; } @@ -2446,9 +2409,7 @@ static struct ObjectEventTemplate *FindObjectEventTemplateByLocalId(u8 localId, for (i = 0; i < count; i++) { if (templates[i].localId == localId) - { return &templates[i]; - } } return NULL; } @@ -2457,16 +2418,14 @@ struct ObjectEventTemplate *GetBaseTemplateForObjectEvent(const struct ObjectEve { int i; - if (objectEvent->mapNum != gSaveBlock1Ptr->location.mapNum || objectEvent->mapGroup != gSaveBlock1Ptr->location.mapGroup) - { + if (objectEvent->mapNum != gSaveBlock1Ptr->location.mapNum + || objectEvent->mapGroup != gSaveBlock1Ptr->location.mapGroup) return NULL; - } + for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++) { if (objectEvent->localId == gSaveBlock1Ptr->objectEventTemplates[i].localId) - { return &gSaveBlock1Ptr->objectEventTemplates[i]; - } } return NULL; } @@ -2498,9 +2457,7 @@ void TryOverrideTemplateCoordsForObjectEvent(const struct ObjectEvent *objectEve objectEventTemplate = GetBaseTemplateForObjectEvent(objectEvent); if (objectEventTemplate != NULL) - { objectEventTemplate->movementType = movementType; - } } void TryOverrideObjectEventTemplateCoords(u8 localId, u8 mapNum, u8 mapGroup) @@ -2548,15 +2505,12 @@ u16 GetObjectPaletteTag(u8 palSlot) u8 i; if (palSlot < 10) - { return sObjectPaletteTagSets[sCurrentReflectionType][palSlot]; - } + for (i = 0; sSpecialObjectReflectionPaletteSets[i].tag != OBJ_EVENT_PAL_TAG_NONE; i++) { if (sSpecialObjectReflectionPaletteSets[i].tag == sCurrentSpecialObjectPaletteTag) - { return sSpecialObjectReflectionPaletteSets[i].data[sCurrentReflectionType]; - } } return OBJ_EVENT_PAL_TAG_NONE; } @@ -2581,9 +2535,7 @@ bool8 MovementType_WanderAround_Step1(struct ObjectEvent *objectEvent, struct Sp bool8 MovementType_WanderAround_Step2(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (!ObjectEventExecSingleMovementAction(objectEvent, sprite)) - { return FALSE; - } SetMovementDelay(sprite, sMovementDelaysMedium[Random() & 3]); sprite->sTypeFuncId = 3; return TRUE; @@ -2644,13 +2596,11 @@ bool8 ObjectEventIsTrainerAndCloseToPlayer(struct ObjectEvent *objectEvent) s16 maxY; if (!TestPlayerAvatarFlags(PLAYER_AVATAR_FLAG_DASH)) - { return FALSE; - } + if (objectEvent->trainerType != TRAINER_TYPE_NORMAL && objectEvent->trainerType != TRAINER_TYPE_BURIED) - { return FALSE; - } + PlayerGetDestCoords(&playerX, &playerY); objX = objectEvent->currentCoords.x; objY = objectEvent->currentCoords.y; @@ -2658,10 +2608,10 @@ bool8 ObjectEventIsTrainerAndCloseToPlayer(struct ObjectEvent *objectEvent) minY = objY - objectEvent->trainerRange_berryTreeId; maxX = objX + objectEvent->trainerRange_berryTreeId; maxY = objY + objectEvent->trainerRange_berryTreeId; - if (minX > playerX || maxX < playerX || minY > playerY || maxY < playerY) - { + if (minX > playerX || maxX < playerX + || minY > playerY || maxY < playerY) return FALSE; - } + return TRUE; } @@ -3229,10 +3179,8 @@ bool8 MovementType_FaceDownAndUp_Step4(struct ObjectEvent *objectEvent, struct S u8 directions[2]; memcpy(directions, gUpAndDownDirections, sizeof gUpAndDownDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_SOUTH); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3281,10 +3229,8 @@ bool8 MovementType_FaceLeftAndRight_Step4(struct ObjectEvent *objectEvent, struc u8 directions[2]; memcpy(directions, gLeftAndRightDirections, sizeof gLeftAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_EAST_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3333,10 +3279,8 @@ bool8 MovementType_FaceUpAndLeft_Step4(struct ObjectEvent *objectEvent, struct S u8 directions[2]; memcpy(directions, gUpAndLeftDirections, sizeof gUpAndLeftDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3385,10 +3329,8 @@ bool8 MovementType_FaceUpAndRight_Step4(struct ObjectEvent *objectEvent, struct u8 directions[2]; memcpy(directions, gUpAndRightDirections, sizeof gUpAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_EAST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3437,10 +3379,8 @@ bool8 MovementType_FaceDownAndLeft_Step4(struct ObjectEvent *objectEvent, struct u8 directions[2]; memcpy(directions, gDownAndLeftDirections, sizeof gDownAndLeftDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_SOUTH_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3489,10 +3429,8 @@ bool8 MovementType_FaceDownAndRight_Step4(struct ObjectEvent *objectEvent, struc u8 directions[2]; memcpy(directions, gDownAndRightDirections, sizeof gDownAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_SOUTH_EAST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 1]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3541,10 +3479,8 @@ bool8 MovementType_FaceDownUpAndLeft_Step4(struct ObjectEvent *objectEvent, stru u8 directions[4]; memcpy(directions, gDownUpAndLeftDirections, sizeof gDownUpAndLeftDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_SOUTH_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 3]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3593,10 +3529,8 @@ bool8 MovementType_FaceDownUpAndRight_Step4(struct ObjectEvent *objectEvent, str u8 directions[4]; memcpy(directions, gDownUpAndRightDirections, sizeof gDownUpAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_SOUTH_EAST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 3]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3645,10 +3579,8 @@ bool8 MovementType_FaceUpLeftAndRight_Step4(struct ObjectEvent *objectEvent, str u8 directions[4]; memcpy(directions, gUpLeftAndRightDirections, sizeof gUpLeftAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_NORTH_EAST_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 3]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3697,10 +3629,8 @@ bool8 MovementType_FaceDownLeftAndRight_Step4(struct ObjectEvent *objectEvent, s u8 directions[4]; memcpy(directions, gDownLeftAndRightDirections, sizeof gDownLeftAndRightDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_SOUTH_EAST_WEST); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[Random() & 3]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 1; return TRUE; @@ -3729,9 +3659,7 @@ bool8 MovementType_RotateCounterclockwise_Step1(struct ObjectEvent *objectEvent, bool8 MovementType_RotateCounterclockwise_Step2(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (WaitForMovementDelay(sprite) || ObjectEventIsTrainerAndCloseToPlayer(objectEvent)) - { sprite->sTypeFuncId = 3; - } return FALSE; } @@ -3741,10 +3669,8 @@ bool8 MovementType_RotateCounterclockwise_Step3(struct ObjectEvent *objectEvent, u8 directions[5]; memcpy(directions, gCounterclockwiseDirections, sizeof gCounterclockwiseDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_ANY); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[objectEvent->facingDirection]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 0; return TRUE; @@ -3773,9 +3699,7 @@ bool8 MovementType_RotateClockwise_Step1(struct ObjectEvent *objectEvent, struct bool8 MovementType_RotateClockwise_Step2(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (WaitForMovementDelay(sprite) || ObjectEventIsTrainerAndCloseToPlayer(objectEvent)) - { sprite->sTypeFuncId = 3; - } return FALSE; } @@ -3785,10 +3709,8 @@ bool8 MovementType_RotateClockwise_Step3(struct ObjectEvent *objectEvent, struct u8 directions[5]; memcpy(directions, gClockwiseDirections, sizeof gClockwiseDirections); direction = TryGetTrainerEncounterDirection(objectEvent, RUNFOLLOW_ANY); - if (direction == 0) - { + if (direction == DIR_NONE) direction = directions[objectEvent->facingDirection]; - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 0; return TRUE; @@ -3809,9 +3731,7 @@ bool8 MovementType_WalkBackAndForth_Step1(struct ObjectEvent *objectEvent, struc direction = gInitialMovementTypeFacingDirections[objectEvent->movementType]; if (objectEvent->directionSequenceIndex) - { direction = GetOppositeDirection(direction); - } SetObjectEventDirection(objectEvent, direction); sprite->sTypeFuncId = 2; return TRUE; @@ -3908,9 +3828,8 @@ u8 MovementType_WalkSequenceUpRightLeftDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpRightLeftDownDirections)]; memcpy(directions, gUpRightLeftDownDirections, sizeof(gUpRightLeftDownDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3921,9 +3840,8 @@ u8 MovementType_WalkSequenceRightLeftDownUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightLeftDownUpDirections)]; memcpy(directions, gRightLeftDownUpDirections, sizeof(gRightLeftDownUpDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3934,9 +3852,8 @@ u8 MovementType_WalkSequenceDownUpRightLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gDownUpRightLeftDirections)]; memcpy(directions, gDownUpRightLeftDirections, sizeof(gDownUpRightLeftDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3947,9 +3864,8 @@ u8 MovementType_WalkSequenceLeftDownUpRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftDownUpRightDirections)]; memcpy(directions, gLeftDownUpRightDirections, sizeof(gLeftDownUpRightDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3960,9 +3876,8 @@ u8 MovementType_WalkSequenceUpLeftRightDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpLeftRightDownDirections)]; memcpy(directions, gUpLeftRightDownDirections, sizeof(gUpLeftRightDownDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3973,9 +3888,8 @@ u8 MovementType_WalkSequenceLeftRightDownUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftRightDownUpDirections)]; memcpy(directions, gLeftRightDownUpDirections, sizeof(gLeftRightDownUpDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3986,9 +3900,8 @@ u8 MovementType_WalkSequenceDownUpLeftRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gStandardDirections)]; memcpy(directions, gStandardDirections, sizeof(gStandardDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -3999,9 +3912,8 @@ u8 MovementType_WalkSequenceRightDownUpLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightDownUpLeftDirections)]; memcpy(directions, gRightDownUpLeftDirections, sizeof(gRightDownUpLeftDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4012,9 +3924,8 @@ u8 MovementType_WalkSequenceLeftUpDownRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftUpDownRightDirections)]; memcpy(directions, gLeftUpDownRightDirections, sizeof(gLeftUpDownRightDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4025,9 +3936,8 @@ u8 MovementType_WalkSequenceUpDownRightLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpDownRightLeftDirections)]; memcpy(directions, gUpDownRightLeftDirections, sizeof(gUpDownRightLeftDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4038,9 +3948,8 @@ u8 MovementType_WalkSequenceRightLeftUpDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightLeftUpDownDirections)]; memcpy(directions, gRightLeftUpDownDirections, sizeof(gRightLeftUpDownDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4051,9 +3960,8 @@ u8 MovementType_WalkSequenceDownRightLeftUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gDownRightLeftUpDirections)]; memcpy(directions, gDownRightLeftUpDirections, sizeof(gDownRightLeftUpDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4064,9 +3972,8 @@ u8 MovementType_WalkSequenceRightUpDownLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightUpDownLeftDirections)]; memcpy(directions, gRightUpDownLeftDirections, sizeof(gRightUpDownLeftDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4077,9 +3984,8 @@ u8 MovementType_WalkSequenceUpDownLeftRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpDownLeftRightDirections)]; memcpy(directions, gUpDownLeftRightDirections, sizeof(gUpDownLeftRightDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4090,9 +3996,8 @@ u8 MovementType_WalkSequenceLeftRightUpDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftRightUpDownDirections)]; memcpy(directions, gLeftRightUpDownDirections, sizeof(gLeftRightUpDownDirections)); if (objectEvent->directionSequenceIndex == 1 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 2; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4103,9 +4008,8 @@ u8 MovementType_WalkSequenceDownLeftRightUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gDownLeftRightUpDirections)]; memcpy(directions, gDownLeftRightUpDirections, sizeof(gDownLeftRightUpDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4116,9 +4020,8 @@ u8 MovementType_WalkSequenceUpLeftDownRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpLeftDownRightDirections)]; memcpy(directions, gUpLeftDownRightDirections, sizeof(gUpLeftDownRightDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4129,9 +4032,8 @@ u8 MovementType_WalkSequenceDownRightUpLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gDownRightUpLeftDirections)]; memcpy(directions, gDownRightUpLeftDirections, sizeof(gDownRightUpLeftDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4142,9 +4044,8 @@ u8 MovementType_WalkSequenceLeftDownRightUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftDownRightUpDirections)]; memcpy(directions, gLeftDownRightUpDirections, sizeof(gLeftDownRightUpDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4155,9 +4056,8 @@ u8 MovementType_WalkSequenceRightUpLeftDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightUpLeftDownDirections)]; memcpy(directions, gRightUpLeftDownDirections, sizeof(gRightUpLeftDownDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4168,9 +4068,8 @@ u8 MovementType_WalkSequenceUpRightDownLeft_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gUpRightDownLeftDirections)]; memcpy(directions, gUpRightDownLeftDirections, sizeof(gUpRightDownLeftDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4181,9 +4080,8 @@ u8 MovementType_WalkSequenceDownLeftUpRight_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gDownLeftUpRightDirections)]; memcpy(directions, gDownLeftUpRightDirections, sizeof(gDownLeftUpRightDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.y == objectEvent->currentCoords.y) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4194,9 +4092,8 @@ u8 MovementType_WalkSequenceLeftUpRightDown_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gLeftUpRightDownDirections)]; memcpy(directions, gLeftUpRightDownDirections, sizeof(gLeftUpRightDownDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4207,9 +4104,8 @@ u8 MovementType_WalkSequenceRightDownLeftUp_Step1(struct ObjectEvent *objectEven u8 directions[sizeof(gRightDownLeftUpDirections)]; memcpy(directions, gRightDownLeftUpDirections, sizeof(gRightDownLeftUpDirections)); if (objectEvent->directionSequenceIndex == 2 && objectEvent->initialCoords.x == objectEvent->currentCoords.x) - { objectEvent->directionSequenceIndex = 3; - } + return MoveNextDirectionInSequence(objectEvent, sprite, directions); } @@ -4219,19 +4115,16 @@ bool8 MovementType_CopyPlayer_Step0(struct ObjectEvent *objectEvent, struct Spri { ClearObjectEventMovement(objectEvent, sprite); if (objectEvent->directionSequenceIndex == 0) - { objectEvent->directionSequenceIndex = GetPlayerFacingDirection(); - } sprite->sTypeFuncId = 1; return TRUE; } bool8 MovementType_CopyPlayer_Step1(struct ObjectEvent *objectEvent, struct Sprite *sprite) { - if (gObjectEvents[gPlayerAvatar.objectEventId].movementActionId == 0xFF || gPlayerAvatar.tileTransitionState == T_TILE_CENTER) - { + if (gObjectEvents[gPlayerAvatar.objectEventId].movementActionId == MOVEMENT_ACTION_NONE || gPlayerAvatar.tileTransitionState == T_TILE_CENTER) return FALSE; - } + return gCopyPlayerMovementFuncs[PlayerGetCopyableMovement()](objectEvent, sprite, GetPlayerMovementDirection(), NULL); } @@ -4252,13 +4145,13 @@ bool8 CopyablePlayerMovement_None(struct ObjectEvent *objectEvent, struct Sprite bool8 CopyablePlayerMovement_FaceDirection(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { - ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, playerDirection))); + ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, playerDirection))); objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; } -bool8 CopyablePlayerMovement_GoSpeed0(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) +bool8 CopyablePlayerMovement_WalkNormal(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { u32 direction; s16 x; @@ -4271,7 +4164,7 @@ bool8 CopyablePlayerMovement_GoSpeed0(struct ObjectEvent *objectEvent, struct Sp if (direction == DIR_NONE) { direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); objectEvent->singleMovementActive = TRUE; @@ -4281,52 +4174,52 @@ bool8 CopyablePlayerMovement_GoSpeed0(struct ObjectEvent *objectEvent, struct Sp } else { - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); } ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); ObjectEventSetSingleMovement(objectEvent, sprite, GetWalkNormalMovementAction(direction)); + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } + objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; } -bool8 CopyablePlayerMovement_GoSpeed1(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) +bool8 CopyablePlayerMovement_WalkFast(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { u32 direction; s16 x; s16 y; direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); ObjectEventSetSingleMovement(objectEvent, sprite, GetWalkFastMovementAction(direction)); + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } + objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; } -bool8 CopyablePlayerMovement_GoSpeed2(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) +bool8 CopyablePlayerMovement_WalkFaster(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { u32 direction; s16 x; s16 y; direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); ObjectEventSetSingleMovement(objectEvent, sprite, GetWalkFasterMovementAction(direction)); + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } + objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; @@ -4339,49 +4232,30 @@ bool8 CopyablePlayerMovement_Slide(struct ObjectEvent *objectEvent, struct Sprit s16 y; direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); ObjectEventSetSingleMovement(objectEvent, sprite, GetSlideMovementAction(direction)); + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } + objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; } -bool8 cph_IM_DIFFERENT(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) +bool8 CopyablePlayerMovement_JumpInPlace(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { u32 direction; direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); ObjectEventSetSingleMovement(objectEvent, sprite, GetJumpInPlaceMovementAction(direction)); objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; } -bool8 CopyablePlayerMovement_GoSpeed4(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) -{ - u32 direction; - s16 x; - s16 y; - - direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); - ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); - ObjectEventSetSingleMovement(objectEvent, sprite, GetJumpMovementAction(direction)); - if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { - ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } - objectEvent->singleMovementActive = TRUE; - sprite->sTypeFuncId = 2; - return TRUE; -} - bool8 CopyablePlayerMovement_Jump(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) { u32 direction; @@ -4389,15 +4263,34 @@ bool8 CopyablePlayerMovement_Jump(struct ObjectEvent *objectEvent, struct Sprite s16 y; direction = playerDirection; - direction = state_to_direction(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); + ObjectEventMoveDestCoords(objectEvent, direction, &x, &y); + ObjectEventSetSingleMovement(objectEvent, sprite, GetJumpMovementAction(direction)); + + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) + ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); + + objectEvent->singleMovementActive = TRUE; + sprite->sTypeFuncId = 2; + return TRUE; +} + +bool8 CopyablePlayerMovement_Jump2(struct ObjectEvent *objectEvent, struct Sprite *sprite, u8 playerDirection, bool8 tileCallback(u8)) +{ + u32 direction; + s16 x; + s16 y; + + direction = playerDirection; + direction = GetCopyDirection(gInitialMovementTypeFacingDirections[objectEvent->movementType], objectEvent->directionSequenceIndex, direction); x = objectEvent->currentCoords.x; y = objectEvent->currentCoords.y; MoveCoordsInDirection(direction, &x, &y, 2, 2); ObjectEventSetSingleMovement(objectEvent, sprite, GetJump2MovementAction(direction)); + if (GetCollisionAtCoords(objectEvent, x, y, direction) || (tileCallback != NULL && !tileCallback(MapGridGetMetatileBehaviorAt(x, y)))) - { ObjectEventSetSingleMovement(objectEvent, sprite, GetFaceDirectionMovementAction(direction)); - } + objectEvent->singleMovementActive = TRUE; sprite->sTypeFuncId = 2; return TRUE; @@ -4475,9 +4368,7 @@ bool8 MovementType_Buried_Step0(struct ObjectEvent *objectEvent, struct Sprite * bool8 MovementType_MoveInPlace_Step1(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (ObjectEventExecSingleMovementAction(objectEvent, sprite)) - { sprite->sTypeFuncId = 0; - } return FALSE; } @@ -4552,7 +4443,7 @@ static void ClearObjectEventMovement(struct ObjectEvent *objectEvent, struct Spr objectEvent->singleMovementActive = FALSE; objectEvent->heldMovementActive = FALSE; objectEvent->heldMovementFinished = FALSE; - objectEvent->movementActionId = 0xFF; + objectEvent->movementActionId = MOVEMENT_ACTION_NONE; sprite->sTypeFuncId = 0; } @@ -4780,9 +4671,8 @@ static bool8 IsMetatileDirectionallyImpassable(struct ObjectEvent *objectEvent, { if (gOppositeDirectionBlockedMetatileFuncs[direction - 1](objectEvent->currentMetatileBehavior) || gDirectionBlockedMetatileFuncs[direction - 1](MapGridGetMetatileBehaviorAt(x, y))) - { return TRUE; - } + return FALSE; } @@ -4799,9 +4689,7 @@ static bool8 DoesObjectCollideWithObjectAt(struct ObjectEvent *objectEvent, s16 if ((curObject->currentCoords.x == x && curObject->currentCoords.y == y) || (curObject->previousCoords.x == x && curObject->previousCoords.y == y)) { if (AreZCoordsCompatible(objectEvent->currentElevation, curObject->currentElevation)) - { return TRUE; - } } } } @@ -4814,9 +4702,7 @@ bool8 IsBerryTreeSparkling(u8 localId, u8 mapNum, u8 mapGroup) if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId) && gSprites[gObjectEvents[objectEventId].spriteId].sBerryTreeFlags & BERRY_FLAG_SPARKLING) - { return TRUE; - } return FALSE; } @@ -4826,9 +4712,7 @@ void SetBerryTreeJustPicked(u8 localId, u8 mapNum, u8 mapGroup) u8 objectEventId; if (!TryGetObjectEventIdByLocalIdAndMap(localId, mapNum, mapGroup, &objectEventId)) - { gSprites[gObjectEvents[objectEventId].spriteId].sBerryTreeFlags |= BERRY_FLAG_JUST_PICKED; - } } #undef sTimer @@ -4933,7 +4817,7 @@ bool8 ObjectEventIsMovementOverridden(struct ObjectEvent *objectEvent) bool8 ObjectEventIsHeldMovementActive(struct ObjectEvent *objectEvent) { - if (objectEvent->heldMovementActive && objectEvent->movementActionId != 0xFF) + if (objectEvent->heldMovementActive && objectEvent->movementActionId != MOVEMENT_ACTION_NONE) return TRUE; return FALSE; @@ -4966,7 +4850,7 @@ void ObjectEventClearHeldMovementIfActive(struct ObjectEvent *objectEvent) void ObjectEventClearHeldMovement(struct ObjectEvent *objectEvent) { - objectEvent->movementActionId = 0xFF; + objectEvent->movementActionId = MOVEMENT_ACTION_NONE; objectEvent->heldMovementActive = FALSE; objectEvent->heldMovementFinished = FALSE; gSprites[objectEvent->spriteId].sTypeFuncId = 0; @@ -5002,14 +4886,12 @@ void UpdateObjectEventCurrentMovement(struct ObjectEvent *objectEvent, struct Sp { DoGroundEffects_OnSpawn(objectEvent, sprite); TryEnableObjectEventAnim(objectEvent, sprite); + if (ObjectEventIsHeldMovementActive(objectEvent)) - { ObjectEventExecHeldMovementAction(objectEvent, sprite); - } else if (!objectEvent->frozen) - { while (callback(objectEvent, sprite)); - } + DoGroundEffects_OnBeginStep(objectEvent, sprite); DoGroundEffects_OnFinishStep(objectEvent, sprite); UpdateObjectEventSpriteAnimPause(objectEvent, sprite); @@ -5068,46 +4950,45 @@ u8 GetOppositeDirection(u8 direction) memcpy(directions, sOppositeDirections, sizeof sOppositeDirections); if (direction <= DIR_NONE || direction > (sizeof sOppositeDirections)) - { return direction; - } + return directions[direction - 1]; } -static u32 zffu_offset_calc(u8 a0, u8 a1) +// Takes the player's original and current direction and gives a direction the copy NPC should consider as the player's direction. +// See comments at the table's definition. +static u32 GetPlayerDirectionForCopy(u8 initDir, u8 moveDir) { - return gUnknown_0850DC2F[a0 - 1][a1 - 1]; + return sPlayerDirectionsForCopy[initDir - 1][moveDir - 1]; } -static u32 state_to_direction(u8 a0, u32 a1, u32 a2) +// copyInitDir is the initial facing direction of the copying NPC. +// playerInitDir is the direction the player was facing when the copying NPC was spawned, as set by MovementType_CopyPlayer_Step0. +// playerMoveDir is the direction the player is currently moving. +static u32 GetCopyDirection(u8 copyInitDir, u32 playerInitDir, u32 playerMoveDir) { - u32 zffuOffset; - u8 a1_2; - u8 a2_2; + u32 dir; + u8 _playerInitDir = playerInitDir; + u8 _playerMoveDir = playerMoveDir; + if (_playerInitDir == DIR_NONE || _playerMoveDir == DIR_NONE + || _playerInitDir > DIR_EAST || _playerMoveDir > DIR_EAST) + return DIR_NONE; - a1_2 = a1; - a2_2 = a2; - if (a1_2 == 0 || a2_2 == 0 || a1_2 > DIR_EAST || a2_2 > DIR_EAST) - { - return 0; - } - zffuOffset = zffu_offset_calc(a1_2, a2); - return gUnknown_0850DC3F[a0 - 1][zffuOffset - 1]; + dir = GetPlayerDirectionForCopy(_playerInitDir, playerMoveDir); + return sPlayerDirectionToCopyDirection[copyInitDir - 1][dir - 1]; } static void ObjectEventExecHeldMovementAction(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (gMovementActionFuncs[objectEvent->movementActionId][sprite->sActionFuncId](objectEvent, sprite)) - { objectEvent->heldMovementFinished = TRUE; - } } static bool8 ObjectEventExecSingleMovementAction(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (gMovementActionFuncs[objectEvent->movementActionId][sprite->sActionFuncId](objectEvent, sprite)) { - objectEvent->movementActionId = 0xFF; + objectEvent->movementActionId = MOVEMENT_ACTION_NONE; sprite->sActionFuncId = 0; return TRUE; } @@ -5795,9 +5676,8 @@ bool8 MovementAction_WalkInPlace_Step1(struct ObjectEvent *objectEvent, struct S bool8 MovementAction_WalkInPlaceSlow_Step1(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (sprite->data[3] & 1) - { sprite->animDelayCounter++; - } + return MovementAction_WalkInPlace_Step1(objectEvent, sprite); } @@ -6255,9 +6135,10 @@ bool8 MovementAction_FacePlayer_Step0(struct ObjectEvent *objectEvent, struct Sp u8 playerObjectId; if (!TryGetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0, &playerObjectId)) - { - FaceDirection(objectEvent, sprite, GetDirectionToFace(objectEvent->currentCoords.x, objectEvent->currentCoords.y, gObjectEvents[playerObjectId].currentCoords.x, gObjectEvents[playerObjectId].currentCoords.y)); - } + FaceDirection(objectEvent, sprite, GetDirectionToFace(objectEvent->currentCoords.x, + objectEvent->currentCoords.y, + gObjectEvents[playerObjectId].currentCoords.x, + gObjectEvents[playerObjectId].currentCoords.y)); sprite->sActionFuncId = 1; return TRUE; } @@ -6267,9 +6148,10 @@ bool8 MovementAction_FaceAwayPlayer_Step0(struct ObjectEvent *objectEvent, struc u8 playerObjectId; if (!TryGetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0, &playerObjectId)) - { - FaceDirection(objectEvent, sprite, GetOppositeDirection(GetDirectionToFace(objectEvent->currentCoords.x, objectEvent->currentCoords.y, gObjectEvents[playerObjectId].currentCoords.x, gObjectEvents[playerObjectId].currentCoords.y))); - } + FaceDirection(objectEvent, sprite, GetOppositeDirection(GetDirectionToFace(objectEvent->currentCoords.x, + objectEvent->currentCoords.y, + gObjectEvents[playerObjectId].currentCoords.x, + gObjectEvents[playerObjectId].currentCoords.y))); sprite->sActionFuncId = 1; return TRUE; } @@ -7399,9 +7281,7 @@ bool8 MovementAction_PauseSpriteAnim(struct ObjectEvent *objectEvent, struct Spr static void UpdateObjectEventSpriteAnimPause(struct ObjectEvent *objectEvent, struct Sprite *sprite) { if (objectEvent->disableAnim) - { sprite->animPaused = TRUE; - } } static void TryEnableObjectEventAnim(struct ObjectEvent *objectEvent, struct Sprite *sprite) @@ -7552,14 +7432,10 @@ static void GetGroundEffectFlags_LongGrassOnBeginStep(struct ObjectEvent *objEve static void GetGroundEffectFlags_Tracks(struct ObjectEvent *objEvent, u32 *flags) { if (MetatileBehavior_IsDeepSand(objEvent->previousMetatileBehavior)) - { *flags |= GROUND_EFFECT_FLAG_DEEP_SAND; - } else if (MetatileBehavior_IsSandOrDeepSand(objEvent->previousMetatileBehavior) || MetatileBehavior_IsFootprints(objEvent->previousMetatileBehavior)) - { *flags |= GROUND_EFFECT_FLAG_SAND; - } } static void GetGroundEffectFlags_SandHeap(struct ObjectEvent *objEvent, u32 *flags) @@ -7569,14 +7445,14 @@ static void GetGroundEffectFlags_SandHeap(struct ObjectEvent *objEvent, u32 *fla { if (!objEvent->inSandPile) { - objEvent->inSandPile = 0; - objEvent->inSandPile = 1; + objEvent->inSandPile = FALSE; + objEvent->inSandPile = TRUE; *flags |= GROUND_EFFECT_FLAG_SAND_PILE; } } else { - objEvent->inSandPile = 0; + objEvent->inSandPile = FALSE; } } @@ -7589,14 +7465,14 @@ static void GetGroundEffectFlags_ShallowFlowingWater(struct ObjectEvent *objEven { if (!objEvent->inShallowFlowingWater) { - objEvent->inShallowFlowingWater = 0; - objEvent->inShallowFlowingWater = 1; + objEvent->inShallowFlowingWater = FALSE; + objEvent->inShallowFlowingWater = TRUE; *flags |= GROUND_EFFECT_FLAG_SHALLOW_FLOWING_WATER; } } else { - objEvent->inShallowFlowingWater = 0; + objEvent->inShallowFlowingWater = FALSE; } } @@ -7604,9 +7480,7 @@ static void GetGroundEffectFlags_Puddle(struct ObjectEvent *objEvent, u32 *flags { if (MetatileBehavior_IsPuddle(objEvent->currentMetatileBehavior) && MetatileBehavior_IsPuddle(objEvent->previousMetatileBehavior)) - { *flags |= GROUND_EFFECT_FLAG_PUDDLE; - } } static void GetGroundEffectFlags_Ripple(struct ObjectEvent *objEvent, u32 *flags) @@ -7622,14 +7496,14 @@ static void GetGroundEffectFlags_ShortGrass(struct ObjectEvent *objEvent, u32 *f { if (!objEvent->inShortGrass) { - objEvent->inShortGrass = 0; - objEvent->inShortGrass = 1; + objEvent->inShortGrass = FALSE; + objEvent->inShortGrass = TRUE; *flags |= GROUND_EFFECT_FLAG_SHORT_GRASS; } } else { - objEvent->inShortGrass = 0; + objEvent->inShortGrass = FALSE; } } @@ -7640,14 +7514,14 @@ static void GetGroundEffectFlags_HotSprings(struct ObjectEvent *objEvent, u32 *f { if (!objEvent->inHotSprings) { - objEvent->inHotSprings = 0; - objEvent->inHotSprings = 1; + objEvent->inHotSprings = FALSE; + objEvent->inHotSprings = TRUE; *flags |= GROUND_EFFECT_FLAG_HOT_SPRINGS; } } else { - objEvent->inHotSprings = 0; + objEvent->inHotSprings = FALSE; } } @@ -8213,11 +8087,11 @@ bool8 FreezeObjectEvent(struct ObjectEvent *objectEvent) } else { - objectEvent->frozen = 1; + objectEvent->frozen = TRUE; objectEvent->spriteAnimPausedBackup = gSprites[objectEvent->spriteId].animPaused; objectEvent->spriteAffineAnimPausedBackup = gSprites[objectEvent->spriteId].affineAnimPaused; - gSprites[objectEvent->spriteId].animPaused = 1; - gSprites[objectEvent->spriteId].affineAnimPaused = 1; + gSprites[objectEvent->spriteId].animPaused = TRUE; + gSprites[objectEvent->spriteId].affineAnimPaused = TRUE; return FALSE; } } diff --git a/src/field_control_avatar.c b/src/field_control_avatar.c index e58fb783b..6d338c06d 100644 --- a/src/field_control_avatar.c +++ b/src/field_control_avatar.c @@ -421,7 +421,7 @@ static const u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 me return SecretBase_EventScript_SandOrnament; if (MetatileBehavior_IsSecretBaseShieldOrToyTV(metatileBehavior) == TRUE) return SecretBase_EventScript_ShieldOrToyTV; - if (MetatileBehavior_IsMB_C6(metatileBehavior) == TRUE) + if (MetatileBehavior_IsSecretBaseDecorationBase(metatileBehavior) == TRUE) { CheckInteractedWithFriendsFurnitureBottom(); return NULL; @@ -729,9 +729,8 @@ static bool8 TryStartWarpEventScript(struct MapPosition *position, u16 metatileB DoTeleportTileWarp(); return TRUE; } - if (MetatileBehavior_IsBridgeOverOcean(metatileBehavior) == TRUE) + if (MetatileBehavior_IsUnionRoomWarp(metatileBehavior) == TRUE) { - // Maybe unused? This MB is used by log bridges, but there's never a warp event on them. DoSpinExitWarp(); return TRUE; } @@ -762,7 +761,7 @@ static bool8 IsWarpMetatileBehavior(u16 metatileBehavior) && MetatileBehavior_IsAquaHideoutWarp(metatileBehavior) != TRUE && MetatileBehavior_IsMtPyreHole(metatileBehavior) != TRUE && MetatileBehavior_IsMossdeepGymWarp(metatileBehavior) != TRUE - && MetatileBehavior_IsBridgeOverOcean(metatileBehavior) != TRUE) + && MetatileBehavior_IsUnionRoomWarp(metatileBehavior) != TRUE) return FALSE; return TRUE; } diff --git a/src/field_player_avatar.c b/src/field_player_avatar.c index 00800636d..bc86a8008 100644 --- a/src/field_player_avatar.c +++ b/src/field_player_avatar.c @@ -225,10 +225,10 @@ static void (*const sPlayerAvatarTransitionFuncs[])(struct ObjectEvent *) = static bool8 (*const sArrowWarpMetatileBehaviorChecks[])(u8) = { - MetatileBehavior_IsSouthArrowWarp, - MetatileBehavior_IsNorthArrowWarp, - MetatileBehavior_IsWestArrowWarp, - MetatileBehavior_IsEastArrowWarp, + [DIR_SOUTH - 1] = MetatileBehavior_IsSouthArrowWarp, + [DIR_NORTH - 1] = MetatileBehavior_IsNorthArrowWarp, + [DIR_WEST - 1] = MetatileBehavior_IsWestArrowWarp, + [DIR_EAST - 1] = MetatileBehavior_IsEastArrowWarp, }; static const u8 sRivalAvatarGfxIds[][2] = @@ -281,10 +281,10 @@ static const u8 sPlayerAvatarGfxToStateFlag[2][5][2] = static bool8 (*const sArrowWarpMetatileBehaviorChecks2[])(u8) = //Duplicate of sArrowWarpMetatileBehaviorChecks { - MetatileBehavior_IsSouthArrowWarp, - MetatileBehavior_IsNorthArrowWarp, - MetatileBehavior_IsWestArrowWarp, - MetatileBehavior_IsEastArrowWarp, + [DIR_SOUTH - 1] = MetatileBehavior_IsSouthArrowWarp, + [DIR_NORTH - 1] = MetatileBehavior_IsNorthArrowWarp, + [DIR_WEST - 1] = MetatileBehavior_IsWestArrowWarp, + [DIR_EAST - 1] = MetatileBehavior_IsEastArrowWarp, }; static bool8 (*const sPushBoulderFuncs[])(struct Task *, struct ObjectEvent *, struct ObjectEvent *) = @@ -929,9 +929,9 @@ static bool8 PlayerCheckIfAnimFinishedOrInactive(void) return ObjectEventCheckHeldMovementStatus(&gObjectEvents[gPlayerAvatar.objectEventId]); } -static void PlayerSetCopyableMovement(u8 a) +static void PlayerSetCopyableMovement(u8 movement) { - gObjectEvents[gPlayerAvatar.objectEventId].playerCopyableMovement = a; + gObjectEvents[gPlayerAvatar.objectEventId].playerCopyableMovement = movement; } u8 PlayerGetCopyableMovement(void) @@ -955,65 +955,65 @@ void PlayerSetAnimId(u8 movementActionId, u8 copyableMovement) void PlayerWalkNormal(u8 direction) { - PlayerSetAnimId(GetWalkNormalMovementAction(direction), 2); + PlayerSetAnimId(GetWalkNormalMovementAction(direction), COPY_MOVE_WALK); } void PlayerWalkFast(u8 direction) { - PlayerSetAnimId(GetWalkFastMovementAction(direction), 2); + PlayerSetAnimId(GetWalkFastMovementAction(direction), COPY_MOVE_WALK); } -void PlayerRideWaterCurrent(u8 a) +void PlayerRideWaterCurrent(u8 direction) { - PlayerSetAnimId(GetRideWaterCurrentMovementAction(a), 2); + PlayerSetAnimId(GetRideWaterCurrentMovementAction(direction), COPY_MOVE_WALK); } void PlayerWalkFaster(u8 direction) { - PlayerSetAnimId(GetWalkFasterMovementAction(direction), 2); + PlayerSetAnimId(GetWalkFasterMovementAction(direction), COPY_MOVE_WALK); } -static void PlayerRun(u8 a) +static void PlayerRun(u8 direction) { - PlayerSetAnimId(GetPlayerRunMovementAction(a), 2); + PlayerSetAnimId(GetPlayerRunMovementAction(direction), COPY_MOVE_WALK); } -void PlayerOnBikeCollide(u8 a) +void PlayerOnBikeCollide(u8 direction) { - PlayCollisionSoundIfNotFacingWarp(a); - PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(a), 2); + PlayCollisionSoundIfNotFacingWarp(direction); + PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(direction), COPY_MOVE_WALK); } -void PlayerOnBikeCollideWithFarawayIslandMew(u8 a) +void PlayerOnBikeCollideWithFarawayIslandMew(u8 direction) { - PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(a), 2); + PlayerSetAnimId(GetWalkInPlaceNormalMovementAction(direction), COPY_MOVE_WALK); } -static void PlayerNotOnBikeCollide(u8 a) +static void PlayerNotOnBikeCollide(u8 direction) { - PlayCollisionSoundIfNotFacingWarp(a); - PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(a), 2); + PlayCollisionSoundIfNotFacingWarp(direction); + PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(direction), COPY_MOVE_WALK); } -static void PlayerNotOnBikeCollideWithFarawayIslandMew(u8 a) +static void PlayerNotOnBikeCollideWithFarawayIslandMew(u8 direction) { - PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(a), 2); + PlayerSetAnimId(GetWalkInPlaceSlowMovementAction(direction), COPY_MOVE_WALK); } void PlayerFaceDirection(u8 direction) { - PlayerSetAnimId(GetFaceDirectionMovementAction(direction), 1); + PlayerSetAnimId(GetFaceDirectionMovementAction(direction), COPY_MOVE_FACE); } void PlayerTurnInPlace(u8 direction) { - PlayerSetAnimId(GetWalkInPlaceFastMovementAction(direction), 1); + PlayerSetAnimId(GetWalkInPlaceFastMovementAction(direction), COPY_MOVE_FACE); } void PlayerJumpLedge(u8 direction) { PlaySE(SE_LEDGE); - PlayerSetAnimId(GetJump2MovementAction(direction), 8); + PlayerSetAnimId(GetJump2MovementAction(direction), COPY_MOVE_JUMP2); } // Stop player on current facing direction once they're done moving and if they're not currently Acro Biking on bumpy slope @@ -1029,81 +1029,82 @@ void PlayerFreeze(void) // wheelie idle void PlayerIdleWheelie(u8 direction) { - PlayerSetAnimId(GetAcroWheelieFaceDirectionMovementAction(direction), 1); + PlayerSetAnimId(GetAcroWheelieFaceDirectionMovementAction(direction), COPY_MOVE_FACE); } // normal to wheelie void PlayerStartWheelie(u8 direction) { - PlayerSetAnimId(GetAcroPopWheelieFaceDirectionMovementAction(direction), 1); + PlayerSetAnimId(GetAcroPopWheelieFaceDirectionMovementAction(direction), COPY_MOVE_FACE); } // wheelie to normal void PlayerEndWheelie(u8 direction) { - PlayerSetAnimId(GetAcroEndWheelieFaceDirectionMovementAction(direction), 1); + PlayerSetAnimId(GetAcroEndWheelieFaceDirectionMovementAction(direction), COPY_MOVE_FACE); } // wheelie hopping standing -void PlayerStandingHoppingWheelie(u8 a) +void PlayerStandingHoppingWheelie(u8 direction) { PlaySE(SE_BIKE_HOP); - PlayerSetAnimId(GetAcroWheelieHopFaceDirectionMovementAction(a), 1); + PlayerSetAnimId(GetAcroWheelieHopFaceDirectionMovementAction(direction), COPY_MOVE_FACE); } // wheelie hopping moving -void PlayerMovingHoppingWheelie(u8 a) +void PlayerMovingHoppingWheelie(u8 direction) { PlaySE(SE_BIKE_HOP); - PlayerSetAnimId(GetAcroWheelieHopDirectionMovementAction(a), 2); + PlayerSetAnimId(GetAcroWheelieHopDirectionMovementAction(direction), COPY_MOVE_WALK); } // wheelie hopping ledge -void PlayerLedgeHoppingWheelie(u8 a) +void PlayerLedgeHoppingWheelie(u8 direction) { PlaySE(SE_BIKE_HOP); - PlayerSetAnimId(GetAcroWheelieJumpDirectionMovementAction(a), 8); + PlayerSetAnimId(GetAcroWheelieJumpDirectionMovementAction(direction), COPY_MOVE_JUMP2); } // acro turn jump void PlayerAcroTurnJump(u8 direction) { PlaySE(SE_BIKE_HOP); - PlayerSetAnimId(GetJumpInPlaceTurnAroundMovementAction(direction), 1); + PlayerSetAnimId(GetJumpInPlaceTurnAroundMovementAction(direction), COPY_MOVE_FACE); } void PlayerWheelieInPlace(u8 direction) { PlaySE(SE_WALL_HIT); - PlayerSetAnimId(GetAcroWheelieInPlaceDirectionMovementAction(direction), 2); + PlayerSetAnimId(GetAcroWheelieInPlaceDirectionMovementAction(direction), COPY_MOVE_WALK); } void PlayerPopWheelieWhileMoving(u8 direction) { - PlayerSetAnimId(GetAcroPopWheelieMoveDirectionMovementAction(direction), 2); + PlayerSetAnimId(GetAcroPopWheelieMoveDirectionMovementAction(direction), COPY_MOVE_WALK); } void PlayerWheelieMove(u8 direction) { - PlayerSetAnimId(GetAcroWheelieMoveDirectionMovementAction(direction), 2); + PlayerSetAnimId(GetAcroWheelieMoveDirectionMovementAction(direction), COPY_MOVE_WALK); } void PlayerEndWheelieWhileMoving(u8 direction) { - PlayerSetAnimId(GetAcroEndWheelieMoveDirectionMovementAction(direction), 2); + PlayerSetAnimId(GetAcroEndWheelieMoveDirectionMovementAction(direction), COPY_MOVE_WALK); } -static void PlayCollisionSoundIfNotFacingWarp(u8 a) +static void PlayCollisionSoundIfNotFacingWarp(u8 direction) { s16 x, y; u8 metatileBehavior = gObjectEvents[gPlayerAvatar.objectEventId].currentMetatileBehavior; - if (!sArrowWarpMetatileBehaviorChecks[a - 1](metatileBehavior)) + if (!sArrowWarpMetatileBehaviorChecks[direction - 1](metatileBehavior)) { - if (a == 2) + // Check if walking up into a door + if (direction == DIR_NORTH) { PlayerGetDestCoords(&x, &y); - MoveCoords(2, &x, &y); + MoveCoords(direction, &x, &y); if (MetatileBehavior_IsWarpDoor(MapGridGetMetatileBehaviorAt(x, y))) return; } diff --git a/src/field_weather.c b/src/field_weather.c index c067e8ebd..cd3436441 100644 --- a/src/field_weather.c +++ b/src/field_weather.c @@ -719,7 +719,7 @@ void ApplyWeatherGammaShiftIfIdle(s8 gammaIndex) } } -void sub_80ABC7C(u8 gammaIndex, u8 gammaTargetIndex, u8 gammaStepDelay) +void ApplyWeatherGammaShiftIfIdle_Gradual(u8 gammaIndex, u8 gammaTargetIndex, u8 gammaStepDelay) { if (gWeatherPtr->palProcessingState == WEATHER_PAL_STATE_IDLE) { @@ -779,7 +779,7 @@ void FadeScreen(u8 mode, s8 delay) if (fadeOut) { if (useWeatherPal) - CpuFastCopy(gPlttBufferFaded, gPlttBufferUnfaded, 0x400); + CpuFastCopy(gPlttBufferFaded, gPlttBufferUnfaded, PLTT_BUFFER_SIZE * 2); BeginNormalPaletteFade(PALETTES_ALL, delay, 0, 16, fadeColor); gWeatherPtr->palProcessingState = WEATHER_PAL_STATE_SCREEN_FADING_OUT; diff --git a/src/field_weather_effect.c b/src/field_weather_effect.c index 27ade478f..5720363c2 100644 --- a/src/field_weather_effect.c +++ b/src/field_weather_effect.c @@ -1181,7 +1181,7 @@ void Thunderstorm_Main(void) case TSTORM_STATE_FADE_THUNDER_LONG: if (--gWeatherPtr->thunderDelay == 0) { - sub_80ABC7C(19, 3, 5); + ApplyWeatherGammaShiftIfIdle_Gradual(19, 3, 5); gWeatherPtr->initStep++; } break; diff --git a/src/international_string_util.c b/src/international_string_util.c index b2ee9743e..21e3e4bee 100644 --- a/src/international_string_util.c +++ b/src/international_string_util.c @@ -214,10 +214,11 @@ int GetNicknameLanguage(u8 *str) return GAME_LANGUAGE; } -void sub_81DB620(int windowId, int columnStart, int rowStart, int numFillTiles, int numRows) +// Used by Pokénav's Match Call to erase the previous trainer's flavor text when switching between their info pages. +void FillWindowTilesByRow(int windowId, int columnStart, int rowStart, int numFillTiles, int numRows) { u8 *windowTileData; - int fillSize, windowRowSize, rowsToFill; + int fillSize, windowRowSize, i; struct Window *window = &gWindows[windowId]; fillSize = numFillTiles * TILE_SIZE_4BPP; @@ -225,12 +226,10 @@ void sub_81DB620(int windowId, int columnStart, int rowStart, int numFillTiles, windowTileData = window->tileData + (rowStart * windowRowSize) + (columnStart * TILE_SIZE_4BPP); if (numRows > 0) { - rowsToFill = numRows; - while (rowsToFill) + for (i = numRows; i != 0; i--) { CpuFastFill8(0x11, windowTileData, fillSize); windowTileData += windowRowSize; - rowsToFill--; } } } diff --git a/src/item_menu.c b/src/item_menu.c index 32b035ec9..720c6f398 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -669,7 +669,7 @@ void VBlankCB_BagMenuRun(void) static void CB2_Bag(void) { - while(MenuHelpers_CallLinkSomething() != TRUE && SetupBagMenu() != TRUE && MenuHelpers_LinkSomething() != TRUE) + while(MenuHelpers_ShouldWaitForLinkRecv() != TRUE && SetupBagMenu() != TRUE && MenuHelpers_IsLinkActive() != TRUE) {}; } @@ -705,7 +705,7 @@ static bool8 SetupBagMenu(void) gMain.state++; break; case 6: - if (!MenuHelpers_LinkSomething()) + if (!MenuHelpers_IsLinkActive()) ResetTasks(); gMain.state++; break; @@ -1215,7 +1215,7 @@ static void Task_BagMenu_HandleInput(u8 taskId) u16* cursorPos = &gBagPosition.cursorPosition[gBagPosition.pocket]; s32 listPosition; - if (MenuHelpers_CallLinkSomething() != TRUE && !gPaletteFade.active) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE && !gPaletteFade.active) { switch (GetSwitchBagPocketDirection()) { @@ -1354,7 +1354,7 @@ static void Task_SwitchBagPocket(u8 taskId) { s16* data = gTasks[taskId].data; - if (!MenuHelpers_LinkSomething() && !IsWallysBag()) + if (!MenuHelpers_IsLinkActive() && !IsWallysBag()) { switch (GetSwitchBagPocketDirection()) { @@ -1449,7 +1449,7 @@ static void Task_HandleSwappingItemsInput(u8 taskId) { s16* data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (JOY_NEW(SELECT_BUTTON)) { @@ -1589,7 +1589,7 @@ static void OpenContextMenu(u8 taskId) case ITEMMENULOCATION_BERRY_TREE: case ITEMMENULOCATION_ITEMPC: default: - if (MenuHelpers_LinkSomething() == TRUE || InUnionRoom() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE || InUnionRoom() == TRUE) { if (gBagPosition.pocket == KEYITEMS_POCKET || !IsHoldingItemAllowed(gSpecialVar_ItemId)) { @@ -1691,7 +1691,7 @@ static void Task_ItemContext_Normal(u8 taskId) static void Task_ItemContext_SingleRow(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s8 selection = Menu_ProcessInputNoWrap(); switch (selection) @@ -1712,7 +1712,7 @@ static void Task_ItemContext_SingleRow(u8 taskId) static void Task_ItemContext_MultipleRows(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s8 cursorPos = Menu_GetCursorPos(); if (JOY_NEW(DPAD_UP)) diff --git a/src/item_use.c b/src/item_use.c index 833e80b97..cee451f36 100755 --- a/src/item_use.c +++ b/src/item_use.c @@ -612,7 +612,7 @@ static void Task_StandingOnHiddenItem(u8 taskId) void ItemUseOutOfBattle_PokeblockCase(u8 taskId) { - if (MenuHelpers_LinkSomething() == TRUE) // link func + if (MenuHelpers_IsLinkActive() == TRUE) { DisplayDadsAdviceCannotUseItemMessage(taskId, gTasks[taskId].tUsingRegisteredKeyItem); } diff --git a/src/link.c b/src/link.c index 8ba7610cc..f5eb8b68c 100644 --- a/src/link.c +++ b/src/link.c @@ -1832,9 +1832,9 @@ u32 GetLinkRecvQueueLength(void) return gLink.recvQueue.count; } -bool32 IsLinkRecvQueueLengthAtLeast3(void) +bool32 IsLinkRecvQueueAtOverworldMax(void) { - if (GetLinkRecvQueueLength() > 2) + if (GetLinkRecvQueueLength() >= OVERWORLD_RECV_QUEUE_MAX) return TRUE; return FALSE; diff --git a/src/mail.c b/src/mail.c index 8bb6f6991..1cbfd8baa 100644 --- a/src/mail.c +++ b/src/mail.c @@ -593,10 +593,8 @@ static bool8 MailReadBuildGraphics(void) } break; case 15: - if (Overworld_LinkRecvQueueLengthMoreThan2() == TRUE) - { + if (Overworld_IsRecvQueueAtMax() == TRUE) return FALSE; - } break; case 16: SetVBlankCallback(VBlankCB_MailRead); @@ -641,7 +639,7 @@ static void CB2_InitMailRead(void) SetMainCallback2(CB2_MailRead); break; } - } while (MenuHelpers_LinkSomething() != TRUE); + } while (MenuHelpers_IsLinkActive() != TRUE); } static void BufferMailText(void) diff --git a/src/menu_helpers.c b/src/menu_helpers.c index 331755590..3e6b6a914 100644 --- a/src/menu_helpers.c +++ b/src/menu_helpers.c @@ -278,51 +278,45 @@ u8 GetLRKeysPressedAndHeld(void) bool8 IsHoldingItemAllowed(u16 itemId) { // Enigma Berry can't be held in link areas - if (itemId != ITEM_ENIGMA_BERRY) - return TRUE; - else if (gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(TRADE_CENTER) - && gSaveBlock1Ptr->location.mapNum == MAP_NUM(TRADE_CENTER)) + if (itemId == ITEM_ENIGMA_BERRY + && ((gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(TRADE_CENTER) + && gSaveBlock1Ptr->location.mapNum == MAP_NUM(TRADE_CENTER)) + || InUnionRoom() == TRUE)) return FALSE; - else if (InUnionRoom() != TRUE) - return TRUE; else - return FALSE; + return TRUE; } bool8 IsWritingMailAllowed(u16 itemId) { - if (IsUpdateLinkStateCBActive() != TRUE && InUnionRoom() != TRUE) + if ((IsOverworldLinkActive() == TRUE || InUnionRoom() == TRUE) && ItemIsMail(itemId) == TRUE) + return FALSE; + else return TRUE; - else if (ItemIsMail(itemId) != TRUE) +} + +bool8 MenuHelpers_IsLinkActive(void) +{ + if (IsOverworldLinkActive() == TRUE || gReceivedRemoteLinkPlayers == 1) return TRUE; else return FALSE; } -bool8 MenuHelpers_LinkSomething(void) +static bool8 IsActiveOverworldLinkBusy(void) { - if (IsUpdateLinkStateCBActive() == TRUE || gReceivedRemoteLinkPlayers == 1) - return TRUE; - else + if (!MenuHelpers_IsLinkActive()) return FALSE; + else + return Overworld_IsRecvQueueAtMax(); } -static bool8 sub_81221D0(void) +bool8 MenuHelpers_ShouldWaitForLinkRecv(void) { - if (!MenuHelpers_LinkSomething()) - return FALSE; - else - return Overworld_LinkRecvQueueLengthMoreThan2(); -} - -bool8 MenuHelpers_CallLinkSomething(void) -{ - if (sub_81221D0() == TRUE) + if (IsActiveOverworldLinkBusy() == TRUE || IsLinkRecvQueueAtOverworldMax() == TRUE ) return TRUE; - else if (IsLinkRecvQueueLengthAtLeast3() != TRUE) - return FALSE; else - return TRUE; + return FALSE; } void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage) diff --git a/src/metatile_behavior.c b/src/metatile_behavior.c index 3829523df..ad5eb42cc 100644 --- a/src/metatile_behavior.c +++ b/src/metatile_behavior.c @@ -136,8 +136,8 @@ static const u8 sTileBitAttributes[] = [MB_BRIDGE_OVER_POND_MED_EDGE_2] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), [MB_BRIDGE_OVER_POND_HIGH_EDGE_1] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), [MB_BRIDGE_OVER_POND_HIGH_EDGE_2] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), - [MB_UNUSED_BRIDGE_1] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), - [MB_UNUSED_BRIDGE_2] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), + [MB_UNUSED_BRIDGE] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), + [MB_BIKE_BRIDGE_OVER_BARRIER] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), [MB_COUNTER] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), [MB_UNUSED_81] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), [MB_UNUSED_82] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), @@ -208,7 +208,7 @@ static const u8 sTileBitAttributes[] = [MB_HOLDS_LARGE_DECORATION] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), [MB_SECRET_BASE_TV_SHIELD] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), [MB_PLAYER_ROOM_PC_ON] = TILE_ATTRIBUTES(TRUE, FALSE, FALSE), - [MB_C6] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), + [MB_SECRET_BASE_DECORATION_BASE] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), [MB_SECRET_BASE_POSTER] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), [MB_UNUSED_C8] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), [MB_UNUSED_C9] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE), @@ -711,9 +711,9 @@ bool8 MetatileBehavior_IsSecretBaseImpassable(u8 metatileBehavior) return FALSE; } -bool8 MetatileBehavior_IsMB_C6(u8 metatileBehavior) +bool8 MetatileBehavior_IsSecretBaseDecorationBase(u8 metatileBehavior) { - if (metatileBehavior == MB_C6) + if (metatileBehavior == MB_SECRET_BASE_DECORATION_BASE) return TRUE; else return FALSE; @@ -901,8 +901,8 @@ bool8 MetatileBehavior_IsBridgeOverWater(u8 metatileBehavior) || metatileBehavior == MB_BRIDGE_OVER_POND_HIGH) || (metatileBehavior == MB_BRIDGE_OVER_POND_HIGH_EDGE_1 || metatileBehavior == MB_BRIDGE_OVER_POND_HIGH_EDGE_2 - || metatileBehavior == MB_UNUSED_BRIDGE_1 - || metatileBehavior == MB_UNUSED_BRIDGE_2)) + || metatileBehavior == MB_UNUSED_BRIDGE + || metatileBehavior == MB_BIKE_BRIDGE_OVER_BARRIER)) return TRUE; else return FALSE; @@ -1262,12 +1262,11 @@ bool8 MetatileBehavior_IsAquaHideoutWarp(u8 metatileBehavior) return FALSE; } -// Very odd, used to initiate a teleport-style warp. -// No warp events seem to be on a metatile of this kind, and it's -// used by log bridges over ocean-style water, which wouldn't make -// sense to have a warp like this. -bool8 MetatileBehavior_IsBridgeOverOcean(u8 metatileBehavior) +bool8 MetatileBehavior_IsUnionRoomWarp(u8 metatileBehavior) { + // This metatile behavior is re-used for some reason by + // the Union Room exit metatile. This function is used to + // initiate a teleport-style warp. if (metatileBehavior == MB_BRIDGE_OVER_OCEAN) return TRUE; else diff --git a/src/minigame_countdown.c b/src/minigame_countdown.c index cd2971732..178b6d5b7 100644 --- a/src/minigame_countdown.c +++ b/src/minigame_countdown.c @@ -447,7 +447,7 @@ static bool32 RunMinigameCountdownDigitsAnim(u8 spriteId) switch (sprite->sState) { case 0: - sub_8007E18(sprite, 0x800, 0x1A); + SetSpriteMatrixAnchor(sprite, NO_ANCHOR, 26); sprite->sState++; // fallthrough case 1: diff --git a/src/mystery_event_menu.c b/src/mystery_event_menu.c index 297883e9c..f6c07acd1 100644 --- a/src/mystery_event_menu.c +++ b/src/mystery_event_menu.c @@ -20,14 +20,11 @@ #include "decompress.h" #include "constants/rgb.h" -// this file's functions static void CB2_MysteryEventMenu(void); static void PrintMysteryMenuText(u8 windowId, const u8 *text, u8 x, u8 y, s32 speed); -// EWRAM vars -static EWRAM_DATA u8 sUnknown_0203BCF8 = 0; // set but unused +static EWRAM_DATA u8 sUnused = 0; // set but unused -// const rom data static const struct BgTemplate sBgTemplates[] = { { @@ -270,7 +267,7 @@ static void CB2_MysteryEventMenu(void) if (!IsTextPrinterActive(0)) { gMain.state++; - sUnknown_0203BCF8 = 0; + sUnused = 0; } break; case 14: diff --git a/src/mystery_gift_menu.c b/src/mystery_gift_menu.c index e1236adfd..709aa28c0 100644 --- a/src/mystery_gift_menu.c +++ b/src/mystery_gift_menu.c @@ -347,7 +347,7 @@ static const struct ListMenuTemplate sListMenu_Receive = { .cursorKind = 0 }; -static const u8 *const Unref_082F0710[] = { +static const u8 *const sUnusedMenuTexts[] = { gText_VarietyOfEventsImportedWireless, gText_WonderCardsInPossession, gText_ReadNewsThatArrived, diff --git a/src/overworld.c b/src/overworld.c index a8d9a4a83..8b32b286c 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -166,7 +166,7 @@ static void ResetPlayerHeldKeys(u16 *); static u16 KeyInterCB_SelfIdle(u32); static u16 KeyInterCB_DeferToEventScript(u32); static u16 GetDirectionForDpadKey(u16); -static void CB1_UpdateLinkState(void); +static void CB1_OverworldLink(void); static void SetKeyInterceptCallback(u16 (*func)(u32)); static void SetFieldVBlankCallback(void); static void FieldClearVBlankHBlankCallbacks(void); @@ -1417,9 +1417,9 @@ static void ResetSafariZoneFlag_(void) ResetSafariZoneFlag(); } -bool32 IsUpdateLinkStateCBActive(void) +bool32 IsOverworldLinkActive(void) { - if (gMain.callback1 == CB1_UpdateLinkState) + if (gMain.callback1 == CB1_OverworldLink) return TRUE; else return FALSE; @@ -1606,7 +1606,7 @@ static void CB2_LoadMapOnReturnToFieldCableClub(void) if (LoadMapInStepsLink(&gMain.state)) { SetFieldVBlankCallback(); - SetMainCallback1(CB1_UpdateLinkState); + SetMainCallback1(CB1_OverworldLink); ResetAllMultiplayerState(); SetMainCallback2(CB2_Overworld); } @@ -1614,7 +1614,7 @@ static void CB2_LoadMapOnReturnToFieldCableClub(void) void CB2_ReturnToField(void) { - if (IsUpdateLinkStateCBActive() == TRUE) + if (IsOverworldLinkActive() == TRUE) { SetMainCallback2(CB2_ReturnToFieldLink); } @@ -1636,7 +1636,7 @@ static void CB2_ReturnToFieldLocal(void) static void CB2_ReturnToFieldLink(void) { - if (!Overworld_LinkRecvQueueLengthMoreThan2() && ReturnToFieldLink(&gMain.state)) + if (!Overworld_IsRecvQueueAtMax() && ReturnToFieldLink(&gMain.state)) SetMainCallback2(CB2_Overworld); } @@ -1644,7 +1644,7 @@ void CB2_ReturnToFieldFromMultiplayer(void) { FieldClearVBlankHBlankCallbacks(); StopMapMusic(); - SetMainCallback1(CB1_UpdateLinkState); + SetMainCallback1(CB1_OverworldLink); ResetAllMultiplayerState(); if (gWirelessCommType != 0) @@ -2226,7 +2226,7 @@ static void CreateLinkPlayerSprites(void) } -static void CB1_UpdateLinkState(void) +static void CB1_OverworldLink(void) { if (gWirelessCommType == 0 || !IsRfuRecvQueueEmpty() || !IsSendingKeysToLink()) { @@ -2442,7 +2442,7 @@ static void UpdateHeldKeyCode(u16 key) if (gWirelessCommType != 0 && GetLinkSendQueueLength() > 1 - && IsUpdateLinkStateCBActive() == TRUE + && IsOverworldLinkActive() == TRUE && IsSendingKeysToLink() == TRUE) { switch (key) @@ -2541,7 +2541,7 @@ static u16 KeyInterCB_DeferToEventScript(u32 key) static u16 KeyInterCB_DeferToRecvQueue(u32 key) { u16 retVal; - if (GetLinkRecvQueueLength() > 2) + if (GetLinkRecvQueueLength() >= OVERWORLD_RECV_QUEUE_MAX) { retVal = LINK_KEY_CODE_EMPTY; } @@ -2613,7 +2613,7 @@ static u16 KeyInterCB_WaitForPlayersToExit(u32 keyOrPlayerId) { // 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. + // CB1_OverworldLink. if (sPlayerLinkStates[keyOrPlayerId] != PLAYER_LINK_STATE_EXITING_ROOM) CheckRfuKeepAliveTimer(); if (AreAllPlayersInLinkState(PLAYER_LINK_STATE_EXITING_ROOM) == TRUE) @@ -2834,11 +2834,11 @@ static void RunTerminateLinkScript(void) ScriptContext2_Enable(); } -bool32 Overworld_LinkRecvQueueLengthMoreThan2(void) +bool32 Overworld_IsRecvQueueAtMax(void) { - if (!IsUpdateLinkStateCBActive()) + if (!IsOverworldLinkActive()) return FALSE; - if (GetLinkRecvQueueLength() >= 3) + if (GetLinkRecvQueueLength() >= OVERWORLD_RECV_QUEUE_MAX) sReceivingFromLink = TRUE; else sReceivingFromLink = FALSE; @@ -2849,9 +2849,9 @@ bool32 Overworld_RecvKeysFromLinkIsRunning(void) { u8 temp; - if (GetLinkRecvQueueLength() < 2) + if (GetLinkRecvQueueLength() < OVERWORLD_RECV_QUEUE_MAX - 1) return FALSE; - else if (IsUpdateLinkStateCBActive() != TRUE) + else if (IsOverworldLinkActive() != TRUE) return FALSE; else if (IsSendingKeysToLink() != TRUE) return FALSE; @@ -2875,7 +2875,7 @@ bool32 Overworld_SendKeysToLinkIsRunning(void) { if (GetLinkSendQueueLength() < 2) return FALSE; - else if (IsUpdateLinkStateCBActive() != TRUE) + else if (IsOverworldLinkActive() != TRUE) return FALSE; else if (IsSendingKeysToLink() != TRUE) return FALSE; diff --git a/src/palette_util.c b/src/palette_util.c index 3fbde9284..08239c336 100755 --- a/src/palette_util.c +++ b/src/palette_util.c @@ -439,7 +439,7 @@ void UpdatePulseBlend(struct PulseBlend *pulseBlend) } // Below used for the Roulette grid -void ClearTilemapRect(u16 *dest, u16 src, u8 left, u8 top, u8 width, u8 height) +void FillTilemapRect(u16 *dest, u16 value, u8 left, u8 top, u8 width, u8 height) { u16 *_dest; u8 i; @@ -450,9 +450,7 @@ void ClearTilemapRect(u16 *dest, u16 src, u8 left, u8 top, u8 width, u8 height) { _dest = dest + i * 32; for (j = 0; j < width; j++) - { - *_dest++ = src; - } + *_dest++ = value; } } @@ -468,8 +466,39 @@ void SetTilemapRect(u16 *dest, u16 *src, u8 left, u8 top, u8 width, u8 height) { _dest = dest + i * 32; for (j = 0; j < width; j++) - { *_dest++ = *_src++; - } + } +} + +static void FillTilemapRect_Unused(void *dest, u16 value, u8 left, u8 top, u8 width, u8 height) +{ + u8 i, j; + u8 x, y; + + for (i = 0, y = top; i < height; i++) + { + for (x = left, j = 0; j < width; j++) + { + *(u16 *)((dest) + (y * 64 + x * 2)) = value; + x = (x + 1) % 32; + } + y = (y + 1) % 32; + } +} + +static void SetTilemapRect_Unused(void *dest, const u16 *src, u8 left, u8 top, u8 width, u8 height) +{ + u8 i, j; + u8 x, y; + const u16 *_src; + + for (i = 0, _src = src, y = top; i < height; i++) + { + for (x = left, j = 0; j < width; j++) + { + *(u16 *)((dest) + (y * 64 + x * 2)) = *(_src++); + x = (x + 1) % 32; + } + y = (y + 1) % 32; } } diff --git a/src/party_menu.c b/src/party_menu.c index 38ca8c9d5..14215f9c0 100755 --- a/src/party_menu.c +++ b/src/party_menu.c @@ -476,7 +476,7 @@ static void CB2_InitPartyMenu(void) { while (TRUE) { - if (MenuHelpers_CallLinkSomething() == TRUE || ShowPartyMenu() == TRUE || MenuHelpers_LinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE || ShowPartyMenu() == TRUE || MenuHelpers_IsLinkActive() == TRUE) break; } } @@ -509,7 +509,7 @@ static bool8 ShowPartyMenu(void) gMain.state++; break; case 5: - if (!MenuHelpers_LinkSomething()) + if (!MenuHelpers_IsLinkActive()) ResetTasks(); gMain.state++; break; @@ -1182,7 +1182,7 @@ u8 GetPartyMenuType(void) void Task_HandleChooseMonInput(u8 taskId) { - if (!gPaletteFade.active && MenuHelpers_CallLinkSomething() != TRUE) + if (!gPaletteFade.active && MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s8 *slotPtr = GetCurrentPartySlotPtr(); @@ -1319,7 +1319,7 @@ static void HandleChooseMonCancel(u8 taskId, s8 *slotPtr) PlaySE(SE_SELECT); if (DisplayCancelChooseMonYesNo(taskId) != TRUE) { - if (!MenuHelpers_LinkSomething()) + if (!MenuHelpers_IsLinkActive()) gSpecialVar_0x8004 = PARTY_SIZE + 1; gPartyMenuUseExitCallback = FALSE; *slotPtr = PARTY_SIZE + 1; @@ -1659,7 +1659,7 @@ bool8 IsPartyMenuTextPrinterActive(void) static void Task_WaitForLinkAndReturnToChooseMon(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { DisplayPartyMenuStdMessage(PARTY_MSG_CHOOSE_MON); gTasks[taskId].func = Task_HandleChooseMonInput; @@ -1672,7 +1672,7 @@ static void Task_ReturnToChooseMonAfterText(u8 taskId) { ClearStdWindowAndFrameToTransparent(6, 0); ClearWindowTilemap(6); - if (MenuHelpers_LinkSomething() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE) { gTasks[taskId].func = Task_WaitForLinkAndReturnToChooseMon; } @@ -2663,7 +2663,7 @@ static void Task_TryCreateSelectionWindow(u8 taskId) static void Task_HandleSelectionMenuInput(u8 taskId) { - if (!gPaletteFade.active && MenuHelpers_CallLinkSomething() != TRUE) + if (!gPaletteFade.active && MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { s8 input; s16 *data = gTasks[taskId].data; @@ -3635,7 +3635,7 @@ static void CursorCb_FieldMove(u8 taskId) PartyMenuRemoveWindow(&sPartyMenuInternal->windowId[0]); PartyMenuRemoveWindow(&sPartyMenuInternal->windowId[1]); - if (MenuHelpers_LinkSomething() == TRUE || InUnionRoom() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE || InUnionRoom() == TRUE) { if (fieldMove == FIELD_MOVE_MILK_DRINK || fieldMove == FIELD_MOVE_SOFT_BOILED) DisplayPartyMenuStdMessage(PARTY_MSG_CANT_USE_HERE); diff --git a/src/pokeblock.c b/src/pokeblock.c index 647a85b10..67c9602d2 100644 --- a/src/pokeblock.c +++ b/src/pokeblock.c @@ -505,11 +505,11 @@ static void CB2_InitPokeblockMenu(void) { while (1) { - if (MenuHelpers_CallLinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE) break; if (InitPokeblockMenu() == TRUE) break; - if (MenuHelpers_LinkSomething() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE) break; } } @@ -1004,7 +1004,7 @@ static void Task_HandlePokeblockMenuInput(u8 taskId) { s16 *data = gTasks[taskId].data; - if (!gPaletteFade.active && MenuHelpers_CallLinkSomething() != TRUE) + if (!gPaletteFade.active && MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (JOY_NEW(SELECT_BUTTON)) { @@ -1057,7 +1057,7 @@ static void Task_HandlePokeblocksSwapInput(u8 taskId) { s16 *data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE) return; if (JOY_NEW(SELECT_BUTTON)) @@ -1161,7 +1161,7 @@ static void Task_HandlePokeblockActionsInput(u8 taskId) { s8 itemId; - if (MenuHelpers_CallLinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE) return; itemId = Menu_ProcessInputNoWrap(); diff --git a/src/pokeblock_feed.c b/src/pokeblock_feed.c index 2241d1668..1253f2285 100644 --- a/src/pokeblock_feed.c +++ b/src/pokeblock_feed.c @@ -688,11 +688,11 @@ void PreparePokeblockFeedScene(void) { while (1) { - if (MenuHelpers_CallLinkSomething() == TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() == TRUE) break; if (LoadPokeblockFeedScene() == TRUE) break; - if (MenuHelpers_LinkSomething() == TRUE) + if (MenuHelpers_IsLinkActive() == TRUE) break; } } diff --git a/src/pokemon_jump.c b/src/pokemon_jump.c index ab86ee09f..2ad25576f 100755 --- a/src/pokemon_jump.c +++ b/src/pokemon_jump.c @@ -3197,7 +3197,7 @@ static void LoadPokeJumpGfx(void) FillBgTilemapBufferRect_Palette0(BG_INTERFACE, 0, 0, 0, 0x20, 0x20); PrintScoreSuffixes(); PrintScore(0); - sub_8098C6C(0, 1, 0xE0); + LoadUserWindowBorderGfxOnBg(0, 1, 0xE0); CopyBgTilemapBufferToVram(BG_INTERFACE); CopyBgTilemapBufferToVram(BG_VENUSAUR); CopyBgTilemapBufferToVram(BG_BONUSES); diff --git a/src/pokemon_summary_screen.c b/src/pokemon_summary_screen.c index dc05c52c4..4dbdd1334 100644 --- a/src/pokemon_summary_screen.c +++ b/src/pokemon_summary_screen.c @@ -1146,7 +1146,7 @@ static void VBlank(void) static void CB2_InitSummaryScreen(void) { - while (MenuHelpers_CallLinkSomething() != TRUE && LoadGraphics() != TRUE && MenuHelpers_LinkSomething() != TRUE); + while (MenuHelpers_ShouldWaitForLinkRecv() != TRUE && LoadGraphics() != TRUE && MenuHelpers_IsLinkActive() != TRUE); } static bool8 LoadGraphics(void) @@ -1490,7 +1490,7 @@ static void BeginCloseSummaryScreen(u8 taskId) static void CloseSummaryScreen(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE && !gPaletteFade.active) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE && !gPaletteFade.active) { SetMainCallback2(sMonSummaryScreen->callback); gLastViewedMonIndex = sMonSummaryScreen->curMonIndex; @@ -1508,7 +1508,7 @@ static void CloseSummaryScreen(u8 taskId) static void Task_HandleInput(u8 taskId) { - if (MenuHelpers_CallLinkSomething() != TRUE && !gPaletteFade.active) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE && !gPaletteFade.active) { if (JOY_NEW(DPAD_UP)) { @@ -1660,7 +1660,7 @@ static void Task_ChangeSummaryMon(u8 taskId) gSprites[sMonSummaryScreen->spriteIds[SPRITE_ARR_ID_MON]].data[2] = 0; break; default: - if (MenuHelpers_CallLinkSomething() == 0 && FuncIsActiveTask(Task_ShowStatusWindow) == 0) + if (!MenuHelpers_ShouldWaitForLinkRecv() && !FuncIsActiveTask(Task_ShowStatusWindow)) { data[0] = 0; gTasks[taskId].func = Task_HandleInput; @@ -1889,7 +1889,7 @@ static void Task_HandleInput_MoveSelect(u8 taskId) { s16 *data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() != 1) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (JOY_NEW(DPAD_UP)) { @@ -2029,7 +2029,7 @@ static void Task_HandleInput_MovePositionSwitch(u8 taskId) { s16* data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (JOY_NEW(DPAD_UP)) { @@ -2170,7 +2170,7 @@ static void Task_HandleReplaceMoveInput(u8 taskId) { s16* data = gTasks[taskId].data; - if (MenuHelpers_CallLinkSomething() != TRUE) + if (MenuHelpers_ShouldWaitForLinkRecv() != TRUE) { if (gPaletteFade.active != TRUE) { diff --git a/src/pokenav.c b/src/pokenav.c index 925560543..d30c523f9 100644 --- a/src/pokenav.c +++ b/src/pokenav.c @@ -212,7 +212,7 @@ u32 CreateLoopedTask(LoopedTask loopedTask, u32 priority) { u16 taskId; - if (!IsUpdateLinkStateCBActive()) + if (!IsOverworldLinkActive()) taskId = CreateTask(Task_RunLoopedTask, priority); else taskId = CreateTask(Task_RunLoopedTask_LinkMode, priority); @@ -288,7 +288,7 @@ static void Task_RunLoopedTask_LinkMode(u8 taskId) s16 *state; u32 action; - if (Overworld_LinkRecvQueueLengthMoreThan2()) + if (Overworld_IsRecvQueueAtMax()) return; task = (LoopedTask)GetWordTaskArg(taskId, 1); diff --git a/src/pokenav_match_call_ui.c b/src/pokenav_match_call_ui.c index ba495245b..f022f04bf 100644 --- a/src/pokenav_match_call_ui.c +++ b/src/pokenav_match_call_ui.c @@ -6,6 +6,7 @@ #include "bg.h" #include "menu.h" #include "decompress.h" +#include "international_string_util.h" // TODO: This UI isnt just for match call, seems to be the general pokenav list UI @@ -66,8 +67,6 @@ struct PokenavSub17 u32 loopedTaskId; }; -extern void sub_81DB620(u32 windowId, u32 a1, u32 a2, u32 a3, u32 a4); - void sub_81C82E4(struct PokenavSub17 *matchCall); bool32 CopyPokenavListMenuTemplate(struct PokenavSub17Substruct *a0, const struct BgTemplate *a1, struct PokenavListTemplate *a2, s32 a3); void InitMatchCallWindowState(struct MatchCallWindowState *a0, struct PokenavListTemplate *a1); @@ -754,7 +753,7 @@ static void PrintMatchCallFlavorText(struct MatchCallWindowState *a0, struct Pok if (str != NULL) { - sub_81DB620(list->listWindow.windowId, 1, r6 * 2, list->listWindow.unk4 - 1, 2); + FillWindowTilesByRow(list->listWindow.windowId, 1, r6 * 2, list->listWindow.unk4 - 1, 2); AddTextPrinterParameterized(list->listWindow.windowId, 7, str, 2, (r6 << 4) + 1, TEXT_SPEED_FF, NULL); CopyWindowRectToVram(list->listWindow.windowId, 2, 0, r6 * 2, list->listWindow.unk4, 2); } diff --git a/src/roulette.c b/src/roulette.c index b6ef1168b..acfeea2b7 100644 --- a/src/roulette.c +++ b/src/roulette.c @@ -1317,28 +1317,28 @@ static void UpdateGridSelectionRect(u8 selectionId) switch (selectionId) { case SELECTION_NONE: - ClearTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); + FillTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); break; case COL_WYNAUT: case COL_AZURILL: case COL_SKITTY: case COL_MAKUHITA: temp0 = (selectionId * 3 + 14); - ClearTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); + FillTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); SetTilemapRect(&sRoulette->tilemapBuffers[0][0], &sRoulette->gridTilemap[281], temp0, 7, 3, 13); break; case ROW_ORANGE: case ROW_GREEN: case ROW_PURPLE: temp1 = ((selectionId - 1) / 5 * 3 + 10); - ClearTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); + FillTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); SetTilemapRect(&sRoulette->tilemapBuffers[0][0], &sRoulette->gridTilemap[320], 14, temp1, 16, 3); break; // Individual square default: temp0 = GET_COL(selectionId) * 3 + 14; temp1 = ((selectionId - 1) / 5 * 3 + 7); - ClearTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); + FillTilemapRect(&sRoulette->tilemapBuffers[0][0], 0, 14, 7, 16, 13); SetTilemapRect(&sRoulette->tilemapBuffers[0][0], &sRoulette->gridTilemap[272], temp0, temp1, 3, 3); break; } diff --git a/src/scrcmd.c b/src/scrcmd.c index 3e3beee59..96d95da27 100644 --- a/src/scrcmd.c +++ b/src/scrcmd.c @@ -1207,7 +1207,7 @@ bool8 ScrCmd_turnvobject(struct ScriptContext *ctx) // The player is frozen after waiting for their current movement to finish. bool8 ScrCmd_lockall(struct ScriptContext *ctx) { - if (IsUpdateLinkStateCBActive()) + if (IsOverworldLinkActive()) { return FALSE; } @@ -1223,7 +1223,7 @@ bool8 ScrCmd_lockall(struct ScriptContext *ctx) // The player and selected object are frozen after waiting for their current movement to finish. bool8 ScrCmd_lock(struct ScriptContext *ctx) { - if (IsUpdateLinkStateCBActive()) + if (IsOverworldLinkActive()) { return FALSE; } @@ -2196,7 +2196,7 @@ bool8 ScrCmd_selectapproachingtrainer(struct ScriptContext *ctx) bool8 ScrCmd_lockfortrainer(struct ScriptContext *ctx) { - if (IsUpdateLinkStateCBActive()) + if (IsOverworldLinkActive()) { return FALSE; } diff --git a/src/start_menu.c b/src/start_menu.c index 59279d6b9..236c6e404 100644 --- a/src/start_menu.c +++ b/src/start_menu.c @@ -251,7 +251,7 @@ static void BuildStartMenuActions(void) { sNumStartMenuActions = 0; - if (IsUpdateLinkStateCBActive() == TRUE) + if (IsOverworldLinkActive() == TRUE) { BuildLinkModeStartMenu(); } @@ -554,7 +554,7 @@ void Task_ShowStartMenu(u8 taskId) void ShowStartMenu(void) { - if (!IsUpdateLinkStateCBActive()) + if (!IsOverworldLinkActive()) { FreezeObjectEvents(); PlayerFreeze(); @@ -679,7 +679,7 @@ static bool8 StartMenuPlayerNameCallback(void) RemoveExtraStartMenuWindows(); CleanupOverworldWindowsAndTilemaps(); - if (IsUpdateLinkStateCBActive() || InUnionRoom()) + if (IsOverworldLinkActive() || InUnionRoom()) ShowPlayerTrainerCard(CB2_ReturnToFieldWithOpenMenu); // Display trainer card else if (FlagGet(FLAG_SYS_FRONTIER_PASS)) ShowFrontierPass(CB2_ReturnToFieldWithOpenMenu); // Display frontier pass diff --git a/src/text_window.c b/src/text_window.c index 864bd0831..798b791ce 100644 --- a/src/text_window.c +++ b/src/text_window.c @@ -6,7 +6,6 @@ #include "bg.h" #include "graphics.h" -// const rom data const u8 gTextWindowFrame1_Gfx[] = INCBIN_U8("graphics/text_window/1.4bpp"); static const u8 sTextWindowFrame2_Gfx[] = INCBIN_U8("graphics/text_window/2.4bpp"); static const u8 sTextWindowFrame3_Gfx[] = INCBIN_U8("graphics/text_window/3.4bpp"); @@ -190,7 +189,8 @@ const u16 *GetOverworldTextboxPalettePtr(void) return gMessageBox_Pal; } -void sub_8098C6C(u8 bg, u16 destOffset, u8 palOffset) +// Effectively LoadUserWindowBorderGfx but specifying the bg directly instead of a window from that bg +void LoadUserWindowBorderGfxOnBg(u8 bg, u16 destOffset, u8 palOffset) { LoadBgTiles(bg, sWindowFrames[gSaveBlock2Ptr->optionsWindowFrameType].tiles, 0x120, destOffset); LoadPalette(GetWindowFrameTilesPal(gSaveBlock2Ptr->optionsWindowFrameType)->pal, palOffset, 0x20); diff --git a/src/trainer_card.c b/src/trainer_card.c index bad015644..45902e7f8 100755 --- a/src/trainer_card.c +++ b/src/trainer_card.c @@ -456,7 +456,7 @@ static void Task_TrainerCard(u8 taskId) } break; case STATE_WAIT_FLIP_TO_BACK: - if (IsCardFlipTaskActive() && Overworld_LinkRecvQueueLengthMoreThan2() != TRUE) + if (IsCardFlipTaskActive() && Overworld_IsRecvQueueAtMax() != TRUE) { PlaySE(SE_RG_CARD_OPEN); sData->mainState = STATE_HANDLE_INPUT_BACK; @@ -513,7 +513,7 @@ static void Task_TrainerCard(u8 taskId) CloseTrainerCard(taskId); break; case STATE_WAIT_FLIP_TO_FRONT: - if (IsCardFlipTaskActive() && Overworld_LinkRecvQueueLengthMoreThan2() != TRUE) + if (IsCardFlipTaskActive() && Overworld_IsRecvQueueAtMax() != TRUE) { sData->mainState = STATE_HANDLE_INPUT_FRONT; PlaySE(SE_RG_CARD_OPEN); @@ -1663,7 +1663,7 @@ static bool8 Task_AnimateCardFlipDown(struct Task* task) static bool8 Task_DrawFlippedCardSide(struct Task* task) { sData->allowDMACopy = FALSE; - if (Overworld_LinkRecvQueueLengthMoreThan2() == TRUE) + if (Overworld_IsRecvQueueAtMax() == TRUE) return FALSE; do diff --git a/src/union_room_chat.c b/src/union_room_chat.c index ca6f8bd1d..a1aae08cf 100755 --- a/src/union_room_chat.c +++ b/src/union_room_chat.c @@ -249,7 +249,7 @@ static void SetBgTilemapBuffers(void); static void ClearBg0(void); static void LoadChatWindowBorderGfx(void); static void LoadChatWindowGfx(void); -static void sub_8020680(void); +static void LoadChatUnkPalette(void); static void LoadChatMessagesWindow(void); static void LoadKeyboardWindow(void); static void LoadKeyboardSwapWindow(void); @@ -1983,10 +1983,10 @@ static int GetShouldShowCaseToggleIcon(void) { u8 *str = GetLastCharOfMessagePtr(); u32 character = *str; - if (character > 0xFF || sCaseToggleTable[character] == character || sCaseToggleTable[character] == 0) - return 3; + if (character > EOS || sCaseToggleTable[character] == character || sCaseToggleTable[character] == CHAR_SPACE) + return 3; // Don't show else - return 0; + return 0; // Show } static u8 *GetChatHostName(void) @@ -2063,7 +2063,7 @@ static void Task_ReceiveChatMessage(u8 taskId) switch (buffer[0]) { default: - case CHAT_MESSAGE_CHAT: tNextState = 3; break; + case CHAT_MESSAGE_CHAT: tNextState = 3; break; case CHAT_MESSAGE_JOIN: tNextState = 3; break; case CHAT_MESSAGE_LEAVE: tNextState = 4; break; case CHAT_MESSAGE_DROP: tNextState = 5; break; @@ -2246,7 +2246,7 @@ static bool32 Display_LoadGfx(u8 *state) LoadChatWindowGfx(); break; case 4: - sub_8020680(); + LoadChatUnkPalette(); break; case 5: LoadChatMessagesWindow(); @@ -3074,7 +3074,7 @@ static void LoadChatWindowGfx(void) CopyBgTilemapBufferToVram(2); } -static void sub_8020680(void) +static void LoadChatUnkPalette(void) { LoadPalette(sUnk_Palette1, 0x80, sizeof(sUnk_Palette1)); RequestDma3Fill(0, (void *)BG_CHAR_ADDR(1) + 0x20, 0x20, 1); From dc44548e43b8cc9dcd8e32061c67ed84073a8631 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 2 Nov 2021 20:28:17 -0400 Subject: [PATCH 2/2] Sync libgcnmultiboot --- src/libgcnmultiboot.s | 429 +++++++++++++++++++++++++----------------- 1 file changed, 260 insertions(+), 169 deletions(-) diff --git a/src/libgcnmultiboot.s b/src/libgcnmultiboot.s index dbf70ccf8..5ecc7d2f9 100644 --- a/src/libgcnmultiboot.s +++ b/src/libgcnmultiboot.s @@ -4,11 +4,36 @@ .include "asm/macros.inc" .include "constants/constants.inc" + .equiv GCMB_STRUCT_COUNTER1, 0x00 + .equiv GCMB_STRUCT_COUNTER2, 0x01 + .equiv GCMB_STRUCT_MBPROGRESS, 0x02 + .equiv GCMB_STRUCT_SAVEDVCOUNT, 0x03 + .equiv GCMB_STRUCT_KEYA, 0x04 + .equiv GCMB_STRUCT_KEYB, 0x08 + .equiv GCMB_STRUCT_KEYC, 0x0C + .equiv GCMB_STRUCT_BOOT_KEY, 0x10 + .equiv GCMB_STRUCT_IMAGE_SIZE, 0x12 + .equiv GCMB_STRUCT_SESSION_KEY, 0x14 + .equiv GCMB_STRUCT_HASH_VAL, 0x18 + .equiv GCMB_STRUCT_KEYC_DERIVATION, 0x1C .equiv GCMB_STRUCT_BASE_DEST_PTR, 0x20 .equiv GCMB_STRUCT_CUR_DEST_PTR, 0x24 .equiv GCMB_STRUCT_SERIAL_INTR_HANDLER, 0x28 - .equiv ROM_HEADER_NINTENDO_LOGO_OFFSET, 0x4 + .equiv ROM_HEADER_NINTENDO_LOGO_OFFSET, 0x04 + .equiv ROM_HEADER_NINTENDO_LOGO_LENGTH, 0x98 + .equiv ROM_HEADER_NINTENDO_LOGO_END, 0xA0 + + .equiv MBPROGRESS_NONE, 0x00 + .equiv MBPROGRESS_LOGO_CORRECT, 0x01 + .equiv MBPROGRESS_READY_TO_BOOT, 0x02 + + .equiv GCMB_MAGIC_BOOTKEY_HASHVAL, 0xBB + .equiv GCMB_MAGIC_BOOTKEY, 0xBB + .equiv GCMB_MAGIC_COUNTER2, 0xCC + .equiv GCMB_MAGIC_KEYA, 0xDD + .equiv GCMB_MAGIC_KEYB, 0xEE + .equiv GCMB_MAGIC_KEYCDERIVATION, 0xFF .syntax unified @@ -22,7 +47,7 @@ GameCubeMultiBoot_Hash: @ 82DED70 movs r2, 0x20 GameCubeMultiBoot_Hash_Loop: - lsrs r3, #1 + lsrs r3, 1 bcc GameCubeMultiBoot_Hash_SkipEor eors r3, r4 @@ -37,137 +62,177 @@ GameCubeMultiBoot_Hash_SkipEor: thumb_func_start GameCubeMultiBoot_Main @ void GameCubeMultiBoot_Main(struct GameCubeMultiBoot *mb); GameCubeMultiBoot_Main: @ 82DED84 - ldr r1, [r0, #GCMB_STRUCT_SERIAL_INTR_HANDLER] - cmp r1, #0 - beq _082DEDAA - ldrb r1, [r0, 0x1] - adds r1, 0x1 - strb r1, [r0, 0x1] - ldrb r1, [r0, 0x2] - cmp r1, 0x2 - beq _082DEDF4 - ldr r3, pool_InterruptRegs - ldrh r2, [r3, #OFFSET_REG_IME - 0x200] - movs r1, 0 - strh r1, [r3, #OFFSET_REG_IME - 0x200] - ldrb r1, [r0] - cmp r1, 0xA - bgt _082DEDA8 - adds r1, 0x1 - strb r1, [r0] -_082DEDA8: - strh r2, [r3, #OFFSET_REG_IME - 0x200] -_082DEDAA: - bcs GameCubeMultiBoot_Init - ldrb r1, [r0, 0x2] + @ If there is no interrupt handler, skip counter manipulation + ldr r1, [r0, GCMB_STRUCT_SERIAL_INTR_HANDLER] cmp r1, 0 - bne _082DEDF6 - ldr r1, [r0, #GCMB_STRUCT_CUR_DEST_PTR] - ldr r2, [r0, #GCMB_STRUCT_BASE_DEST_PTR] + beq GameCubeMultiBoot_Main_SkipCounters + @ Increment the second counter + ldrb r1, [r0, GCMB_STRUCT_COUNTER2] + adds r1, 0x1 + strb r1, [r0, GCMB_STRUCT_COUNTER2] + @ If there is nothing more to do, bail out + ldrb r1, [r0, GCMB_STRUCT_MBPROGRESS] + cmp r1, MBPROGRESS_READY_TO_BOOT + beq GameCubeMultiBoot_Main_Return + @ Save current interrupt master register value + ldr r3, pool_InterruptRegs + ldrh r2, [r3, OFFSET_REG_IME - 0x200] + @ Disable all interrupts + movs r1, 0 + strh r1, [r3, OFFSET_REG_IME - 0x200] + @ Increment the first counter, if it's less than or equal to 10. + ldrb r1, [r0, GCMB_STRUCT_COUNTER1] + cmp r1, 0xA + bgt GameCubeMultiBoot_Main_SkipCounter1Inc + adds r1, 0x1 + strb r1, [r0, GCMB_STRUCT_COUNTER1] +GameCubeMultiBoot_Main_SkipCounter1Inc: + @ Load the saved interrupt master register value (re-enables interrupts if they were enabled before) + strh r2, [r3, OFFSET_REG_IME - 0x200] +GameCubeMultiBoot_Main_SkipCounters: + @ Initialise multiboot structures if required + bcs GameCubeMultiBoot_Init + @ Skip this section (check Nintendo logo) if the check has already passed + ldrb r1, [r0, GCMB_STRUCT_MBPROGRESS] + cmp r1, MBPROGRESS_NONE + bne GameCubeMultiBoot_Main_SkipLogoCheck + @ Bail out if no multiboot image data has been transferred yet + ldr r1, [r0, GCMB_STRUCT_CUR_DEST_PTR] + ldr r2, [r0, GCMB_STRUCT_BASE_DEST_PTR] subs r1, r2 - beq _082DEE76 - cmp r1, 0xA0 - bcc _082DEE76 + beq GameCubeMultiBoot_Main_Return2 + @ Also bail out if not enough data has been transferred + cmp r1, ROM_HEADER_NINTENDO_LOGO_END + bcc GameCubeMultiBoot_Main_Return2 + @ Compare the Nintendo logo of the transferred multiboot image header, with the one in the ROM image of the inserted cart push {r4-r6} - movs r1, 0x98 - adds r2, #ROM_HEADER_NINTENDO_LOGO_OFFSET + movs r1, ROM_HEADER_NINTENDO_LOGO_LENGTH + adds r2, ROM_HEADER_NINTENDO_LOGO_OFFSET ldr r4, pool_NintendoLogo -_082DEDC6: +GameCubeMultiBoot_Main_LogoCmpLoop: ldm r2!, {r5} ldm r4!, {r6} cmp r5, r6 - bne _082DEDDC + bne GameCubeMultiBoot_Main_LogoCmpEnd subs r1, 0x4 - bne _082DEDC6 + bne GameCubeMultiBoot_Main_LogoCmpLoop ldm r2!, {r5} ldm r4!, {r6} eors r5, r6 - lsrs r5, #8 - str r2, [r0, #GCMB_STRUCT_BASE_DEST_PTR] -_082DEDDC: + lsrs r5, 8 + str r2, [r0, GCMB_STRUCT_BASE_DEST_PTR] +GameCubeMultiBoot_Main_LogoCmpEnd: pop {r4-r6} + @ Throw everything away if the logo data didn't match bne GameCubeMultiBoot_Init - movs r1, 0x1 - strb r1, [r0, 0x2] - ldr r1, [r0, 0x4] - ldr r2, [r0, 0x8] + @ Logo matched, set the relevent multiboot progress bit + movs r1, MBPROGRESS_LOGO_CORRECT + strb r1, [r0, GCMB_STRUCT_MBPROGRESS] + @ XOR together KeyA and KeyB to get the initial multiboot image checksum value + ldr r1, [r0, GCMB_STRUCT_KEYA] + ldr r2, [r0, GCMB_STRUCT_KEYB] eors r1, r2 - str r1, [r0, 0x18] + str r1, [r0, GCMB_STRUCT_HASH_VAL] + @ ...also use it as the initial value for the image encryption session key. Algorithm is the same as the GBA BIOS multiboot: sessionkey = (initialvalue * 0x6177614b) + 1 ldr r2, pool_Kawa muls r1, r2 adds r1, 0x1 - str r1, [r0, 0x14] -_082DEDF4: + str r1, [r0, GCMB_STRUCT_SESSION_KEY] +GameCubeMultiBoot_Main_Return: bx lr -_082DEDF6: - ldr r1, [r0, #GCMB_STRUCT_CUR_DEST_PTR] +GameCubeMultiBoot_Main_SkipLogoCheck: + @ If this code is executed, then the logo check has passed, and the data being transferred in is encrypted. + @ Set up registers. + ldr r1, [r0, GCMB_STRUCT_CUR_DEST_PTR] mov r12, r1 - ldr r3, [r0, 0x18] + ldr r3, [r0, GCMB_STRUCT_HASH_VAL] push {r4-r7} - ldr r4, [r0, #GCMB_STRUCT_BASE_DEST_PTR] + ldr r4, [r0, GCMB_STRUCT_BASE_DEST_PTR] ldr r5, pool_Kawa - ldr r6, [r0, 0x14] + ldr r6, [r0, GCMB_STRUCT_SESSION_KEY] ldr r7, pool_HashVal -_082DEE06: +GameCubeMultiBoot_Main_ImageDecryptHashLoop: + @ If there's no more data, break out of the loop cmp r4, r12 - bcs _082DEE26 + bcs GameCubeMultiBoot_Main_ImageDecryptHashEnd + @ Get the next uint32 ldr r1, [r4] + @ Decrypt the ciphertext: plaintext = (ciphertext ^ sessionkey) + hashval eors r1, r6 adds r1, r3 + @ Save the current uint32 of plaintext and advance the pointer stm r4!, {r1} + @ Advance the hashval with this uint32 of plaintext -- this is the same code as GameCubeMultiBoot_Hash. eors r3, r1 movs r2, 0x20 -_082DEE16: - lsrs r3, #1 - bcc _082DEE1C +GameCubeMultiBoot_Main_HashLoop: + lsrs r3, 1 + bcc GameCubeMultiBoot_Main_HashSkipEor eors r3, r7 -_082DEE1C: +GameCubeMultiBoot_Main_HashSkipEor: subs r2, 0x1 - bne _082DEE16 + bne GameCubeMultiBoot_Main_HashLoop + @ Advance the sessionkey with the usual algorithm: sessionkey = (sessionkey * 0x6177614b) + 1 muls r6, r5 adds r6, 0x1 - b _082DEE06 -_082DEE26: - str r4, [r0, #GCMB_STRUCT_BASE_DEST_PTR] - str r6, [r0, 0x14] + b GameCubeMultiBoot_Main_ImageDecryptHashLoop +GameCubeMultiBoot_Main_ImageDecryptHashEnd: + @ Save the new pointer, sessionkey, hashval + str r4, [r0, GCMB_STRUCT_BASE_DEST_PTR] + str r6, [r0, GCMB_STRUCT_SESSION_KEY] pop {r4-r7} - str r3, [r0, 0x18] - ldrh r1, [r0, 0x12] - cmp r1, #0 - bne _082DEE76 - ldr r1, [r0, #GCMB_STRUCT_CUR_DEST_PTR] - ldr r2, [r0, #GCMB_STRUCT_BASE_DEST_PTR] + str r3, [r0, GCMB_STRUCT_HASH_VAL] + @ Bail out if the image size is unknown + ldrh r1, [r0, GCMB_STRUCT_IMAGE_SIZE] + cmp r1, 0 + bne GameCubeMultiBoot_Main_Return2 + @ Bail out if no image data has been transferred + ldr r1, [r0, GCMB_STRUCT_CUR_DEST_PTR] + ldr r2, [r0, GCMB_STRUCT_BASE_DEST_PTR] cmp r1, r2 - bne _082DEE76 - ldr r1, [r0, 0xC] - cmp r1, #0 - beq _082DEE60 - ldrh r1, [r0, 0x10] - cmp r1, #0 - beq _082DEDF4 + bne GameCubeMultiBoot_Main_Return2 + @ If KeyC hasn't been generated yet, go generate it + ldr r1, [r0, GCMB_STRUCT_KEYC] + cmp r1, 0 + beq GameCubeMultiBoot_Main_GenerateKeyC + @ If the other side hasn't sent its boot key yet, bail out + ldrh r1, [r0, GCMB_STRUCT_BOOT_KEY] + cmp r1, 0 + beq GameCubeMultiBoot_Main_Return + @ Save off LR so it doesn't get clobbered by the upcoming function call mov r12, lr - movs r1, 0xBB - ldr r3, [r0, 0xC] + @ Generate the real boot key, which is the checksum of a hardcoded value and KeyC + movs r1, GCMB_MAGIC_BOOTKEY_HASHVAL + ldr r3, [r0, GCMB_STRUCT_KEYC] bl GameCubeMultiBoot_Hash - ldrh r1, [r0, 0x10] + ldrh r1, [r0, GCMB_STRUCT_BOOT_KEY] + @ Restore the saved LR value mov lr, r12 + @ Compare the two boot keys (real and passed in), if they don't match then throw everything away subs r1, r3 bne GameCubeMultiBoot_Init - movs r1, 0x2 - strb r1, [r0, 0x2] + @ The two boot keys matched, tell the caller that the image is ready to boot + movs r1, MBPROGRESS_READY_TO_BOOT + strb r1, [r0, GCMB_STRUCT_MBPROGRESS] + @ Nothing more to do, return. bx lr -_082DEE60: +GameCubeMultiBoot_Main_GenerateKeyC: + @ Save off LR so it doesn't get clobbered by the upcoming function call mov r12, lr - ldrb r1, [r0, 0x3] - lsls r1, #24 + @ KeyC = (SavedVCount << 24) - 1 + ldrb r1, [r0, GCMB_STRUCT_SAVEDVCOUNT] + lsls r1, 24 subs r1, 0x1 - str r1, [r0, 0xC] + str r1, [r0, GCMB_STRUCT_KEYC] + @ Hash the KeyC with the multiboot image checksum to generate the KeyC derivation material to be sent to the other side of the link bl GameCubeMultiBoot_Hash - lsls r3, #8 - adds r3, 0xFF - str r3, [r0, 0x1C] + @ Make sure the sent KeyC derivation material contains a magic value so that the other side can detect it + lsls r3, 8 + adds r3, GCMB_MAGIC_KEYCDERIVATION + @ Save off the KeyC derivation material and return to caller + str r3, [r0, GCMB_STRUCT_KEYC_DERIVATION] bx r12 -_082DEE76: +GameCubeMultiBoot_Main_Return2: bx lr thumb_func_end GameCubeMultiBoot_Main @@ -182,12 +247,15 @@ pool_NintendoLogo: .4byte RomHeaderNintendoLogo thumb_func_start GameCubeMultiBoot_ExecuteProgram @ void GameCubeMultiBoot_ExecuteProgram(struct GameCubeMultiBoot *mb); GameCubeMultiBoot_ExecuteProgram: @ 82DEE84 - ldrb r1, [r0, 0x2] - cmp r1, 0x2 + @ If there's no multiboot image ready, just return to caller + ldrb r1, [r0, GCMB_STRUCT_MBPROGRESS] + cmp r1, MBPROGRESS_READY_TO_BOOT bne GameCubeMultiBoot_ExecuteProgram_Fail + @ Disable interrupts ldr r3, pool_InterruptRegs - movs r1, #0 - strh r1, [r3, #OFFSET_REG_IME - 0x200] + movs r1, 0 + strh r1, [r3, OFFSET_REG_IME - 0x200] + @ Jump to the real entry point of the multiboot image (past the image header), in ARM mode ldr r1, pool_MultiBootLoadAddr adds r1, 0xC0 bx r1 @@ -201,25 +269,25 @@ GameCubeMultiBoot_Init: @ 82DEE98 ldr r3, pool_InterruptRegs @ Save IME register. - ldrh r2, [r3, #OFFSET_REG_IME - 0x200] + ldrh r2, [r3, OFFSET_REG_IME - 0x200] @ Disable interrupts. movs r1, 0 - strh r1, [r3, #OFFSET_REG_IME - 0x200] + strh r1, [r3, OFFSET_REG_IME - 0x200] @ Set the handler to the "Stop" routine. @ Unless the first command that is received is a device reset command, the @ "Stop" routine will be executed and no further commands will be processed. adr r3, GcMbIntrHandler_Stop - str r3, [r0, #GCMB_STRUCT_SERIAL_INTR_HANDLER] + str r3, [r0, GCMB_STRUCT_SERIAL_INTR_HANDLER] ldrb r3, [r0, 0x3] push {r3} ldrb r3, [r0, 0x1] push {r0,r3} - adds r3, r0, #0 - adds r3, #GCMB_STRUCT_BASE_DEST_PTR + adds r3, r0, 0 + adds r3, GCMB_STRUCT_BASE_DEST_PTR @ clear all but the last 3 fields of the struct GameCubeMultiBoot_Init_ClearStructLoop: @@ -261,7 +329,7 @@ GameCubeMultiBoot_Init_ClearStructLoop: strh r1, [r3, OFFSET_REG_IE - 0x200] @ Restore IME register. - strh r2, [r3, #OFFSET_REG_IME - 0x200] + strh r2, [r3, OFFSET_REG_IME - 0x200] bx lr thumb_func_end GameCubeMultiBoot_Init @@ -275,11 +343,11 @@ GameCubeMultiBoot_HandleSerialInterrupt: @ 82DEEE2 ldrh r1, [r3, OFFSET_REG_JOYCNT - 0x120] strh r1, [r3, OFFSET_REG_JOYCNT - 0x120] - movs r2, #0 + movs r2, 0 strb r2, [r0] - ldr r2, [r0, #GCMB_STRUCT_SERIAL_INTR_HANDLER] - cmp r2, #0 + ldr r2, [r0, GCMB_STRUCT_SERIAL_INTR_HANDLER] + cmp r2, 0 beq GameCubeMultiBoot_HandleSerialInterruptDone lsrs r1, 1 @ was a device reset command received? @@ -297,7 +365,7 @@ GcMbIntrHandler_Stop: strh r2, [r3, OFFSET_REG_JOYSTAT - 0x120] GameCubeMultiBoot_SetInterruptHandler: - str r2, [r0, #GCMB_STRUCT_SERIAL_INTR_HANDLER] + str r2, [r0, GCMB_STRUCT_SERIAL_INTR_HANDLER] GameCubeMultiBoot_ReadVCount: ldr r3, pool_RegDispstat @@ -308,19 +376,25 @@ GameCubeMultiBoot_HandleSerialInterruptDone: bx lr GameCubeMultiBoot_BeginHandshake: + @ Throw away anything that got sent ldr r1, [r3, OFFSET_REG_JOY_RECV - 0x120] + @ Send the game code, the other side of the link must send back the same game code ldr r1, pool_RubyUSAGameCode str r1, [r3, OFFSET_REG_JOY_TRANS - 0x120] movs r1, 0x10 strh r1, [r3, OFFSET_REG_JOYSTAT - 0x120] - ldrb r1, [r0, 0x3] - strb r1, [r0, 0x9] - ldrb r1, [r0, 0x2] + @ Use the saved VCount value to provide 8 bits of entropy for KeyB + ldrb r1, [r0, GCMB_STRUCT_SAVEDVCOUNT] + strb r1, [r0, GCMB_STRUCT_KEYB + 1] + @ If a multiboot image has been transferred at least enough such that the Nintendo logo check has passed, stop everything. + ldrb r1, [r0, GCMB_STRUCT_MBPROGRESS] cmp r1, 0 bne GcMbIntrHandler_Stop + @ Set the image destination pointers. ldr r1, pool_MultiBootLoadAddr - str r1, [r0, #GCMB_STRUCT_BASE_DEST_PTR] - str r1, [r0, #GCMB_STRUCT_CUR_DEST_PTR] + str r1, [r0, GCMB_STRUCT_BASE_DEST_PTR] + str r1, [r0, GCMB_STRUCT_CUR_DEST_PTR] + @ Set the new interrupt handler. adr r2, GcMbIntrHandler_CheckGameCodeSent b GameCubeMultiBoot_SetInterruptHandler @@ -347,149 +421,166 @@ GameCubeMultiBoot_CheckHandshakeResponse: ldr r2, pool_RubyUSAGameCode cmp r1, r2 bne GcMbIntrHandler_Stop @ stop if the GameCube didn't reply with the same game code - ldrb r1, [r0, 0x3] - strb r1, [r0, 0xB] - adr r2, GcMbIntrHandler_82DEF44 + @ Use the saved VCount value to provide another 8 bits of entropy for KeyB. + ldrb r1, [r0, GCMB_STRUCT_SAVEDVCOUNT] + strb r1, [r0, GCMB_STRUCT_KEYB + 3] + adr r2, GcMbIntrHandler_ReceiveKeyA b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEF44: @ 82DEF44 +GcMbIntrHandler_ReceiveKeyA: @ 82DEF44 lsrs r1, 1 @ is receive complete? bcc GcMbIntrHandler_Stop @ branch if not ldr r1, [r3, OFFSET_REG_JOY_RECV - 0x120] + @ make sure top 8 bits of the received value is the KeyA magic number, stop if KeyA is invalid lsrs r2, r1, 24 - cmp r2, 0xDD + cmp r2, GCMB_MAGIC_KEYA bne GcMbIntrHandler_Stop - str r1, [r0, 0x4] - ldrb r1, [r0, 0x1] - strb r1, [r0, 0xA] + @ save received KeyA + str r1, [r0, GCMB_STRUCT_KEYA] + @ use the second GameCubeMultiBoot_Main() counter as another 8 bits of entropy for KeyB + ldrb r1, [r0, GCMB_STRUCT_COUNTER2] + strb r1, [r0, GCMB_STRUCT_KEYB + 2] movs r2, 0 movs r3, 0 - ldr r1, [r0, 0x8] + ldr r1, [r0, GCMB_STRUCT_KEYB] lsrs r1, 8 -_082DEF5E: + @ make sure KeyB is valid (other side of the link is supposed to check KeyB too), if it's not then change the byte that was just set so it is +GameCubeMultiBoot_KeyBCheckLoop: lsrs r1, 1 adcs r2, r3 cmp r1, 0 - bne _082DEF5E + bne GameCubeMultiBoot_KeyBCheckLoop cmp r2, 0xE - bgt _082DEF70 + bgt GameCubeMultiBoot_KeyBSaveNewByte cmp r2, 0x7 - bge _082DEF72 + bge GameCubeMultiBoot_KeyBCheckEnd movs r1, 0xFF -_082DEF70: - strb r1, [r0, 0xA] -_082DEF72: - ldr r1, [r0, 0x8] - adds r1, 0xEE +GameCubeMultiBoot_KeyBSaveNewByte: + strb r1, [r0, GCMB_STRUCT_KEYB + 2] +GameCubeMultiBoot_KeyBCheckEnd: + @ add in the KeyB magic number and send off KeyB + ldr r1, [r0, GCMB_STRUCT_KEYB] + adds r1, GCMB_MAGIC_KEYB ldr r3, pool_SerialRegs str r1, [r3, OFFSET_REG_JOY_TRANS - 0x120] movs r1, 0x30 strh r1, [r3, OFFSET_REG_JOYSTAT - 0x120] - adr r2, GcMbIntrHandler_82DEF84 + @ set new interrupt handler + adr r2, GcMbIntrHandler_CheckKeyBSent b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEF84: @ 82DEF84 +GcMbIntrHandler_CheckKeyBSent: @ 82DEF84 lsls r1, 31 bcc GcMbIntrHandler_Stop @ stop if send failed - bmi _082DEF94 @ branch if receive is complete - adr r2, GcMbIntrHandler_82DEF90 + bmi GameCubeMultiBoot_CheckImageSizeResponse @ branch if receive is complete + adr r2, GcMbIntrHandler_CheckImageSizeResponse b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEF90: @ 82DEF90 +GcMbIntrHandler_CheckImageSizeResponse: @ 82DEF90 lsrs r1, 1 @ is receive complete? bcc GcMbIntrHandler_Stop @ branch if not -_082DEF94: +GameCubeMultiBoot_CheckImageSizeResponse: ldr r1, [r3, OFFSET_REG_JOY_RECV - 0x120] - ldr r2, _082DF034 + ldr r2, GameCubeMultiBoot_MaximumImageSizeUInt32s cmp r1, r2 bhs GcMbIntrHandler_Stop adds r1, 0x1 adds r1, r1 - strh r1, [r0, 0x12] - ldrb r1, [r0, 0x2] + strh r1, [r0, GCMB_STRUCT_IMAGE_SIZE] + ldrb r1, [r0, GCMB_STRUCT_MBPROGRESS] cmp r1, 0 -_082DEFA6: +GcMbIntrHandler_StopIfNotEqual: bne GcMbIntrHandler_Stop ldr r1, pool_MultiBootLoadAddr - str r1, [r0, #GCMB_STRUCT_BASE_DEST_PTR] - str r1, [r0, #GCMB_STRUCT_CUR_DEST_PTR] - adr r2, GcMbIntrHandler_82DEFB4 + str r1, [r0, GCMB_STRUCT_BASE_DEST_PTR] + str r1, [r0, GCMB_STRUCT_CUR_DEST_PTR] + adr r2, GcMbIntrHandler_CheckImageResponse b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEFB4: @ 82DEFB4 +GcMbIntrHandler_CheckImageResponse: @ 82DEFB4 lsrs r1, 1 @ is receive complete? bcc GcMbIntrHandler_Stop @ branch if not - ldr r2, [r0, #GCMB_STRUCT_CUR_DEST_PTR] + ldr r2, [r0, GCMB_STRUCT_CUR_DEST_PTR] movs r1, 0x4 ands r1, r2 adds r1, 0x8 lsls r1, 2 strh r1, [r3, OFFSET_REG_JOYSTAT - 0x120] + @ get the recieved uint32 ldr r1, [r3, OFFSET_REG_JOY_RECV - 0x120] + @ put it in the current destination pointer and advance that pointer stm r2!, {r1} - str r2, [r0, #GCMB_STRUCT_CUR_DEST_PTR] - ldrh r1, [r0, 0x12] + @ save off the advanced pointer + str r2, [r0, GCMB_STRUCT_CUR_DEST_PTR] + @ decrease the image size (in uint32s) + ldrh r1, [r0, GCMB_STRUCT_IMAGE_SIZE] subs r1, 0x1 - strh r1, [r0, 0x12] + strh r1, [r0, GCMB_STRUCT_IMAGE_SIZE] + @ branch away if the transfer is not yet complete bne GameCubeMultiBoot_ReadVCount -_082DEFD2: - ldrb r1, [r0, 0x1] +GcMbIntrHandler_SendCounter2: + @ send counter2 with magic number + ldrb r1, [r0, GCMB_STRUCT_COUNTER2] lsls r1, 8 - adds r1, 0xCC + adds r1, GCMB_MAGIC_COUNTER2 str r1, [r3, OFFSET_REG_JOY_TRANS - 0x120] - adr r2, _082DEFE0 + adr r2, GcMbIntrHandler_CheckCounter2Sent b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -_082DEFE0: +GcMbIntrHandler_CheckCounter2Sent: lsls r1, 31 -_082DEFE2: - bcc GcMbIntrHandler_Stop - ldr r1, [r0, 0x1C] +GcMbIntrHandler_StopIfSendFailed: + bcc GcMbIntrHandler_Stop @ stop if send failed + @ if KeyC derivation value has not yet been generated, send Counter2 again, otherwise, send KeyC derivation + ldr r1, [r0, GCMB_STRUCT_KEYC_DERIVATION] cmp r1, 0 - beq _082DEFD2 + beq GcMbIntrHandler_SendCounter2 str r1, [r3, OFFSET_REG_JOY_TRANS - 0x120] - adr r2, GcMbIntrHandler_82DEFF0 + adr r2, GcMbIntrHandler_CheckKeyCDerivationSent b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEFF0: @ 82DEFF0 +GcMbIntrHandler_CheckKeyCDerivationSent: @ 82DEFF0 lsls r1, 31 - bcc _082DEFE2 @ branch if send failed - bmi _082DF000 @ branch if receive is complete - adr r2, GcMbIntrHandler_82DEFFC + bcc GcMbIntrHandler_StopIfSendFailed @ branch if send failed + bmi GameCubeMultiBoot_CheckBootKeyResponse @ branch if receive is complete + adr r2, GcMbIntrHandler_CheckBootKeyResponse b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DEFFC: @ 82DEFFC +GcMbIntrHandler_CheckBootKeyResponse: @ 82DEFFC lsrs r1, 1 @ is receive complete? - bcc _082DEFE2 @ branch if not + bcc GcMbIntrHandler_StopIfSendFailed @ branch if not -_082DF000: +GameCubeMultiBoot_CheckBootKeyResponse: ldr r1, [r3, OFFSET_REG_JOY_RECV - 0x120] + @ make sure received boot key contains expected magic number, stop if not lsrs r2, r1, 24 - cmp r2, 0xBB - bne _082DEFA6 - strh r1, [r0, 0x10] - adr r2, GcMbIntrHandler_82DF010 + cmp r2, GCMB_MAGIC_BOOTKEY + bne GcMbIntrHandler_StopIfNotEqual + @ save received bootkey to be checked in GameCubeMultiBoot_Main() + strh r1, [r0, GCMB_STRUCT_BOOT_KEY] + @ stop if anything more gets sent + adr r2, GcMbIntrHandler_StopUnconditionally b GameCubeMultiBoot_SetInterruptHandler .align 2, 0 -GcMbIntrHandler_82DF010: @ 82DF010 +GcMbIntrHandler_StopUnconditionally: @ 82DF010 b GcMbIntrHandler_Stop thumb_func_end GameCubeMultiBoot_HandleSerialInterrupt @@ -500,11 +591,11 @@ GameCubeMultiBoot_Quit: @ 82DF012 ldr r3, pool_InterruptRegs @ Save IME register. - ldrh r2, [r3, #OFFSET_REG_IME - 0x200] + ldrh r2, [r3, OFFSET_REG_IME - 0x200] @ Disable interrupts. movs r1, 0 - strh r1, [r3, #OFFSET_REG_IME - 0x200] + strh r1, [r3, OFFSET_REG_IME - 0x200] ldr r3, pool_SerialRegs @@ -528,14 +619,14 @@ GameCubeMultiBoot_Quit: @ 82DF012 strh r1, [r3, OFFSET_REG_IE - 0x200] @ Restore IME register. - strh r2, [r3, #OFFSET_REG_IME - 0x200] + strh r2, [r3, OFFSET_REG_IME - 0x200] bx lr thumb_func_end GameCubeMultiBoot_Quit .align 2, 0 -_082DF034: .4byte 0x4000 +GameCubeMultiBoot_MaximumImageSizeUInt32s: .4byte 0x4000 pool_InterruptRegs: .4byte REG_BASE + 0x200