Skip to content

Commit d95eda5

Browse files
committed
Fix player collision check for simulation
1 parent 42c1c0e commit d95eda5

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/result/Drawable.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ interface Drawable {
5656
}
5757

5858
fun SafeContext.withShape(shape: VoxelShape, offset: BlockPos, color: Color, mask: Int = DirectionMask.ALL) {
59-
if (shape.isEmpty) return
59+
if (shape.isEmpty) {
60+
withBox(Box(offset), color, mask)
61+
return
62+
}
6063
shape.boundingBoxes.forEach { box ->
6164
withBox(box.offset(offset), color, mask)
6265
}

common/src/main/kotlin/com/lambda/interaction/construction/simulation/Simulation.kt

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.lambda.config.groups.InventoryConfig
2323
import com.lambda.context.SafeContext
2424
import com.lambda.interaction.construction.blueprint.Blueprint
2525
import com.lambda.interaction.construction.result.BuildResult
26+
import com.lambda.interaction.construction.result.Drawable
2627
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
2728
import com.lambda.interaction.request.rotation.RotationConfig
2829
import com.lambda.module.modules.client.TaskFlowModule
@@ -32,9 +33,11 @@ import com.lambda.util.world.FastVector
3233
import com.lambda.util.world.toBlockPos
3334
import com.lambda.util.world.toVec3d
3435
import net.minecraft.client.network.ClientPlayerEntity
36+
import net.minecraft.util.math.BlockPos
3537
import net.minecraft.util.math.Box
3638
import net.minecraft.util.math.Direction
3739
import net.minecraft.util.math.Vec3d
40+
import java.awt.Color
3841

3942
data class Simulation(
4043
val blueprint: Blueprint,
@@ -47,31 +50,37 @@ data class Simulation(
4750
private fun FastVector.toView(): Vec3d = toVec3d().add(0.5, ClientPlayerEntity.DEFAULT_EYE_HEIGHT.toDouble(), 0.5)
4851

4952
fun simulate(
50-
pos: FastVector
51-
) =
52-
cache.getOrPut(pos) {
53-
val view = pos.toView()
54-
runSafe {
55-
if (blueprint.isOutOfBounds(view) && blueprint.getClosestPointTo(view)
56-
.distanceTo(view) > 10.0
57-
) return@getOrPut emptySet()
58-
val blockPos = pos.toBlockPos()
59-
if (!playerFitsIn(Vec3d.ofBottomCenter(blockPos))) return@getOrPut emptySet()
60-
if (!blockPos.down().blockState(world)
61-
.isSideSolidFullSquare(world, blockPos, Direction.UP)
62-
) return@getOrPut emptySet()
63-
}
53+
pos: FastVector,
54+
) = cache.getOrPut(pos) {
55+
val view = pos.toView()
56+
val isOutOfBounds = blueprint.isOutOfBounds(view)
57+
val isTooFar = blueprint.getClosestPointTo(view).distanceTo(view) > 10.0
58+
runSafe {
59+
if (isOutOfBounds && isTooFar) return@getOrPut emptySet()
60+
val blockPos = pos.toBlockPos()
61+
val isWalkable = blockPos.down().blockState(world).isSideSolidFullSquare(world, blockPos, Direction.UP)
62+
if (!isWalkable) return@getOrPut emptySet()
63+
if (!playerFitsIn(blockPos)) return@getOrPut emptySet()
64+
}
65+
66+
blueprint.simulate(view, interact, rotation, inventory, build)
67+
}
6468

65-
blueprint.simulate(view, interact, rotation, inventory, build)
69+
fun goodPositions() = cache.filter { it.value.any { it.rank.ordinal < 4 } }.map { PossiblePos(it.key.toBlockPos()) }
70+
71+
class PossiblePos(val pos: BlockPos): Drawable {
72+
override fun SafeContext.buildRenderer() {
73+
withBox(Vec3d.ofBottomCenter(pos).playerBox(), Color(0, 255, 0, 50))
6674
}
75+
}
6776

68-
private fun SafeContext.playerFitsIn(pos: Vec3d): Boolean {
69-
val pBox = player.boundingBox
70-
val aabb = Box(pBox.minX, pBox.minY - 1.0E-6, pBox.minZ, pBox.maxX, pBox.minY, pBox.maxZ)
71-
return world.isSpaceEmpty(aabb.offset(pos))
77+
private fun SafeContext.playerFitsIn(pos: BlockPos): Boolean {
78+
return world.isSpaceEmpty(Vec3d.ofBottomCenter(pos).playerBox())
7279
}
7380

7481
companion object {
82+
fun Vec3d.playerBox(): Box = Box(x - 0.3, y, z - 0.3, x + 0.3, y + 1.8, z + 0.3).contract(1.0E-6)
83+
7584
fun Blueprint.simulation(
7685
interact: InteractionConfig = TaskFlowModule.interact,
7786
rotation: RotationConfig = TaskFlowModule.rotation,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ object VisibilityChecker {
138138
) {
139139
excludedSides.forEach { side ->
140140
if (excludedSides.isNotEmpty() && side !in excludedSides) return@forEach
141-
val (minX, minY, minZ, maxX, maxY, maxZ) = box.shrink(0.01, 0.01, 0.01).bounds(side)
141+
val (minX, minY, minZ, maxX, maxY, maxZ) = box.contract(1.0E-6).bounds(side)
142142
val stepX = (maxX - minX) / resolution
143143
val stepY = (maxY - minY) / resolution
144144
val stepZ = (maxZ - minZ) / resolution

common/src/main/kotlin/com/lambda/task/tasks/BuildTask.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import com.lambda.util.BlockUtils.blockState
4747
import com.lambda.util.Communication.info
4848
import com.lambda.util.Formatting.string
4949
import com.lambda.util.extension.Structure
50+
import com.lambda.util.world.toFastVec
5051
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket
5152
import net.minecraft.util.math.BlockPos
5253
import java.util.concurrent.ConcurrentLinkedQueue
@@ -68,6 +69,7 @@ class BuildTask @Ta5kBuilder constructor(
6869
private var currentPlacement: PlaceContext? = null
6970
private var placements = 0
7071
private var breaks = 0
72+
private var goodPositions = setOf<BlockPos>()
7173

7274
override fun SafeContext.onStart() {
7375
(blueprint as? DynamicBlueprint)?.create()
@@ -98,9 +100,16 @@ class BuildTask @Ta5kBuilder constructor(
98100
return@listen
99101
}
100102

103+
// val sim = blueprint.simulation(interact, rotation, inventory)
104+
// BlockPos.iterateOutwards(player.blockPos, 5, 5, 5).forEach { pos ->
105+
// sim.simulate(pos.toFastVec())
106+
// }
107+
101108
// ToDo: Simulate for each pair player positions that work
102109
val results = blueprint.simulate(player.eyePos, interact, rotation, inventory, build)
103-
TaskFlowModule.drawables = results.filterIsInstance<Drawable>().plus(pendingPlacements.toList())
110+
TaskFlowModule.drawables = results.filterIsInstance<Drawable>()
111+
.plus(pendingPlacements.toList())
112+
// .plus(sim.goodPositions())
104113

105114
val instantResults = results.filterIsInstance<BreakResult.Break>()
106115
.filter { it.context.instantBreak }

0 commit comments

Comments
 (0)