11using namespace assembly_detail;
13void knownFields(
const Value&
value, std::initializer_list<const char*> names) {
14 if (!
value.isObject())
throw std::runtime_error(
"Expected schema object");
16 if (std::none_of(names.begin(), names.end(), [&](
const char*
name) { return key == name; }))
17 throw std::runtime_error(
"Unknown core field: " +
key);
20void knownInstance(
const Value&
value) {
22 if (!
value.isObject())
throw std::runtime_error(
"Expected instance object");
24 const auto* expected =
shape.find(
key);
25 const auto* actual =
value.find(
key);
26 if (!expected || (expected->type() != actual->type() && !(expected->isDouble() && actual->isInt64())))
27 throw std::runtime_error(
"Unknown or incorrectly typed instance field: " +
key);
30void knownCurve(
const Value&
value) {
31 knownFields(
value, {
"groupId",
"buildingId",
"level",
"subdivisions",
"controlPoints",
"surfaceProviderName",
32 "surfaceId",
"surfaceRevision",
"surfaceSamples",
"instances"});
34 for (
const auto& instance : *
values->getIf<
Value::
Array>()) knownInstance(instance);
37 const auto*
p =
v.find(
key);
38 if (!
p)
throw std::runtime_error(std::string(
"Missing field: ") +
key);
41int integer(
const Value&
v,
int min = std::numeric_limits<int>::min(),
int max = std::numeric_limits<int>::max()) {
42 if (!
v.isInt64() ||
v.asInt() <
min ||
v.asInt() >
max)
throw std::runtime_error(
"Integer outside schema range");
43 return int(
v.asInt());
46 if (!
v.isNumeric())
throw std::runtime_error(
"Expected number");
47 double n =
v.isInt64() ? double(
v.asInt()) :
v.asDouble();
48 if (!std::isfinite(
n) || std::abs(
n) > 1e10)
throw std::runtime_error(
"Invalid finite number");
51const std::string&
text(
const Value&
v) {
52 if (!
v.isString())
throw std::runtime_error(
"Expected string");
57 if (!
a)
throw std::runtime_error(
"Expected array");
60void schema(
const Value&
v,
const char*
id) {
62 throw std::runtime_error(
"Unsupported schema/version; no implicit migration");
66 auto id = T::parse(
text(
v));
67 if (!
id ||
id->isNil())
throw std::runtime_error(
"Invalid persistent identity");
71Value strings(
const Map& map) {
73 for (
const auto& [k,
v] : map) o[k] =
v;
79 for (
const auto&
p :
b.parts)
81 for (
const auto&
c :
b.curves) curves.emplace_back(codec::edgeCurveValue(
c.
group,
c.instances));
84 {
"id",
b.id.format()},
85 {
"assetVersion", int64_t(
b.version)},
86 {
"cellSize",
b.cellSize},
87 {
"floorHeight",
b.floorHeight},
88 {
"parts", std::move(
parts)},
89 {
"curves", std::move(curves)},
90 {
"definitions",
b.definitions}};
91 auto check = decodeBlueprint(
v);
92 if (!check.ok())
return fail<Value>(
"Cannot encode invalid blueprint");
97 knownFields(
v, {
"schema",
"version",
"id",
"assetVersion",
"cellSize",
"floorHeight",
"parts",
"curves",
99 schema(
v,
"eve.building.blueprint");
102 b.version = uint64_t(integer(
required(
v,
"assetVersion"), 1));
105 if (
b.cellSize <= 0 ||
b.floorHeight <= 0)
throw std::runtime_error(
"Invalid blueprint scale");
111 for (
const auto&
p : array(
required(
v,
"parts"))) {
112 knownFields(
p, {
"key",
"instance"});
114 int key = integer(
required(
p,
"key"), 1, std::numeric_limits<int>::max() - 1);
115 auto part = codec::parseInstance(
required(
p,
"instance"));
116 const bool partOk = part.ok();
117 if (!keys.insert(
key).second || !partOk || part.value().instanceId !=
key ||
118 !part.value().garrison.empty() || !
b.definitions.find(part.value().buildingId))
119 throw std::runtime_error(
"Invalid blueprint part or external entity reference");
120 b.parts.push_back({{uint32_t(
key)}, std::move(part).takeValue()});
122 if (
b.parts.empty() ||
b.parts.size() > 100000)
123 throw std::runtime_error(
"Blueprint must contain 1..100000 parts");
124 std::set<int> curveMembers;
125 for (
const auto&
c : array(
required(
v,
"curves"))) {
127 auto curve = codec::parseEdgeCurve(
c);
128 if (!
curve.ok())
throw std::runtime_error(
"Invalid curve payload");
129 for (
const auto&
p :
curve.value().instances) {
130 auto found = std::find_if(
b.parts.begin(),
b.parts.end(),
131 [&](
const auto& part) { return part.local.instanceId == p.instanceId; });
132 if (
found ==
b.parts.end() || !curveMembers.insert(
p.instanceId).second ||
133 codec::instanceValue(
found->local) != codec::instanceValue(
p))
134 throw std::runtime_error(
"Curve membership differs from blueprint parts");
136 b.curves.push_back(std::move(
curve).takeValue());
139 }
catch (
const std::exception& e) {
140 return fail<BuildingBlueprint>(e.what());
145 if (!checked.ok())
return fail<Value>(
"World grid is not serializable by house schema");
147 for (
int id :
w.instanceOrder_) {
148 auto it =
w.buildings_.find(
id);
149 if (it ==
w.buildings_.end())
return fail<Value>(
"Invalid world instance order");
150 instances.emplace_back(codec::instanceValue(codec::snapshot(it->second)));
152 for (
int y = 0;
y <
w.height_; ++
y)
153 for (
int x = 0;
x <
w.width_; ++
x)
terrain.emplace_back(
w.getTerrain(
x,
y));
154 for (
auto id :
w.edgeCurveGroupIds()) {
155 const auto&
g =
w.edgeCurveGroups_.at(
id.
value);
156 std::vector<BuildingInstanceSnapshot>
members;
157 for (
int member :
g.instanceIds) {
158 auto it =
w.buildings_.find(
member);
159 if (it ==
w.buildings_.end())
return fail<Value>(
"Dangling curve member");
160 members.push_back(codec::snapshot(it->second));
162 curves.emplace_back(codec::edgeCurveValue(
g,
members));
164 for (
const auto& [
id,
a] :
w.assemblyState_->records) {
168 {
"key", int64_t(
key)}, {
"record",
member}, {
"persistentId",
id.child(std::to_string(
key)).format()}});
170 {
"blueprint",
a.blueprint.format()},
171 {
"nextPartKey", int64_t(
a.nextPartKey)},
172 {
"blueprintVersion", int64_t(
a.blueprintVersion)},
174 {
"members", std::move(
members)}});
180 {
"height",
w.height_},
181 {
"cellSize",
w.getCellSize()},
182 {
"plane",
w.getGridPlaneName()},
183 {
"originX",
w.getOriginX()},
184 {
"originY",
w.getOriginY()},
185 {
"floorHeight",
w.floorHeight_},
186 {
"activeLevel",
w.activeLevel_},
187 {
"snapMode",
w.snapMode_},
188 {
"validateRule",
w.validateRule_},
189 {
"extra", strings(
w.extra_)},
190 {
"nextInstance",
w.instanceCounter_},
191 {
"nextCurve", int64_t(
w.nextEdgeCurveGroupId_)},
192 {
"terrain", std::move(
terrain)},
193 {
"instances", std::move(instances)},
194 {
"curves", std::move(curves)},
195 {
"assemblies", std::move(houses)},
199Result<std::unique_ptr<PlacementWorld>> AssemblyService::decodeWorld(
const Value&
v) {
202 v, {
"schema",
"version",
"worldId",
"width",
"height",
"cellSize",
"plane",
203 "originX",
"originY",
"floorHeight",
"activeLevel",
"snapMode",
"validateRule",
"extra",
204 "nextInstance",
"nextCurve",
"terrain",
"instances",
"curves",
"assemblies",
"definitions"});
205 schema(
v,
"eve.building.world");
207 if (!defs.ok())
throw std::runtime_error(
"World definitions missing or changed");
209 if (int64_t(
width) *
height > 4000000)
throw std::runtime_error(
"World exceeds cell budget");
211 if (
size <= 0)
throw std::runtime_error(
"Invalid cell size");
213 w->publishEvents_ =
false;
216 if (plane !=
"xz" && plane !=
"xy")
throw std::runtime_error(
"Invalid plane");
217 w->setGridPlane(plane);
220 if (
w->floorHeight_ <= 0)
throw std::runtime_error(
"Invalid floor height");
224 const auto& extra =
required(
v,
"extra");
225 if (!extra.isObject())
throw std::runtime_error(
"Invalid extras");
228 if (
terrain.size() !=
size_t(
width) *
height)
throw std::runtime_error(
"Terrain dimensions mismatch");
230 w->terrainOverrides_.assign(
terrain.size(), 1);
232 const auto& nextCurve =
required(
v,
"nextCurve");
233 if (!nextCurve.isInt64() || nextCurve.asInt() <= 0)
throw std::runtime_error(
"Invalid curve sequence");
234 w->nextEdgeCurveGroupId_ = uint64_t(nextCurve.asInt());
237 auto p = codec::parseInstance(
record);
238 if (!
p.ok())
throw std::runtime_error(
"Invalid instance payload");
239 int id =
p.value().instanceId;
240 if (
id >
w->instanceCounter_ || !
required(
v,
"definitions").find(
p.value().buildingId) ||
241 !
w->buildings_.emplace(
id, codec::runtime(
p.value())).second)
242 throw std::runtime_error(
"Duplicate/unpinned instance record");
243 w->instanceOrder_.push_back(
id);
247 auto c = codec::parseEdgeCurve(
record);
248 if (!
c.ok())
throw std::runtime_error(
"Invalid curve");
250 for (
const auto&
p :
payload.instances) {
251 auto it =
w->buildings_.find(
p.instanceId);
252 if (it ==
w->buildings_.end() ||
253 codec::instanceValue(codec::snapshot(it->second)) != codec::instanceValue(
p))
254 throw std::runtime_error(
"Curve member payload mismatch");
256 if (
payload.group.id.value >=
w->nextEdgeCurveGroupId_ ||
257 !
w->edgeCurveGroups_.emplace(
payload.group.id.value,
payload.group).second)
258 throw std::runtime_error(
"Duplicate curve record");
261 knownFields(
record, {
"id",
"blueprint",
"blueprintVersion",
"frame",
"members",
"nextPartKey"});
268 if (!
pose.ok())
throw std::runtime_error(
"Invalid house frame");
269 a.frame =
pose.value();
271 knownFields(
m, {
"key",
"record",
"persistentId"});
275 throw std::runtime_error(
"Invalid persistent part identity");
277 if (!
w->assemblyState_->records.emplace(
a.id,
a).second)
278 throw std::runtime_error(
"Duplicate house identity");
281 auto valid = validateFinal(*
w);
283 return fail<std::unique_ptr<PlacementWorld>>(
"Saved world failed final-state validation",
285 return Result<std::unique_ptr<PlacementWorld>>::success(std::move(
w));
286 }
catch (
const std::exception& e) {
287 return fail<std::unique_ptr<PlacementWorld>>(e.what());
291 auto parsed = decodeWorld(
v);
293 w.swapState(*parsed.value());
294 if (
w.publishEvents_) {
298 PlacementSystem::pushEvent(std::move(e));
306 return restoreWorld(
w, desired);
std::vector< eve::artifact::PartView > parts
eve::EntitySpatialPose pose
ScriptComponentRecord record
Module-neutral grid topology and sizing (no building/map dependency).
std::set< std::string > values
AbilityActivationGroup group
float(ui::Theme::* member)[4]
std::map< std::string, std::span< const std::uint8_t > > members
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
Process-local slot identity tagged by the registry that owns it.
std::map< std::string, Value > Object
std::vector< Value > Array
static Result< Value > encodeBlueprint(const BuildingBlueprint &blueprint)
Encode a blueprint in the versioned canonical schema. @cost Copies and validates all parts and curve ...
EVENGINE_API_FOUNDATION public API.
Value poseValue(AssemblyPose p)
Value definitionsFor(const PlacementWorld &w)
Result< AssemblyPose > parsePose(const Value &v)
Result< void > checkDefinitions(const Value &value)
Result< void > checkGrid(const PlacementWorld &world)
Value instanceValue(const BuildingInstanceSnapshot &instance)
Encode a complete owning instance; pure, caller-thread, linear in instance metadata.
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
void keys(const Value &value, std::initializer_list< const char * > names)
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Versioned house recipe; admission validates all fields before use. @ownership Owns local parts,...
一次成功放置变更的事件(供脚本 poll / C++ hook)。
std::string action
place / remove / move / rotate