19 return it ==
extra.end() ? fallback : it->second;
23std::unordered_map<std::string, TraitDefinition> &traitTable() {
24 static std::unordered_map<std::string, TraitDefinition>
t;
30 if (def.
id.empty())
return;
31 traitTable()[def.
id] = def;
35 auto &
t = traitTable();
37 return it ==
t.end() ? nullptr : &it->second;
50 if (
error) *
error = err.empty() ?
"invalid json" : err;
55 const auto parseOne = [&](
Value o) {
56 if (!o.isObject())
return;
58 def.
id = o.getString(
"id");
59 if (def.
id.empty())
return;
60 def.
tags = o.getStringArray(
"tags");
61 def.
extra = o.getStringMap(
"extra");
62 const Value traits = o.
get(
"traits");
63 for (
size_t i = 0; i < traits.
size(); ++i) {
66 spec.
kind =
t.getString(
"kind");
67 spec.
target =
t.getString(
"target");
68 spec.
value =
t.getDouble(
"value", 0.0);
69 if (spec.
kind.empty())
continue;
70 def.
traits.push_back(std::move(spec));
72 traitTable()[def.
id] = std::move(def);
76 for (
size_t i = 0; i <
root.size(); ++i) parseOne(
root.at(i));
77 }
else if (
root.isObject()) {
92bool matches(
const TraitSpec &spec,
const std::string &
target) {
return spec.target.empty() || spec.target ==
target; }
100 auto &ts = *actor->traits();
106 for (
const auto &spec : def->
traits) {
107 if (spec.kind !=
"paramRate" || spec.target.empty())
continue;
109 spec.value - 1.0, 0);
111 ts.active.push_back(std::move(inst));
116 if (!actor)
return false;
117 auto &ts = *actor->traits();
118 for (
auto it = ts.active.begin(); it != ts.active.end(); ++it) {
128 if (!actor)
return 0;
129 auto &ts = *actor->traits();
131 for (
auto it = ts.active.begin(); it != ts.active.end();) {
132 if (it->source ==
source) {
134 it = ts.active.erase(it);
144 if (!actor)
return 0;
145 auto &ts = *actor->traits();
147 for (
auto it = ts.active.begin(); it != ts.active.end();) {
148 if (it->traitId == traitId) {
150 it = ts.active.erase(it);
161 auto &ts = *actor->traits();
162 for (
const auto &inst : ts.active) {
169 if (!actor)
return false;
170 for (
const auto &inst : actor->traits()->active) {
171 if (inst.traitId == traitId)
return true;
179 if (!actor ||
index < 0 ||
size_t(
index) >= actor->traits()->active.size())
return 0;
180 return actor->traits()->active[size_t(
index)].instanceId;
184 if (!actor ||
index < 0 ||
size_t(
index) >= actor->traits()->active.size())
return {};
185 return actor->traits()->active[size_t(
index)].traitId;
189 if (!actor ||
index < 0 ||
size_t(
index) >= actor->traits()->active.size())
return {};
190 return actor->traits()->active[size_t(
index)].source;
194 if (!actor)
return 1.0;
195 double product = 1.0;
196 for (
const auto &inst : actor->traits()->active) {
199 for (
const auto &spec : def->
traits) {
200 if (spec.kind ==
"paramRate" && spec.target == param) product *= spec.value;
207 if (!actor)
return 1.0;
208 double product = 1.0;
209 for (
const auto &inst : actor->traits()->active) {
212 for (
const auto &spec : def->
traits) {
213 if (spec.kind ==
"elementRate" && matches(spec,
element)) product *= spec.value;
220 if (!actor)
return 1.0;
221 double product = 1.0;
222 for (
const auto &inst : actor->traits()->active) {
225 for (
const auto &spec : def->
traits) {
226 if (spec.kind ==
"stateRate" && matches(spec, stateId)) product *= spec.value;
233 if (!actor)
return false;
234 for (
const auto &inst : actor->traits()->active) {
237 for (
const auto &spec : def->
traits) {
238 if (spec.kind ==
"stateResist" && matches(spec, stateId))
return true;
245 if (!actor)
return 0.0;
247 for (
const auto &inst : actor->traits()->active) {
250 for (
const auto &spec : def->
traits) {
251 if (spec.kind ==
"exParam" && spec.target == exParam) sum += spec.value;
258 std::vector<std::string>
out;
259 if (!actor)
return out;
260 for (
const auto &inst : actor->traits()->active) {
263 for (
const auto &spec : def->
traits) {
264 if (spec.kind ==
"attackElement" && !spec.target.empty()) {
265 if (std::find(
out.begin(),
out.end(), spec.target) ==
out.end())
out.push_back(spec.target);
273 std::vector<std::string>
out;
274 if (!actor)
return out;
275 for (
const auto &inst : actor->traits()->active) {
278 for (
const auto &spec : def->
traits) {
279 if (spec.kind ==
"attackState" && !spec.target.empty()) {
280 if (std::find(
out.begin(),
out.end(), spec.target) ==
out.end())
out.push_back(spec.target);
288 if (!actor)
return 0.0;
290 for (
const auto &inst : actor->traits()->active) {
293 for (
const auto &spec : def->
traits) {
294 if (spec.kind ==
"attackSpeed") sum += spec.value;
301 if (!actor)
return 0;
303 for (
const auto &inst : actor->traits()->active) {
306 for (
const auto &spec : def->
traits) {
307 if (spec.kind ==
"attackTimes") sum +=
static_cast<int>(spec.value);
RPG's thin compatibility facade over the attributes module.
特征系统:施加/移除运行时特征,并提供耐性/特殊参数/攻击附加等查询。
特征(Trait):RPG Maker 式数据驱动的能力/特性模板与进程级注册表。
EVENGINE_API_FOUNDATION public API.
static Document parse(const std::string &text, std::string *error=nullptr)
Parse.
EVENGINE_API_FOUNDATION public API.
Value get(const char *key) const
Returns the value.
size_t size() const
Returns the size of .
Value at(size_t index) const
At.
static int removeAllModifiersBySource(RPGActor *actor, const std::string &source)
Compatibility all-attribute source removal facade.
static eve::Result< ModifierId > addModifier(RPGActor *actor, AttributeModifier modifier)
Add one canonical modifier to an actor.
属性 / 状态 / 技能三表合一的 ECS 实体。
static int count()
Returns the number of .
static bool remove(const std::string &id)
Remove a trait by id (compatibility facade returning whether it was present).
static void clear()
Clears .
static int loadFromJson(const std::string &json, std::string *error=nullptr)
从 JSON 数组/对象批量注册,返回成功数量。元素形如: { "id":"fire_resist", "traits":[{"kind":"elementRate",...
static void registerTrait(const TraitDefinition &def)
Registers trait.
static const TraitDefinition * find(const std::string &id)
Find a registered trait definition by id.
static int removeBySource(RPGActor *actor, const std::string &source)
移除该 actor 上所有来自 source 的特征;返回移除数。
static bool hasTrait(RPGActor *actor, const std::string &traitId)
是否已施加某特征。
static double getElementRate(RPGActor *actor, const std::string &element)
元素耐性乘积(1 = 无修正,0.5 = 减半)。
static int getAttackTimesAdd(RPGActor *actor)
攻击次数附加(整数加值)。
static double getExParam(RPGActor *actor, const std::string &exParam)
特殊参数加值(hit/evasion/critRate/critEvade/magicEvade/magicReflect/...)。
static double getAttackSpeed(RPGActor *actor)
攻击速度修正(加值)。
static std::string getSourceAt(RPGActor *actor, int index)
Returns the source at.
static void removeAll(RPGActor *actor)
移除全部特征。
static std::string getTraitIdAt(RPGActor *actor, int index)
Returns the trait id at.
static bool remove(RPGActor *actor, int instanceId)
按实例 id 移除(compatibility facade (脚本兼容门面);成功返回 true)。
static std::vector< std::string > getAttackStates(RPGActor *actor)
攻击附加状态列表。
static double getParamRate(RPGActor *actor, const std::string ¶m)
参数倍率乘积(如 attack → 1.5 表示 +50)。
static bool isStateResist(RPGActor *actor, const std::string &stateId)
是否免疫某状态(stateResist 命中;target 空 = 全部免疫)。
static int apply(RPGActor *actor, const std::string &traitId, const std::string &source="")
施加一个特征。
static int getCount(RPGActor *actor)
Returns the count.
static std::vector< std::string > getAttackElements(RPGActor *actor)
攻击附加元素列表。
static int getInstanceIdAt(RPGActor *actor, int index)
Returns the instance id at.
static double getStateRate(RPGActor *actor, const std::string &stateId)
状态施加率乘积(1 = 正常)。
static int removeByTrait(RPGActor *actor, const std::string &traitId)
移除该 actor 上所有 traitId 匹配的特征;返回移除数。
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
std::unordered_map< std::string, std::string > extra
std::vector< TraitSpec > traits
std::string getExtra(const std::string &key, const std::string &fallback={}) const
Returns the extra.
std::vector< std::string > tags
bool hasTag(const std::string &tag) const
True when tag.
std::string kind
"paramRate" | "elementRate" | "stateRate" | "stateResist" | "exParam" | "attackElement" | "attackStat...
std::string target
目标名(参数/元素/状态/exParam 名);部分 kind 可空。
double value
倍率或加值;攻击次数为整数加值,其它多为倍率。