2021-12-05 17:54:36 +00:00
|
|
|
#include "CDBehaviorParameterTable.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
namespace {
|
|
|
|
std::unordered_map<std::string, uint32_t> m_ParametersList;
|
|
|
|
};
|
|
|
|
|
2023-08-11 04:27:40 +00:00
|
|
|
uint64_t GetKey(const uint32_t behaviorID, const uint32_t parameterID) {
|
|
|
|
uint64_t key = behaviorID;
|
|
|
|
key <<= 31U;
|
|
|
|
key |= parameterID;
|
2023-08-04 02:31:02 +00:00
|
|
|
|
2023-08-11 04:27:40 +00:00
|
|
|
return key;
|
2023-08-04 02:31:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 04:27:40 +00:00
|
|
|
void CDBehaviorParameterTable::LoadValuesFromDatabase() {
|
2022-01-15 18:37:43 +00:00
|
|
|
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter");
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& entries = GetEntriesMutable();
|
2022-01-15 18:37:43 +00:00
|
|
|
while (!tableData.eof()) {
|
2023-08-04 02:31:02 +00:00
|
|
|
uint32_t behaviorID = tableData.getIntField("behaviorID", -1);
|
2023-01-07 09:48:59 +00:00
|
|
|
auto candidateStringToAdd = std::string(tableData.getStringField("parameterID", ""));
|
2022-07-12 03:43:09 +00:00
|
|
|
auto parameter = m_ParametersList.find(candidateStringToAdd);
|
2023-08-04 02:31:02 +00:00
|
|
|
uint32_t parameterId;
|
2022-07-12 03:43:09 +00:00
|
|
|
if (parameter != m_ParametersList.end()) {
|
2023-08-04 02:31:02 +00:00
|
|
|
parameterId = parameter->second;
|
2022-07-12 03:43:09 +00:00
|
|
|
} else {
|
2023-08-04 02:31:02 +00:00
|
|
|
parameterId = m_ParametersList.insert(std::make_pair(candidateStringToAdd, m_ParametersList.size())).first->second;
|
2022-07-12 03:43:09 +00:00
|
|
|
}
|
2023-08-11 04:27:40 +00:00
|
|
|
uint64_t hash = GetKey(behaviorID, parameterId);
|
2023-08-04 02:31:02 +00:00
|
|
|
float value = tableData.getFloatField("value", -1.0f);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
entries.insert(std::make_pair(hash, value));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-01-15 18:37:43 +00:00
|
|
|
tableData.nextRow();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
tableData.finalize();
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:43:52 +00:00
|
|
|
float CDBehaviorParameterTable::GetValue(const uint32_t behaviorID, const std::string& name, const float defaultValue) {
|
2024-02-09 13:37:58 +00:00
|
|
|
auto parameterID = m_ParametersList.find(name);
|
|
|
|
if (parameterID == m_ParametersList.end()) return defaultValue;
|
2023-08-11 04:27:40 +00:00
|
|
|
auto hash = GetKey(behaviorID, parameterID->second);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-01-24 22:02:56 +00:00
|
|
|
// Search for specific parameter
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& entries = GetEntriesMutable();
|
|
|
|
auto it = entries.find(hash);
|
|
|
|
return it != entries.end() ? it->second : defaultValue;
|
2022-07-09 06:07:52 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-09 06:07:52 +00:00
|
|
|
std::map<std::string, float> CDBehaviorParameterTable::GetParametersByBehaviorID(uint32_t behaviorID) {
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& entries = GetEntriesMutable();
|
2022-12-29 09:43:52 +00:00
|
|
|
uint64_t hashBase = behaviorID;
|
2022-07-09 06:07:52 +00:00
|
|
|
std::map<std::string, float> returnInfo;
|
2023-08-04 02:31:02 +00:00
|
|
|
for (auto& [parameterString, parameterId] : m_ParametersList) {
|
2023-08-11 04:27:40 +00:00
|
|
|
uint64_t hash = GetKey(hashBase, parameterId);
|
2024-02-09 13:37:58 +00:00
|
|
|
auto infoCandidate = entries.find(hash);
|
|
|
|
if (infoCandidate != entries.end()) {
|
2023-08-04 02:31:02 +00:00
|
|
|
returnInfo.insert(std::make_pair(parameterString, infoCandidate->second));
|
2022-07-09 06:07:52 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-09 06:07:52 +00:00
|
|
|
return returnInfo;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|