载入中...
搜索中...
未找到
RpgStateProvider.cpp
浏览该文件的文档.
1#include "rpg/RpgState.h"
2
3#include "common/Capability.h"
5#include "rpg/RPGActor.h"
6#include "rpg/SkillSystem.h"
7#include "rpg/SkillTypes.h"
8
9#include <algorithm>
10#include <string>
11#include <utility>
12
13namespace eve::rpg {
14namespace {
15
16double toNumber(const Value& v) { return v.isInt64() ? double(v.asInt()) : v.asDouble(); }
17
19class RpgStateProvider : public eve::caps::IStateProvider {
20public:
21 const char* stateKind() const override { return "rpg"; }
22
23 Result<Value> capture() override { return captureRpgState(); }
24 Result<void> restore(const Value& in) override { return restoreRpgState(in); }
25 Result<void> resetToDefaults() override { return resetRpgStateResult(); }
26};
27
28struct Register {
29 Register() {
30 static RpgStateProvider provider;
31 eve::cap::addListener<eve::caps::IStateProvider>(&provider);
32 }
33} g_register;
34
35} // namespace
36
38 Value::Array actors;
39 for (RPGActor* actor : RPGActor::liveActors()) {
41 for (const auto& kv : actor->skills()->known) {
42 skills.emplace(kv.first, Value::number(kv.second.cooldownRemaining));
43 }
44 Value::Object casting;
45 const CastingState& c = actor->skills()->casting;
46 casting.emplace("active", Value::boolean(c.active));
47 casting.emplace("skillId", Value::string(c.skillId));
48 casting.emplace("remaining", Value::number(c.remaining));
49 casting.emplace("totalCastTime", Value::number(c.totalCastTime));
50 actors.emplace_back(
51 Value::Object{{"skills", Value(std::move(skills))}, {"casting", Value(std::move(casting))}});
52 }
53 return Result<Value>::success(Value(std::move(actors)));
54}
55
57 if (!in.isArray()) {
59 Diagnostic::error(DiagnosticCode::InvalidArgument, "rpg: expected array of actors"));
60 }
61 const std::vector<RPGActor*>& actors = RPGActor::liveActors();
62 if (in.arraySize() != actors.size())
64 Diagnostic::error(DiagnosticCode::Conflict, "rpg: actor count changed during reload"));
65 struct Pending {
66 std::vector<std::pair<std::string, float>> cooldowns;
67 CastingState casting;
68 };
69 std::vector<Pending> pending(in.arraySize());
70 for (size_t i = 0; i < in.arraySize(); ++i) {
71 const Value& entry = in.at(i);
72 if (!entry.isObject()) {
74 Diagnostic::error(DiagnosticCode::InvalidArgument, "rpg: actor state is not an object"));
75 }
76 RPGActor* actor = actors[i];
77 if (const Value* skills = entry.find("skills"); skills && skills->isObject()) {
78 for (const auto& key : skills->keys()) {
79 const Value* v = skills->find(key);
80 if (!v || !v->isNumeric()) continue;
81 if (SkillSystem::knows(actor, key)) {
82 pending[i].cooldowns.emplace_back(key, static_cast<float>(toNumber(*v)));
83 }
84 }
85 }
86 if (const Value* c = entry.find("casting"); c && c->isObject()) {
87 const Value* active = c->find("active");
88 if (active && active->isBool() && active->asBool()) {
89 const Value* skillId = c->find("skillId");
90 const std::string id = skillId && skillId->isString() ? skillId->asString() : "";
91 if (!id.empty() && SkillSystem::knows(actor, id)) {
92 pending[i].casting.active = true;
93 pending[i].casting.skillId = id;
94 if (const Value* r = c->find("remaining"); r && r->isNumeric())
95 pending[i].casting.remaining = static_cast<float>(toNumber(*r));
96 if (const Value* t = c->find("totalCastTime"); t && t->isNumeric())
97 pending[i].casting.totalCastTime = static_cast<float>(toNumber(*t));
98 }
99 }
100 }
101 }
102 for (size_t i = 0; i < actors.size(); ++i) {
103 for (const auto& [skillId, cooldown] : pending[i].cooldowns)
104 SkillSystem::setCooldownRemaining(actors[i], skillId, cooldown);
105 actors[i]->skills()->casting = std::move(pending[i].casting);
106 }
107 return Result<void>::success();
108}
109
114
116 auto captured = captureRpgState();
117 if (!captured.ok()) return false;
118 out = toLegacyStateValue(std::move(captured).takeValue());
119 return true;
120}
121
122bool restoreRpgState(const StateValue& in, std::string* err) {
124 if (!result.ok()) {
125 if (err) *err = result.diagnostics().front().message();
126 return false;
127 }
128 return true;
129}
130
133 if (!result.ok()) return false;
134 return true;
135}
136
137} // namespace eve::rpg
bool & active
std::vector< QuestEvent > pending
SQObjectPtr key
Definition ECS.cpp:155
double r
float v
std::int32_t c
std::string id
Poco::JSON::Object::Ptr result
float t
std::vector< std::string > skills
ScriptStructure out
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
std::map< std::string, Value > Object
Definition Value.h:34
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
std::vector< Value > Array
Definition Value.h:33
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:119
Runtime-state serialization for state hot reload.
EVENGINE_API_FOUNDATION public API.
Definition Json.h:39
bool isObject() const
True when object.
Definition Json.cpp:332
size_t size() const
Returns the size of .
Definition Json.cpp:352
bool isArray() const
True when array.
Definition Json.cpp:333
Value at(size_t index) const
At.
Definition Json.cpp:359
bool isString() const
True when string.
Definition Json.cpp:331
std::string asString(const std::string &fallback={}) const
As string.
Definition Json.cpp:412
属性 / 状态 / 技能三表合一的 ECS 实体。
Definition RPGActor.h:34
static const std::vector< RPGActor * > & liveActors()
返回当前 Table 中所有存活的 RPGActor(含派生类)。 StatusSystem::update / SkillSystem::update 用它遍历所有 actor 逐帧推进。
Definition RPGActor.cpp:27
static void cancelCast(RPGActor *actor)
打断当前读条(不退还已扣除的消耗/冷却)。
static void setCooldownRemaining(RPGActor *actor, const std::string &skillId, float seconds)
Sets the cooldown remaining.
static bool knows(RPGActor *actor, const std::string &skillId)
Knows.
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
EVENGINE_API_PLATFORM bool resetRpgState()
Resets rpg state.
EVENGINE_API_PLATFORM Result< Value > captureRpgState()
Capture RPG hot-reload state as an owning canonical value.
EVENGINE_API_PLATFORM Result< void > restoreRpgState(const Value &state)
Validate every actor snapshot before publishing any RPG hot-reload state.
EVENGINE_API_PLATFORM Result< void > resetRpgStateResult()
Cancel all in-flight RPG casts.
StateValue toLegacyStateValue(const Value &value)
Project a canonical value into the legacy reload snapshot format.
Value toCanonicalStateValue(const StateValue &value)
Copy a legacy reload tree into the canonical owning value protocol.
施法状态:同一时间每个 actor 只有一个进行中的引导/读条(可按需扩展为多槽)。
Definition SkillTypes.h:16