summary refs log tree commit diff
path: root/src/World/VoxelTraversal.hpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2024-02-06 02:10:11 +0100
committerMel <einebeere@gmail.com>2024-02-06 02:10:11 +0100
commit1f89fb20d41f9597840250352b40357fc3f0e5d1 (patch)
tree7c54e15746740f985dcc6938a64710ed3b3cea3a /src/World/VoxelTraversal.hpp
parentc61e09ce986fa99debea040a7c0ac42749ad8052 (diff)
downloadmeowcraft-1f89fb20d41f9597840250352b40357fc3f0e5d1.tar.zst
meowcraft-1f89fb20d41f9597840250352b40357fc3f0e5d1.zip
Add iteration limit to voxel traversal
Diffstat (limited to 'src/World/VoxelTraversal.hpp')
-rw-r--r--src/World/VoxelTraversal.hpp6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/World/VoxelTraversal.hpp b/src/World/VoxelTraversal.hpp
index 08a53eb..f7b67be 100644
--- a/src/World/VoxelTraversal.hpp
+++ b/src/World/VoxelTraversal.hpp
@@ -17,6 +17,8 @@ struct TraversalResult {
     Position::BlockWorldOffset normal;
 };
 
+#define MC_WORLD_VOXEL_TRAVERSAL_MAX_ITERATIONS 500
+
 // Amanatides, John, and Andrew Woo. 1987.
 // "A Fast Voxel Traversal Algorithm for Ray Tracing."
 // https://doi.org/10.2312/EGTP.19871000.
@@ -52,7 +54,11 @@ TraversalResult traverse(Ray ray, P&& predicate, Real max_distance = 0) {
     // This just means we add the direction of the ray at the axis we have progressed along.
     Position::WorldOffset approach = {};
 
+    UInt iterations = 0;
     while (!predicate(block_pos)) {
+        if (iterations++ > MC_WORLD_VOXEL_TRAVERSAL_MAX_ITERATIONS)
+            return {};
+
         if (max_distance > 0 && approach.magnitude() > max_distance)
             return {};