From 1f89fb20d41f9597840250352b40357fc3f0e5d1 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 6 Feb 2024 02:10:11 +0100 Subject: Add iteration limit to voxel traversal --- src/World/VoxelTraversal.hpp | 6 ++++++ 1 file changed, 6 insertions(+) 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 {}; -- cgit 1.4.1