diff --git a/dDatabase/Tables/CDRebuildComponentTable.cpp b/dDatabase/Tables/CDRebuildComponentTable.cpp index f5706a28..4a05e413 100644 --- a/dDatabase/Tables/CDRebuildComponentTable.cpp +++ b/dDatabase/Tables/CDRebuildComponentTable.cpp @@ -1,7 +1,6 @@ #include "CDRebuildComponentTable.h" void CDRebuildComponentTable::LoadValuesFromDatabase() { - // First, get the size of the table unsigned int size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM RebuildComponent"); @@ -11,16 +10,13 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { tableSize.nextRow(); } - tableSize.finalize(); - - // Reserve the size this->entries.reserve(size); // Now get the data - auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RebuildComponent"); + auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM RebuildComponent order by id"); while (!tableData.eof()) { CDRebuildComponent entry; - entry.id = tableData.getIntField("id", -1); + uint32_t id = tableData.getIntField("id", -1); entry.reset_time = tableData.getFloatField("reset_time", -1.0f); entry.complete_time = tableData.getFloatField("complete_time", -1.0f); entry.take_imagination = tableData.getIntField("take_imagination", -1); @@ -31,23 +27,14 @@ void CDRebuildComponentTable::LoadValuesFromDatabase() { entry.post_imagination_cost = tableData.getIntField("post_imagination_cost", -1); entry.time_before_smash = tableData.getFloatField("time_before_smash", -1.0f); - this->entries.push_back(entry); + this->entries.push_back(std::make_pair(id, entry)); tableData.nextRow(); } - - tableData.finalize(); } -std::vector CDRebuildComponentTable::Query(std::function predicate) { - - std::vector data = cpplinq::from(this->entries) - >> cpplinq::where(predicate) - >> cpplinq::to_vector(); - - return data; +const std::optional CDRebuildComponentTable::Get(uint32_t componentId) { + auto result = std::lower_bound(this->entries.begin(), this->entries.end(), componentId, [](const auto& entry, const auto& componentId) { + return entry.first < componentId; + }); + return result == this->entries.end() ? std::nullopt : std::make_optional(result->second); } - -const std::vector& CDRebuildComponentTable::GetEntries() const { - return this->entries; -} - diff --git a/dDatabase/Tables/CDRebuildComponentTable.h b/dDatabase/Tables/CDRebuildComponentTable.h index fc78e904..8a40f646 100644 --- a/dDatabase/Tables/CDRebuildComponentTable.h +++ b/dDatabase/Tables/CDRebuildComponentTable.h @@ -4,7 +4,6 @@ #include "CDTable.h" struct CDRebuildComponent { - unsigned int id; //!< The component Id float reset_time; //!< The reset time float complete_time; //!< The complete time unsigned int take_imagination; //!< The amount of imagination it costs @@ -18,13 +17,11 @@ struct CDRebuildComponent { class CDRebuildComponentTable : public CDTable { private: - std::vector entries; + std::vector> entries; public: void LoadValuesFromDatabase(); // Queries the table with a custom "where" clause - std::vector Query(std::function predicate); - - const std::vector& GetEntries() const; + const std::optional Get(uint32_t componentId); }; diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index d80130c2..4b3637fd 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -536,17 +536,17 @@ void Entity::Initialize() { m_Components.insert(std::make_pair(eReplicaComponentType::QUICK_BUILD, comp)); CDRebuildComponentTable* rebCompTable = CDClientManager::Instance().GetTable(); - std::vector rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == rebuildComponentID); }); + auto rebCompData = rebCompTable->Get(rebuildComponentID); - if (rebCompData.size() > 0) { - comp->SetResetTime(rebCompData[0].reset_time); - comp->SetCompleteTime(rebCompData[0].complete_time); - comp->SetTakeImagination(rebCompData[0].take_imagination); - comp->SetInterruptible(rebCompData[0].interruptible); - comp->SetSelfActivator(rebCompData[0].self_activator); - comp->SetActivityId(rebCompData[0].activityID); - comp->SetPostImaginationCost(rebCompData[0].post_imagination_cost); - comp->SetTimeBeforeSmash(rebCompData[0].time_before_smash); + if (rebCompData) { + comp->SetResetTime(rebCompData->reset_time); + comp->SetCompleteTime(rebCompData->complete_time); + comp->SetTakeImagination(rebCompData->take_imagination); + comp->SetInterruptible(rebCompData->interruptible); + comp->SetSelfActivator(rebCompData->self_activator); + comp->SetActivityId(rebCompData->activityID); + comp->SetPostImaginationCost(rebCompData->post_imagination_cost); + comp->SetTimeBeforeSmash(rebCompData->time_before_smash); const auto rebuildResetTime = GetVar(u"rebuild_reset_time");