mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 10:53:44 +00:00
feat(skills): save running skill cooldown groups (charxml <skil sc>)
The client reads the cooldowns still running from the charxml on load
(LWOSkillComponent::LoadFromSaveData 0x00c2a9c0): <skil sc="..."> split
on ';' and ':' (the delimiter string at 0x015b1714 is L";:") into a
cooldown group and the seconds left, each put in its cooldown map under
{hasCooldownGroup = 1, group}, the key it checks when the player casts a
skill of that group (0x00c11400). Live wrote <skil/> with nothing running
and e.g. <skil sc="17:15.6958;"/> otherwise (226 live charxmls; groups
8, 17 and 78 seen, the times below the groups' SkillBehavior cooldowns).
The claude-client-re-docs page described sc as "id;time" pairs; the
live saves and the delimiters show "group:time;".
DLU wrote no <skil>, so relogging or changing zones cleared every
cooldown (e.g. the 90 s imagination and faction skills).
- A player's successful cast (CastPlayerSkill) starts its
SkillBehavior.cooldowngroup's cooldown (groups 0 and up, cooldown > 0),
counted down in Update.
- Saved as live wrote it; loaded back when the player's SkillComponent
is created, so a cooldown survives several zone changes. Old saves
without <skil> load with none running.
Check in game: use a skill with a long cooldown (e.g. a faction kit's
special skill, a consumable with a cooldown), change zones or log out and
back in right away: its cooldown is still shown and counts down from
where it was (minus the loading time on the server).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -595,7 +595,8 @@ void Entity::Initialize() {
|
||||
|
||||
const auto skillID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SKILL, -1);
|
||||
if (skillID > -1 || m_Character) {
|
||||
AddComponent<SkillComponent>(skillID);
|
||||
auto* const skillComponent = AddComponent<SkillComponent>(skillID);
|
||||
if (m_Character) skillComponent->LoadFromXml(m_Character->GetXMLDoc());
|
||||
}
|
||||
|
||||
const auto combatAiID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::BASE_COMBAT_AI);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
*/
|
||||
|
||||
#include "SkillComponent.h"
|
||||
#include "GeneralUtils.h"
|
||||
#include "tinyxml2.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
@@ -47,9 +49,51 @@ bool SkillComponent::CastPlayerSkill(const uint32_t behaviorId, const uint32_t s
|
||||
|
||||
context->ExecuteUpdates();
|
||||
|
||||
if (!context->failed && m_Parent->IsPlayer()) StartCooldown(skillID);
|
||||
|
||||
return !context->failed;
|
||||
}
|
||||
|
||||
void SkillComponent::StartCooldown(const uint32_t skillID) {
|
||||
const auto& skill = CDClientManager::GetTable<CDSkillBehaviorTable>()->GetSkillByID(skillID);
|
||||
// The client keys cooldown groups 0 and up (a null group is -1); skills without a cooldown have none running
|
||||
const auto group = static_cast<int32_t>(skill.cooldowngroup);
|
||||
if (group < 0 || skill.cooldown <= 0.0f) return;
|
||||
auto& left = m_CooldownGroups[group];
|
||||
left = std::max(left, skill.cooldown);
|
||||
}
|
||||
|
||||
void SkillComponent::LoadFromXml(const tinyxml2::XMLDocument& doc) {
|
||||
m_CooldownGroups.clear();
|
||||
const auto* const obj = doc.FirstChildElement("obj");
|
||||
const auto* const skil = obj ? obj->FirstChildElement("skil") : nullptr;
|
||||
const char* const cooldowns = skil ? skil->Attribute("sc") : nullptr;
|
||||
if (!cooldowns) return;
|
||||
|
||||
// Alternating group and seconds, split on ';' and ':' as the client does
|
||||
const auto tokens = GeneralUtils::SplitString(std::string(cooldowns), ';');
|
||||
for (const auto& entry : tokens) {
|
||||
const auto pair = GeneralUtils::SplitString(entry, ':');
|
||||
if (pair.size() != 2) continue;
|
||||
const auto group = GeneralUtils::TryParse<int32_t>(pair[0]);
|
||||
const auto left = GeneralUtils::TryParse<float>(pair[1]);
|
||||
if (group && left && *left > 0.0f) m_CooldownGroups[*group] = *left;
|
||||
}
|
||||
}
|
||||
|
||||
void SkillComponent::UpdateXml(tinyxml2::XMLDocument& doc) {
|
||||
auto* const obj = doc.FirstChildElement("obj");
|
||||
if (!obj) return;
|
||||
auto* skil = obj->FirstChildElement("skil");
|
||||
if (!skil) skil = obj->InsertNewChildElement("skil");
|
||||
|
||||
// Live: <skil/> with nothing running, else e.g. <skil sc="17:15.6958;"/>
|
||||
std::ostringstream cooldowns;
|
||||
for (const auto& [group, left] : m_CooldownGroups) cooldowns << group << ':' << left << ';';
|
||||
if (cooldowns.view().empty()) skil->DeleteAttribute("sc");
|
||||
else skil->SetAttribute("sc", cooldowns.str().c_str());
|
||||
}
|
||||
|
||||
void SkillComponent::SyncPlayerSkill(const uint32_t skillUid, const uint32_t syncId, RakNet::BitStream& bitStream) {
|
||||
const auto index = this->m_managedBehaviors.equal_range(skillUid);
|
||||
|
||||
@@ -141,6 +185,11 @@ void SkillComponent::RegisterPlayerProjectile(const LWOOBJID projectileId, Behav
|
||||
}
|
||||
|
||||
void SkillComponent::Update(const float deltaTime) {
|
||||
for (auto it = m_CooldownGroups.begin(); it != m_CooldownGroups.end();) {
|
||||
it->second -= deltaTime;
|
||||
it = it->second <= 0.0f ? m_CooldownGroups.erase(it) : std::next(it);
|
||||
}
|
||||
|
||||
if (!m_Parent->HasComponent(eReplicaComponentType::BASE_COMBAT_AI) && m_Parent->GetLOT() != 1) {
|
||||
CalculateUpdate(deltaTime);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,27 @@ public:
|
||||
*/
|
||||
void Update(float deltaTime) override;
|
||||
|
||||
/**
|
||||
* Loads the cooldown groups still running at the last save: <skil sc="group:seconds;group:seconds;"/>
|
||||
*/
|
||||
void LoadFromXml(const tinyxml2::XMLDocument& doc) override;
|
||||
|
||||
/**
|
||||
* Saves the running cooldown groups the way live did and the client reads them (LWOSkillComponent::LoadFromSaveData
|
||||
* 0x00c2a9c0 splits sc on ';' and ':' into a cooldown group and the seconds left)
|
||||
*/
|
||||
void UpdateXml(tinyxml2::XMLDocument& doc) override;
|
||||
|
||||
/**
|
||||
* Starts the cooldown of a skill's cooldown group (SkillBehavior.cooldowngroup, cooldown) when the player casts it
|
||||
*/
|
||||
void StartCooldown(uint32_t skillID);
|
||||
|
||||
/**
|
||||
* The cooldown groups running and the seconds left on each
|
||||
*/
|
||||
const std::map<int32_t, float>& GetCooldownGroups() const { return m_CooldownGroups; }
|
||||
|
||||
/**
|
||||
* Computes server-side skill updates.
|
||||
*/
|
||||
@@ -223,6 +244,9 @@ private:
|
||||
*/
|
||||
uint32_t m_skillUid;
|
||||
|
||||
// Cooldown group -> seconds left, for the player's save
|
||||
std::map<int32_t, float> m_CooldownGroups;
|
||||
|
||||
/**
|
||||
* Cache for looking up a behavior id via a skill ID
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#include "Entity.h"
|
||||
#include "GameMessages.h"
|
||||
#include "MissionComponent.h"
|
||||
#include "SkillComponent.h"
|
||||
#include "CDSkillBehaviorTable.h"
|
||||
#include "CDClientManager.h"
|
||||
#include "eMissionLockState.h"
|
||||
|
||||
#include "tinyxml2.h"
|
||||
@@ -109,3 +112,54 @@ TEST_F(CharacterSaveFieldsTests, TooltipFlagsMissingInOldSave) {
|
||||
characterComponent->UpdateXml(doc);
|
||||
EXPECT_STREQ(doc.FirstChildElement("obj")->FirstChildElement("char")->Attribute("ttip"), "0");
|
||||
}
|
||||
|
||||
// <skil sc>: the cooldown groups still running and the seconds left, as live wrote them (sc="17:15.6958;") and the
|
||||
// client reads them (split on ';' and ':').
|
||||
TEST_F(CharacterSaveFieldsTests, SkillCooldownsRoundTrip) {
|
||||
CDSkillBehavior skill{};
|
||||
skill.skillID = 394;
|
||||
skill.cooldowngroup = 17;
|
||||
skill.cooldown = 30.0f;
|
||||
CDClientManager::GetEntriesMutable<CDSkillBehaviorTable>()[394] = skill;
|
||||
CDSkillBehavior ungrouped{};
|
||||
ungrouped.skillID = 395;
|
||||
ungrouped.cooldowngroup = static_cast<uint32_t>(-1);
|
||||
ungrouped.cooldown = 30.0f;
|
||||
CDClientManager::GetEntriesMutable<CDSkillBehaviorTable>()[395] = ungrouped;
|
||||
|
||||
tinyxml2::XMLDocument doc;
|
||||
Parse(doc, R"(<obj v="1"><skil sc="8:26.6559;78:30.7363;"/></obj>)");
|
||||
|
||||
Entity player(26, info);
|
||||
auto* const skills = player.AddComponent<SkillComponent>(-1);
|
||||
skills->LoadFromXml(doc);
|
||||
ASSERT_EQ(skills->GetCooldownGroups().size(), 2u);
|
||||
EXPECT_FLOAT_EQ(skills->GetCooldownGroups().at(8), 26.6559f);
|
||||
|
||||
skills->Update(26.7f); // group 8 runs out
|
||||
skills->StartCooldown(394);
|
||||
skills->StartCooldown(395); // no group: not saved
|
||||
skills->UpdateXml(doc);
|
||||
EXPECT_EQ(Print(doc), R"(<obj v="1"><skil sc="17:30;78:4.0363;"/></obj>)");
|
||||
|
||||
Entity reloaded(27, info);
|
||||
auto* const reloadedSkills = reloaded.AddComponent<SkillComponent>(-1);
|
||||
reloadedSkills->LoadFromXml(doc);
|
||||
EXPECT_EQ(reloadedSkills->GetCooldownGroups().size(), 2u);
|
||||
EXPECT_FLOAT_EQ(reloadedSkills->GetCooldownGroups().at(17), 30.0f);
|
||||
|
||||
CDClientManager::GetEntriesMutable<CDSkillBehaviorTable>().clear();
|
||||
}
|
||||
|
||||
// Saves without <skil> load with no cooldowns and gain <skil/>, as live wrote it with none running.
|
||||
TEST_F(CharacterSaveFieldsTests, SkillCooldownsMissingInOldSave) {
|
||||
tinyxml2::XMLDocument doc;
|
||||
Parse(doc, R"(<obj v="1"/>)");
|
||||
|
||||
Entity player(28, info);
|
||||
auto* const skills = player.AddComponent<SkillComponent>(-1);
|
||||
skills->LoadFromXml(doc);
|
||||
EXPECT_TRUE(skills->GetCooldownGroups().empty());
|
||||
skills->UpdateXml(doc);
|
||||
EXPECT_EQ(Print(doc), R"(<obj v="1"><skil/></obj>)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user