two tables done

This commit is contained in:
David Markowitz 2023-07-25 21:29:06 -07:00
parent ff173dffce
commit 771eb65b92
16 changed files with 60 additions and 74 deletions

View File

@ -1,8 +1,6 @@
#include "CDSkillBehaviorTable.h" #include "CDSkillBehaviorTable.h"
void CDSkillBehaviorTable::LoadValuesFromDatabase() { void CDSkillBehaviorTable::LoadValuesFromDatabase() {
m_empty = CDSkillBehavior();
// First, get the size of the table // First, get the size of the table
unsigned int size = 0; unsigned int size = 0;
auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM SkillBehavior"); auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM SkillBehavior");
@ -49,12 +47,8 @@ void CDSkillBehaviorTable::LoadValuesFromDatabase() {
tableData.finalize(); tableData.finalize();
} }
const CDSkillBehavior& CDSkillBehaviorTable::GetSkillByID(unsigned int skillID) { const std::optional<CDSkillBehavior> CDSkillBehaviorTable::GetSkillByID(unsigned int skillID) {
std::map<unsigned int, CDSkillBehavior>::iterator it = this->entries.find(skillID); auto it = this->entries.find(skillID);
if (it != this->entries.end()) { return it != this->entries.end() ? std::make_optional(it->second) : std::nullopt;
return it->second;
}
return m_empty;
} }

View File

@ -28,12 +28,10 @@ struct CDSkillBehavior {
class CDSkillBehaviorTable : public CDTable<CDSkillBehaviorTable> { class CDSkillBehaviorTable : public CDTable<CDSkillBehaviorTable> {
private: private:
std::map<unsigned int, CDSkillBehavior> entries; std::map<unsigned int, CDSkillBehavior> entries;
CDSkillBehavior m_empty;
public: public:
void LoadValuesFromDatabase(); void LoadValuesFromDatabase();
// Gets an entry by skillID // Gets an entry by skillID
const CDSkillBehavior& GetSkillByID(unsigned int skillID); const std::optional<CDSkillBehavior> GetSkillByID(unsigned int skillID);
}; };

View File

@ -5,6 +5,7 @@
#include "DluAssert.h" #include "DluAssert.h"
#include <functional> #include <functional>
#include <optional>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>

View File

@ -1,50 +1,24 @@
#include "CDVendorComponentTable.h" #include "CDVendorComponentTable.h"
void CDVendorComponentTable::LoadValuesFromDatabase() { void CDVendorComponentTable::LoadValuesFromDatabase() {
// First, get the size of the table
unsigned int size = 0;
auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM VendorComponent");
while (!tableSize.eof()) {
size = tableSize.getIntField(0, 0);
tableSize.nextRow();
}
tableSize.finalize();
// Reserve the size
this->entries.reserve(size);
// Now get the data // Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM VendorComponent"); auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM VendorComponent");
while (!tableData.eof()) { while (!tableData.eof()) {
CDVendorComponent entry; CDVendorComponent entry;
entry.id = tableData.getIntField("id", -1); uint32_t id = tableData.getIntField("id", -1);
entry.buyScalar = tableData.getFloatField("buyScalar", -1.0f); entry.buyScalar = tableData.getFloatField("buyScalar", -1.0f);
entry.sellScalar = tableData.getFloatField("sellScalar", -1.0f); entry.sellScalar = tableData.getFloatField("sellScalar", -1.0f);
entry.refreshTimeSeconds = tableData.getFloatField("refreshTimeSeconds", -1.0f); entry.refreshTimeSeconds = tableData.getFloatField("refreshTimeSeconds", -1.0f);
entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1); entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1);
this->entries.push_back(entry); this->entries.insert_or_assign(id, entry);
tableData.nextRow(); tableData.nextRow();
} }
tableData.finalize(); tableData.finalize();
} }
//! Queries the table with a custom "where" clause const std::optional<CDVendorComponent> CDVendorComponentTable::Query(uint32_t id) {
std::vector<CDVendorComponent> CDVendorComponentTable::Query(std::function<bool(CDVendorComponent)> predicate) { const auto& iter = entries.find(id);
return iter != entries.end() ? std::make_optional(iter->second) : std::nullopt;
std::vector<CDVendorComponent> data = cpplinq::from(this->entries)
>> cpplinq::where(predicate)
>> cpplinq::to_vector();
return data;
} }
//! Gets all the entries in the table
const std::vector<CDVendorComponent>& CDVendorComponentTable::GetEntries() const {
return this->entries;
}

View File

@ -4,7 +4,6 @@
#include "CDTable.h" #include "CDTable.h"
struct CDVendorComponent { struct CDVendorComponent {
unsigned int id; //!< The Component ID
float buyScalar; //!< Buy Scalar (what does that mean?) float buyScalar; //!< Buy Scalar (what does that mean?)
float sellScalar; //!< Sell Scalar (what does that mean?) float sellScalar; //!< Sell Scalar (what does that mean?)
float refreshTimeSeconds; //!< The refresh time float refreshTimeSeconds; //!< The refresh time
@ -13,13 +12,11 @@ struct CDVendorComponent {
class CDVendorComponentTable : public CDTable<CDVendorComponentTable> { class CDVendorComponentTable : public CDTable<CDVendorComponentTable> {
private: private:
std::vector<CDVendorComponent> entries; std::map<uint32_t, CDVendorComponent> entries;
public: public:
void LoadValuesFromDatabase(); void LoadValuesFromDatabase();
// Queries the table with a custom "where" clause // Queries the table with a custom "where" clause
std::vector<CDVendorComponent> Query(std::function<bool(CDVendorComponent)> predicate); const std::optional<CDVendorComponent> Query(uint32_t id);
const std::vector<CDVendorComponent>& GetEntries(void) const;
}; };

View File

@ -2,8 +2,6 @@
#include "CDTable.h" #include "CDTable.h"
#include <optional>
struct CDZoneTable { struct CDZoneTable {
unsigned int zoneID; //!< The Zone ID of the object unsigned int zoneID; //!< The Zone ID of the object
unsigned int locStatus; //!< The Locale Status(?) unsigned int locStatus; //!< The Locale Status(?)

View File

@ -1628,9 +1628,9 @@ void Entity::PickupItem(const LWOOBJID& objectID) {
std::vector<CDObjectSkills> skills = skillsTable->Query([=](CDObjectSkills entry) {return (entry.objectTemplate == p.second.lot); }); std::vector<CDObjectSkills> skills = skillsTable->Query([=](CDObjectSkills entry) {return (entry.objectTemplate == p.second.lot); });
for (CDObjectSkills skill : skills) { for (CDObjectSkills skill : skills) {
CDSkillBehaviorTable* skillBehTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); CDSkillBehaviorTable* skillBehTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
CDSkillBehavior behaviorData = skillBehTable->GetSkillByID(skill.skillID); auto behaviorData = skillBehTable->GetSkillByID(skill.skillID);
if (!behaviorData) continue;
SkillComponent::HandleUnmanaged(behaviorData.behaviorID, GetObjectID()); SkillComponent::HandleUnmanaged(behaviorData->behaviorID, GetObjectID());
auto* missionComponent = GetComponent<MissionComponent>(); auto* missionComponent = GetComponent<MissionComponent>();

View File

@ -42,7 +42,8 @@ void OverTimeBehavior::Load() {
// Since m_Action is a skillID and not a behavior, get is correlated behaviorID. // Since m_Action is a skillID and not a behavior, get is correlated behaviorID.
CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
m_ActionBehaviorId = skillTable->GetSkillByID(m_Action).behaviorID; auto skillData = skillTable->GetSkillByID(m_Action);
if (skillData) m_ActionBehaviorId = skillData->behaviorID;
m_Delay = GetFloat("delay"); m_Delay = GetFloat("delay");
m_NumIntervals = GetInt("num_intervals"); m_NumIntervals = GetInt("num_intervals");

View File

@ -104,7 +104,12 @@ void BuffComponent::ApplyBuff(const int32_t id, const float duration, const LWOO
if (parameter.name == "overtime") { if (parameter.name == "overtime") {
auto* behaviorTemplateTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); auto* behaviorTemplateTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
behaviorID = behaviorTemplateTable->GetSkillByID(parameter.values[0]).behaviorID; auto skillInfo = behaviorTemplateTable->GetSkillByID(parameter.values[0]);
if (skillInfo) {
behaviorID = skillInfo->behaviorID;
} else {
Game::logger->Log("BuffComponent", "Failed to find skill info for skill ID %d!", parameter.values[0]);
}
stacks = static_cast<int32_t>(parameter.values[1]); stacks = static_cast<int32_t>(parameter.values[1]);
tick = parameter.values[2]; tick = parameter.values[2];
const auto unknown2 = parameter.values[3]; // Always 0 const auto unknown2 = parameter.values[3]; // Always 0

View File

@ -1341,8 +1341,12 @@ std::vector<uint32_t> InventoryComponent::FindBuffs(Item* item, bool castOnEquip
for (const auto& result : results) { for (const auto& result : results) {
if (result.castOnType == 1) { if (result.castOnType == 1) {
const auto entry = behaviors->GetSkillByID(result.skillID); const auto entry = behaviors->GetSkillByID(result.skillID);
if (!entry) {
Game::logger->Log("InventoryComponent", "Buff %i not in database!", result.skillID);
if (entry.skillID == 0) { continue;
}
if (entry->skillID == 0) {
Game::logger->Log("InventoryComponent", "Failed to find buff behavior for skill (%i)!", result.skillID); Game::logger->Log("InventoryComponent", "Failed to find buff behavior for skill (%i)!", result.skillID);
continue; continue;
@ -1353,7 +1357,7 @@ std::vector<uint32_t> InventoryComponent::FindBuffs(Item* item, bool castOnEquip
} }
// If item is not a proxy, add its buff to the added buffs. // If item is not a proxy, add its buff to the added buffs.
if (item->GetParent() == LWOOBJID_EMPTY) buffs.push_back(static_cast<uint32_t>(entry.behaviorID)); if (item->GetParent() == LWOOBJID_EMPTY) buffs.push_back(static_cast<uint32_t>(entry->behaviorID));
} }
} }

View File

@ -235,7 +235,12 @@ bool SkillComponent::CastSkill(const uint32_t skillId, LWOOBJID target, const LW
// if it's not in the cache look it up and cache it // if it's not in the cache look it up and cache it
if (pair == m_skillBehaviorCache.end()) { if (pair == m_skillBehaviorCache.end()) {
auto skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); auto skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
behaviorId = skillTable->GetSkillByID(skillId).behaviorID; auto skill = skillTable->GetSkillByID(skillId);
if (!skill) {
Game::logger->LogDebug("SkillComponent", "Tried to cast skill %i but found no skill", skillId);
return false;
}
behaviorId = skill->behaviorID;
m_skillBehaviorCache.insert_or_assign(skillId, behaviorId); m_skillBehaviorCache.insert_or_assign(skillId, behaviorId);
} else { } else {
behaviorId = pair->second; behaviorId = pair->second;

View File

@ -127,12 +127,12 @@ void VendorComponent::SetupConstants() {
int componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::VENDOR); int componentID = compRegistryTable->GetByIDAndType(m_Parent->GetLOT(), eReplicaComponentType::VENDOR);
auto* vendorComponentTable = CDClientManager::Instance().GetTable<CDVendorComponentTable>(); auto* vendorComponentTable = CDClientManager::Instance().GetTable<CDVendorComponentTable>();
std::vector<CDVendorComponent> vendorComps = vendorComponentTable->Query([=](CDVendorComponent entry) { return (entry.id == componentID); }); auto vendorCompData = vendorComponentTable->Query(componentID);
if (vendorComps.empty()) return; if (!vendorCompData) return;
m_BuyScalar = vendorComps[0].buyScalar; m_BuyScalar = vendorCompData->buyScalar;
m_SellScalar = vendorComps[0].sellScalar; m_SellScalar = vendorCompData->sellScalar;
m_RefreshTimeSeconds = vendorComps[0].refreshTimeSeconds; m_RefreshTimeSeconds = vendorCompData->refreshTimeSeconds;
m_LootMatrixID = vendorComps[0].LootMatrixIndex; m_LootMatrixID = vendorCompData->LootMatrixIndex;
} }
bool VendorComponent::SellsItem(const LOT item) const { bool VendorComponent::SellsItem(const LOT item) const {

View File

@ -288,7 +288,12 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
} }
CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); CDSkillBehaviorTable* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
unsigned int behaviorId = skillTable->GetSkillByID(startSkill.skillID).behaviorID; auto skill = skillTable->GetSkillByID(startSkill.skillID);
if (!skill) {
Game::logger->Log("GameMessageHandler", "Failed to find skill %d in the skill table!", startSkill.skillID);
return;
}
unsigned int behaviorId = skill->behaviorID;
bool success = false; bool success = false;
@ -301,7 +306,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
if (success && entity->GetCharacter()) { if (success && entity->GetCharacter()) {
DestroyableComponent* destComp = entity->GetComponent<DestroyableComponent>(); DestroyableComponent* destComp = entity->GetComponent<DestroyableComponent>();
destComp->SetImagination(destComp->GetImagination() - skillTable->GetSkillByID(startSkill.skillID).imaginationcost); destComp->SetImagination(destComp->GetImagination() - skill->imaginationcost);
} }
delete bs; delete bs;

View File

@ -131,11 +131,12 @@ void ItemSet::OnEquip(const LOT lot) {
for (const auto skill : skillSet) { for (const auto skill : skillSet) {
auto* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); auto* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
const auto behaviorId = skillTable->GetSkillByID(skill).behaviorID; auto skillData = skillTable->GetSkillByID(skill);
if (!skillData) continue;
missionComponent->Progress(eMissionTaskType::USE_SKILL, skill); missionComponent->Progress(eMissionTaskType::USE_SKILL, skill);
skillComponent->HandleUnmanaged(behaviorId, m_InventoryComponent->GetParent()->GetObjectID()); skillComponent->HandleUnmanaged(skillData->behaviorID, m_InventoryComponent->GetParent()->GetObjectID());
} }
} }
@ -163,9 +164,10 @@ void ItemSet::OnUnEquip(const LOT lot) {
for (const auto skill : skillSet) { for (const auto skill : skillSet) {
auto* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); auto* skillTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
const auto behaviorId = skillTable->GetSkillByID(skill).behaviorID; auto skillData = skillTable->GetSkillByID(skill);
if (!skillData) continue;
skillComponent->HandleUnCast(behaviorId, m_InventoryComponent->GetParent()->GetObjectID()); skillComponent->HandleUnCast(skillData->behaviorID, m_InventoryComponent->GetParent()->GetObjectID());
} }
} }

View File

@ -23,9 +23,10 @@ void QbEnemyStunner::OnRebuildComplete(Entity* self, Entity* target) {
// For each skill, cast it with the associated behavior ID. // For each skill, cast it with the associated behavior ID.
for (auto skill : skills) { for (auto skill : skills) {
CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
CDSkillBehavior behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID); auto behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID);
if (!behaviorData) continue;
skillBehaviorMap.insert(std::make_pair(skill.skillID, behaviorData.behaviorID)); skillBehaviorMap.insert(std::make_pair(skill.skillID, behaviorData->behaviorID));
} }
// If there are no skills found, insert a default skill to use. // If there are no skills found, insert a default skill to use.

View File

@ -17,10 +17,11 @@ void FireFirstSkillonStartup::OnStartup(Entity* self) {
// For each skill, cast it with the associated behavior ID. // For each skill, cast it with the associated behavior ID.
for (auto skill : skills) { for (auto skill : skills) {
CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>(); CDSkillBehaviorTable* skillBehaviorTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
CDSkillBehavior behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID); auto behaviorData = skillBehaviorTable->GetSkillByID(skill.skillID);
if (!behaviorData) continue;
// Should parent entity be null, make the originator self. // Should parent entity be null, make the originator self.
const auto target = self->GetParentEntity() ? self->GetParentEntity()->GetObjectID() : self->GetObjectID(); const auto target = self->GetParentEntity() ? self->GetParentEntity()->GetObjectID() : self->GetObjectID();
skillComponent->CalculateBehavior(skill.skillID, behaviorData.behaviorID, LWOOBJID_EMPTY, false, false, target); skillComponent->CalculateBehavior(skill.skillID, behaviorData->behaviorID, LWOOBJID_EMPTY, false, false, target);
} }
} }