载入中...
搜索中...
未找到
PlacementWorld.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
4
5
13#include "common/Result.h"
14
15#include <memory>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20namespace eve::map {
21class TileLayer;
22}
23
24namespace eve::grid {
25struct GridConfig;
26}
27
28namespace eve::building {
29
30class Ghost;
31class AssemblyService;
32struct AssemblyState;
33
36public:
37 using ChannelMap = std::unordered_map<std::string, std::vector<int>>;
39 PlacementWorld(int width, int height, float cellSize = 32.f);
42
43 PlacementWorld(const PlacementWorld &) = delete;
45
47 void destroy();
48
50 std::string getId() const { return id_; }
52 void setId(const std::string &id) { id_ = id; }
53
55 int getWidth() const { return width_; }
57 int getHeight() const { return height_; }
59 float getCellSize() const;
61 void setCellSize(float s);
62
64 float getOriginX() const;
66 float getOriginY() const;
68 void setOrigin(float x, float y);
69
71 float getFloorHeight() const { return floorHeight_; }
73 void setFloorHeight(float height);
75 int getActiveLevel() const { return activeLevel_; }
77 void setActiveLevel(int level) { activeLevel_ = level; }
78
80 std::string getSnapMode() const { return snapMode_; }
82 void setSnapMode(const std::string &mode) { snapMode_ = mode; }
84 std::string getValidateRule() const { return validateRule_; }
86 void setValidateRule(const std::string &rule) { validateRule_ = rule; }
87
89 void setExtra(const std::string &key, const std::string &value);
91 std::string getExtra(const std::string &key, const std::string &fallback = {}) const;
92
93 // ---- Grid 配置(默认正交 2D,等价旧行为)----
95 const grid::GridConfig &getGrid() const;
97 grid::GridConfig &getGrid();
99 void setGridLayout(const std::string &layout);
101 std::string getGridLayoutName() const;
103 void setGridPlane(const std::string &plane);
105 std::string getGridPlaneName() const;
107 void setCellGap(float gapX, float gapY);
109 void setHexSideLength(float s);
111 void setStagger(const std::string &axis, const std::string &index);
114 void setGridFromLayer(map::TileLayer *layer);
116 bool hasGridFromLayer() const { return tileLayer_ != nullptr; }
117
118 // ---- 坐标换算 ----
122 int worldToCellX(float worldX) const;
125 int worldToCellY(float worldY) const;
127 float cellToWorldX(int cellX) const;
129 float cellToWorldY(int cellY) const;
132 void cellToWorldPlane(int cellX, int cellY, float &px, float &py) const;
135 void cellToWorld3D(int cellX, int cellY, float elevation, float &worldX, float &worldY,
136 float &worldZ) const;
138 float cellToWorld3DX(int cellX, int cellY, float elevation) const;
140 float cellToWorld3DY(int cellX, int cellY, float elevation) const;
142 float cellToWorld3DZ(int cellX, int cellY, float elevation) const;
145 void worldToCell3D(float worldX, float worldY, float worldZ, int &cellX, int &cellY) const;
147 int worldToCell3DX(float worldX, float worldY, float worldZ) const;
149 int worldToCell3DY(float worldX, float worldY, float worldZ) const;
150
152 void fillTerrain(int semantic);
154 void setTerrain(int cellX, int cellY, int semantic);
157 int getTerrain(int cellX, int cellY) const;
159 bool inBounds(int cellX, int cellY) const;
160
161 // ---- Tilemap 绑定 ----
163 void bindTileLayer(map::TileLayer *layer);
165 map::TileLayer *getTileLayer() const { return tileLayer_; }
167 void clearTileLayer();
169 void setTerrainGidMapJson(const std::string &json);
171 void setTerrainGid(int gid, int semantic);
173 void clearTerrainGidMap();
174
175 // ---- 占用查询 ----
179 int getOccupant(int cellX, int cellY) const;
181 int getOccupantInChannel(const std::string &channel, int cellX, int cellY) const;
183 int getOccupantAtLevel(const std::string &channel, int cellX, int cellY, int level) const;
186 int getAnyOccupant(int cellX, int cellY) const;
188 int getAnyOccupantAtLevel(int cellX, int cellY, int level) const;
190 bool isCellEmpty(int cellX, int cellY) const;
192 bool isCellEmptyInChannel(const std::string &channel, int cellX, int cellY) const;
194 int getEdgeOccupant(const std::string &channel, int cellX, int cellY,
195 const std::string &direction) const;
197 int getEdgeOccupantAtLevel(const std::string &channel, int cellX, int cellY,
198 const std::string &direction, int level) const;
200 bool isEdgeEmpty(const std::string &channel, int cellX, int cellY,
201 const std::string &direction) const;
203 int getCornerOccupant(const std::string &channel, int vertexX, int vertexY) const;
205 int getCornerOccupantAtLevel(const std::string &channel, int vertexX, int vertexY,
206 int level) const;
208 int getAnyCornerOccupantAtLevel(int vertexX, int vertexY, int level) const;
210 bool isCornerEmpty(const std::string &channel, int vertexX, int vertexY) const;
212 int getFreeOccupant(const std::string &channel, float worldX, float worldY) const;
214 int getFreeOccupantAtLevel(const std::string &channel, float worldX, float worldY,
215 int level) const;
217 int getEdgeConnectionMask(int instanceId) const;
219 std::string getEdgeVariant(int instanceId) const;
221 int getBuildingCount() const;
223 bool hasBuilding(int instanceId) const;
225 std::string getBuildingId(int instanceId) const;
227 int getBuildingCellX(int instanceId) const;
229 int getBuildingCellY(int instanceId) const;
231 float getBuildingWorldX(int instanceId) const;
233 float getBuildingWorldY(int instanceId) const;
236 float getBuildingWorldZ(int instanceId) const;
239 float getBuildingElevation(int instanceId) const;
241 int getBuildingLevel(int instanceId) const;
243 int getBuildingSupportCount(int instanceId) const;
245 int getBuildingSupportAt(int instanceId, int index) const;
247 int getBuildingDependentCount(int instanceId) const;
249 std::string getBuildingSurfaceId(int instanceId) const;
251 int64_t getBuildingSurfaceRevision(int instanceId) const;
253 float getBuildingSurfaceNormalX(int instanceId) const;
255 float getBuildingSurfaceNormalY(int instanceId) const;
257 float getBuildingSurfaceNormalZ(int instanceId) const;
259 std::string getBuildingChannel(int instanceId) const;
261 float getBuildingRotation(int instanceId) const;
263 std::string getBuildingProp(int instanceId, const std::string &key,
264 const std::string &fallback = {}) const;
266 void setBuildingProp(int instanceId, const std::string &key, const std::string &value);
268 bool buildingHasTag(int instanceId, const std::string &tag) const;
270 int getBuildingInstanceAt(int index) const;
272 int getEdgeCurveGroupCount() const { return static_cast<int>(edgeCurveGroups_.size()); }
274 EdgeCurveGroupId nextEdgeCurveGroupId() const { return {nextEdgeCurveGroupId_}; }
276 std::vector<EdgeCurveGroupId> edgeCurveGroupIds() const;
282 [[nodiscard]] eve::Result<EdgeCurveGroup> edgeCurveGroup(EdgeCurveGroupId id) const;
284 [[nodiscard]] eve::Result<EdgeCurveGroup> edgeCurveGroupForInstance(int instanceId) const;
285
287 bool canPlace(const std::string &buildingId, int cellX, int cellY, float rotationDeg = 0.f);
289 std::string canPlaceReason(const std::string &buildingId, int cellX, int cellY,
290 float rotationDeg = 0.f);
292 int placeAt(const std::string &buildingId, int cellX, int cellY, float rotationDeg = 0.f);
294 int placeAtWorld(const std::string &buildingId, float worldX, float worldY,
295 float rotationDeg = 0.f);
297 int placeAtWorld3D(const std::string &buildingId, float worldX, float worldY, float worldZ,
298 float rotationDeg = 0.f);
300 int placeGhost(Ghost *ghost);
302 int placeEdge(const std::string &buildingId, int cellX, int cellY,
303 const std::string &direction);
305 bool canPlaceEdge(const std::string &buildingId, int cellX, int cellY,
306 const std::string &direction);
308 std::string canPlaceEdgeReason(const std::string &buildingId, int cellX, int cellY,
309 const std::string &direction);
311 int placeCorner(const std::string &buildingId, int vertexX, int vertexY);
313 bool canPlaceCorner(const std::string &buildingId, int vertexX, int vertexY);
315 std::string canPlaceCornerReason(const std::string &buildingId, int vertexX, int vertexY);
317 int placeFree(const std::string &buildingId, float worldX, float worldY,
318 float elevation = 0.f, float rotationDeg = 0.f);
320 bool canPlaceFree(const std::string &buildingId, float worldX, float worldY);
322 std::string canPlaceFreeReason(const std::string &buildingId, float worldX, float worldY);
324 bool removeBuilding(int instanceId);
326 std::string canRemoveBuildingReason(int instanceId) const;
331 int removeBuildingCascade(int instanceId);
333 bool moveBuilding(int instanceId, int cellX, int cellY, float rotationDeg = -1.f);
335 void clearBuildings();
336
338 [[nodiscard]] std::unique_ptr<PlacementWorld> cloneState() const;
340 void swapState(PlacementWorld& candidate) noexcept;
341
342 // ---- 供 System 直接访问 ----
344 const std::vector<int> &occupancy() const { return occupancy_; }
346 std::vector<int> &occupancy() { return occupancy_; }
348 const std::vector<int> &terrain() const { return terrain_; }
350 std::vector<int> &terrain() { return terrain_; }
351 const std::unordered_map<std::string, std::vector<int>> &allChannels() const {
352 return allChannels_;
353 }
354 std::unordered_map<std::string, std::vector<int>> &allChannels() { return allChannels_; }
357 std::vector<int> &channelOccupancy(const std::string &channel);
359 std::vector<int> &channelOccupancy(const std::string &channel, int level);
360 const std::unordered_map<int, PlacedBuilding> &buildings() const { return buildings_; }
361 std::unordered_map<int, PlacedBuilding> &buildings() { return buildings_; }
362
363private:
364 friend class PlacementSystem;
365 friend class AssemblyService;
366
367 std::unique_ptr<AssemblyState> assemblyState_;
368 int instanceCounter_ = 0;
369
370 int terrainFromGid(int cellX, int cellY) const;
371 ChannelMap &edgeChannels(EdgeAxis axis, int level);
377 const ChannelMap *findEdgeChannels(EdgeAxis axis, int level) const;
378 ChannelMap &cornerChannels(int level);
384 const ChannelMap *findCornerChannels(int level) const;
385
386 std::string id_;
387 int width_ = 0;
388 int height_ = 0;
389 float floorHeight_ = 3.f;
390 int activeLevel_ = 0;
391 std::unique_ptr<grid::GridConfig> grid_;
392 std::string snapMode_ = "grid";
393 std::string validateRule_ = "default";
394 std::vector<int> occupancy_;
395 std::vector<int> terrain_;
396 std::vector<uint8_t> terrainOverrides_;
397 std::unordered_map<std::string, std::vector<int>> allChannels_;
398 std::unordered_map<std::string, std::vector<int>> horizontalEdgeChannels_;
399 std::unordered_map<std::string, std::vector<int>> verticalEdgeChannels_;
400 std::unordered_map<std::string, std::vector<int>> cornerChannels_;
401 std::unordered_map<int, ChannelMap> cellChannelsByLevel_;
402 std::unordered_map<int, ChannelMap> horizontalEdgesByLevel_;
403 std::unordered_map<int, ChannelMap> verticalEdgesByLevel_;
404 std::unordered_map<int, ChannelMap> cornersByLevel_;
405 std::unordered_map<int, PlacedBuilding> buildings_;
406 std::unordered_map<uint64_t, EdgeCurveGroup> edgeCurveGroups_;
407 uint64_t nextEdgeCurveGroupId_ = 1;
408 bool publishEvents_ = true;
409 std::unordered_map<std::string, std::string> extra_;
410 std::vector<int> instanceOrder_;
411
412 // tilemap 绑定(可空)
413 map::TileLayer *tileLayer_ = nullptr;
414 bool terrainBound_ = false;
415 std::unordered_map<int, int> terrainGidMap_;
416};
417
418} // namespace eve::building
double y
double x
const std::string & s
SQObjectPtr key
Definition ECS.cpp:155
int instanceId
#define EVENGINE_API_WORLD
Definition Export.h:109
std::string layout
float elevation
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
void(* destroy)(void *)
JointPoint2D axis
std::string id
int level
const std::string * tag
TileLayer * layer
Object::Ptr value
Move-only, checked operation results for the common layer.
RoadLaneDirection direction
uint32_t semantic
Share string attribute map operations through the Windows Foundation DLL.
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Canonical house composition, blueprint and persistence operations. @thread World/registry owner-threa...
EVENGINE_API_WORLD public API.
Definition Ghost.h:16
EVENGINE_API_WORLD public API.
格子型建筑放置世界(脚本可直接操作)。
int getWidth() const
尺寸 / 格子大小 / 原点。
int getActiveLevel() const
Floor used by compatibility placement/query APIs.
std::unordered_map< std::string, std::vector< int > > ChannelMap
~PlacementWorld()
Placement world.
void setValidateRule(const std::string &rule)
Sets the validate rule.
PlacementWorld(const PlacementWorld &)=delete
float getFloorHeight() const
Height between discrete floors along the grid plane normal.
const std::unordered_map< int, PlacedBuilding > & buildings() const
const std::vector< int > & terrain() const
Terrain.
const std::vector< int > & occupancy() const
Occupancy.
std::string getSnapMode() const
放置策略:吸附模式 / 校验规则。
std::string getId() const
世界 id(用于变更事件定位)。
std::string getValidateRule() const
Returns the validate rule.
bool hasGridFromLayer() const
True when grid from layer.
int getEdgeCurveGroupCount() const
Number of complete authoritative edge-curve groups in this world.
void setActiveLevel(int level)
Select the floor used by compatibility placement/query APIs.
map::TileLayer * getTileLayer() const
Returns the tile layer.
std::unordered_map< int, PlacedBuilding > & buildings()
void setId(const std::string &id)
Sets the id.
EdgeCurveGroupId nextEdgeCurveGroupId() const
Return the next monotonic curve-group identity without reserving it.
std::vector< int > & occupancy()
Occupancy.
void setSnapMode(const std::string &mode)
Sets the snap mode.
const std::unordered_map< std::string, std::vector< int > > & allChannels() const
int getHeight() const
Returns the height.
std::unordered_map< std::string, std::vector< int > > & allChannels()
std::vector< int > & terrain()
Terrain.
PlacementWorld & operator=(const PlacementWorld &)=delete
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:33
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
EdgeAxis
Canonical orientation of an edge in logical grid coordinates.
放置世界:格子占用(多通道)+ 地形语义 + 已放置建筑实例。 行为由 PlacementSystem 提供;本类暴露便于脚本绑定的薄封装方法。 坐标换算统一走 eve::grid(支持 rectang...
Strong world-local identity of one authoritative edge-curve group.
Grid shape, cell metrics, origin, and placement plane. Pure data; use GridProjection helpers for worl...
Definition GridConfig.h:34