From e8487187d8e9b39dd56420733d6dbd6629bc721b Mon Sep 17 00:00:00 2001 From: fallenoak Date: Thu, 16 Oct 2025 17:11:11 -0500 Subject: [PATCH] feat(component): add BuildComponentArray --- src/component/CCharacterComponent.cpp | 16 ++++ src/component/CCharacterComponent.hpp | 2 + src/component/Util.cpp | 129 ++++++++++++++++++++++++++ src/component/Util.hpp | 2 + src/object/Types.hpp | 9 +- 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 src/component/Util.cpp diff --git a/src/component/CCharacterComponent.cpp b/src/component/CCharacterComponent.cpp index fc4f41e..e422bec 100644 --- a/src/component/CCharacterComponent.cpp +++ b/src/component/CCharacterComponent.cpp @@ -1,8 +1,12 @@ #include "component/CCharacterComponent.hpp" +#include "component/Util.hpp" +#include "db/Db.hpp" #include "model/CM2Model.hpp" +#include "object/Types.hpp" #include st_race* CCharacterComponent::s_chrVarArray; +uint32_t CCharacterComponent::s_chrVarArrayLength; int32_t s_bInRenderPrep = 0; @@ -20,6 +24,18 @@ void CCharacterComponent::Initialize() { void CCharacterComponent::Initialize(EGxTexFormat textureFormat, uint32_t textureLevel, int32_t thread, int32_t compress) { // TODO + + CCharacterComponent::InitDbData(); + + // TODO +} + +void CCharacterComponent::InitDbData() { + uint32_t varArrayLength = (g_chrRacesDB.m_maxID + 1) * UNITSEX_NUM_SEXES; + CCharacterComponent::s_chrVarArrayLength = varArrayLength; + + BuildComponentArray(varArrayLength, &CCharacterComponent::s_chrVarArray); + // TODO CountFacialFeatures(varArrayLength, &CCharacterComponent::s_characterFacialHairStylesList); } void CCharacterComponent::CreateBaseTexture() { diff --git a/src/component/CCharacterComponent.hpp b/src/component/CCharacterComponent.hpp index b4c7617..e3e23f4 100644 --- a/src/component/CCharacterComponent.hpp +++ b/src/component/CCharacterComponent.hpp @@ -49,11 +49,13 @@ class CCharacterComponent { public: // Static variables static st_race* s_chrVarArray; + static uint32_t s_chrVarArrayLength; // Static functions static CCharacterComponent* AllocComponent(); static void Initialize(); static void Initialize(EGxTexFormat textureFormat, uint32_t textureLevel, int32_t thread, int32_t compress); + static void InitDbData(); // Member variables uint32_t m_flags = 0x1 | 0x2 | 0x4; diff --git a/src/component/Util.cpp b/src/component/Util.cpp new file mode 100644 index 0000000..9baedf7 --- /dev/null +++ b/src/component/Util.cpp @@ -0,0 +1,129 @@ +#include "component/Util.hpp" +#include "db/Db.hpp" +#include "object/Types.hpp" +#include + +int32_t BuildComponentArray(uint32_t varArrayLength, st_race** varArrayPtr) { + if (!varArrayLength) { + return 0; + } + + auto varArray = new (STORM_ALLOC(sizeof(st_race) * varArrayLength)) st_race[varArrayLength]; + + int32_t prevRaceID = g_charSectionsDB.GetNumRecords() > 0 ? g_charSectionsDB.GetRecordByIndex(0)->m_raceID : 4; + int32_t prevSexID = g_charSectionsDB.GetNumRecords() > 0 ? g_charSectionsDB.GetRecordByIndex(0)->m_sexID : 8; + int32_t prevBaseSection = g_charSectionsDB.GetNumRecords() > 0 ? g_charSectionsDB.GetRecordByIndex(0)->m_baseSection : 12; + int32_t prevVariationIndex = 0; + + // Build sections + + int32_t variationIndex = 0; + + for (int32_t i = 0; i < g_charSectionsDB.GetNumRecords(); i++) { + auto sectionsRec = g_charSectionsDB.GetRecordByIndex(i); + + if (sectionsRec->m_baseSection >= NUM_COMPONENT_VARIATIONS) { + continue; + } + + auto sectionChange = prevRaceID != sectionsRec->m_raceID + || prevSexID != sectionsRec->m_sexID + || prevBaseSection != sectionsRec->m_baseSection; + + auto lastRecord = i == g_charSectionsDB.GetNumRecords() - 1; + + if (sectionChange || lastRecord) { + auto& section = varArray[(prevRaceID * UNITSEX_NUM_SEXES + prevSexID)].sections[prevBaseSection]; + + section.variationCount = variationIndex + 1; + + if (section.variationCount > 0) { + section.variationArray = new (STORM_ALLOC(sizeof(st_variation) * section.variationCount)) st_variation[section.variationCount]; + } + + variationIndex = 0; + } + + if (variationIndex <= sectionsRec->m_variationIndex) { + variationIndex = sectionsRec->m_variationIndex; + } + + prevRaceID = sectionsRec->m_raceID; + prevSexID = sectionsRec->m_sexID; + prevBaseSection = sectionsRec->m_baseSection; + } + + // Build variations + + prevRaceID = 1; + prevSexID = 0; + prevBaseSection = 0; + prevVariationIndex = 0; + + int32_t colorIndex = 0; + + for (int32_t i = 0; i < g_charSectionsDB.GetNumRecords(); i++) { + auto sectionsRec = g_charSectionsDB.GetRecordByIndex(i); + + if (sectionsRec->m_baseSection >= NUM_COMPONENT_VARIATIONS) { + continue; + } + + auto sectionChange = prevRaceID != sectionsRec->m_raceID + || prevSexID != sectionsRec->m_sexID + || prevBaseSection != sectionsRec->m_baseSection + || prevVariationIndex != sectionsRec->m_variationIndex; + + auto lastRecord = i == g_charSectionsDB.GetNumRecords() - 1; + + if (sectionChange || lastRecord) { + auto& section = varArray[(prevRaceID * UNITSEX_NUM_SEXES + prevSexID)].sections[prevBaseSection]; + + if (section.variationCount > 0) { + auto& variation = section.variationArray[prevVariationIndex]; + + variation.colorCount = colorIndex + 1; + + if (variation.colorCount > 0) { + variation.colorArray = new (STORM_ALLOC(sizeof(st_color) * variation.colorCount)) st_color[variation.colorCount]; + } + } + + colorIndex = 0; + } + + if (colorIndex <= sectionsRec->m_colorIndex) { + colorIndex = sectionsRec->m_colorIndex; + } + + prevRaceID = sectionsRec->m_raceID; + prevSexID = sectionsRec->m_sexID; + prevBaseSection = sectionsRec->m_baseSection; + prevVariationIndex = sectionsRec->m_variationIndex; + } + + // Build colors + + for (int32_t i = 0; i < g_charSectionsDB.GetNumRecords(); i++) { + auto sectionsRec = g_charSectionsDB.GetRecordByIndex(i); + + if (sectionsRec->m_baseSection >= NUM_COMPONENT_VARIATIONS) { + continue; + } + + auto& section = varArray[(sectionsRec->m_raceID * UNITSEX_NUM_SEXES + sectionsRec->m_sexID)].sections[sectionsRec->m_baseSection]; + + if (section.variationCount > 0 && sectionsRec->m_variationIndex < section.variationCount) { + auto& variation = section.variationArray[sectionsRec->m_variationIndex]; + + if (variation.colorCount > 0 && sectionsRec->m_colorIndex < variation.colorCount) { + auto& color = variation.colorArray[sectionsRec->m_colorIndex]; + color.rec = sectionsRec; + } + } + } + + *varArrayPtr = varArray; + + return 1; +} diff --git a/src/component/Util.hpp b/src/component/Util.hpp index 0016b82..21e44db 100644 --- a/src/component/Util.hpp +++ b/src/component/Util.hpp @@ -26,4 +26,6 @@ struct st_variation { st_color* colorArray = nullptr; }; +int32_t BuildComponentArray(uint32_t varArrayLength, st_race** varArrayPtr); + #endif diff --git a/src/object/Types.hpp b/src/object/Types.hpp index 8d79722..cf08448 100644 --- a/src/object/Types.hpp +++ b/src/object/Types.hpp @@ -2,11 +2,12 @@ #define OBJECT_TYPES_HPP enum UNIT_SEX { - UNITSEX_MALE = 0, - UNITSEX_FEMALE = 1, - UNITSEX_NONE = 2, + UNITSEX_MALE = 0, + UNITSEX_FEMALE = 1, + UNITSEX_NUM_SEXES = 2, + UNITSEX_NONE = 2, UNITSEX_LAST, - UNITSEX_BOTH = 3, + UNITSEX_BOTH = 3, }; #endif