diff --git a/dDatabase/CDClientDatabase/CDClientManager.cpp b/dDatabase/CDClientDatabase/CDClientManager.cpp index ca8ff0ea..d76e89d2 100644 --- a/dDatabase/CDClientDatabase/CDClientManager.cpp +++ b/dDatabase/CDClientDatabase/CDClientManager.cpp @@ -3,6 +3,7 @@ #include "CDAnimationsTable.h" #include "CDBehaviorParameterTable.h" #include "CDBehaviorTemplateTable.h" +#include "CDBaseCombatAIComponentTable.h" #include "CDClientDatabase.h" #include "CDComponentsRegistryTable.h" #include "CDCurrencyTableTable.h" @@ -65,6 +66,7 @@ DEFINE_TABLE_STORAGE(CDActivityRewardsTable); DEFINE_TABLE_STORAGE(CDActivitiesTable); DEFINE_TABLE_STORAGE(CDAnimationsTable); +DEFINE_TABLE_STORAGE(CDBaseCombatAIComponentTable); DEFINE_TABLE_STORAGE(CDBehaviorParameterTable); DEFINE_TABLE_STORAGE(CDBehaviorTemplateTable); DEFINE_TABLE_STORAGE(CDBrickIDTableTable); @@ -154,6 +156,7 @@ void CDClientManager::LoadValuesFromDatabase() { void CDClientManager::LoadValuesFromDefaults() { LOG("Loading default CDClient tables!"); + CDBaseCombatAIComponentTable::Instance().LoadValuesFromDefaults(); CDPetComponentTable::Instance().LoadValuesFromDefaults(); CDActivitiesTable::Instance().LoadValuesFromDefaults(); CDActivityRewardsTable::Instance().LoadValuesFromDefaults(); diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.cpp b/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.cpp new file mode 100644 index 00000000..90baaaac --- /dev/null +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.cpp @@ -0,0 +1,67 @@ +#include "CDBaseCombatAIComponentTable.h" + +namespace { + // Default entries for fallback + CDBaseCombatAIComponent defaultEntry{ + .id = 1, + .aggroRadius = 25.0f, + .tetherSpeed = 4.0f, + .pursuitSpeed = 2.0f, + .softTetherRadius = 25.0f, + .hardTetherRadius = 100.0f, + }; +} + +void CDBaseCombatAIComponentTable::LoadValuesFromDatabase() { + // First, get the size of the table + uint32_t size = 0; + auto tableSize = CDClientDatabase::CreatePreppedStmt("SELECT COUNT(*) FROM BaseCombatAIComponent"); + auto tableSizeResult = tableSize.execQuery(); + while (!tableSizeResult.eof()) { + size = tableSizeResult.getIntField(0, 0); + tableSizeResult.nextRow(); + } + tableSizeResult.finalize(); + + // Reserve the size + auto& entries = GetEntriesMutable(); + entries.reserve(size); + + // Now get the data + auto tableData = CDClientDatabase::CreatePreppedStmt("SELECT * FROM BaseCombatAIComponent"); + auto tableDataResult = tableData.execQuery(); + + while (!tableDataResult.eof()) { + CDBaseCombatAIComponent entry; + + entry.id = tableDataResult.getIntField("id", -1); + entry.aggroRadius = tableDataResult.getFloatField("aggroRadius", 25.0f); + entry.tetherSpeed = tableDataResult.getFloatField("tetherSpeed", 4.0f); + entry.pursuitSpeed = tableDataResult.getFloatField("pursuitSpeed", 2.0f); + entry.softTetherRadius = tableDataResult.getFloatField("softTetherRadius", 25.0f); + entry.hardTetherRadius = tableDataResult.getFloatField("hardTetherRadius", 100.0f); + + entries.push_back(entry); + tableDataResult.nextRow(); + } + + tableData.finalize(); +} + +void CDBaseCombatAIComponentTable::LoadValuesFromDefaults() { + auto& entries = GetEntriesMutable(); + entries.clear(); + entries.push_back(defaultEntry); +} + +std::vector CDBaseCombatAIComponentTable::Query(std::function predicate) { + std::vector data = cpplinq::from(GetEntries()) + >> cpplinq::where(predicate) + >> cpplinq::to_vector(); + + return data; +} + +const std::vector& CDBaseCombatAIComponentTable::GetEntries() const { + return CDTable::GetEntries(); +} \ No newline at end of file diff --git a/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.h b/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.h new file mode 100644 index 00000000..ca4af585 --- /dev/null +++ b/dDatabase/CDClientDatabase/CDClientTables/CDBaseCombatAIComponentTable.h @@ -0,0 +1,24 @@ +#ifndef CDBASECOMBATAICOMPONENTTABLE_H +#define CDBASECOMBATAICOMPONENTTABLE_H + +#include "CDTable.h" + +struct CDBaseCombatAIComponent { + int32_t id; + float aggroRadius; + float tetherSpeed; + float pursuitSpeed; + float softTetherRadius; + float hardTetherRadius; +}; + +class CDBaseCombatAIComponentTable : public CDTable> { +public: + void LoadValuesFromDatabase(); + void LoadValuesFromDefaults(); + + std::vector Query(std::function predicate); + const std::vector& GetEntries() const; +}; + +#endif //CDBASECOMBATAICOMPONENTTABLE_H \ No newline at end of file diff --git a/dDatabase/CDClientDatabase/CDClientTables/CMakeLists.txt b/dDatabase/CDClientDatabase/CDClientTables/CMakeLists.txt index 57731ad9..958e4ec4 100644 --- a/dDatabase/CDClientDatabase/CDClientTables/CMakeLists.txt +++ b/dDatabase/CDClientDatabase/CDClientTables/CMakeLists.txt @@ -1,6 +1,7 @@ set(DDATABASE_CDCLIENTDATABASE_CDCLIENTTABLES_SOURCES "CDActivitiesTable.cpp" "CDActivityRewardsTable.cpp" "CDAnimationsTable.cpp" + "CDBaseCombatAIComponentTable.cpp" "CDBehaviorParameterTable.cpp" "CDBehaviorTemplateTable.cpp" "CDBrickIDTableTable.cpp" diff --git a/dGame/dComponents/BaseCombatAIComponent.cpp b/dGame/dComponents/BaseCombatAIComponent.cpp index fbe5a382..fb0d69f2 100644 --- a/dGame/dComponents/BaseCombatAIComponent.cpp +++ b/dGame/dComponents/BaseCombatAIComponent.cpp @@ -13,6 +13,7 @@ #include "CDClientDatabase.h" #include "CDClientManager.h" +#include "CDBaseCombatAIComponentTable.h" #include "DestroyableComponent.h" #include @@ -41,31 +42,20 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id) m_ForcedTetherTime = 0.0f; //Grab the aggro information from BaseCombatAI: - auto componentQuery = CDClientDatabase::CreatePreppedStmt( - "SELECT aggroRadius, tetherSpeed, pursuitSpeed, softTetherRadius, hardTetherRadius FROM BaseCombatAIComponent WHERE id = ?;"); - componentQuery.bind(1, static_cast(id)); + auto* componentTable = CDClientManager::GetTable(); + auto componentEntries = componentTable->Query([id](CDBaseCombatAIComponent entry) { + return entry.id == static_cast(id); + }); - auto componentResult = componentQuery.execQuery(); - - if (!componentResult.eof()) { - if (!componentResult.fieldIsNull("aggroRadius")) - m_AggroRadius = componentResult.getFloatField("aggroRadius"); - - if (!componentResult.fieldIsNull("tetherSpeed")) - m_TetherSpeed = componentResult.getFloatField("tetherSpeed"); - - if (!componentResult.fieldIsNull("pursuitSpeed")) - m_PursuitSpeed = componentResult.getFloatField("pursuitSpeed"); - - if (!componentResult.fieldIsNull("softTetherRadius")) - m_SoftTetherRadius = componentResult.getFloatField("softTetherRadius"); - - if (!componentResult.fieldIsNull("hardTetherRadius")) - m_HardTetherRadius = componentResult.getFloatField("hardTetherRadius"); + if (!componentEntries.empty()) { + const auto& component = componentEntries[0]; + m_AggroRadius = component.aggroRadius; + m_TetherSpeed = component.tetherSpeed; + m_PursuitSpeed = component.pursuitSpeed; + m_SoftTetherRadius = component.softTetherRadius; + m_HardTetherRadius = component.hardTetherRadius; } - componentResult.finalize(); - // Get aggro and tether radius from settings and use this if it is present. Only overwrite the // radii if it is greater than the one in the database. if (m_Parent) {