Files
DarkflameServer/dGame/dComponents/Character/LevelProgressionComponent.cpp
Aaron Kimbrell 1382606bd1 fix: constructions keep pending changes of five more components
The level, forced movement, possessable, possessor and combat AI parts
are each read behind a flag bit (LWOLevelProgressionComponent::
Deserialize 0x00c63bd0 and the others). DLU cleared their dirty flags
when it wrote a construction, which goes only to the client the object
came into range of, so a change waiting for the next update never
reached the clients already watching: a level up, a mount or dismount,
a rail ride.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:26:46 -06:00

98 lines
3.4 KiB
C++

#include "LevelProgressionComponent.h"
#include "MissionMessages.h"
#include "ControllablePhysicsComponent.h"
#include "InventoryComponent.h"
#include "CharacterComponent.h"
#include "tinyxml2.h"
#include "CDRewardsTable.h"
LevelProgressionComponent::LevelProgressionComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
m_Parent = parent;
m_Level = 1;
m_SpeedBase = 500.0f;
m_CharacterVersion = eCharacterVersion::LIVE;
}
void LevelProgressionComponent::WriteSaveData(tinyxml2::XMLDocument& doc) {
tinyxml2::XMLElement* level = doc.FirstChildElement("obj")->FirstChildElement("lvl");
if (!level) {
LOG("Failed to find lvl tag while updating XML!");
return;
}
level->SetAttribute("l", m_Level);
level->SetAttribute("sb", m_SpeedBase);
level->SetAttribute("cv", static_cast<uint32_t>(m_CharacterVersion));
}
void LevelProgressionComponent::LoadFromSaveData(const tinyxml2::XMLDocument& doc) {
auto* level = doc.FirstChildElement("obj")->FirstChildElement("lvl");
if (!level) {
LOG("Failed to find lvl tag while loading XML!");
return;
}
level->QueryAttribute("l", &m_Level);
level->QueryAttribute("sb", &m_SpeedBase);
uint32_t characterVersion;
level->QueryAttribute("cv", &characterVersion);
m_CharacterVersion = static_cast<eCharacterVersion>(characterVersion);
}
void LevelProgressionComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
outBitStream.Write(bIsInitialUpdate || m_DirtyLevelInfo);
if (bIsInitialUpdate || m_DirtyLevelInfo) outBitStream.Write(m_Level);
// A construction goes to one client; the players already watching still need a waiting change
if (!bIsInitialUpdate) m_DirtyLevelInfo = false;
}
void LevelProgressionComponent::HandleLevelUp() {
auto* rewardsTable = CDClientManager::GetTable<CDRewardsTable>();
const auto& rewards = rewardsTable->GetByLevelID(m_Level);
bool rewardingItem = rewards.size() > 0;
auto* inventoryComponent = m_Parent->GetComponent<InventoryComponent>();
auto* controllablePhysicsComponent = m_Parent->GetComponent<ControllablePhysicsComponent>();
if (!inventoryComponent || !controllablePhysicsComponent) return;
GameMessages::NotifyLevelRewards levelRewards;
levelRewards.target = m_Parent->GetObjectID();
levelRewards.level = m_Level;
// Tell the client we beginning to send level rewards.
levelRewards.sendingRewards = rewardingItem;
if (rewardingItem) levelRewards.SendToClient(m_Parent->GetSystemAddress());
for (const auto& reward : rewards) {
switch (reward.rewardType) {
case 0:
inventoryComponent->AddItem(reward.value, reward.count, eLootSourceType::LEVEL_REWARD);
break;
case 4:
{
auto* items = inventoryComponent->GetInventory(eInventoryType::ITEMS);
items->SetSize(items->GetSize() + reward.value);
}
break;
case 9:
SetSpeedBase(static_cast<float>(reward.value) );
controllablePhysicsComponent->SetSpeedMultiplier(GetSpeedBase() / 500.0f);
break;
case 11:
case 12:
break;
default:
break;
}
}
// Tell the client we have finished sending level rewards.
levelRewards.sendingRewards = !rewardingItem;
if (rewardingItem) levelRewards.SendToClient(m_Parent->GetSystemAddress());
}
void LevelProgressionComponent::SetRetroactiveBaseSpeed(){
if (m_Level >= 20) m_SpeedBase = 525.0f;
auto* controllablePhysicsComponent = m_Parent->GetComponent<ControllablePhysicsComponent>();
if (controllablePhysicsComponent) controllablePhysicsComponent->SetSpeedMultiplier(m_SpeedBase / 500.0f);
}