载入中...
搜索中...
未找到
WorldState.cpp
浏览该文件的文档.
1#include "rpg/WorldState.h"
2
3#include "rpg/GameState.h"
4
5#include <string>
6
7namespace eve::rpg {
8namespace {
9
10constexpr std::size_t maxIdBytes = 256;
11
12bool validId(std::string_view id) {
13 if (id.empty() || id.size() > maxIdBytes) return false;
14 for (unsigned char character : id)
15 if (character < 0x20 || character == 0x7f) return false;
16 return true;
17}
18
19std::string scope(std::string_view mapId) { return "world.object:" + std::string(mapId); }
20
21} // namespace
22
23bool WorldState::isObjectConsumed(std::string_view mapId, std::string_view objectId) const {
24 if (!gameState_ || !validId(mapId) || !validId(objectId)) return false;
25 const std::string owningScope = scope(mapId);
26 const std::string owningObjectId(objectId);
27 return gameState_->hasSelfVariable(owningScope, owningObjectId) &&
28 gameState_->getSelfVariable(owningScope, owningObjectId) != 0.0;
29}
30
31eve::Result<void> WorldState::consumeObject(std::string_view mapId, std::string_view objectId) {
32 if (!validId(mapId)) return eve::Result<void>::failure(eve::Diagnostic::error(
33 eve::DiagnosticCode::InvalidArgument, "world-state IDs must be non-empty, bounded, and control-free",
34 "mapId", {}, "rpg.world-state"));
35 if (!validId(objectId)) return eve::Result<void>::failure(eve::Diagnostic::error(
36 eve::DiagnosticCode::InvalidArgument, "world-state IDs must be non-empty, bounded, and control-free",
37 "objectId", {}, "rpg.world-state"));
38 if (isObjectConsumed(mapId, objectId))
40 gameState_->setSelfVariable(scope(mapId), std::string(objectId), 1.0);
42}
43
44eve::Result<void> WorldState::resetObject(std::string_view mapId, std::string_view objectId) {
45 if (!validId(mapId)) return eve::Result<void>::failure(eve::Diagnostic::error(
46 eve::DiagnosticCode::InvalidArgument, "world-state IDs must be non-empty, bounded, and control-free",
47 "mapId", {}, "rpg.world-state"));
48 if (!validId(objectId)) return eve::Result<void>::failure(eve::Diagnostic::error(
49 eve::DiagnosticCode::InvalidArgument, "world-state IDs must be non-empty, bounded, and control-free",
50 "objectId", {}, "rpg.world-state"));
51 if (!isObjectConsumed(mapId, objectId))
53 gameState_->setSelfVariable(scope(mapId), std::string(objectId), 0.0);
55}
56
57} // namespace eve::rpg
全局游戏状态:开关(bool)、变量(数字)、独立变量(按作用域)。
JobScope scope
std::string id
Definition PlayHost.cpp:108
float size
Definition TreeMesh.cpp:156
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
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 Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
double getSelfVariable(const std::string &scope, const std::string &name) const
Returns the self variable.
Definition GameState.cpp:75
bool hasSelfVariable(const std::string &scope, const std::string &name) const
True when self variable.
Definition GameState.cpp:82
void setSelfVariable(const std::string &scope, const std::string &name, double value)
独立变量:scope(如 "map:1:event:3")下按名字存数字。
Definition GameState.cpp:71
eve::Result< void > resetObject(std::string_view mapId, std::string_view objectId)
Idempotently clear the consumed fact for one stable map object.
bool isObjectConsumed(std::string_view mapId, std::string_view objectId) const
Query whether a stable object has been consumed in one map.
eve::Result< void > consumeObject(std::string_view mapId, std::string_view objectId)
Idempotently publish the consumed fact for one stable map object.
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。