From 89e07f8dbbb42db6b0468d02c1e1e2b15106d93a Mon Sep 17 00:00:00 2001 From: Sirius902 <10891979+Sirius902@users.noreply.github.com> Date: Sun, 10 Jul 2022 06:30:19 -0700 Subject: [PATCH] Dodongo's Cavern blue warp crash fix (#622) * Doodong's Cavern blue warp crash fix * >= not > * Don't waste a line of space --- soh/include/z64bgcheck.h | 1 + soh/soh/z_scene_otr.cpp | 3 ++- soh/src/code/z_play.c | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/soh/include/z64bgcheck.h b/soh/include/z64bgcheck.h index 87c8e246a..30e18afed 100644 --- a/soh/include/z64bgcheck.h +++ b/soh/include/z64bgcheck.h @@ -89,6 +89,7 @@ typedef struct { /* 0x20 */ CamData* cameraDataList; /* 0x24 */ u16 numWaterBoxes; /* 0x28 */ WaterBox* waterBoxes; + size_t cameraDataListLen; // OTRTODO: Added to allow for bounds checking the cameraDataList. } CollisionHeader; // original name: BGDataInfo typedef struct { diff --git a/soh/soh/z_scene_otr.cpp b/soh/soh/z_scene_otr.cpp index 85c9e3c70..8c5839f6e 100644 --- a/soh/soh/z_scene_otr.cpp +++ b/soh/soh/z_scene_otr.cpp @@ -158,6 +158,7 @@ bool func_80098674(GlobalContext* globalCtx, Ship::SceneCommand* cmd) } colHeader->cameraDataList = (CamData*)malloc(sizeof(CamData) * colRes->camData->entries.size()); + colHeader->cameraDataListLen = colRes->camData->entries.size(); for (int i = 0; i < colRes->camData->entries.size(); i++) { @@ -930,4 +931,4 @@ extern "C" s32 OTRfunc_8009728C(GlobalContext* globalCtx, RoomContext* roomCtx, } return 0; -} \ No newline at end of file +} diff --git a/soh/src/code/z_play.c b/soh/src/code/z_play.c index bb579dce2..28582b0e4 100644 --- a/soh/src/code/z_play.c +++ b/soh/src/code/z_play.c @@ -383,6 +383,19 @@ void Gameplay_Init(GameState* thisx) { Camera_InitPlayerSettings(&globalCtx->mainCamera, player); Camera_ChangeMode(&globalCtx->mainCamera, CAM_MODE_NORMAL); + // OTRTODO: Bounds check cameraDataList to guard against scenes spawning the player with + // an out of bounds background camera index. This requires adding an extra field to the + // CollisionHeader struct to save the length of cameraDataList. + // Fixes Dodongo's Cavern blue warp crash. + { + CollisionHeader* colHeader = BgCheck_GetCollisionHeader(&globalCtx->colCtx, BGCHECK_SCENE); + + // If the player's start cam is out of bounds, set it to 0xFF so it isn't used. + if (colHeader != NULL && ((player->actor.params & 0xFF) >= colHeader->cameraDataListLen)) { + player->actor.params |= 0xFF; + } + } + playerStartCamId = player->actor.params & 0xFF; if (playerStartCamId != 0xFF) { osSyncPrintf("player has start camera ID (" VT_FGCOL(BLUE) "%d" VT_RST ")\n", playerStartCamId);