Skip to content

Commit 54ee6e7

Browse files
committed
more cleanup
1 parent ff78db9 commit 54ee6e7

File tree

10 files changed

+171
-288
lines changed

10 files changed

+171
-288
lines changed

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
@@ -37,8 +37,8 @@ object BlockHalfPreProcessor : PlacementProcessor() {
3737
val slab = state.get(Properties.BLOCK_HALF) ?: return
3838

3939
val surfaceScan = when (slab) {
40-
BlockHalf.BOTTOM -> SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Y)
41-
BlockHalf.TOP -> SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Y)
40+
BlockHalf.BOTTOM -> SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.Y)
41+
BlockHalf.TOP -> SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.Y)
4242
}
4343

4444
accumulator.offerSurfaceScan(surfaceScan)

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
@@ -39,19 +39,19 @@ object DoorHingePreProcessor : PlacementProcessor() {
3939
val side = state.get(Properties.DOOR_HINGE) ?: return@runSafe
4040
val scanner = when (state.get(Properties.HORIZONTAL_FACING) ?: return@runSafe) {
4141
Direction.NORTH ->
42-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.X)
43-
else SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.X)
42+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.X)
43+
else SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.X)
4444
Direction.EAST ->
45-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Z)
46-
else SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Z)
45+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.Z)
46+
else SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.Z)
4747
Direction.SOUTH ->
48-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.X)
49-
else SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.X)
48+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.X)
49+
else SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.X)
5050
Direction.DOWN,
5151
Direction.UP,
5252
Direction.WEST ->
53-
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Z)
54-
else SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Z)
53+
if (side == DoorHinge.LEFT) SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.Z)
54+
else SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.Z)
5555
}
5656
accumulator.offerSurfaceScan(scanner)
5757
} ?: Unit

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ object SlabPreProcessor : PlacementProcessor() {
4040
val currentState = runSafe { blockState(pos) } ?: return
4141

4242
val surfaceScan = when (slab) {
43-
SlabType.BOTTOM -> SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Y)
44-
SlabType.TOP -> SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Y)
43+
SlabType.BOTTOM -> SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.Y)
44+
SlabType.TOP -> SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.Y)
4545
SlabType.DOUBLE -> {
4646
accumulator.addIgnores(Properties.SLAB_TYPE)
4747
if (currentState.block !is SlabBlock) SurfaceScan.DEFAULT
4848
else when (currentState.get(Properties.SLAB_TYPE)) {
49-
SlabType.BOTTOM -> SurfaceScan(ScanMode.GREATER_BLOCK_HALF, Direction.Axis.Y)
50-
else -> SurfaceScan(ScanMode.LESSER_BLOCK_HALF, Direction.Axis.Y)
49+
SlabType.BOTTOM -> SurfaceScan(ScanMode.GreaterBlockHalf, Direction.Axis.Y)
50+
else -> SurfaceScan(ScanMode.LesserBlockHalf, Direction.Axis.Y)
5151
}
5252
}
5353
}

src/main/kotlin/com/lambda/interaction/construction/simulation/SimChecker.kt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,28 @@
1717

1818
package com.lambda.interaction.construction.simulation
1919

20+
import com.lambda.interaction.construction.processing.PreProcessingInfo
2021
import com.lambda.interaction.construction.result.BuildResult
2122
import com.lambda.interaction.construction.result.Dependable
2223
import com.lambda.interaction.construction.result.results.GenericResult
24+
import com.lambda.interaction.request.rotating.Rotation.Companion.rotationTo
25+
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.CheckedHit
26+
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.getVisibleSurfaces
27+
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.scanSurfaces
28+
import com.lambda.util.math.distSq
29+
import com.lambda.util.math.vec3d
30+
import com.lambda.util.world.raycast.RayCastUtils.blockResult
31+
import io.ktor.util.collections.*
32+
import kotlinx.coroutines.Dispatchers
33+
import kotlinx.coroutines.joinAll
34+
import kotlinx.coroutines.launch
35+
import kotlinx.coroutines.withContext
36+
import net.minecraft.util.hit.BlockHitResult
37+
import net.minecraft.util.math.BlockPos
38+
import net.minecraft.util.math.Direction
39+
import net.minecraft.util.math.Vec3d
40+
import net.minecraft.util.shape.VoxelShape
41+
import kotlin.math.pow
2342

2443
@DslMarker
2544
annotation class SimCheckerDsl
@@ -48,4 +67,62 @@ abstract class SimChecker<T : BuildResult> {
4867
}
4968
)
5069
}
70+
71+
suspend fun ISimInfo.scanShape(
72+
pov: Vec3d,
73+
voxelShape: VoxelShape,
74+
pos: BlockPos,
75+
sides: Set<Direction>,
76+
preProcessing: PreProcessingInfo
77+
): Set<CheckedHit>? {
78+
val boxes = voxelShape.boundingBoxes.map { it.offset(pos) }
79+
80+
val reachSq = buildConfig.interactReach.pow(2)
81+
82+
val validHits = ConcurrentSet<CheckedHit>()
83+
val misses = ConcurrentSet<Vec3d>()
84+
85+
withContext(Dispatchers.Default) {
86+
boxes.map { box ->
87+
launch {
88+
val sides = if (buildConfig.checkSideVisibility || buildConfig.strictRayCast) {
89+
sides.intersect(box.getVisibleSurfaces(pov))
90+
} else sides
91+
92+
scanSurfaces(box, sides, buildConfig.resolution, preProcessing.surfaceScan) { side, vec ->
93+
if (pov distSq vec > reachSq) {
94+
misses.add(vec)
95+
return@scanSurfaces
96+
}
97+
98+
val newRotation = pov.rotationTo(vec)
99+
100+
val hit = if (buildConfig.strictRayCast) {
101+
newRotation.rayCast(buildConfig.interactReach, pov)?.blockResult ?: return@scanSurfaces
102+
} else {
103+
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov) ?: return@scanSurfaces
104+
BlockHitResult(hitVec, side, pos, false)
105+
}
106+
107+
if (hit.blockPos != pos || hit.side != side) return@scanSurfaces
108+
val checked = CheckedHit(hit, newRotation, buildConfig.interactReach)
109+
110+
validHits.add(checked)
111+
}
112+
}
113+
}.joinAll()
114+
}
115+
116+
if (validHits.isEmpty()) {
117+
if (misses.isNotEmpty()) {
118+
result(GenericResult.OutOfReach(pos, pov, misses))
119+
return null
120+
}
121+
122+
result(GenericResult.NotVisible(pos, pos, pov.distanceTo(pos.vec3d)))
123+
return null
124+
}
125+
126+
return validHits
127+
}
51128
}

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/BreakChecker.kt

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

2020
import com.lambda.context.AutomatedSafeContext
21-
import com.lambda.context.SafeContext
2221
import com.lambda.interaction.construction.context.BreakContext
2322
import com.lambda.interaction.construction.result.BuildResult
2423
import com.lambda.interaction.construction.result.Dependable
@@ -38,12 +37,8 @@ import com.lambda.interaction.material.StackSelection.Companion.select
3837
import com.lambda.interaction.material.StackSelection.Companion.selectStack
3938
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
4039
import com.lambda.interaction.material.container.MaterialContainer
41-
import com.lambda.interaction.request.rotating.Rotation.Companion.rotationTo
4240
import com.lambda.interaction.request.rotating.RotationManager
4341
import com.lambda.interaction.request.rotating.RotationRequest
44-
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.CheckedHit
45-
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.getVisibleSurfaces
46-
import com.lambda.interaction.request.rotating.visibilty.VisibilityChecker.scanSurfaces
4742
import com.lambda.interaction.request.rotating.visibilty.lookAt
4843
import com.lambda.interaction.request.rotating.visibilty.lookAtBlock
4944
import com.lambda.threading.runSafe
@@ -52,14 +47,7 @@ import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
5247
import com.lambda.util.BlockUtils.instantBreakable
5348
import com.lambda.util.BlockUtils.isEmpty
5449
import com.lambda.util.item.ItemStackUtils.inventoryIndexOrSelected
55-
import com.lambda.util.math.distSq
56-
import com.lambda.util.math.vec3d
5750
import com.lambda.util.world.raycast.RayCastUtils.blockResult
58-
import io.ktor.util.collections.*
59-
import kotlinx.coroutines.Dispatchers
60-
import kotlinx.coroutines.joinAll
61-
import kotlinx.coroutines.launch
62-
import kotlinx.coroutines.withContext
6351
import net.minecraft.block.BlockState
6452
import net.minecraft.block.FallingBlock
6553
import net.minecraft.block.Waterloggable
@@ -75,12 +63,9 @@ import net.minecraft.registry.tag.ItemTags.IRON_TOOL_MATERIALS
7563
import net.minecraft.registry.tag.ItemTags.NETHERITE_TOOL_MATERIALS
7664
import net.minecraft.registry.tag.ItemTags.STONE_TOOL_MATERIALS
7765
import net.minecraft.registry.tag.ItemTags.WOODEN_TOOL_MATERIALS
78-
import net.minecraft.util.hit.BlockHitResult
7966
import net.minecraft.util.math.BlockPos
8067
import net.minecraft.util.math.Direction
81-
import net.minecraft.util.math.Vec3d
8268
import kotlin.jvm.optionals.getOrNull
83-
import kotlin.math.pow
8469

8570
class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
8671
: SimChecker<BreakResult>(), Dependable,
@@ -134,14 +119,12 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
134119
}
135120

136121
private suspend fun AutomatedSafeContext.checkBreaks(): Boolean {
137-
/* player is standing on top of the block */
138122
if (breakConfig.avoidSupporting) player.supportingBlockPos.getOrNull()?.let { support ->
139123
if (support != pos) return@let
140124
result(BreakResult.PlayerOnTop(pos, state))
141125
return true
142126
}
143127

144-
/* liquid needs to be submerged first to be broken */
145128
if (targetState.getState(pos).isAir && !state.fluidState.isEmpty && state.isReplaceable) {
146129
result(BreakResult.Submerge(pos, state))
147130
return simInfo(pos, state, TargetState.Solid(emptySet()))?.checkPlacements() ?: true
@@ -151,19 +134,16 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
151134

152135
if (breakConfig.avoidLiquids && affectsFluids()) return true
153136

154-
val voxelShape = state.getOutlineShape(world, pos)
155-
156-
val boxes = voxelShape.boundingBoxes.map { it.offset(pos) }
157-
158137
val swapStack = getSwapStack() ?: return true
159138
val instant = instantBreakable(
160139
state, pos,
161140
if (breakConfig.swapMode.isEnabled()) swapStack else player.mainHandStack,
162141
breakConfig.breakThreshold
163142
)
164143

165-
/* the player is buried inside the block */
166-
if (boxes.any { it.contains(pov) }) {
144+
val shape = state.getOutlineShape(world, pos)
145+
146+
if (shape.boundingBoxes.map { it.offset(pos) }.any { it.contains(pov) }) {
167147
val currentCast = RotationManager.activeRotation.rayCast(buildConfig.interactReach, pov)
168148
currentCast?.blockResult?.let { blockHit ->
169149
val rotationRequest = RotationRequest(lookAtBlock(pos), this)
@@ -181,58 +161,14 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
181161
return true
182162
}
183163

184-
val validHits = ConcurrentSet<CheckedHit>()
185-
val misses = ConcurrentSet<Vec3d>()
186-
val reachSq = buildConfig.interactReach.pow(2)
187-
188-
withContext(Dispatchers.Default) {
189-
boxes.map { box ->
190-
launch {
191-
val sides = if (buildConfig.checkSideVisibility)
192-
box.getVisibleSurfaces(pov)
193-
else Direction.entries.toSet()
194-
195-
scanSurfaces(box, sides, buildConfig.resolution) { side, vec ->
196-
if (pov distSq vec > reachSq) {
197-
misses.add(vec)
198-
return@scanSurfaces
199-
}
200-
201-
val newRotation = pov.rotationTo(vec)
202-
203-
val hit = if (buildConfig.strictRayCast) {
204-
newRotation.rayCast(buildConfig.interactReach, pov)?.blockResult
205-
} else {
206-
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov)
207-
BlockHitResult(hitVec, side, pos, false)
208-
} ?: return@scanSurfaces
209-
210-
if (hit.blockResult?.blockPos != pos) return@scanSurfaces
211-
val checked = CheckedHit(hit, newRotation, buildConfig.interactReach)
212-
213-
validHits.add(checked)
214-
}
215-
}
216-
}.joinAll()
217-
}
218-
219-
if (validHits.isEmpty()) {
220-
if (misses.isNotEmpty()) {
221-
result(GenericResult.OutOfReach(pos, pov, misses))
222-
return true
223-
}
224-
225-
result(GenericResult.NotVisible(pos, pos, pov.distanceTo(pos.vec3d)))
226-
return true
227-
}
164+
val validHits = scanShape(pov, shape, pos, Direction.entries.toSet(), preProcessing) ?: return true
228165

229166
val bestHit = buildConfig.pointSelection.select(validHits) ?: return true
230-
val blockHit = bestHit.hit.blockResult ?: return true
231167
val target = lookAt(bestHit.targetRotation, 0.001)
232168
val rotationRequest = RotationRequest(target, this)
233169

234170
val breakContext = BreakContext(
235-
blockHit,
171+
bestHit.hit.blockResult ?: return true,
236172
rotationRequest,
237173
swapStack.inventoryIndexOrSelected,
238174
stackSelection,
@@ -245,7 +181,7 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
245181
return true
246182
}
247183

248-
private fun SafeContext.getSwapStack(): ItemStack? {
184+
private fun AutomatedSafeContext.getSwapStack(): ItemStack? {
249185
val silentSwapSelection = selectContainer {
250186
ofAnyType(MaterialContainer.Rank.HOTBAR)
251187
}

0 commit comments

Comments
 (0)