Skip to content

Commit a78b69b

Browse files
committed
basic air place logic in the build sim for a start
1 parent 5d63517 commit a78b69b

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.lambda.interaction.material.StackSelection.Companion.select
3434
import com.lambda.interaction.material.StackSelection.Companion.selectStack
3535
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
3636
import com.lambda.interaction.material.container.MaterialContainer
37+
import com.lambda.interaction.request.placing.PlaceConfig
3738
import com.lambda.interaction.request.rotation.Rotation.Companion.rotation
3839
import com.lambda.interaction.request.rotation.Rotation.Companion.rotationTo
3940
import com.lambda.interaction.request.rotation.RotationConfig
@@ -72,6 +73,7 @@ import net.minecraft.util.math.BlockPos
7273
import net.minecraft.util.math.Box
7374
import net.minecraft.util.math.Direction
7475
import net.minecraft.util.math.Vec3d
76+
import net.minecraft.util.shape.VoxelShapes
7577
import kotlin.jvm.optionals.getOrNull
7678
import kotlin.math.pow
7779

@@ -87,11 +89,11 @@ object BuildSimulator {
8789
checkRequirements(pos, target, build)?.let {
8890
return@flatMap setOf(it)
8991
}
90-
checkPlaceResults(pos, target, eye, interact, rotation, inventory).let {
92+
checkPlaceResults(pos, target, eye, build.placeSettings, interact, rotation, inventory).let {
9193
if (it.isEmpty()) return@let
9294
return@flatMap it
9395
}
94-
checkBreakResults(pos, eye, interact, rotation, inventory, build).let {
96+
checkBreakResults(pos, eye, build.placeSettings, interact, rotation, inventory, build).let {
9597
if (it.isEmpty()) return@let
9698
return@flatMap it
9799
}
@@ -148,6 +150,7 @@ object BuildSimulator {
148150
pos: BlockPos,
149151
target: TargetState,
150152
eye: Vec3d,
153+
place: PlaceConfig,
151154
interact: InteractionConfig,
152155
rotation: RotationConfig,
153156
inventory: InventoryConfig
@@ -160,10 +163,15 @@ object BuildSimulator {
160163
val preprocessing = target.findProcessorForState()
161164

162165
preprocessing.sides.forEach { neighbor ->
163-
val hitPos = if (targetPosState.isAir || targetPosState.isLiquid) pos.offset(neighbor) else pos
166+
val hitPos = if (!place.airPlace.isEnabled() && targetPosState.isAir || targetPosState.isLiquid)
167+
pos.offset(neighbor)
168+
else pos
164169
val hitSide = neighbor.opposite
165170

166-
val voxelShape = blockState(hitPos).getOutlineShape(world, hitPos)
171+
val voxelShape = blockState(hitPos).getOutlineShape(world, hitPos).let { outlineShape ->
172+
if (!outlineShape.isEmpty || !place.airPlace.isEnabled()) outlineShape
173+
else VoxelShapes.fullCube()
174+
}
167175
if (voxelShape.isEmpty) return@forEach
168176

169177
val boxes = voxelShape.boundingBoxes.map { it.offset(hitPos) }
@@ -220,7 +228,7 @@ object BuildSimulator {
220228
// ToDo: For each hand and sneak or not?
221229
val fakePlayer = copyPlayer(player).apply {
222230
setPos(eye.x, eye.y - standingEyeHeight, eye.z)
223-
this.rotation = checkedHit.targetRotation
231+
if (place.rotateForPlace) this.rotation = checkedHit.targetRotation
224232
}
225233

226234
val checkedResult = checkedHit.hit
@@ -329,6 +337,7 @@ object BuildSimulator {
329337
private fun SafeContext.checkBreakResults(
330338
pos: BlockPos,
331339
eye: Vec3d,
340+
place: PlaceConfig,
332341
interact: InteractionConfig,
333342
rotation: RotationConfig,
334343
inventory: InventoryConfig,
@@ -356,7 +365,7 @@ object BuildSimulator {
356365

357366
/* liquid needs to be submerged first to be broken */
358367
if (!state.fluidState.isEmpty && state.isReplaceable) {
359-
val submerge = checkPlaceResults(pos, TargetState.Solid, eye, interact, rotation, inventory)
368+
val submerge = checkPlaceResults(pos, TargetState.Solid, eye, place, interact, rotation, inventory)
360369
acc.add(BreakResult.Submerge(pos, state, submerge))
361370
acc.addAll(submerge)
362371
return acc
@@ -371,9 +380,9 @@ object BuildSimulator {
371380
acc.add(BreakResult.BlockedByLiquid(pos, state))
372381
adjacentLiquids.forEach { liquidPos ->
373382
val submerge = if (blockState(liquidPos).isReplaceable) {
374-
checkPlaceResults(liquidPos, TargetState.Solid, eye, interact, rotation, inventory)
383+
checkPlaceResults(liquidPos, TargetState.Solid, eye, place, interact, rotation, inventory)
375384
} else {
376-
checkBreakResults(liquidPos, eye, interact, rotation, inventory, build)
385+
checkBreakResults(liquidPos, eye, place, interact, rotation, inventory, build)
377386
}
378387
acc.addAll(submerge)
379388
}

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceConfig.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ abstract class PlaceConfig(
4040
enum class AirPlaceMode {
4141
None,
4242
Standard,
43-
Grim
43+
Grim;
44+
45+
fun isEnabled() = this != None
4446
}
4547

4648
enum class PlaceConfirmationMode {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ enum class PointSelection(val select: (MutableList<VisibilityChecker.CheckedHit>
2828
}
2929
}),
3030
Optimum( optimum@ { hits ->
31-
val optimum = hits.map { it.hit.pos }.reduceOrNull { acc, vec3d ->
32-
acc.add(vec3d)
33-
}?.multiply(1.0 / hits.size.toDouble()) ?: return@optimum null
31+
val optimum = hits
32+
.map { it.hit.pos }.reduceOrNull { acc, vec3d ->
33+
acc?.let { it.add(vec3d) } ?: acc
34+
}
35+
?.multiply(1.0 / hits.size.toDouble()) ?: return@optimum null
3436

3537
hits.minByOrNull {
3638
it.hit.pos distSq optimum

0 commit comments

Comments
 (0)