From 4a87ed709b43de81c5b2eee553cfb1dc5edaa84c Mon Sep 17 00:00:00 2001 From: Christopher Leggett Date: Sun, 31 Jul 2022 23:26:48 -0400 Subject: [PATCH] Adds ItemTableManager class. --- soh/ItemTableManager.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ soh/ItemTableManager.h | 40 ++++++++++++++++++++++++++++++ soh/soh.vcxproj | 3 ++- soh/soh.vcxproj.filters | 14 ++++++++++- 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 soh/ItemTableManager.cpp create mode 100644 soh/ItemTableManager.h diff --git a/soh/ItemTableManager.cpp b/soh/ItemTableManager.cpp new file mode 100644 index 0000000000..7768f291e0 --- /dev/null +++ b/soh/ItemTableManager.cpp @@ -0,0 +1,53 @@ +#include "ItemTableManager.h" + +ItemTableManager::ItemTableManager() { +} + +ItemTableManager::~ItemTableManager() { + this->itemTables.clear(); +} + +bool ItemTableManager::AddItemTable(std::string tableID) { + ItemTable newItemTable; + return itemTables.emplace(tableID, newItemTable).second; +} + +bool ItemTableManager::AddItemEntry(std::string tableID, uint8_t getItemID, uint8_t itemID, uint16_t objectID, + int8_t drawID, + uint8_t textID, uint8_t field, bool chestAnim) { + ItemTable* itemTable = RetrieveItemTable(tableID); + if (itemTable == NULL) { + return false; + } + GetItemEntry getItemEntry = GET_ITEM(itemID, objectID, drawID, textID, field, chestAnim); + return itemTable->emplace(getItemID, getItemEntry).second; +} + +GetItemEntry ItemTableManager::RetrieveItemEntry(std::string tableID, uint8_t itemID) { + ItemTable* itemTable = RetrieveItemTable(tableID); + if (itemTable != NULL) { + auto foundItemEntry = itemTable->find(itemID); + if (foundItemEntry != itemTable->end()) { + return foundItemEntry->second; + } + } + return GET_ITEM_NONE; +} + +bool ItemTableManager::ClearItemTable(std::string tableID) { + ItemTable* itemTable = RetrieveItemTable(tableID); + if (itemTable != NULL) { + itemTable->clear(); + return true; + } + return false; +} + +ItemTable* ItemTableManager::RetrieveItemTable(std::string tableID) { + auto foundItemTable = itemTables.find(tableID); + if (foundItemTable == itemTables.end()) { + return nullptr; + } + ItemTable& itemTable = foundItemTable->second; + return &itemTable; +} diff --git a/soh/ItemTableManager.h b/soh/ItemTableManager.h new file mode 100644 index 0000000000..dccbf7d2c1 --- /dev/null +++ b/soh/ItemTableManager.h @@ -0,0 +1,40 @@ +#pragma once +#include +#include "z64item.h" +#include "z64object.h" + +#define CHEST_ANIM_SHORT 0 +#define CHEST_ANIM_LONG 1 + +typedef struct { + /* 0x00 */ uint8_t itemId; + /* 0x01 */ uint8_t field; // various bit-packed data + /* 0x02 */ int8_t gi; // defines the draw id and chest opening animation + /* 0x03 */ uint8_t textId; + /* 0x04 */ uint16_t objectId; +} GetItemEntry; // size = 0x06 + +#define GET_ITEM(itemId, objectId, drawId, textId, field, chestAnim) \ + { itemId, field, (chestAnim != CHEST_ANIM_SHORT ? 1 : -1) * (drawId + 1), textId, objectId } + +#define GET_ITEM_NONE \ + { ITEM_NONE, 0, 0, 0, OBJECT_INVALID } + +typedef std::unordered_map ItemTable; + +class ItemTableManager { + public: + static ItemTableManager* Instance; + ItemTableManager(); + ~ItemTableManager(); + bool AddItemTable(std::string tableID); + bool AddItemEntry(std::string tableID, uint8_t getItemID, uint8_t itemID, uint16_t objectID, int8_t drawID, + uint8_t textID, uint8_t field, bool chestAnim); + GetItemEntry RetrieveItemEntry(std::string tableID, uint8_t itemID); + bool ClearItemTable(std::string tableID); + + private: + std::unordered_map itemTables; + + ItemTable* RetrieveItemTable(std::string tableID); +}; diff --git a/soh/soh.vcxproj b/soh/soh.vcxproj index bc7c4d6462..a1fa3cceda 100644 --- a/soh/soh.vcxproj +++ b/soh/soh.vcxproj @@ -179,6 +179,7 @@ + @@ -941,9 +942,9 @@ - + diff --git a/soh/soh.vcxproj.filters b/soh/soh.vcxproj.filters index f0c05dee89..0401485039 100644 --- a/soh/soh.vcxproj.filters +++ b/soh/soh.vcxproj.filters @@ -82,6 +82,12 @@ {04fc1c52-49ff-48e2-ae23-2c00867374f8} + + {01811baa-7b90-4452-8ef0-d4cdc5dbdebd} + + + {1f217bdc-2227-4079-a077-bcab7f7d1481} + @@ -2373,6 +2379,9 @@ Source Files\src + + Source Files\soh\Enhancements\item-tables + @@ -4058,6 +4067,9 @@ Source Files\src + + Header Files\soh\Enhancements\item-tables + @@ -4070,4 +4082,4 @@ - + \ No newline at end of file