summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2026-09-04 00:46:51 +0200
committerMel <mel@rnrd.eu>2026-09-04 00:46:51 +0200
commit00d2b26a6e1566faae85fb7bc6870f0f2db7ac7f (patch)
treed2678ba7b36e0863e68094208059b94bbdda24d3
parent1952628825ee526e4ae32170266f8eaa323a0e16 (diff)
downloadmeowcraft-00d2b26a6e1566faae85fb7bc6870f0f2db7ac7f.tar.zst
meowcraft-00d2b26a6e1566faae85fb7bc6870f0f2db7ac7f.zip
Make tree generation portable for ARM (and other architectures with saturating unsigned rounding of floats)
Signed-off-by: Mel <mel@rnrd.eu>
-rw-r--r--src/World/Generation/Decoration.cpp19
-rw-r--r--src/World/Generation/Decoration.hpp2
2 files changed, 18 insertions, 3 deletions
diff --git a/src/World/Generation/Decoration.cpp b/src/World/Generation/Decoration.cpp
index ecfa49b..8ab7c2a 100644
--- a/src/World/Generation/Decoration.cpp
+++ b/src/World/Generation/Decoration.cpp
@@ -14,6 +14,20 @@ void Decorator::put_block(Chunk& chunk, Pos pos, BlockType block) {
}
}
+void Decorator::put_block(Chunk& chunk, Pos pos, Position::BlockLocalOffset offset, BlockType block) {
+ Int x = TO(Int, pos.x()) + offset.x();
+ Int y = TO(Int, pos.y()) + offset.y();
+ Int z = TO(Int, pos.z()) + offset.z();
+
+ if (x < 0 || x >= TO(Int, Chunk::Width)
+ || y < 0 || y >= TO(Int, Chunk::Height)
+ || z < 0 || z >= TO(Int, Chunk::Width)) {
+ return;
+ }
+
+ put_block(chunk, Pos{x, y, z}, block);
+}
+
void Decorator::draw_column(Chunk& chunk, Pos pos, UInt height, BlockType block) {
Pos current_pos = pos;
for (UInt i = 0; i < height; i++) {
@@ -28,15 +42,14 @@ void Decorator::draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, Real radius,
auto ortho1 = normalized_axis.any_orthogonal();
auto ortho2 = normalized_axis.cross(ortho1);
- auto r = [](const Real x) { return static_cast<UInt>(std::round(x)); };
+ auto r = [](const Real x) { return TO(I16, std::lround(x)); };
Int radius_round = std::round(radius);
for (I32 d1 = -radius_round; d1 <= radius_round; d1++) {
Real height = std::sqrt(radius * radius - d1 * d1);
for (I32 d2 = -height; d2 <= (Int)height; d2++) {
auto p = ortho1 * d1 + ortho2 * d2;
- Pos block_pos = pos + Pos{r(p.x()), r(p.y()), r(p.z())};
- put_block(chunk, block_pos, block);
+ put_block(chunk, pos, Position::BlockLocalOffset{r(p.x()), r(p.y()), r(p.z())}, block);
}
}
}
diff --git a/src/World/Generation/Decoration.hpp b/src/World/Generation/Decoration.hpp
index c157688..4f74800 100644
--- a/src/World/Generation/Decoration.hpp
+++ b/src/World/Generation/Decoration.hpp
@@ -14,6 +14,8 @@ public:
virtual void decorate_chunk(Chunk& chunk) = 0;
static void put_block(Chunk& chunk, Pos pos, BlockType block);
+ static void put_block(Chunk& chunk, Pos pos, Position::BlockLocalOffset offset, BlockType block);
+
static void draw_column(Chunk& chunk, Pos pos, UInt height, BlockType block);
static void draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, Real radius, BlockType block);
};