Skip to content

Commit

Permalink
Fix OnItemReceive hook for items in the extendedVanillaGetItem table
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox committed Nov 8, 2023
1 parent 30ab8e9 commit 5f0082b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
44 changes: 38 additions & 6 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ extern "C" void VanillaItemTable_Init() {
}
}

std::unordered_map<uint32_t, uint32_t> ItemIDtoGetItemID{
std::unordered_map<ItemID, GetItemID> ItemIDtoGetItemIDMap {
{ ITEM_ARROWS_LARGE, GI_ARROWS_LARGE },
{ ITEM_ARROWS_MEDIUM, GI_ARROWS_MEDIUM },
{ ITEM_ARROWS_SMALL, GI_ARROWS_SMALL },
Expand Down Expand Up @@ -663,7 +663,8 @@ std::unordered_map<uint32_t, uint32_t> ItemIDtoGetItemID{
{ ITEM_BUG, GI_BUGS },
{ ITEM_BULLET_BAG_30, GI_BULLET_BAG_30 },
{ ITEM_BULLET_BAG_40, GI_BULLET_BAG_40 },
{ ITEM_BULLET_BAG_50, GI_BULLET_BAG_50 }, { ITEM_CHICKEN, GI_CHICKEN },
{ ITEM_BULLET_BAG_50, GI_BULLET_BAG_50 },
{ ITEM_CHICKEN, GI_CHICKEN },
{ ITEM_CLAIM_CHECK, GI_CLAIM_CHECK },
{ ITEM_COJIRO, GI_COJIRO },
{ ITEM_COMPASS, GI_COMPASS },
Expand Down Expand Up @@ -759,11 +760,42 @@ std::unordered_map<uint32_t, uint32_t> ItemIDtoGetItemID{
{ ITEM_WEIRD_EGG, GI_WEIRD_EGG }
};

extern "C" int32_t GetGIID(uint32_t itemID) {
if (ItemIDtoGetItemID.contains(itemID)) {
return ItemIDtoGetItemID.at(itemID);
extern "C" GetItemID RetrieveGetItemIDFromItemID(ItemID itemID) {
if (ItemIDtoGetItemIDMap.contains(itemID)) {
return ItemIDtoGetItemIDMap.at(itemID);
}
return GI_MAX;
}

std::unordered_map<ItemID, RandomizerGet> ItemIDtoRandomizerGetMap {
{ ITEM_SONG_MINUET, RG_MINUET_OF_FOREST },
{ ITEM_SONG_BOLERO, RG_BOLERO_OF_FIRE },
{ ITEM_SONG_SERENADE, RG_SERENADE_OF_WATER },
{ ITEM_SONG_REQUIEM, RG_REQUIEM_OF_SPIRIT },
{ ITEM_SONG_NOCTURNE, RG_NOCTURNE_OF_SHADOW },
{ ITEM_SONG_PRELUDE, RG_PRELUDE_OF_LIGHT },
{ ITEM_SONG_LULLABY, RG_ZELDAS_LULLABY },
{ ITEM_SONG_EPONA, RG_EPONAS_SONG },
{ ITEM_SONG_SARIA, RG_SARIAS_SONG },
{ ITEM_SONG_SUN, RG_SUNS_SONG },
{ ITEM_SONG_TIME, RG_SONG_OF_TIME },
{ ITEM_SONG_STORMS, RG_SONG_OF_STORMS },
{ ITEM_MEDALLION_FOREST, RG_FOREST_MEDALLION },
{ ITEM_MEDALLION_FIRE, RG_FIRE_MEDALLION },
{ ITEM_MEDALLION_WATER, RG_WATER_MEDALLION },
{ ITEM_MEDALLION_SPIRIT, RG_SPIRIT_MEDALLION },
{ ITEM_MEDALLION_SHADOW, RG_SHADOW_MEDALLION },
{ ITEM_MEDALLION_LIGHT, RG_LIGHT_MEDALLION },
{ ITEM_KOKIRI_EMERALD, RG_KOKIRI_EMERALD },
{ ITEM_GORON_RUBY, RG_GORON_RUBY },
{ ITEM_ZORA_SAPPHIRE, RG_ZORA_SAPPHIRE },
};

extern "C" RandomizerGet RetrieveRandomizerGetFromItemID(ItemID itemID) {
if (ItemIDtoRandomizerGetMap.contains(itemID)) {
return ItemIDtoRandomizerGetMap.at(itemID);
}
return -1;
return RG_MAX;
}

extern "C" void OTRExtScanner() {
Expand Down
3 changes: 2 additions & 1 deletion soh/soh/OTRGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ void Gfx_RegisterBlendedTexture(const char* name, u8* mask, u8* replacement);
void SaveManager_ThreadPoolWait();
void CheckTracker_OnMessageClose();

int32_t GetGIID(uint32_t itemID);
GetItemID RetrieveGetItemIDFromItemID(ItemID itemID);
RandomizerGet RetrieveRandomizerGetFromItemID(ItemID itemID);
#endif

#ifdef __cplusplus
Expand Down
32 changes: 23 additions & 9 deletions soh/src/code/z_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1803,12 +1803,22 @@ u8 Return_Item(u8 itemID, ModIndex modId, ItemID returnItem) {
GetItemEntry gie = { ITEM_SOLD_OUT, 0, 0, 0, 0, 0, 0, 0, false, ITEM_FROM_NPC, ITEM_CATEGORY_LESSER, NULL };
return Return_Item_Entry(gie, returnItem);
}
int32_t get = GetGIID(itemID);
if (get == -1) {
modId = MOD_RANDOMIZER;
get = itemID;

GetItemID getItemID = RetrieveGetItemIDFromItemID(itemID);
if (getItemID != GI_MAX) {
// Vanilla ItemID with an associated GetItemID
return Return_Item_Entry(ItemTable_RetrieveEntry(modId, getItemID), returnItem);
}

RandomizerGet randomizerGet = RetrieveRandomizerGetFromItemID(itemID);
if (randomizerGet != RG_MAX) {
// Vanilla ItemID with an associated RandomizerGet (These are items in extendedVanillaGetItemTable)
return Return_Item_Entry(ItemTable_RetrieveEntry(MOD_RANDOMIZER, randomizerGet), returnItem);
}
return Return_Item_Entry(ItemTable_RetrieveEntry(modId, get), returnItem);

// All randomizer items should go through Randomizer_Item_Give, so this should never be reached
// but leaving this here just in case, as it was in the original behavior
return Return_Item_Entry(ItemTable_RetrieveEntry(MOD_RANDOMIZER, itemID), returnItem);
}

/**
Expand Down Expand Up @@ -6554,11 +6564,15 @@ void Interface_Update(PlayState* play) {
gSaveContext.pendingSale = ITEM_NONE;
gSaveContext.pendingSaleMod = MOD_NONE;
if (tempSaleMod == MOD_NONE) {
s16 giid = GetGIID(tempSaleItem);
if (giid == -1) {
tempSaleMod = MOD_RANDOMIZER;
GetItemID getItemID = RetrieveGetItemIDFromItemID(tempSaleItem);
RandomizerGet randomizerGet = RetrieveRandomizerGetFromItemID(tempSaleItem);
if (getItemID != GI_MAX) {
tempSaleItem = getItemID;
} else {
tempSaleItem = giid;
if (randomizerGet != RG_MAX) {
tempSaleItem = randomizerGet;
}
tempSaleMod = MOD_RANDOMIZER;
}
}
GameInteractor_ExecuteOnSaleEndHooks(ItemTable_RetrieveEntry(tempSaleMod, tempSaleItem));
Expand Down

0 comments on commit 5f0082b

Please sign in to comment.