载入中...
搜索中...
未找到
Settlement.cpp
浏览该文件的文档.
1#include "rpg/Settlement.h"
2
3#include <algorithm>
4
5namespace eve::rpg {
6
7double SettlementContext::get(const std::string &key, double fallback) const {
8 auto it = values.find(key);
9 return it == values.end() ? fallback : it->second;
10}
11
12void SettlementContext::set(const std::string &key, double value) { values[key] = value; }
13
14bool SettlementContext::has(const std::string &key) const { return values.find(key) != values.end(); }
15
16void SettlementContext::addTag(const std::string &tag) {
17 if (!hasTag(tag)) tags.push_back(tag);
18}
19
20bool SettlementContext::hasTag(const std::string &tag) const {
21 return std::find(tags.begin(), tags.end(), tag) != tags.end();
22}
23
24namespace {
25
26struct StageEntry {
27 std::string name;
28 int priority = 0;
29 bool enabled = true;
31};
32
33std::unordered_map<std::string, std::vector<StageEntry>> &pipelines() {
34 static std::unordered_map<std::string, std::vector<StageEntry>> p;
35 return p;
36}
37
38std::vector<StageEntry> *findPipeline(const std::string &pipeline) {
39 auto it = pipelines().find(pipeline);
40 return it == pipelines().end() ? nullptr : &it->second;
41}
42
43StageEntry *findStage(std::vector<StageEntry> &stages, const std::string &name) {
44 auto it = std::find_if(stages.begin(), stages.end(),
45 [&](const StageEntry &s) { return s.name == name; });
46 return it == stages.end() ? nullptr : &(*it);
47}
48
49} // namespace
50
51void SettlementPipeline::registerStage(const std::string &pipeline, const std::string &stage,
52 int priority, Stage fn) {
53 auto &stages = pipelines()[pipeline];
54 if (StageEntry *existing = findStage(stages, stage)) {
55 existing->priority = priority;
56 existing->fn = std::move(fn);
57 existing->enabled = true;
58 return;
59 }
60 StageEntry entry;
61 entry.name = stage;
62 entry.priority = priority;
63 entry.enabled = true;
64 entry.fn = std::move(fn);
65 stages.push_back(std::move(entry));
66}
67
68bool SettlementPipeline::unregisterStage(const std::string &pipeline, const std::string &stage) {
69 auto *stages = findPipeline(pipeline);
70 if (!stages) return false;
71 size_t before = stages->size();
72 stages->erase(std::remove_if(stages->begin(), stages->end(),
73 [&](const StageEntry &s) { return s.name == stage; }),
74 stages->end());
75 return stages->size() != before;
76}
77
78bool SettlementPipeline::setStageEnabled(const std::string &pipeline, const std::string &stage,
79 bool enabled) {
80 auto *stages = findPipeline(pipeline);
81 if (!stages) return false;
82 StageEntry *s = findStage(*stages, stage);
83 if (!s) return false;
84 s->enabled = enabled;
85 return true;
86}
87
88bool SettlementPipeline::setStagePriority(const std::string &pipeline, const std::string &stage,
89 int priority) {
90 auto *stages = findPipeline(pipeline);
91 if (!stages) return false;
92 StageEntry *s = findStage(*stages, stage);
93 if (!s) return false;
94 s->priority = priority;
95 return true;
96}
97
98bool SettlementPipeline::hasStage(const std::string &pipeline, const std::string &stage) {
99 auto *stages = findPipeline(pipeline);
100 if (!stages) return false;
101 return findStage(*stages, stage) != nullptr;
102}
103
104int SettlementPipeline::stageCount(const std::string &pipeline) {
105 auto *stages = findPipeline(pipeline);
106 return stages ? int(stages->size()) : 0;
107}
108
109void SettlementPipeline::clearPipeline(const std::string &pipeline) { pipelines().erase(pipeline); }
110
111void SettlementPipeline::run(const std::string &pipeline, SettlementContext &ctx) {
112 auto *stages = findPipeline(pipeline);
113 if (!stages) return;
114
115 std::vector<StageEntry *> ordered;
116 ordered.reserve(stages->size());
117 for (auto &s : *stages)
118 if (s.enabled) ordered.push_back(&s);
119 std::stable_sort(ordered.begin(), ordered.end(),
120 [](const StageEntry *a, const StageEntry *b) { return a->priority < b->priority; });
121
122 for (StageEntry *s : ordered) {
123 if (ctx.cancelled) break;
124 if (s->fn) s->fn(ctx);
125 }
126}
127
128} // namespace eve::rpg
int priority
std::string value
uint32_t a
uint32_t b
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
bool enabled
uint32_t s
Definition Weather.cpp:28
static bool unregisterStage(const std::string &pipeline, const std::string &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)
static bool hasStage(const std::string &pipeline, const std::string &stage)
static bool setStagePriority(const std::string &pipeline, const std::string &stage, int priority)
static bool setStageEnabled(const std::string &pipeline, const std::string &stage, bool enabled)
std::function< void(SettlementContext &)> Stage
Definition Settlement.h:52
static void clearPipeline(const std::string &pipeline)
RPG 模块入口:属性 / 效果 / 状态 / 技能 / 结算五套系统的脚本绑定与帧调度点。
结算上下文:通用的读写数据袋,字段完全由调用方与各阶段自行约定。
Definition Settlement.h:33
std::unordered_map< std::string, double > values
Definition Settlement.h:39
void addTag(const std::string &tag)
void set(const std::string &key, double value)
double get(const std::string &key, double fallback=0.0) const
Definition Settlement.cpp:7
bool has(const std::string &key) const
std::vector< std::string > tags
Definition Settlement.h:40
bool hasTag(const std::string &tag) const