From 42ffe7758ce9dac752b4152f33f244b790cc6ef1 Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 14:36:58 -0500 Subject: [PATCH 1/6] Support C++ code in base patch --- base/Dockerfile | 1 + base/Makefile | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/base/Dockerfile b/base/Dockerfile index b3d197af..1d887dac 100644 --- a/base/Dockerfile +++ b/base/Dockerfile @@ -29,6 +29,7 @@ RUN pip install -r requirements.txt # Add needed source files to the docker build ADD data/* /app/ ADD code/* /app/code/ +ADD code/external/ /app/code/external/ ADD extract_rom.py /app ADD rebuild_rom.py /app ADD Makefile /app diff --git a/base/Makefile b/base/Makefile index f679c60d..fce39e28 100644 --- a/base/Makefile +++ b/base/Makefile @@ -1,16 +1,21 @@ .PHONY: all debug -# Set ARM compiler +# Set ARM compiler (using g++ for C++ support) +CXX=arm-none-eabi-g++ CC=arm-none-eabi-gcc -# Set up compilation flags -CFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Wall -Os - -# C source file directory +# C++ source file directory SRC_DIR=code -SRC=$(wildcard $(SRC_DIR)/*.c) -OBJ=$(SRC:.c=.o) -ASM=$(SRC:.c=.asm) +SRC_CPP=$(wildcard $(SRC_DIR)/*.cpp) +SRC_C=$(wildcard $(SRC_DIR)/*.c) +OBJ_CPP=$(SRC_CPP:.cpp=.o) +OBJ_C=$(SRC_C:.c=.o) +OBJ=$(OBJ_CPP) $(OBJ_C) +ASM=$(SRC_CPP:.cpp=.asm) $(SRC_C:.c=.asm) + +# Set up compilation flags +CXXFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Os -nostdlib -fno-exceptions -fno-rtti -Wno-multichar +CFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Wall -Os -nostdlib INPUT_NDS_FILE=$(PH_ROM_PATH) PREPATCHED_NDS_FILE=prepatched.nds @@ -35,10 +40,18 @@ $(PREPATCHED_NDS_FILE): $(INPUT_NDS_FILE) # Same as `all`, but also includes readable .asm file debug: $(OBJ) $(ASM) +# Compile C++ source file to assembly code +%.asm: %.cpp + $(CXX) $(CXXFLAGS) -S "$<" -o "$@" -fverbose-asm + # Compile C source file to assembly code %.asm: %.c $(CC) $(CFLAGS) -S "$<" -o "$@" -fverbose-asm +# Compile C++ source file to object file +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c "$<" -o "$@" + # Compile C source file to object file %.o: %.c $(CC) $(CFLAGS) -c "$<" -o "$@" From b5f5e11a28181aff24d5ea9d80bf526943f4cbc8 Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 14:36:43 -0500 Subject: [PATCH 2/6] Convert C code to C++ --- ...tom_salvage_item.c => custom_salvage_item.cpp} | 6 +++++- .../{extend_RUPY_npc.c => extend_RUPY_npc.cpp} | 4 ++++ ...m_function.c => extend_give_item_function.cpp} | 6 +++++- base/code/{faster_boat.c => faster_boat.cpp} | 4 ++++ ...n_shop.c => fixed_random_treasure_in_shop.cpp} | 4 ++++ .../code/{get_item_model.c => get_item_model.cpp} | 6 +++++- base/code/{ph.h => ph.hpp} | 15 ++++++++++++++- ..._sword_check.c => progressive_sword_check.cpp} | 4 ++++ .../code/{rando_settings.c => rando_settings.cpp} | 4 ++++ base/code/rando_settings.h | 13 +++++++++++++ ...{set_initial_flags.c => set_initial_flags.cpp} | 6 +++++- ..._item.c => spawn_custom_freestanding_item.cpp} | 6 +++++- 12 files changed, 72 insertions(+), 6 deletions(-) rename base/code/{custom_salvage_item.c => custom_salvage_item.cpp} (95%) rename base/code/{extend_RUPY_npc.c => extend_RUPY_npc.cpp} (95%) rename base/code/{extend_give_item_function.c => extend_give_item_function.cpp} (82%) rename base/code/{faster_boat.c => faster_boat.cpp} (95%) rename base/code/{fixed_random_treasure_in_shop.c => fixed_random_treasure_in_shop.cpp} (89%) rename base/code/{get_item_model.c => get_item_model.cpp} (94%) rename base/code/{ph.h => ph.hpp} (94%) rename base/code/{progressive_sword_check.c => progressive_sword_check.cpp} (96%) rename base/code/{rando_settings.c => rando_settings.cpp} (86%) rename base/code/{set_initial_flags.c => set_initial_flags.cpp} (98%) rename base/code/{spawn_custom_freestanding_item.c => spawn_custom_freestanding_item.cpp} (91%) diff --git a/base/code/custom_salvage_item.c b/base/code/custom_salvage_item.cpp similarity index 95% rename from base/code/custom_salvage_item.c rename to base/code/custom_salvage_item.cpp index f667734b..0f233f6b 100644 --- a/base/code/custom_salvage_item.c +++ b/base/code/custom_salvage_item.cpp @@ -1,10 +1,12 @@ -#include "ph.h" +#include "ph.hpp" #include #define MAX_HEALTH_ADDR 0x021BA348 #define CURRENT_HEALTH_ADDR 0x021BA34A #define PLAYER_HEALTH_PTR_ADDR 0x2146470 +extern "C" { + void custom_salvage_item(uint32_t item_id_with_flag) { uint16_t item_id = item_id_with_flag & 0x7FFF; // Unset the most significant bit @@ -25,3 +27,5 @@ void custom_salvage_item(uint32_t item_id_with_flag) { uint32_t player_data = *player_health_ptr; *((uint16_t *)(player_data + 0xEC)) = item_id; // Store the item ID in the specified offset } + +} // extern "C" diff --git a/base/code/extend_RUPY_npc.c b/base/code/extend_RUPY_npc.cpp similarity index 95% rename from base/code/extend_RUPY_npc.c rename to base/code/extend_RUPY_npc.cpp index 5655c469..bb0a376a 100644 --- a/base/code/extend_RUPY_npc.c +++ b/base/code/extend_RUPY_npc.cpp @@ -1,5 +1,7 @@ #include +extern "C" { + uint32_t extend_RUPY_npc(uint32_t *addr) { uint32_t rupy_type = addr[0x56]; switch (rupy_type) { @@ -24,3 +26,5 @@ uint32_t extend_RUPY_npc(uint32_t *addr) { return 0xffffffff; } } + +} // extern "C" diff --git a/base/code/extend_give_item_function.c b/base/code/extend_give_item_function.cpp similarity index 82% rename from base/code/extend_give_item_function.c rename to base/code/extend_give_item_function.cpp index fea31b72..f5c637db 100644 --- a/base/code/extend_give_item_function.c +++ b/base/code/extend_give_item_function.cpp @@ -1,6 +1,8 @@ -#include "ph.h" +#include "ph.hpp" #include +extern "C" { + void extend_give_item_function(ItemManager *inventory, int32_t item_id) { switch (item_id) { case 0x45: // phantom sword @@ -8,3 +10,5 @@ void extend_give_item_function(ItemManager *inventory, int32_t item_id) { break; } } + +} // extern "C" diff --git a/base/code/faster_boat.c b/base/code/faster_boat.cpp similarity index 95% rename from base/code/faster_boat.c rename to base/code/faster_boat.cpp index 06cd8272..38124f12 100644 --- a/base/code/faster_boat.c +++ b/base/code/faster_boat.cpp @@ -1,5 +1,7 @@ #include +extern "C" { + void faster_boat() { uint32_t boat_speed_offset = (*(uint32_t *)(0x20EE4B4) + 0xF7FFF5B4); @@ -22,3 +24,5 @@ void faster_boat() { } return; } + +} // extern "C" diff --git a/base/code/fixed_random_treasure_in_shop.c b/base/code/fixed_random_treasure_in_shop.cpp similarity index 89% rename from base/code/fixed_random_treasure_in_shop.c rename to base/code/fixed_random_treasure_in_shop.cpp index 1ed28df3..99107f4e 100644 --- a/base/code/fixed_random_treasure_in_shop.c +++ b/base/code/fixed_random_treasure_in_shop.cpp @@ -1,5 +1,7 @@ #include +extern "C" { + __attribute__((target("thumb"))) void fixed_random_treasure_in_shop(char *stack_ptr, char *nsbmd_file, char *nsbtx_file) { for (int i = 0; i < 30; i++) { @@ -7,3 +9,5 @@ fixed_random_treasure_in_shop(char *stack_ptr, char *nsbmd_file, char *nsbtx_fil stack_ptr[i + 0x44] = nsbmd_file[i]; } } + +} // extern "C" diff --git a/base/code/get_item_model.c b/base/code/get_item_model.cpp similarity index 94% rename from base/code/get_item_model.c rename to base/code/get_item_model.cpp index 7a3d0164..e7cf9901 100644 --- a/base/code/get_item_model.c +++ b/base/code/get_item_model.cpp @@ -1,8 +1,10 @@ -#include "ph.h" +#include "ph.hpp" #include #define NEW_DATA_FILE_PATH_PREFIX "r/" +extern "C" { + void get_item_model(uint32_t item_id, char *nsbmd_dest, char *nsbtx_dest) { char *model_name; char *model_file_path_prefix = NEW_DATA_FILE_PATH_PREFIX; @@ -29,3 +31,5 @@ void get_item_model(uint32_t item_id, char *nsbmd_dest, char *nsbtx_dest) { strcat(nsbtx_dest, model_name); strcat(nsbtx_dest, ".nsbtx"); } + +} // extern "C" diff --git a/base/code/ph.h b/base/code/ph.hpp similarity index 94% rename from base/code/ph.h rename to base/code/ph.hpp index 354c5469..9a487a4d 100644 --- a/base/code/ph.h +++ b/base/code/ph.hpp @@ -1,6 +1,13 @@ +#ifndef PH_RANDOMIZER_H +#define PH_RANDOMIZER_H + #include #include +#ifdef __cplusplus +extern "C" { +#endif + // This header file contains definitions for functions, variables, struct // definitions, etc that are present in the base game. @@ -30,7 +37,7 @@ typedef struct { extern bool LoadOverlay(Overlay *overlay); -typedef struct { +typedef struct NPC { uint32_t npc_id; uint32_t (*spawn_function)(void); uint32_t unknown1; @@ -102,3 +109,9 @@ typedef struct { } HealthManager; extern HealthManager *gHealthManager; + +#ifdef __cplusplus +} +#endif + +#endif // PH_RANDOMIZER_H diff --git a/base/code/progressive_sword_check.c b/base/code/progressive_sword_check.cpp similarity index 96% rename from base/code/progressive_sword_check.c rename to base/code/progressive_sword_check.cpp index 153e35db..4cf72518 100644 --- a/base/code/progressive_sword_check.c +++ b/base/code/progressive_sword_check.cpp @@ -6,6 +6,8 @@ #define PHANTOM_SWORD_FLAG_OFFSET (0x12C) #define PHANTOM_SWORD_FLAG_BIT (0x20) +extern "C" { + __attribute__((target("thumb"))) void progressive_sword_check(const uint32_t base_address) { // Address of oshus sword flag uint8_t *oshus_sword = (uint8_t *)(base_address + OSHUS_SWORD_FLAG_OFFSET); @@ -24,3 +26,5 @@ __attribute__((target("thumb"))) void progressive_sword_check(const uint32_t bas *oshus_sword |= OSHUS_SWORD_FLAG_BIT; } } + +} // extern "C" diff --git a/base/code/rando_settings.c b/base/code/rando_settings.cpp similarity index 86% rename from base/code/rando_settings.c rename to base/code/rando_settings.cpp index e9955187..aada9815 100644 --- a/base/code/rando_settings.c +++ b/base/code/rando_settings.cpp @@ -1,6 +1,10 @@ #include "rando_settings.h" +extern "C" { + bool setting_is_enabled(uint8_t offset, uint8_t bit) { uint8_t *base_addr = (uint8_t *)RANDO_SETTINGS_BITMAP_ADDR; return (base_addr[offset] & bit) == bit; } + +} // extern "C" diff --git a/base/code/rando_settings.h b/base/code/rando_settings.h index 0163b3e5..45b6215a 100644 --- a/base/code/rando_settings.h +++ b/base/code/rando_settings.h @@ -1,6 +1,13 @@ +#ifndef RANDO_SETTINGS_H +#define RANDO_SETTINGS_H + #include #include +#ifdef __cplusplus +extern "C" { +#endif + // RAM address of bitmap encoding randomizer settings #define RANDO_SETTINGS_BITMAP_ADDR 0x2058180 @@ -18,3 +25,9 @@ * @return true if setting is enabled, false if it is disabled */ bool setting_is_enabled(uint8_t offset, uint8_t bit); + +#ifdef __cplusplus +} +#endif + +#endif // RANDO_SETTINGS_H diff --git a/base/code/set_initial_flags.c b/base/code/set_initial_flags.cpp similarity index 98% rename from base/code/set_initial_flags.c rename to base/code/set_initial_flags.cpp index 3fe44fd6..d1f83cf6 100644 --- a/base/code/set_initial_flags.c +++ b/base/code/set_initial_flags.cpp @@ -1,7 +1,9 @@ -#include "ph.h" +#include "ph.hpp" #include "rando_settings.h" #include +extern "C" { + #define NO_SETTING -1, 0 typedef struct { @@ -84,3 +86,5 @@ void set_initial_flags(uint32_t base_flag_address) { } } } + +} // extern "C" diff --git a/base/code/spawn_custom_freestanding_item.c b/base/code/spawn_custom_freestanding_item.cpp similarity index 91% rename from base/code/spawn_custom_freestanding_item.c rename to base/code/spawn_custom_freestanding_item.cpp index 9533ec10..004cd3fe 100644 --- a/base/code/spawn_custom_freestanding_item.c +++ b/base/code/spawn_custom_freestanding_item.cpp @@ -2,6 +2,8 @@ #define RUPY 0x52555059 +extern "C" { + /** * @param param_1 - original function arg, don't modify * @param npc_type - 4 character string representing NPC type. @@ -11,7 +13,7 @@ uint16_t spawn_custom_freestanding_item(void *param_1, uint32_t npc_type, void *param_3, uint16_t *param_4) { // declare pointer to the game's `spawn_npc` function - uint16_t (*spawn_npc)(void *, uint32_t, void *, uint16_t *) = (void *)0x20C3FE8; + uint16_t (*spawn_npc)(void *, uint32_t, void *, uint16_t *) = reinterpret_cast(0x20C3FE8); uint16_t *item_id_address; // pull the base address of the NPCA entry's item_id off of the stack @@ -29,3 +31,5 @@ uint16_t spawn_custom_freestanding_item(void *param_1, uint32_t npc_type, void * *param_4 = item_id; return (*spawn_npc)(param_1, RUPY, param_3, param_4); } + +} // extern "C" From 3094cf969e0122b59bc041d996b39c169818492b Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 14:17:42 -0500 Subject: [PATCH 3/6] Add decomp as submodule --- .github/workflows/build-test-release.yml | 3 +++ .gitmodules | 3 +++ base/code/external/ph-decomp | 1 + 3 files changed, 7 insertions(+) create mode 100644 .gitmodules create mode 160000 base/code/external/ph-decomp diff --git a/.github/workflows/build-test-release.yml b/.github/workflows/build-test-release.yml index eb3f1f07..4a8f326f 100644 --- a/.github/workflows/build-test-release.yml +++ b/.github/workflows/build-test-release.yml @@ -289,6 +289,8 @@ jobs: google_drive_url_secret: PH_US_DPAD_GOOGLE_DRIVE_ID steps: - uses: actions/checkout@v4 + with: + submodules: recursive - name: Set up Python uses: actions/setup-python@v5 @@ -404,6 +406,7 @@ jobs: steps: - uses: actions/checkout@v4 with: + submodules: recursive # Also retrieve previous commit so we can check if there are relevant changes fetch-depth: 2 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..dc068475 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "base/code/external/ph-decomp"] + path = base/code/external/ph-decomp + url = https://github.com/zeldaret/ph diff --git a/base/code/external/ph-decomp b/base/code/external/ph-decomp new file mode 160000 index 00000000..5b15874c --- /dev/null +++ b/base/code/external/ph-decomp @@ -0,0 +1 @@ +Subproject commit 5b15874c4d2d7c185c98f2791cf8fa6a9d86a440 From 3f368c75bab53000080743dd7516d9abd8f498ab Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 15:03:15 -0500 Subject: [PATCH 4/6] Add decomp headers to include path --- base/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/Makefile b/base/Makefile index fce39e28..bbd30d99 100644 --- a/base/Makefile +++ b/base/Makefile @@ -14,7 +14,7 @@ OBJ=$(OBJ_CPP) $(OBJ_C) ASM=$(SRC_CPP:.cpp=.asm) $(SRC_C:.c=.asm) # Set up compilation flags -CXXFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Os -nostdlib -fno-exceptions -fno-rtti -Wno-multichar +CXXFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Os -nostdlib -fno-exceptions -fno-rtti -Wno-multichar -fpermissive -I$(SRC_DIR)/external/ph-decomp/include -I$(SRC_DIR)/external/ph-decomp/libs/nds/include -I$(SRC_DIR)/external/ph-decomp/libs/c/include CFLAGS := -marm -mno-thumb-interwork -march=armv5te -mtune=arm946e-s -fno-inline -Wall -Os -nostdlib INPUT_NDS_FILE=$(PH_ROM_PATH) From a41c6a9db6f7620553b8c5ef820ab37418f7e10f Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 15:00:11 -0500 Subject: [PATCH 5/6] Restructure code to use decomp where possible --- base/code/custom_salvage_item.cpp | 19 ++-- base/code/extend_RUPY_npc.cpp | 6 +- base/code/extend_give_item_function.cpp | 5 +- base/code/faster_boat.cpp | 10 +- base/code/fixed_random_treasure_in_shop.cpp | 2 +- base/code/get_item_model.cpp | 3 +- base/code/ph.asm | 3 +- base/code/ph.hpp | 111 ++++--------------- base/code/progressive_sword_check.cpp | 9 +- base/code/rando_settings.cpp | 4 +- base/code/rando_settings.h | 5 +- base/code/set_initial_flags.cpp | 17 ++- base/code/spawn_custom_freestanding_item.cpp | 12 +- 13 files changed, 63 insertions(+), 143 deletions(-) diff --git a/base/code/custom_salvage_item.cpp b/base/code/custom_salvage_item.cpp index 0f233f6b..d354a1fa 100644 --- a/base/code/custom_salvage_item.cpp +++ b/base/code/custom_salvage_item.cpp @@ -1,5 +1,4 @@ #include "ph.hpp" -#include #define MAX_HEALTH_ADDR 0x021BA348 #define CURRENT_HEALTH_ADDR 0x021BA34A @@ -7,25 +6,25 @@ extern "C" { -void custom_salvage_item(uint32_t item_id_with_flag) { - uint16_t item_id = item_id_with_flag & 0x7FFF; // Unset the most significant bit +void custom_salvage_item(u32 item_id_with_flag) { + u16 item_id = item_id_with_flag & 0x7FFF; // Unset the most significant bit // The salvage arm "get item" code doesn't properly set memory values for items // that it was never intended to give, so we need to manually set them here. switch (item_id) { case 0xA: // heart container - gHealthManager->mMaxHealth += 4; // Increment max health by 4 (one heart container) - gHealthManager->mHealth = - gHealthManager->mMaxHealth; // Restore the player's health to the new maximum + gPlayerManager->mMaxHealth += 4; // Increment max health by 4 (one heart container) + gPlayerManager->mHealth = + gPlayerManager->mMaxHealth; // Restore the player's health to the new maximum break; case 0x45: // phantom sword - gItemManager->mItemFlags[1] = gItemManager->mItemFlags[1] | 0x20; + gItemManager->mItemFlags.flags[1] = gItemManager->mItemFlags.flags[1] | 0x20; break; } - uint32_t *player_health_ptr = (uint32_t *)PLAYER_HEALTH_PTR_ADDR; - uint32_t player_data = *player_health_ptr; - *((uint16_t *)(player_data + 0xEC)) = item_id; // Store the item ID in the specified offset + u32 *player_health_ptr = (u32 *)PLAYER_HEALTH_PTR_ADDR; + u32 player_data = *player_health_ptr; + *((u16 *)(player_data + 0xEC)) = item_id; // Store the item ID in the specified offset } } // extern "C" diff --git a/base/code/extend_RUPY_npc.cpp b/base/code/extend_RUPY_npc.cpp index bb0a376a..0ac38cc4 100644 --- a/base/code/extend_RUPY_npc.cpp +++ b/base/code/extend_RUPY_npc.cpp @@ -1,9 +1,9 @@ -#include +#include "ph.hpp" extern "C" { -uint32_t extend_RUPY_npc(uint32_t *addr) { - uint32_t rupy_type = addr[0x56]; +u32 extend_RUPY_npc(u32 *addr) { + u32 rupy_type = addr[0x56]; switch (rupy_type) { case 3: return 9; diff --git a/base/code/extend_give_item_function.cpp b/base/code/extend_give_item_function.cpp index f5c637db..149eae2a 100644 --- a/base/code/extend_give_item_function.cpp +++ b/base/code/extend_give_item_function.cpp @@ -1,12 +1,11 @@ #include "ph.hpp" -#include extern "C" { -void extend_give_item_function(ItemManager *inventory, int32_t item_id) { +void extend_give_item_function(ItemManager *inventory, s32 item_id) { switch (item_id) { case 0x45: // phantom sword - inventory->mItemFlags[1] = inventory->mItemFlags[1] | 0x20; + inventory->mItemFlags.flags[1] = inventory->mItemFlags.flags[1] | 0x20; break; } } diff --git a/base/code/faster_boat.cpp b/base/code/faster_boat.cpp index 38124f12..9d21e9cf 100644 --- a/base/code/faster_boat.cpp +++ b/base/code/faster_boat.cpp @@ -1,21 +1,21 @@ -#include +#include "ph.hpp" extern "C" { void faster_boat() { - uint32_t boat_speed_offset = (*(uint32_t *)(0x20EE4B4) + 0xF7FFF5B4); + u32 boat_speed_offset = (*(u32 *)(0x20EE4B4) + 0xF7FFF5B4); - int16_t *boat_speed = (int16_t *)(0x8000000 + boat_speed_offset); + s16 *boat_speed = (s16 *)(0x8000000 + boat_speed_offset); // Increase boat speed if R button is pressed and the current speed is less // than max speed - if (((*((uint16_t *)0x4000130) >> 8) & 1) == 0 && *boat_speed < 0x540) { + if (((*((u16 *)0x4000130) >> 8) & 1) == 0 && *boat_speed < 0x540) { *boat_speed += 0x20; } // Decrease boat speed if L button is pressed and the current speed is greater // than zero - if (((*((uint16_t *)0x4000130) >> 9) & 1) == 0 && *boat_speed > 0) { + if (((*((u16 *)0x4000130) >> 9) & 1) == 0 && *boat_speed > 0) { *boat_speed -= 0x20; } diff --git a/base/code/fixed_random_treasure_in_shop.cpp b/base/code/fixed_random_treasure_in_shop.cpp index 99107f4e..c78368b2 100644 --- a/base/code/fixed_random_treasure_in_shop.cpp +++ b/base/code/fixed_random_treasure_in_shop.cpp @@ -1,4 +1,4 @@ -#include +#include "ph.hpp" extern "C" { diff --git a/base/code/get_item_model.cpp b/base/code/get_item_model.cpp index e7cf9901..3d2b8abb 100644 --- a/base/code/get_item_model.cpp +++ b/base/code/get_item_model.cpp @@ -1,11 +1,10 @@ #include "ph.hpp" -#include #define NEW_DATA_FILE_PATH_PREFIX "r/" extern "C" { -void get_item_model(uint32_t item_id, char *nsbmd_dest, char *nsbtx_dest) { +void get_item_model(u32 item_id, char *nsbmd_dest, char *nsbtx_dest) { char *model_name; char *model_file_path_prefix = NEW_DATA_FILE_PATH_PREFIX; diff --git a/base/code/ph.asm b/base/code/ph.asm index b18ddfbd..a0db3bcb 100644 --- a/base/code/ph.asm +++ b/base/code/ph.asm @@ -15,5 +15,4 @@ .definelabel got_new_item_model_path_prefix, 0x20e5da8 .definelabel item_id_to_string, 0x20e5c3c .definelabel gItemManager, 0x27e0fb4 -.definelabel GiveItem, 0x20adc7c -.definelabel gHealthManager, 0x27e0fbc +.definelabel gPlayerManager, 0x27e0fbc diff --git a/base/code/ph.hpp b/base/code/ph.hpp index 9a487a4d..eedf0359 100644 --- a/base/code/ph.hpp +++ b/base/code/ph.hpp @@ -1,115 +1,42 @@ #ifndef PH_RANDOMIZER_H #define PH_RANDOMIZER_H -#include -#include - #ifdef __cplusplus extern "C" { #endif +#include "types.h" +#include "string.h" +#ifdef __cplusplus +} +#endif + +#include "Actor/ActorType.hpp" +#include "Item/ItemManager.hpp" +#include "Player/PlayerManager.hpp" // This header file contains definitions for functions, variables, struct // definitions, etc that are present in the base game. -extern void strcat(char *dest, char *src); -extern int strcmp(char *s1, char *s2); -extern int strncmp(char *s1, char *s2, int n); -extern void strcpy(char *dest, char *src); -extern void strncpy(char *dest, char *src, int n); -extern int32_t strlen(char *s); -extern char *strrchr(char *s, int c); -extern char *strstr(char *haystack, char needle); - -// TODO: verify this is correct. I'm not sure if this -// function is really an implementation of sprintf. -extern void sprintf(char *string, char *format); +#ifdef __cplusplus +extern "C" { +#endif typedef struct { - uint32_t id; + u32 id; void *baseAddr; - int32_t textSize; - int32_t bssSize; - int32_t sinitStart; - int32_t sinitEnd; - int32_t fileId; - int32_t fileSize; + s32 textSize; + s32 bssSize; + s32 sinitStart; + s32 sinitEnd; + s32 fileId; + s32 fileSize; } Overlay; extern bool LoadOverlay(Overlay *overlay); -typedef struct NPC { - uint32_t npc_id; - uint32_t (*spawn_function)(void); - uint32_t unknown1; - uint32_t unknown2; - struct NPC *next; -} NPC; - -// searches the list of NPC structs in memory for the given NPC and returns its -// address. -extern NPC *get_npc_address(uint32_t npc_id); // 203e824 - extern char *got_new_item_model_path_prefix; extern char *item_id_to_string[]; -typedef struct { - int32_t mEquippedItem; - int32_t mPrevEquippedItem; - int32_t mForcedItem; // game crashes when any item besides this one is equipped - uint32_t mHourglassSandFrames; - int32_t mEquippedFairy; - void *mFairies[3]; - uint16_t mEquipLoadTimer; - uint16_t mNumRupees; - uint8_t mNumGems[3]; - uint8_t mUnk_027; // padding? - uint32_t mEquippedShipParts[8]; - int8_t mShipParts[8][9]; - int8_t mTreasure[8]; - uint8_t mUnk_098[6]; // max 99 - uint16_t mUnk_09e[6]; // max 9999, corresponds with mUnk_098 - uint16_t mUnk_0aa; // padding? - void *(*mEquipItems)[11]; - uint16_t (*mAmmo)[11]; - uint16_t mQuiverSize; - uint16_t mBombBagSize; - uint16_t mBombchuBagSize; - uint16_t mUnk_0ba; // only between 0 and 9 - uint8_t mPotions[2]; - uint8_t mUnk_0be[2]; // padding? - void *mItemModels[16]; - void *mDungeonItemModels[5]; // non-null in dungeons/caves - void *mModelRender; - int32_t mFanfareItemId; - uint32_t mFanfareSfx; - void *mFanfareItemModel; - void *mUnk_124; - uint32_t mItemFlags[4]; - uint32_t mSalvagedTreasureFlags; - uint32_t mShipPartPricesShown[3]; - uint32_t mTreasurePriceShownFlag; - bool mMuteNextFanfare; - uint8_t mUnk_14d; - uint8_t mUnk_14e[0x2]; // padding? -} ItemManager; - -extern ItemManager *gItemManager; -extern void GiveItem(ItemManager *inventory, int32_t itemId); - -typedef struct { - uint16_t mMaxHealth; - uint16_t mHealth; - int16_t mMaxShipHealth; - int16_t mShipHealth; - uint16_t mSalvageArmHealth; - int16_t mFlags; - uint16_t mUnk_0c; - uint8_t mUnk_0e; - uint8_t mUnk_0f; -} HealthManager; - -extern HealthManager *gHealthManager; - #ifdef __cplusplus } #endif diff --git a/base/code/progressive_sword_check.cpp b/base/code/progressive_sword_check.cpp index 4cf72518..b5eb03e5 100644 --- a/base/code/progressive_sword_check.cpp +++ b/base/code/progressive_sword_check.cpp @@ -1,5 +1,4 @@ -#include -#include +#include "ph.hpp" #define OSHUS_SWORD_FLAG_OFFSET (0x128) #define OSHUS_SWORD_FLAG_BIT (0x1) @@ -8,12 +7,12 @@ extern "C" { -__attribute__((target("thumb"))) void progressive_sword_check(const uint32_t base_address) { +__attribute__((target("thumb"))) void progressive_sword_check(const u32 base_address) { // Address of oshus sword flag - uint8_t *oshus_sword = (uint8_t *)(base_address + OSHUS_SWORD_FLAG_OFFSET); + u8 *oshus_sword = (u8 *)(base_address + OSHUS_SWORD_FLAG_OFFSET); // Address of phantom sword flag - uint8_t *phantom_sword = (uint8_t *)(base_address + PHANTOM_SWORD_FLAG_OFFSET); + u8 *phantom_sword = (u8 *)(base_address + PHANTOM_SWORD_FLAG_OFFSET); // Check if player has the oshus sword bool has_oshus_sword = (*oshus_sword & OSHUS_SWORD_FLAG_BIT) == OSHUS_SWORD_FLAG_BIT; diff --git a/base/code/rando_settings.cpp b/base/code/rando_settings.cpp index aada9815..069b8624 100644 --- a/base/code/rando_settings.cpp +++ b/base/code/rando_settings.cpp @@ -2,8 +2,8 @@ extern "C" { -bool setting_is_enabled(uint8_t offset, uint8_t bit) { - uint8_t *base_addr = (uint8_t *)RANDO_SETTINGS_BITMAP_ADDR; +bool setting_is_enabled(u8 offset, u8 bit) { + u8 *base_addr = (u8 *)RANDO_SETTINGS_BITMAP_ADDR; return (base_addr[offset] & bit) == bit; } diff --git a/base/code/rando_settings.h b/base/code/rando_settings.h index 45b6215a..3168b288 100644 --- a/base/code/rando_settings.h +++ b/base/code/rando_settings.h @@ -1,8 +1,7 @@ #ifndef RANDO_SETTINGS_H #define RANDO_SETTINGS_H -#include -#include +#include "ph.hpp" #ifdef __cplusplus extern "C" { @@ -24,7 +23,7 @@ extern "C" { * `RANDO_SETTINGS_BITMAP_ADDR[offset]` byte * @return true if setting is enabled, false if it is disabled */ -bool setting_is_enabled(uint8_t offset, uint8_t bit); +bool setting_is_enabled(u8 offset, u8 bit); #ifdef __cplusplus } diff --git a/base/code/set_initial_flags.cpp b/base/code/set_initial_flags.cpp index d1f83cf6..3ad1c189 100644 --- a/base/code/set_initial_flags.cpp +++ b/base/code/set_initial_flags.cpp @@ -1,21 +1,20 @@ #include "ph.hpp" #include "rando_settings.h" -#include extern "C" { #define NO_SETTING -1, 0 typedef struct { - uint16_t flag_offset; // offset of the flag from the "base address" (see first + u16 flag_offset; // offset of the flag from the "base address" (see first // arg of set_initial_flags() ) - uint8_t flag_bit; // the bit within the value @ flag_offset that represents + u8 flag_bit; // the bit within the value @ flag_offset that represents // this flag - int32_t setting_offset; // offset of the randomizer setting that this flag is + s32 setting_offset; // offset of the randomizer setting that this flag is // gated behind, or -1 if it's not gated behind any // settings. Make this a 32 bit uint to pad struct to // 16 bytes - uint8_t setting_bit; // the bit within the value @ setting_offset that this + u8 setting_bit; // the bit within the value @ setting_offset that this // represents this setting } Flag; @@ -74,14 +73,14 @@ Flag flags[] = { {0x24, 0x20, NO_SETTING}, // blew dust off of NW sea chart }; -static void set_flag(uint32_t addr, uint8_t bit) { - *((uint8_t *)addr) |= bit; +static void set_flag(u32 addr, u8 bit) { + *((u8 *)addr) |= bit; } -void set_initial_flags(uint32_t base_flag_address) { +void set_initial_flags(u32 base_flag_address) { for (int i = 0; i < sizeof(flags) / sizeof(Flag); i++) { Flag f = flags[i]; - if (f.setting_offset == -1 || setting_is_enabled((uint8_t)f.setting_offset, f.setting_bit)) { + if (f.setting_offset == -1 || setting_is_enabled((u8)f.setting_offset, f.setting_bit)) { set_flag(base_flag_address + f.flag_offset, f.flag_bit); } } diff --git a/base/code/spawn_custom_freestanding_item.cpp b/base/code/spawn_custom_freestanding_item.cpp index 004cd3fe..e47a9f7e 100644 --- a/base/code/spawn_custom_freestanding_item.cpp +++ b/base/code/spawn_custom_freestanding_item.cpp @@ -1,4 +1,4 @@ -#include +#include "ph.hpp" #define RUPY 0x52555059 @@ -10,17 +10,17 @@ extern "C" { * @param param_3 - original function arg, don't modify * @param param_4 - original function arg, don't modify */ -uint16_t spawn_custom_freestanding_item(void *param_1, uint32_t npc_type, void *param_3, - uint16_t *param_4) { +u16 spawn_custom_freestanding_item(void *param_1, u32 npc_type, void *param_3, + u16 *param_4) { // declare pointer to the game's `spawn_npc` function - uint16_t (*spawn_npc)(void *, uint32_t, void *, uint16_t *) = reinterpret_cast(0x20C3FE8); + u16 (*spawn_npc)(void *, u32, void *, u16 *) = reinterpret_cast(0x20C3FE8); - uint16_t *item_id_address; + u16 *item_id_address; // pull the base address of the NPCA entry's item_id off of the stack asm volatile("ldr %0, [sp, #0x4]" : "=r"(item_id_address) :); // get the item id - uint16_t item_id = item_id_address[0x10]; + u16 item_id = item_id_address[0x10]; // if item_id is 0x1, continue as the vanilla game does. if (item_id == 0x1) { From 36a4d0dd99bb83a897fabf6a2f68d739b44fcfad Mon Sep 17 00:00:00 2001 From: mike8699 Date: Sun, 2 Nov 2025 15:43:06 -0500 Subject: [PATCH 6/6] pre-commit --- base/code/ph.hpp | 2 +- base/code/set_initial_flags.cpp | 18 +++++++++--------- base/code/spawn_custom_freestanding_item.cpp | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/base/code/ph.hpp b/base/code/ph.hpp index eedf0359..68949daf 100644 --- a/base/code/ph.hpp +++ b/base/code/ph.hpp @@ -4,8 +4,8 @@ #ifdef __cplusplus extern "C" { #endif -#include "types.h" #include "string.h" +#include "types.h" #ifdef __cplusplus } #endif diff --git a/base/code/set_initial_flags.cpp b/base/code/set_initial_flags.cpp index 3ad1c189..d4496373 100644 --- a/base/code/set_initial_flags.cpp +++ b/base/code/set_initial_flags.cpp @@ -6,16 +6,16 @@ extern "C" { #define NO_SETTING -1, 0 typedef struct { - u16 flag_offset; // offset of the flag from the "base address" (see first - // arg of set_initial_flags() ) - u8 flag_bit; // the bit within the value @ flag_offset that represents - // this flag + u16 flag_offset; // offset of the flag from the "base address" (see first + // arg of set_initial_flags() ) + u8 flag_bit; // the bit within the value @ flag_offset that represents + // this flag s32 setting_offset; // offset of the randomizer setting that this flag is - // gated behind, or -1 if it's not gated behind any - // settings. Make this a 32 bit uint to pad struct to - // 16 bytes - u8 setting_bit; // the bit within the value @ setting_offset that this - // represents this setting + // gated behind, or -1 if it's not gated behind any + // settings. Make this a 32 bit uint to pad struct to + // 16 bytes + u8 setting_bit; // the bit within the value @ setting_offset that this + // represents this setting } Flag; /** diff --git a/base/code/spawn_custom_freestanding_item.cpp b/base/code/spawn_custom_freestanding_item.cpp index e47a9f7e..4153a0ca 100644 --- a/base/code/spawn_custom_freestanding_item.cpp +++ b/base/code/spawn_custom_freestanding_item.cpp @@ -10,10 +10,10 @@ extern "C" { * @param param_3 - original function arg, don't modify * @param param_4 - original function arg, don't modify */ -u16 spawn_custom_freestanding_item(void *param_1, u32 npc_type, void *param_3, - u16 *param_4) { +u16 spawn_custom_freestanding_item(void *param_1, u32 npc_type, void *param_3, u16 *param_4) { // declare pointer to the game's `spawn_npc` function - u16 (*spawn_npc)(void *, u32, void *, u16 *) = reinterpret_cast(0x20C3FE8); + u16 (*spawn_npc)(void *, u32, void *, u16 *) = + reinterpret_cast(0x20C3FE8); u16 *item_id_address; // pull the base address of the NPCA entry's item_id off of the stack