载入中...
搜索中...
未找到
HexSphereMap.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "common/Result.h"
8#include "hexmap/HexCell.h"
9#include "hexmap/HexMetrics.h"
10#include "hexmap/HexNoise.h"
12
13#include <cstdint>
14#include <vector>
15
16namespace eve::hexmap {
17
59public:
61 static constexpr float kDefaultElevationRatio = 0.006f;
62
64 HexSphereMap() = default;
65
80 [[nodiscard]] Result<void> reset(std::int32_t subdivision, float radius, std::uint32_t seed);
81
82 // --- shape and identity -------------------------------------------------
83
85 [[nodiscard]] bool empty() const noexcept { return cells_.empty(); }
87 [[nodiscard]] std::int32_t subdivision() const noexcept { return topology_.subdivision(); }
89 [[nodiscard]] std::int32_t frequency() const noexcept { return topology_.frequency(); }
91 [[nodiscard]] std::int32_t cellCount() const noexcept { return static_cast<std::int32_t>(cells_.size()); }
93 [[nodiscard]] std::int32_t pentagonCount() const noexcept { return topology_.pentagonCount(); }
95 [[nodiscard]] float sphereRadius() const noexcept { return sphereRadius_; }
104 [[nodiscard]] float cellSpacing() const noexcept;
106 [[nodiscard]] float elevationStep() const noexcept { return elevationStep_; }
113 void setElevationStep(float step) noexcept;
115 [[nodiscard]] std::uint32_t seed() const noexcept { return noise_.seed(); }
117 [[nodiscard]] std::uint64_t revision() const noexcept { return revision_; }
119 [[nodiscard]] const HexNoise& noise() const noexcept { return noise_; }
124 [[nodiscard]] const HexSphereTopology& topology() const noexcept { return topology_; }
125
126 // --- topology -----------------------------------------------------------
127
129 [[nodiscard]] bool contains(HexSphereCell cell) const noexcept { return topology_.contains(cell); }
131 [[nodiscard]] bool isPentagon(HexSphereCell cell) const noexcept { return topology_.isPentagon(cell); }
138 [[nodiscard]] std::int32_t neighborCount(HexSphereCell cell) const noexcept {
139 return topology_.neighborCount(cell);
140 }
142 [[nodiscard]] HexSphereCell neighbor(HexSphereCell cell, std::int32_t direction) const noexcept {
143 return topology_.neighbor(cell, direction);
144 }
146 [[nodiscard]] std::int32_t directionOf(HexSphereCell cell, HexSphereCell other) const noexcept {
147 return topology_.directionOf(cell, other);
148 }
150 [[nodiscard]] std::int32_t oppositeDirection(HexSphereCell cell, std::int32_t direction) const noexcept {
151 return topology_.oppositeDirection(cell, direction);
152 }
154 [[nodiscard]] std::int32_t cornerCountOf(HexSphereCell cell) const noexcept {
155 return topology_.cornerCountOf(cell);
156 }
158 [[nodiscard]] HexEdgeType edgeTypeTo(HexSphereCell a, HexSphereCell b) const noexcept;
159
160 // --- geometry -----------------------------------------------------------
161
163 [[nodiscard]] HexVec3 direction(HexSphereCell cell) const noexcept { return topology_.direction(cell); }
170 [[nodiscard]] HexVec3 cornerDirection(HexSphereCell cell, std::int32_t corner) const noexcept {
171 return topology_.corner(cell, corner);
172 }
174 [[nodiscard]] float surfaceRadius(HexSphereCell cell) const noexcept;
176 [[nodiscard]] HexVec3 cellPosition(HexSphereCell cell) const noexcept;
178 [[nodiscard]] HexVec3 cellGroundPosition(HexSphereCell cell) const noexcept;
179
180 // --- cell access --------------------------------------------------------
181
188 [[nodiscard]] const HexCellData* cellAt(HexSphereCell cell) const noexcept;
189
190 [[nodiscard]] std::int32_t elevation(HexSphereCell c) const noexcept;
191 [[nodiscard]] std::int32_t waterLevel(HexSphereCell c) const noexcept;
192 [[nodiscard]] std::int32_t terrainType(HexSphereCell c) const noexcept;
193 [[nodiscard]] std::int32_t urbanLevel(HexSphereCell c) const noexcept;
194 [[nodiscard]] std::int32_t farmLevel(HexSphereCell c) const noexcept;
195 [[nodiscard]] std::int32_t plantLevel(HexSphereCell c) const noexcept;
196 [[nodiscard]] std::int32_t specialIndex(HexSphereCell c) const noexcept;
197 [[nodiscard]] bool isUnderwater(HexSphereCell c) const noexcept;
198 [[nodiscard]] bool hasRiver(HexSphereCell c) const noexcept;
199 [[nodiscard]] bool hasRoad(HexSphereCell c) const noexcept;
200 [[nodiscard]] bool hasRiverThrough(HexSphereCell c, std::int32_t direction) const noexcept;
201 [[nodiscard]] bool isWalled(HexSphereCell c) const noexcept;
203 [[nodiscard]] bool isExplored(HexSphereCell c) const noexcept;
205 [[nodiscard]] bool isExplorable(HexSphereCell c) const noexcept;
207 [[nodiscard]] HexValues values(HexSphereCell c) const noexcept;
209 [[nodiscard]] HexFlags flags(HexSphereCell c) const noexcept;
210
211 // --- picking ------------------------------------------------------------
212
228 [[nodiscard]] Result<HexSphereCell> pickCell(HexVec3 rayOrigin, HexVec3 rayDirection) const;
229
230 // --- neighbourhood queries ----------------------------------------------
231
241 [[nodiscard]] std::int32_t distance(HexSphereCell a, HexSphereCell b) const;
242
254 void collectBrush(HexSphereCell center, std::int32_t radius, std::vector<HexSphereCell>& out) const;
255
257 [[nodiscard]] Result<void> editElevation(HexSphereCell center, std::int32_t radius, std::int32_t delta);
259 [[nodiscard]] Result<void> editWaterLevel(HexSphereCell center, std::int32_t radius, std::int32_t delta);
261 [[nodiscard]] Result<void> editTerrainType(HexSphereCell center, std::int32_t radius, std::int32_t terrainType);
263 [[nodiscard]] Result<void> editFeatureLevel(HexSphereCell center, std::int32_t radius, std::int32_t feature,
264 std::int32_t delta);
265
266 // --- cell authoring -----------------------------------------------------
267
269 [[nodiscard]] Result<void> setElevation(HexSphereCell c, std::int32_t elevation);
271 [[nodiscard]] Result<void> setWaterLevel(HexSphereCell c, std::int32_t waterLevel);
273 [[nodiscard]] Result<void> setTerrainType(HexSphereCell c, std::int32_t terrainType);
275 [[nodiscard]] Result<void> setUrbanLevel(HexSphereCell c, std::int32_t level);
277 [[nodiscard]] Result<void> setFarmLevel(HexSphereCell c, std::int32_t level);
279 [[nodiscard]] Result<void> setPlantLevel(HexSphereCell c, std::int32_t level);
281 [[nodiscard]] Result<void> setSpecialIndex(HexSphereCell c, std::int32_t index);
283 [[nodiscard]] Result<void> setWalled(HexSphereCell c, bool walled);
285 [[nodiscard]] Result<void> setExplored(HexSphereCell c, bool explored);
287 [[nodiscard]] Result<void> setExplorable(HexSphereCell c, bool explorable);
298 [[nodiscard]] Result<void> setCellState(HexSphereCell c, HexValues values, HexFlags flags);
300 [[nodiscard]] Result<void> setOutgoingRiver(HexSphereCell c, std::int32_t direction);
302 [[nodiscard]] Result<void> removeRiver(HexSphereCell c);
304 [[nodiscard]] Result<void> addRoad(HexSphereCell c, std::int32_t direction);
306 [[nodiscard]] Result<void> removeRoads(HexSphereCell c);
307
308 // --- dirty tracking -----------------------------------------------------
309
311 void markAllDirty() noexcept;
313 void markCellDirtyAndNeighbors(HexSphereCell cell) noexcept;
321 [[nodiscard]] std::int32_t takeDirtyCell() noexcept;
323 [[nodiscard]] std::int32_t dirtyCellCount() const noexcept {
324 return static_cast<std::int32_t>(dirtyQueue_.size() - dirtyHead_);
325 }
326
327private:
328 void markCellDirty(HexSphereCell cell) noexcept;
329 void refreshCellDependents(HexSphereCell cell) noexcept;
330 void validateRivers(HexSphereCell cell) noexcept;
336 HexCellData* mutableCell(HexSphereCell cell) noexcept;
337
338 HexSphereTopology topology_{};
339 HexNoise noise_{};
340 std::vector<HexCellData> cells_;
341 std::vector<std::uint8_t> cellDirty_;
342 std::vector<std::int32_t> dirtyQueue_;
343 std::size_t dirtyHead_ = 0;
344 std::uint64_t revision_ = 0;
345 float sphereRadius_ = 0.f;
346 float elevationStep_ = 0.f;
347};
348
349} // namespace eve::hexmap
std::map< std::string, Var > values
#define EVENGINE_API_WORLD
Definition Export.h:109
Packed per-cell state records of the hex map.
std::int32_t waterLevel
Pointy-top hex metrics, directions and vertex helpers.
Deterministic asset-free noise for hex perturbation and generation.
std::int32_t c
Icosahedral hex topology: twelve pentagons and hexagons on a sphere.
float elevation
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
int level
float radius
std::uint32_t seed
Definition PointSet.cpp:807
Move-only, checked operation results for the common layer.
RoadLaneDirection direction
Cell cell
float step
Definition TreeMesh.cpp:314
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Packed boolean cell state (roads, rivers, walls, exploration).
Definition HexCell.h:123
Deterministic four-channel value noise over the XZ plane.
Definition HexNoise.h:23
An editable hex map wrapped onto a sphere.
std::int32_t subdivision() const noexcept
The subdivision level this map was built at.
std::uint32_t seed() const noexcept
The seed this map was built with.
HexSphereCell neighbor(HexSphereCell cell, std::int32_t direction) const noexcept
Neighbour of cell across edge direction, or kNoHexSphereCell.
const HexNoise & noise() const noexcept
Deterministic noise field shared by this map's mesh builders.
std::int32_t directionOf(HexSphereCell cell, HexSphereCell other) const noexcept
Edge index shared with other, or kNoHexSphereCell when not adjacent.
std::int32_t cornerCountOf(HexSphereCell cell) const noexcept
Number of corners of cell (5 or 6), or 0 when out of range.
const HexSphereTopology & topology() const noexcept
The topology backing this map. @ownership Borrowed; valid until this map is destroyed or reset is cal...
bool empty() const noexcept
Whether the map holds any cell.
std::int32_t pentagonCount() const noexcept
Number of pentagonal cells (12 for a non-empty map).
std::int32_t oppositeDirection(HexSphereCell cell, std::int32_t direction) const noexcept
Edge index of cell's edge towards its neighbour across direction.
bool isPentagon(HexSphereCell cell) const noexcept
Whether cell has five edges instead of six.
float sphereRadius() const noexcept
Sphere radius in world units.
HexSphereMap()=default
Hex sphere map.
HexVec3 cornerDirection(HexSphereCell cell, std::int32_t corner) const noexcept
Unit direction of corner corner of cell.
std::int32_t frequency() const noexcept
Subdivision frequency f = 2^subdivision.
std::int32_t cellCount() const noexcept
Number of cells (10 * f^2 + 2).
std::int32_t neighborCount(HexSphereCell cell) const noexcept
Number of edges of cell (5 or 6), or 0 when out of range.
std::uint64_t revision() const noexcept
Monotonic counter bumped by every mutation; used to detect stale meshes.
HexVec3 direction(HexSphereCell cell) const noexcept
Unit direction from the sphere centre to the centre of cell.
bool contains(HexSphereCell cell) const noexcept
Whether cell is a valid id of this map.
Icosahedral (Goldberg) hex topology for a spherical hex map.
Packed numeric cell state.
Definition HexCell.h:38
HexEdgeType
Relationship between two neighbouring cells of different elevation.
Definition HexMetrics.h:93
std::int32_t HexSphereCell
Dense identifier of one cell of a spherical hex topology.
One cell of the hex map: packed values plus packed flags.
Definition HexCell.h:244
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18