@@ -34,6 +34,7 @@ import net.minecraft.util.math.Box
3434import net.minecraft.util.math.Direction
3535import net.minecraft.util.math.Vec3d
3636import java.util.*
37+ import kotlin.math.floor
3738import kotlin.math.pow
3839
3940/* *
@@ -165,53 +166,66 @@ object VisibilityChecker {
165166 */
166167 fun scanSurfaces (
167168 box : Box ,
168- excludedSides : Set <Direction > = emptySet(),
169+ sides : Set <Direction > = emptySet(),
169170 resolution : Int = 5,
170171 scan : SurfaceScan = SurfaceScan .DEFAULT ,
171172 check : (Direction , Vec3d ) -> Unit ,
172173 ) {
173- excludedSides .forEach { side ->
174+ sides .forEach { side ->
174175 val (minX, minY, minZ, maxX, maxY, maxZ) = box.contract(TaskFlowModule .shrinkFactor).bounds(side)
175- val stepX = (maxX - minX) / resolution
176- val stepY = (maxY - minY) / resolution
177- val stepZ = (maxZ - minZ) / resolution
178176
179- // Determine the bounds to scan based on the axis and mode
180- val (startX, endX) = if (scan.axis == Direction .Axis .X && stepX != 0.0 ) {
181- val centerX = (minX + maxX) / 2
177+ // Determine the bounds to scan based on the axis and mode. Skip if no part of the face is in the desired bounds
178+ val (startX, endX) = if (scan.axis == Direction .Axis .X && maxX != minX) {
182179 when (scan.mode) {
183- ScanMode .GREATER_HALF -> centerX + 0.01 to maxX
184- ScanMode .LESSER_HALF -> minX to centerX - 0.01
180+ ScanMode .GREATER_BLOCK_HALF -> (floor(minX) + 0.501 ).let { center ->
181+ if (maxX < center) return @forEach
182+ minX.coerceAtLeast(center) to maxX
183+ }
184+ ScanMode .LESSER_BLOCK_HALF -> (floor(maxX) + 0.499 ).let { center ->
185+ if (minX > center) return @forEach
186+ minX to maxX.coerceAtMost(center)
187+ }
185188 ScanMode .FULL -> minX to maxX
186189 }
187190 } else minX to maxX
188191
189- val (startY, endY) = if (scan.axis == Direction .Axis .Y && stepY != 0.0 ) {
190- val centerY = (minY + maxY) / 2
192+ val (startY, endY) = if (scan.axis == Direction .Axis .Y && maxY != minY) {
191193 when (scan.mode) {
192- ScanMode .GREATER_HALF -> centerY + 0.01 to maxY
193- ScanMode .LESSER_HALF -> minY to centerY - 0.01
194+ ScanMode .GREATER_BLOCK_HALF -> (floor(minY) + 0.501 ).let { center ->
195+ if (maxY < center) return @forEach
196+ minY.coerceAtLeast(center) to maxY
197+ }
198+ ScanMode .LESSER_BLOCK_HALF -> (floor(maxY) + 0.499 ).let { center ->
199+ if (minY > center) return @forEach
200+ minY to maxY.coerceAtMost(center)
201+ }
194202 ScanMode .FULL -> minY to maxY
195203 }
196204 } else minY to maxY
197205
198- val (startZ, endZ) = if (scan.axis == Direction .Axis .Z && stepZ != 0.0 ) {
199- val centerZ = (minZ + maxZ) / 2
206+ val (startZ, endZ) = if (scan.axis == Direction .Axis .Z && maxZ != minZ) {
200207 when (scan.mode) {
201- ScanMode .GREATER_HALF -> centerZ + 0.01 to maxZ
202- ScanMode .LESSER_HALF -> minZ to centerZ - 0.01
208+ ScanMode .GREATER_BLOCK_HALF -> (floor(minZ) + 0.501 ).let { center ->
209+ if (maxZ < center) return @forEach
210+ minZ.coerceAtLeast(center) to maxZ
211+ }
212+ ScanMode .LESSER_BLOCK_HALF -> (floor(maxZ) + 0.499 ).let { center ->
213+ if (minZ > center) return @forEach
214+ minZ to maxZ.coerceAtMost(center)
215+ }
203216 ScanMode .FULL -> minZ to maxZ
204217 }
205218 } else minZ to maxZ
206219
207- (0 .. resolution).forEach outer@{ i ->
208- val x = if (stepX != 0.0 ) startX + stepX * i else startX
209- if (x > endX) return @outer
210- (0 .. resolution).forEach inner@{ j ->
211- val y = if (stepY != 0.0 ) startY + stepY * j else startY
212- if (y > endY) return @inner
220+ val stepX = (endX - startX) / resolution
221+ val stepY = (endY - startY) / resolution
222+ val stepZ = (endZ - startZ) / resolution
223+
224+ (0 .. resolution).forEach outer@ { i ->
225+ val x = if (stepX != 0.0 ) startX + (stepX * i) else startX
226+ (0 .. resolution).forEach inner@ { j ->
227+ val y = if (stepY != 0.0 ) startY + (stepY * j) else startY
213228 val z = if (stepZ != 0.0 ) startZ + stepZ * ((if (stepX != 0.0 ) j else i)) else startZ
214- if (z > endZ) return @inner
215229 check(side, Vec3d (x, y, z))
216230 }
217231 }
0 commit comments