载入中...
搜索中...
未找到
Settlement.cpp
浏览该文件的文档.
1#include "rpg/Settlement.h"
2
4
5#include <algorithm>
6
7namespace eve::rpg {
8
9double SettlementContext::get(const std::string &key, double fallback) const {
10 auto it = values.find(key);
11 return it == values.end() ? fallback : it->second;
12}
13
14void SettlementContext::set(const std::string &key, double value) { values[key] = value; }
15
16bool SettlementContext::has(const std::string &key) const { return values.find(key) != values.end(); }
17
18void SettlementContext::addTag(const std::string &tag) {
19 if (!hasTag(tag)) tags.push_back(tag);
20}
21
22bool SettlementContext::hasTag(const std::string &tag) const {
23 return std::find(tags.begin(), tags.end(), tag) != tags.end();
24}
25
26namespace {
27
28struct StageEntry {
29 std::string name;
30 int priority = 0;
31 bool enabled = true;
33};
34
35std::unordered_map<std::string, std::vector<StageEntry>> &pipelines() {
36 static std::unordered_map<std::string, std::vector<StageEntry>> p;
37 return p;
38}
39
40std::vector<StageEntry> *findPipeline(const std::string &pipeline) {
41 auto it = pipelines().find(pipeline);
42 return it == pipelines().end() ? nullptr : &it->second;
43}
44
45class CompatibilitySettlementPolicy final : public settlement::ISettlementPolicy {
46public:
47 eve::Result<void> validate(settlement::SettlementContext&) override { return eve::Result<void>::success(); }
48 eve::Result<void> sourceModifiers(settlement::SettlementContext&) override {
50 }
51 eve::Result<void> targetMitigation(settlement::SettlementContext&) override {
53 }
54 eve::Result<void> armorShield(settlement::SettlementContext&) override {
56 }
57 eve::Result<void> clamp(settlement::SettlementContext& context) override {
58 return context.setClampMax(context.magnitude());
59 }
60 eve::Result<settlement::PreparedApply> prepareApply(const settlement::SettlementContext&) override {
62 settlement::PreparedApply([] { return eve::Result<void>::success(); }, [] {}));
63 }
64};
65
66SubjectRef compatibilityTarget() {
68 bytes[0] = 0x52;
69 bytes[1] = 0x50;
70 bytes[2] = 0x47;
71 bytes[15] = 1;
73}
74
75StageEntry *findStage(std::vector<StageEntry> &stages, const std::string &name) {
76 auto it = std::find_if(stages.begin(), stages.end(),
77 [&](const StageEntry &s) { return s.name == name; });
78 return it == stages.end() ? nullptr : &(*it);
79}
80
81} // namespace
82
83void SettlementPipeline::registerStage(const std::string &pipeline, const std::string &stage,
84 int priority, Stage fn) {
85 auto &stages = pipelines()[pipeline];
86 if (StageEntry *existing = findStage(stages, stage)) {
87 existing->priority = priority;
88 existing->fn = std::move(fn);
89 existing->enabled = true;
90 return;
91 }
92 StageEntry entry;
93 entry.name = stage;
94 entry.priority = priority;
95 entry.enabled = true;
96 entry.fn = std::move(fn);
97 stages.push_back(std::move(entry));
98}
99
100bool SettlementPipeline::unregisterStage(const std::string &pipeline, const std::string &stage) {
101 auto *stages = findPipeline(pipeline);
102 if (!stages) return false;
103 size_t before = stages->size();
104 stages->erase(std::remove_if(stages->begin(), stages->end(),
105 [&](const StageEntry &s) { return s.name == stage; }),
106 stages->end());
107 return stages->size() != before;
108}
109
110bool SettlementPipeline::setStageEnabled(const std::string &pipeline, const std::string &stage,
111 bool enabled) {
112 auto *stages = findPipeline(pipeline);
113 if (!stages) return false;
114 StageEntry *s = findStage(*stages, stage);
115 if (!s) return false;
116 s->enabled = enabled;
117 return true;
118}
119
120bool SettlementPipeline::setStagePriority(const std::string &pipeline, const std::string &stage,
121 int priority) {
122 auto *stages = findPipeline(pipeline);
123 if (!stages) return false;
124 StageEntry *s = findStage(*stages, stage);
125 if (!s) return false;
126 s->priority = priority;
127 return true;
128}
129
130bool SettlementPipeline::hasStage(const std::string &pipeline, const std::string &stage) {
131 auto *stages = findPipeline(pipeline);
132 if (!stages) return false;
133 return findStage(*stages, stage) != nullptr;
134}
135
136int SettlementPipeline::stageCount(const std::string &pipeline) {
137 auto *stages = findPipeline(pipeline);
138 return stages ? int(stages->size()) : 0;
139}
140
141void SettlementPipeline::clearPipeline(const std::string &pipeline) { pipelines().erase(pipeline); }
142
143void SettlementPipeline::run(const std::string &pipeline, SettlementContext &ctx) {
144 auto *stages = findPipeline(pipeline);
145 if (!stages) return;
146
148 auto installed = canonical.addStage(
149 settlement::StageKind::SourceModifiers, "rpg.compatibility", 0,
150 [stages, &ctx](settlement::SettlementContext&) {
151 std::vector<StageEntry *> ordered;
152 ordered.reserve(stages->size());
153 for (auto &stage : *stages)
154 if (stage.enabled) ordered.push_back(&stage);
155 std::stable_sort(ordered.begin(), ordered.end(),
156 [](const StageEntry *left, const StageEntry *right) {
157 return left->priority < right->priority;
158 });
159 for (StageEntry *stage : ordered) {
160 if (ctx.cancelled) break;
161 if (stage->fn) stage->fn(ctx);
162 }
164 });
165 if (!installed) {
166 installed.ignore("RPG compatibility settlement stage installation");
167 return;
168 }
169
171 request.target = compatibilityTarget();
172 request.kind = ctx.kind.empty() ? "rpg.compatibility" : ctx.kind;
173 request.resource = "legacy.values";
174 request.magnitude = 0.0;
175 CompatibilitySettlementPolicy policy;
176 auto settled = canonical.settle(request, policy);
177 settled.ignore("RPG compatibility settlement execution");
178}
179
180} // namespace eve::rpg
double value
const std::string & s
int priority
glm::vec4 p[6]
const GltfImportRequest & request
std::uint32_t key
HexVec3 left
HexVec3 right
std::uint64_t bytes
std::string name
const std::string * tag
const VegetationPresetContext & context
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:537
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static SubjectRef fromPersistentId(PersistentId id) noexcept
Wrap a persistent identity without changing its bytes.
Definition SubjectRef.h:32
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:114
static bool unregisterStage(const std::string &pipeline, const std::string &stage)
Unregisters stage.
static void registerStage(const std::string &pipeline, const std::string &stage, int priority, Stage fn)
同名 stage 已存在则整体覆盖(fn/priority),并重置为 enabled。
static void run(const std::string &pipeline, SettlementContext &ctx)
按 priority 升序依次执行 pipeline 内已启用的阶段;ctx.cancelled 时提前终止。
static int stageCount(const std::string &pipeline)
Stage count.
static bool hasStage(const std::string &pipeline, const std::string &stage)
True when stage.
static bool setStagePriority(const std::string &pipeline, const std::string &stage, int priority)
Sets the stage priority.
static bool setStageEnabled(const std::string &pipeline, const std::string &stage, bool enabled)
Sets the stage enabled.
std::function< void(SettlementContext &)> Stage
Definition Settlement.h:65
static void clearPipeline(const std::string &pipeline)
Clears pipeline.
Mutable calculation frame visible to policy and custom stage code.
Definition Settlement.h:338
Deterministically ordered, composable settlement pipeline.
Definition Settlement.h:542
eve::Result< void > addStage(StageKind kind, std::string name, int priority, StageFunction function)
Add one named custom stage; duplicate names are rejected.
eve::Result< SettlementResult > settle(const SettlementRequest &request, ISettlementPolicy &policy, game_event::GameEventLog *events=nullptr) const
Execute one settlement transaction against a domain policy.
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
::eve::SubjectRef SubjectRef
Strong reference to a runtime subject.
Definition Targeting.h:76
void validate(const RuntimeTensor &v)
Validate.
StrongUuid< detail::PersistentIdTag > PersistentId
Stable instance identity for persistence, networking and process boundaries.
Definition Identity.h:254
SettlementPipeline::Stage fn
bool enabled
Domain-neutral, deterministic settlement pipeline.
结算上下文:通用的读写数据袋,字段完全由调用方与各阶段自行约定。
Definition Settlement.h:35
std::unordered_map< std::string, double > values
Definition Settlement.h:41
void addTag(const std::string &tag)
Adds tag.
void set(const std::string &key, double value)
Sets the value.
double get(const std::string &key, double fallback=0.0) const
Returns the value.
Definition Settlement.cpp:9
bool has(const std::string &key) const
True when active.
std::string kind
自由字符串,如 "damage" / "heal",供阶段内部分支使用
Definition Settlement.h:38
std::vector< std::string > tags
Definition Settlement.h:42
bool hasTag(const std::string &tag) const
True when tag.
Domain-neutral input to one settlement operation.
Definition Settlement.h:107