载入中...
搜索中...
未找到
HexCell.h
浏览该文件的文档.
1#pragma once
2
5#include "hexmap/HexMetrics.h"
6
7#include <cstdint>
8
9namespace eve::hexmap {
10
12enum class HexTerrainType : std::int32_t {
13 Sand = 0,
14 Grass = 1,
15 Mud = 2,
16 Stone = 3,
17 Snow = 4,
18};
19
21inline constexpr std::int32_t kHexTerrainTypeCount = 5;
22
24[[nodiscard]] constexpr std::int32_t clampTerrainType(std::int32_t index) noexcept {
25 return index < 0 ? 0 : (index >= kHexTerrainTypeCount ? kHexTerrainTypeCount - 1 : index);
26}
27
38class HexValues {
39public:
41 constexpr HexValues() noexcept = default;
43 explicit constexpr HexValues(std::uint32_t packed) noexcept : packed_(packed) {}
44
46 [[nodiscard]] constexpr std::uint32_t raw() const noexcept { return packed_; }
47
49 [[nodiscard]] constexpr std::int32_t elevation() const noexcept {
50 return static_cast<std::int32_t>(get(31u, 0u)) - 15;
51 }
53 [[nodiscard]] constexpr std::int32_t waterLevel() const noexcept { return static_cast<std::int32_t>(get(31u, 5u)); }
55 [[nodiscard]] constexpr std::int32_t urbanLevel() const noexcept { return static_cast<std::int32_t>(get(3u, 10u)); }
57 [[nodiscard]] constexpr std::int32_t farmLevel() const noexcept { return static_cast<std::int32_t>(get(3u, 12u)); }
59 [[nodiscard]] constexpr std::int32_t plantLevel() const noexcept { return static_cast<std::int32_t>(get(3u, 14u)); }
61 [[nodiscard]] constexpr std::int32_t specialIndex() const noexcept {
62 return static_cast<std::int32_t>(get(255u, 16u));
63 }
65 [[nodiscard]] constexpr std::int32_t terrainType() const noexcept {
66 return static_cast<std::int32_t>(get(255u, 24u));
67 }
68
70 [[nodiscard]] constexpr std::int32_t viewElevation() const noexcept {
71 return elevation() > waterLevel() ? elevation() : waterLevel();
72 }
74 [[nodiscard]] constexpr bool isUnderwater() const noexcept { return waterLevel() > elevation(); }
75
76 [[nodiscard]] constexpr HexValues withElevation(std::int32_t value) const noexcept {
77 return HexValues{with(static_cast<std::uint32_t>(value + 15) & 31u, 31u, 0u)};
78 }
80 [[nodiscard]] constexpr HexValues withWaterLevel(std::int32_t value) const noexcept {
81 return HexValues{with(static_cast<std::uint32_t>(value) & 31u, 31u, 5u)};
82 }
84 [[nodiscard]] constexpr HexValues withUrbanLevel(std::int32_t value) const noexcept {
85 return HexValues{with(static_cast<std::uint32_t>(value) & 3u, 3u, 10u)};
86 }
88 [[nodiscard]] constexpr HexValues withFarmLevel(std::int32_t value) const noexcept {
89 return HexValues{with(static_cast<std::uint32_t>(value) & 3u, 3u, 12u)};
90 }
92 [[nodiscard]] constexpr HexValues withPlantLevel(std::int32_t value) const noexcept {
93 return HexValues{with(static_cast<std::uint32_t>(value) & 3u, 3u, 14u)};
94 }
96 [[nodiscard]] constexpr HexValues withSpecialIndex(std::int32_t value) const noexcept {
97 return HexValues{with(static_cast<std::uint32_t>(value) & 255u, 255u, 16u)};
98 }
100 [[nodiscard]] constexpr HexValues withTerrainType(std::int32_t value) const noexcept {
101 return HexValues{with(static_cast<std::uint32_t>(value) & 255u, 255u, 24u)};
102 }
103
104private:
105 [[nodiscard]] constexpr std::uint32_t get(std::uint32_t mask, std::uint32_t shift) const noexcept {
106 return (packed_ >> shift) & mask;
107 }
108 [[nodiscard]] constexpr std::uint32_t with(std::uint32_t value, std::uint32_t mask,
109 std::uint32_t shift) const noexcept {
110 return (packed_ & ~(mask << shift)) | (value << shift);
111 }
112
113 std::uint32_t packed_ = 0;
114};
115
123class HexFlags {
124public:
126 constexpr HexFlags() noexcept = default;
128 explicit constexpr HexFlags(std::uint32_t packed) noexcept : packed_(packed) {}
129
131 [[nodiscard]] constexpr std::uint32_t raw() const noexcept { return packed_; }
132
134 [[nodiscard]] static constexpr HexFlags direction(HexDirection d) noexcept {
135 return HexFlags{1u << static_cast<std::uint32_t>(d)};
136 }
137
139 [[nodiscard]] constexpr bool has(HexFlags other) const noexcept {
140 return (packed_ & other.packed_) == other.packed_ && other.packed_ != 0u;
141 }
143 [[nodiscard]] constexpr bool hasNone(HexFlags other) const noexcept { return (packed_ & other.packed_) == 0u; }
145 [[nodiscard]] constexpr HexFlags with(HexFlags other) const noexcept { return HexFlags{packed_ | other.packed_}; }
147 [[nodiscard]] constexpr HexFlags without(HexFlags other) const noexcept {
148 return HexFlags{packed_ & ~other.packed_};
149 }
150
152 [[nodiscard]] constexpr bool hasRoad(HexDirection d) const noexcept { return has(direction(d)); }
154 [[nodiscard]] constexpr bool hasRiverIn(HexDirection d) const noexcept {
156 return has(HexFlags{direction(d).packed_ << 6});
157 }
159 [[nodiscard]] constexpr bool hasRiverOut(HexDirection d) const noexcept {
161 return has(HexFlags{direction(d).packed_ << 12});
162 }
164 [[nodiscard]] constexpr bool hasAnyRiverIn() const noexcept { return (packed_ & (0x3fu << 6)) != 0u; }
166 [[nodiscard]] constexpr bool hasAnyRiverOut() const noexcept { return (packed_ & (0x3fu << 12)) != 0u; }
168 [[nodiscard]] constexpr bool hasRiver() const noexcept { return hasAnyRiverIn() || hasAnyRiverOut(); }
170 [[nodiscard]] constexpr bool hasRoad() const noexcept { return (packed_ & 0x3fu) != 0u; }
172 [[nodiscard]] constexpr bool hasRiverThrough(HexDirection d) const noexcept {
173 return hasRiverIn(d) || hasRiverOut(d);
174 }
176 [[nodiscard]] constexpr bool hasRiverBeginOrEnd() const noexcept {
177 const std::uint32_t in = (packed_ >> 6) & 0x3fu;
178 const std::uint32_t out = (packed_ >> 12) & 0x3fu;
179 const auto bits = [](std::uint32_t v) {
180 std::uint32_t n = 0;
181 while (v) {
182 n += v & 1u;
183 v >>= 1;
184 }
185 return n;
186 };
187 return (bits(in) + bits(out)) == 1u;
188 }
189
191 [[nodiscard]] constexpr bool isWalled() const noexcept { return (packed_ & (1u << 18)) != 0u; }
193 [[nodiscard]] constexpr bool isExplored() const noexcept { return (packed_ & (1u << 20)) != 0u; }
195 [[nodiscard]] constexpr bool isExplorable() const noexcept { return (packed_ & (1u << 21)) != 0u; }
196
198 [[nodiscard]] constexpr HexFlags withRoad(HexDirection d) const noexcept { return with(direction(d)); }
200 [[nodiscard]] constexpr HexFlags withoutRoad(HexDirection d) const noexcept { return without(direction(d)); }
202 [[nodiscard]] constexpr HexFlags withRiverIn(HexDirection d) const noexcept {
204 return with(HexFlags{direction(d).packed_ << 6});
205 }
207 [[nodiscard]] constexpr HexFlags withoutRiverIn(HexDirection d) const noexcept {
209 return without(HexFlags{direction(d).packed_ << 6});
210 }
212 [[nodiscard]] constexpr HexFlags withRiverOut(HexDirection d) const noexcept {
214 return with(HexFlags{direction(d).packed_ << 12});
215 }
217 [[nodiscard]] constexpr HexFlags withoutRiverOut(HexDirection d) const noexcept {
219 return without(HexFlags{direction(d).packed_ << 12});
220 }
222 [[nodiscard]] constexpr HexFlags withWalled(bool value) const noexcept {
223 return value ? with(HexFlags{1u << 18}) : without(HexFlags{1u << 18});
224 }
226 [[nodiscard]] constexpr HexFlags withExplored(bool value) const noexcept {
227 return value ? with(HexFlags{1u << 20}) : without(HexFlags{1u << 20});
228 }
230 [[nodiscard]] constexpr HexFlags withExplorable(bool value) const noexcept {
231 return value ? with(HexFlags{1u << 21}) : without(HexFlags{1u << 21});
232 }
233
235 [[nodiscard]] static constexpr HexFlags riverMask() noexcept { return HexFlags{(0x3fu << 6) | (0x3fu << 12)}; }
237 [[nodiscard]] static constexpr HexFlags roadMask() noexcept { return HexFlags{0x3fu}; }
238
239private:
240 std::uint32_t packed_ = 0;
241};
242
248
261[[nodiscard]] constexpr bool canHoldUnit(const HexCellData& cell) noexcept {
262 return cell.flags.isExplored() && cell.flags.isExplorable() && !cell.values.isUnderwater();
263}
264
265} // namespace eve::hexmap
double value
int mask
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
Pointy-top hex metrics, directions and vertex helpers.
float v
float d
RoadLaneDirection direction
Cell cell
uint32_t index
Packed boolean cell state (roads, rivers, walls, exploration).
Definition HexCell.h:123
constexpr bool hasRiver() const noexcept
Whether this cell has any river connection at all.
Definition HexCell.h:168
constexpr std::uint32_t raw() const noexcept
Raw.
Definition HexCell.h:131
constexpr bool hasNone(HexFlags other) const noexcept
True when none.
Definition HexCell.h:143
constexpr bool isWalled() const noexcept
True when walled.
Definition HexCell.h:191
constexpr HexFlags without(HexFlags other) const noexcept
Without.
Definition HexCell.h:147
constexpr HexFlags with(HexFlags other) const noexcept
With.
Definition HexCell.h:145
constexpr HexFlags withoutRoad(HexDirection d) const noexcept
Without road.
Definition HexCell.h:200
constexpr bool has(HexFlags other) const noexcept
True when active.
Definition HexCell.h:139
constexpr bool hasRiverBeginOrEnd() const noexcept
Whether the cell is a river source or sink (exactly one connection).
Definition HexCell.h:176
static constexpr HexFlags direction(HexDirection d) noexcept
Direction.
Definition HexCell.h:134
constexpr bool isExplored() const noexcept
True when explored.
Definition HexCell.h:193
static constexpr HexFlags riverMask() noexcept
Mask of every river bit.
Definition HexCell.h:235
constexpr bool hasRiverThrough(HexDirection d) const noexcept
Whether a river enters and/or leaves through d.
Definition HexCell.h:172
constexpr bool hasRiverIn(HexDirection d) const noexcept
True when river in.
Definition HexCell.h:154
constexpr HexFlags withoutRiverIn(HexDirection d) const noexcept
Without river in.
Definition HexCell.h:207
constexpr HexFlags withRiverOut(HexDirection d) const noexcept
With river out.
Definition HexCell.h:212
constexpr bool hasRoad() const noexcept
Whether this cell has any road.
Definition HexCell.h:170
constexpr bool isExplorable() const noexcept
True when explorable.
Definition HexCell.h:195
constexpr HexFlags() noexcept=default
Constructs a HexFlags.
constexpr HexFlags withExplored(bool value) const noexcept
With explored.
Definition HexCell.h:226
constexpr HexFlags withExplorable(bool value) const noexcept
With explorable.
Definition HexCell.h:230
constexpr bool hasAnyRiverOut() const noexcept
Whether any river leaves this cell.
Definition HexCell.h:166
constexpr HexFlags withRoad(HexDirection d) const noexcept
With road.
Definition HexCell.h:198
constexpr bool hasRoad(HexDirection d) const noexcept
True when road.
Definition HexCell.h:152
constexpr bool hasAnyRiverIn() const noexcept
Whether any river flows into this cell.
Definition HexCell.h:164
constexpr HexFlags withoutRiverOut(HexDirection d) const noexcept
Without river out.
Definition HexCell.h:217
constexpr HexFlags withRiverIn(HexDirection d) const noexcept
With river in.
Definition HexCell.h:202
constexpr bool hasRiverOut(HexDirection d) const noexcept
True when river out.
Definition HexCell.h:159
constexpr HexFlags withWalled(bool value) const noexcept
With walled.
Definition HexCell.h:222
static constexpr HexFlags roadMask() noexcept
Mask of every road bit.
Definition HexCell.h:237
Packed numeric cell state.
Definition HexCell.h:38
constexpr std::int32_t farmLevel() const noexcept
Farm level.
Definition HexCell.h:57
constexpr std::int32_t urbanLevel() const noexcept
Urban level.
Definition HexCell.h:55
constexpr HexValues withTerrainType(std::int32_t value) const noexcept
With terrain type.
Definition HexCell.h:100
constexpr HexValues withFarmLevel(std::int32_t value) const noexcept
With farm level.
Definition HexCell.h:88
constexpr bool isUnderwater() const noexcept
Whether the cell is flooded above its terrain surface.
Definition HexCell.h:74
constexpr std::int32_t terrainType() const noexcept
Terrain type.
Definition HexCell.h:65
constexpr HexValues withSpecialIndex(std::int32_t value) const noexcept
With special index.
Definition HexCell.h:96
constexpr HexValues withElevation(std::int32_t value) const noexcept
Definition HexCell.h:76
constexpr std::int32_t specialIndex() const noexcept
Special index.
Definition HexCell.h:61
constexpr std::uint32_t raw() const noexcept
Raw.
Definition HexCell.h:46
constexpr std::int32_t viewElevation() const noexcept
Elevation used for line of sight: the higher of land and water.
Definition HexCell.h:70
constexpr HexValues withPlantLevel(std::int32_t value) const noexcept
With plant level.
Definition HexCell.h:92
constexpr HexValues withWaterLevel(std::int32_t value) const noexcept
With water level.
Definition HexCell.h:80
constexpr std::int32_t elevation() const noexcept
Elevation.
Definition HexCell.h:49
constexpr std::int32_t plantLevel() const noexcept
Plant level.
Definition HexCell.h:59
constexpr HexValues withUrbanLevel(std::int32_t value) const noexcept
With urban level.
Definition HexCell.h:84
constexpr HexValues() noexcept=default
Constructs a HexValues.
constexpr std::int32_t waterLevel() const noexcept
Water level.
Definition HexCell.h:53
constexpr bool canHoldUnit(const HexCellData &cell) noexcept
Whether a cell record is in a state that can hold a unit.
Definition HexCell.h:261
HexTerrainType
Terrain palette index of a hex cell.
Definition HexCell.h:12
constexpr std::int32_t clampTerrainType(std::int32_t index) noexcept
Terrain type index clamped into the supported palette range.
Definition HexCell.h:24
constexpr std::int32_t kHexTerrainTypeCount
Number of entries in the terrain palette.
Definition HexCell.h:21
HexDirection
Hex facing directions, counter-clockwise from north-east.
Definition HexMetrics.h:59
One cell of the hex map: packed values plus packed flags.
Definition HexCell.h:244