DarkflameServer/dGame/dComponents/BuffComponent.cpp

318 lines
9.2 KiB
C++
Raw Normal View History

#include "BuffComponent.h"
#include <BitStream.h>
#include "CDClientDatabase.h"
#include <stdexcept>
#include "DestroyableComponent.h"
#include "Game.h"
#include "dLogger.h"
#include "GameMessages.h"
#include "SkillComponent.h"
#include "ControllablePhysicsComponent.h"
#include "EntityManager.h"
#include "CDClientManager.h"
#include "CDSkillBehaviorTable.h"
2022-07-28 13:39:57 +00:00
std::unordered_map<int32_t, std::vector<BuffParameter>> BuffComponent::m_Cache{};
void BuffComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
2022-07-28 13:39:57 +00:00
if (!bIsInitialUpdate) return;
2023-06-26 08:49:56 +00:00
outBitStream->Write(!m_Buffs.empty());
if (!m_Buffs.empty()) {
outBitStream->Write<uint32_t>(m_Buffs.size());
2022-07-28 13:39:57 +00:00
for (const auto& buff : m_Buffs) {
outBitStream->Write<uint32_t>(buff.first);
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write<uint32_t>(0);
}
}
2023-06-26 08:49:56 +00:00
outBitStream->Write0(); // immunities
}
2022-07-28 13:39:57 +00:00
void BuffComponent::Update(float deltaTime) {
/**
* Loop through all buffs and apply deltaTime to ther time.
* If they have expired, remove the buff and break.
*/
2023-06-26 08:49:56 +00:00
for (auto& [buffId, buffInfo] : m_Buffs) {
// For damage buffs
2023-06-26 08:49:56 +00:00
if (buffInfo.tick != 0.0f && buffInfo.stacks > 0) {
buffInfo.tickTime -= deltaTime;
2023-06-26 08:49:56 +00:00
if (buffInfo.tickTime <= 0.0f) {
buffInfo.tickTime = buffInfo.tick;
buffInfo.stacks--;
2023-06-26 08:49:56 +00:00
SkillComponent::HandleUnmanaged(buffInfo.behaviorID, m_ParentEntity->GetObjectID(), buffInfo.source);
}
}
// These are indefinate buffs, don't update them.
2023-06-26 08:49:56 +00:00
if (buffInfo.time == 0.0f) continue;
2023-06-26 08:49:56 +00:00
buffInfo.time -= deltaTime;
2023-06-26 08:49:56 +00:00
if (buffInfo.time <= 0.0f) {
RemoveBuff(buffId);
2023-06-26 08:49:56 +00:00
break; // Break because we modified or may modify the map.
}
}
}
void BuffComponent::ApplyBuff(const int32_t id, const float duration, const LWOOBJID source, bool addImmunity,
2022-07-28 13:39:57 +00:00
bool cancelOnDamaged, bool cancelOnDeath, bool cancelOnLogout, bool cancelOnRemoveBuff,
bool cancelOnUi, bool cancelOnUnequip, bool cancelOnZone) {
// Prevent buffs from stacking.
2023-06-26 08:49:56 +00:00
if (HasBuff(id)) return;
GameMessages::SendAddBuff(m_ParentEntity->GetObjectID(), source, (uint32_t)id,
2022-07-28 13:39:57 +00:00
(uint32_t)duration * 1000, addImmunity, cancelOnDamaged, cancelOnDeath,
cancelOnLogout, cancelOnRemoveBuff, cancelOnUi, cancelOnUnequip, cancelOnZone);
float tick = 0;
float stacks = 0;
int32_t behaviorID = 0;
const auto& parameters = GetBuffParameters(id);
2023-06-26 08:49:56 +00:00
auto* skillBehaviorTable = CDClientManager::Instance().GetTable<CDSkillBehaviorTable>();
2022-07-28 13:39:57 +00:00
for (const auto& parameter : parameters) {
if (parameter.name == "overtime") {
2023-06-26 08:49:56 +00:00
behaviorID = skillBehaviorTable->GetSkillByID(parameter.values.skillId).behaviorID;
stacks = static_cast<int32_t>(parameter.values.stacks);
tick = parameter.values.tick;
const auto unknown2 = parameter.values.unknown2; // Always 0
}
}
ApplyBuffEffect(id);
Buff buff;
buff.id = id;
buff.time = duration;
buff.tick = tick;
buff.tickTime = tick;
buff.stacks = stacks;
buff.source = source;
buff.behaviorID = behaviorID;
2023-06-26 08:49:56 +00:00
m_Buffs.insert_or_assign(id, buff);
}
void BuffComponent::RemoveBuff(int32_t id, bool fromUnEquip, bool removeImmunity) {
const auto& iter = m_Buffs.find(id);
2023-06-26 08:49:56 +00:00
if (iter == m_Buffs.end()) return;
GameMessages::SendRemoveBuff(m_ParentEntity, fromUnEquip, removeImmunity, id);
m_Buffs.erase(iter);
RemoveBuffEffect(id);
}
2022-07-28 13:39:57 +00:00
void BuffComponent::ApplyBuffEffect(int32_t id) {
const auto& parameters = GetBuffParameters(id);
2022-07-28 13:39:57 +00:00
for (const auto& parameter : parameters) {
if (parameter.name == "max_health") {
const auto maxHealth = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
if (destroyable == nullptr) return;
destroyable->SetMaxHealth(destroyable->GetMaxHealth() + maxHealth);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "max_armor") {
const auto maxArmor = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
if (destroyable == nullptr) return;
destroyable->SetMaxArmor(destroyable->GetMaxArmor() + maxArmor);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "max_imagination") {
const auto maxImagination = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
if (destroyable == nullptr) return;
destroyable->SetMaxImagination(destroyable->GetMaxImagination() + maxImagination);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "speed") {
auto* controllablePhysicsComponent = this->GetParentEntity()->GetComponent<ControllablePhysicsComponent>();
if (!controllablePhysicsComponent) return;
const auto speed = parameter.value;
controllablePhysicsComponent->AddSpeedboost(speed);
}
}
}
2022-07-28 13:39:57 +00:00
void BuffComponent::RemoveBuffEffect(int32_t id) {
const auto& parameters = GetBuffParameters(id);
2022-07-28 13:39:57 +00:00
for (const auto& parameter : parameters) {
if (parameter.name == "max_health") {
const auto maxHealth = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
2023-06-26 08:49:56 +00:00
if (!destroyable) return;
destroyable->SetMaxHealth(destroyable->GetMaxHealth() - maxHealth);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "max_armor") {
const auto maxArmor = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
2023-06-26 08:49:56 +00:00
if (!destroyable) return;
destroyable->SetMaxArmor(destroyable->GetMaxArmor() - maxArmor);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "max_imagination") {
const auto maxImagination = parameter.value;
auto* destroyable = this->GetParentEntity()->GetComponent<DestroyableComponent>();
2023-06-26 08:49:56 +00:00
if (!destroyable) return;
destroyable->SetMaxImagination(destroyable->GetMaxImagination() - maxImagination);
2022-07-28 13:39:57 +00:00
} else if (parameter.name == "speed") {
auto* controllablePhysicsComponent = this->GetParentEntity()->GetComponent<ControllablePhysicsComponent>();
if (!controllablePhysicsComponent) return;
const auto speed = parameter.value;
controllablePhysicsComponent->RemoveSpeedboost(speed);
}
}
}
2022-07-28 13:39:57 +00:00
void BuffComponent::RemoveAllBuffs() {
2023-06-26 08:49:56 +00:00
for (const auto& [buffId, buffInfo] : m_Buffs) {
RemoveBuffEffect(buffId);
}
m_Buffs.clear();
}
2022-07-28 13:39:57 +00:00
void BuffComponent::ReApplyBuffs() {
2023-06-26 08:49:56 +00:00
for (const auto& [buffId, buffInfo] : m_Buffs) {
ApplyBuffEffect(buffId);
}
}
2022-07-28 13:39:57 +00:00
void BuffComponent::LoadFromXml(tinyxml2::XMLDocument* doc) {
// Load buffs
auto* dest = doc->FirstChildElement("obj")->FirstChildElement("dest");
// Make sure we have a clean buff element.
auto* buffElement = dest->FirstChildElement("buff");
// Old character, no buffs to load
2023-06-26 08:49:56 +00:00
if (buffElement) return;
auto* buffEntry = buffElement->FirstChildElement("b");
2022-07-28 13:39:57 +00:00
while (buffEntry != nullptr) {
int32_t id = buffEntry->IntAttribute("id");
float t = buffEntry->FloatAttribute("t");
float tk = buffEntry->FloatAttribute("tk");
int32_t s = buffEntry->FloatAttribute("s");
LWOOBJID sr = buffEntry->Int64Attribute("sr");
int32_t b = buffEntry->IntAttribute("b");
Buff buff;
buff.id = id;
buff.time = t;
buff.tick = tk;
buff.stacks = s;
buff.source = sr;
buff.behaviorID = b;
m_Buffs.emplace(id, buff);
buffEntry = buffEntry->NextSiblingElement("b");
}
}
2022-07-28 13:39:57 +00:00
void BuffComponent::UpdateXml(tinyxml2::XMLDocument* doc) {
// Save buffs
auto* dest = doc->FirstChildElement("obj")->FirstChildElement("dest");
// Make sure we have a clean buff element.
auto* buffElement = dest->FirstChildElement("buff");
2022-07-28 13:39:57 +00:00
if (buffElement == nullptr) {
buffElement = doc->NewElement("buff");
dest->LinkEndChild(buffElement);
2022-07-28 13:39:57 +00:00
} else {
buffElement->DeleteChildren();
}
2022-07-28 13:39:57 +00:00
for (const auto& buff : m_Buffs) {
auto* buffEntry = doc->NewElement("b");
buffEntry->SetAttribute("id", buff.first);
buffEntry->SetAttribute("t", buff.second.time);
buffEntry->SetAttribute("tk", buff.second.tick);
buffEntry->SetAttribute("s", buff.second.stacks);
buffEntry->SetAttribute("sr", buff.second.source);
buffEntry->SetAttribute("b", buff.second.behaviorID);
buffElement->LinkEndChild(buffEntry);
}
}
2022-07-28 13:39:57 +00:00
const std::vector<BuffParameter>& BuffComponent::GetBuffParameters(int32_t buffId) {
const auto& pair = m_Cache.find(buffId);
2023-06-26 08:49:56 +00:00
if (pair != m_Cache.end()) return pair->second;
2023-06-26 08:49:56 +00:00
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM BuffParameters WHERE BuffID = ?;");
2022-07-28 13:39:57 +00:00
query.bind(1, (int)buffId);
auto result = query.execQuery();
2022-07-28 13:39:57 +00:00
std::vector<BuffParameter> parameters{};
2022-07-28 13:39:57 +00:00
while (!result.eof()) {
BuffParameter param;
param.buffId = buffId;
param.name = result.getStringField(1);
param.value = result.getFloatField(2);
2022-07-28 13:39:57 +00:00
if (!result.fieldIsNull(3)) {
2023-06-26 08:49:56 +00:00
const auto parameterInfo = result.getStringField(3);
const auto values = GeneralUtils::SplitString(parameterInfo, ',');
if (values.size() >= 4) {
GeneralUtils::TryParse(values.at(0), param.values.skillId);
GeneralUtils::TryParse(values.at(1), param.values.stacks);
GeneralUtils::TryParse(values.at(2), param.values.tick);
GeneralUtils::TryParse(values.at(3), param.values.unknown2);
} else {
Game::logger->Log("BuffComponent", "Failed to parse %s into parameter struct. Too few parameters to split on.", parameterInfo);
}
}
parameters.push_back(param);
result.nextRow();
}
m_Cache.insert_or_assign(buffId, parameters);
return m_Cache.find(buffId)->second;
}