Skip to content

Commit 57570c0

Browse files
committed
use scan modes based on the block position rather than relative box dimensions
1 parent ffd345f commit 57570c0

File tree

6 files changed

+54
-40
lines changed

6 files changed

+54
-40
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/processing/preprocessors/BlockHalfPreProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ object BlockHalfPreProcessor : PlacementProcessor() {
3636
val slab = state.get(Properties.BLOCK_HALF) ?: return
3737

3838
val surfaceScan = when (slab) {
39-
BlockHalf.BOTTOM -> SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.Y)
40-
BlockHalf.TOP -> SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.Y)
39+
BlockHalf.BOTTOM -> SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Y)
40+
BlockHalf.TOP -> SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Y)
4141
}
4242

4343
accumulator.offerSurfaceScan(surfaceScan)

common/src/main/kotlin/com/lambda/interaction/construction/processing/preprocessors/DoorHingePreProcessor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ object DoorHingePreProcessor : PlacementProcessor() {
3838
val side = state.get(Properties.DOOR_HINGE) ?: return@runSafe
3939
val scanner = when (state.get(Properties.HORIZONTAL_FACING) ?: return@runSafe) {
4040
Direction.NORTH ->
41-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.X)
42-
else SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.X)
41+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.X)
42+
else SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.X)
4343
Direction.EAST ->
44-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.Z)
45-
else SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.Z)
44+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Z)
45+
else SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Z)
4646
Direction.SOUTH ->
47-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.X)
48-
else SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.X)
47+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.X)
48+
else SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.X)
4949
Direction.DOWN,
5050
Direction.UP,
5151
Direction.WEST ->
52-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.Z)
53-
else SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.Z)
52+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Z)
53+
else SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Z)
5454
}
5555
accumulator.offerSurfaceScan(scanner)
5656
} ?: Unit

common/src/main/kotlin/com/lambda/interaction/construction/processing/preprocessors/SlabPreProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ object SlabPreProcessor : PlacementProcessor() {
3636
val slab = state.get(Properties.SLAB_TYPE) ?: return
3737

3838
val surfaceScan = when (slab) {
39-
SlabType.BOTTOM -> SurfaceScan(ScanMode.LESSER_HALF, Direction.Axis.Y)
40-
SlabType.TOP -> SurfaceScan(ScanMode.GREATER_HALF, Direction.Axis.Y)
39+
SlabType.BOTTOM -> SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Y)
40+
SlabType.TOP -> SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Y)
4141
SlabType.DOUBLE -> {
4242
accumulator.addIgnores(Properties.SLAB_TYPE)
4343
SurfaceScan(ScanMode.FULL, Direction.Axis.Y)

common/src/main/kotlin/com/lambda/interaction/construction/verify/ScanMode.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.lambda.interaction.construction.verify
1919

2020
enum class ScanMode(val priority: Int) {
21-
GREATER_HALF(1),
22-
LESSER_HALF(1),
21+
GREATER_BLOCK_HALF(1),
22+
LESSER_BLOCK_HALF(1),
2323
FULL(0)
2424
}

common/src/main/kotlin/com/lambda/interaction/request/rotating/visibilty/PointSelection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum class PointSelection(val select: (MutableList<VisibilityChecker.CheckedHit>
3131
Optimum({ hits ->
3232
val optimum = hits
3333
.map { it.hit.pos }
34-
.reduceOrNull { acc, pos -> acc?.add(pos) }
34+
.reduceOrNull { acc, pos -> pos?.let { acc?.add(it) } }
3535
?.times(1 / hits.size)
3636

3737
optimum?.let {

common/src/main/kotlin/com/lambda/interaction/request/rotating/visibilty/VisibilityChecker.kt

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import net.minecraft.util.math.Box
3434
import net.minecraft.util.math.Direction
3535
import net.minecraft.util.math.Vec3d
3636
import java.util.*
37+
import kotlin.math.floor
3738
import 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

Comments
 (0)