Skip to content

Commit 6d09f1e

Browse files
committed
dsl oriented sim structure. Im not sure if i like how many objects are created but i guess this is a clean looking solution we can potentially improve performance on after the logic is smoothed
1 parent 8896172 commit 6d09f1e

File tree

7 files changed

+117
-105
lines changed

7 files changed

+117
-105
lines changed

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import com.lambda.context.AutomatedSafeContext
2121
import com.lambda.interaction.construction.blueprint.Blueprint
2222
import com.lambda.interaction.construction.result.BuildResult
2323
import com.lambda.interaction.construction.result.results.PostSimResult
24-
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.simInfo
25-
import com.lambda.interaction.construction.simulation.checks.BreakChecker.Companion.checkBreaks
26-
import com.lambda.interaction.construction.simulation.checks.PlaceChecker.Companion.checkPlacements
27-
import com.lambda.interaction.construction.simulation.checks.PostProcessingChecker.Companion.checkPostProcessing
28-
import com.lambda.interaction.construction.simulation.checks.RequirementChecks.checkRequirements
24+
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.sim
25+
import com.lambda.interaction.construction.simulation.checks.BreakSim.Companion.simBreak
26+
import com.lambda.interaction.construction.simulation.checks.PlaceSim.Companion.simPlacement
27+
import com.lambda.interaction.construction.simulation.checks.PostProcessingSim.Companion.simPostProcessing
28+
import com.lambda.interaction.construction.verify.TargetState
2929
import com.lambda.util.BlockUtils.blockState
3030
import io.ktor.util.collections.*
3131
import kotlinx.coroutines.Dispatchers
@@ -38,36 +38,28 @@ object BuildSimulator : SimChecker<PostSimResult>() {
3838
context(automatedSafeContext: AutomatedSafeContext)
3939
fun Blueprint.simulate(
4040
pov: Vec3d = automatedSafeContext.player.eyePos
41-
): Set<BuildResult> =
42-
runBlocking(Dispatchers.Default) {
43-
supervisorScope {
44-
val concurrentSet = ConcurrentSet<BuildResult>()
41+
): Set<BuildResult> = runBlocking(Dispatchers.Default) {
42+
supervisorScope {
43+
val concurrentSet = ConcurrentSet<BuildResult>()
4544

46-
with(automatedSafeContext) {
47-
structure.entries.forEach { (pos, targetState) ->
48-
launch {
49-
val simInfo = simInfo(
50-
pos,
51-
blockState(pos),
52-
targetState,
53-
pov,
54-
concurrentSet
55-
) ?: return@launch
56-
57-
with(simInfo) {
58-
with(null) {
59-
if (checkRequirements() ||
60-
checkPostProcessing() ||
61-
checkBreaks() ||
62-
checkPlacements()) return@launch
63-
else result(PostSimResult.NoMatch(pos))
64-
}
45+
with(automatedSafeContext) {
46+
structure.forEach { (pos, targetState) ->
47+
launch {
48+
sim(pos, blockState(pos), targetState, pov, concurrentSet) {
49+
if (targetState is TargetState.State &&
50+
targetState.matches(state, pos, preProcessing.ignore)
51+
) {
52+
simPostProcessing()
53+
return@sim
6554
}
55+
if (!targetState.isEmpty() && state.isReplaceable) simPlacement()
56+
else simBreak()
6657
}
6758
}
6859
}
69-
70-
concurrentSet
7160
}
61+
62+
concurrentSet
7263
}
64+
}
7365
}

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818
package com.lambda.interaction.construction.simulation
1919

2020
import com.lambda.context.Automated
21+
import com.lambda.context.AutomatedSafeContext
2122
import com.lambda.interaction.construction.processing.PreProcessingInfo
2223
import com.lambda.interaction.construction.processing.ProcessorRegistry.getProcessingInfo
2324
import com.lambda.interaction.construction.result.BuildResult
2425
import com.lambda.interaction.construction.result.Dependable
26+
import com.lambda.interaction.construction.simulation.checks.RequirementChecker.checkRequirements
2527
import com.lambda.interaction.construction.verify.TargetState
2628
import net.minecraft.block.BlockState
2729
import net.minecraft.util.math.BlockPos
2830
import net.minecraft.util.math.Vec3d
2931
import java.util.*
3032

33+
@DslMarker
34+
annotation class SimBuilderDsl
35+
3136
interface ISimInfo : Automated {
3237
val pos: BlockPos
3338
val state: BlockState
@@ -38,32 +43,53 @@ interface ISimInfo : Automated {
3843
val dependencyStack: Stack<Dependable>
3944

4045
companion object {
41-
@SimCheckerDsl
42-
fun Automated.simInfo(
46+
@SimBuilderDsl
47+
private suspend fun ISimInfo.sim(
48+
dependable: Dependable?,
49+
simBuilder: suspend context(Dependable?) SimBuilder.() -> Unit
50+
) = SimBuilder(this).run { with(dependable) { simBuilder() } }
51+
52+
@SimBuilderDsl
53+
suspend fun AutomatedSafeContext.sim(
4354
pos: BlockPos,
4455
state: BlockState,
4556
targetState: TargetState,
4657
pov: Vec3d,
4758
concurrentResults: MutableSet<BuildResult>,
48-
): SimInfo? {
49-
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
50-
return SimInfo(pos, state, targetState, preProcessingInfo, pov, concurrentResults, this)
59+
simBuilder: suspend context(Dependable?) SimBuilder.() -> Unit
60+
) {
61+
SimInfo(
62+
pos, state, targetState,
63+
targetState.getProcessingInfo(pos) ?: return,
64+
pov, concurrentResults, this
65+
).takeIf { it.checkRequirements() }?.sim(null, simBuilder)
5166
}
5267

53-
@SimCheckerDsl
54-
fun ISimInfo.simInfo(
68+
@SimBuilderDsl
69+
context(_: AutomatedSafeContext, dependable: Dependable)
70+
suspend fun ISimInfo.sim(
5571
pos: BlockPos = this.pos,
5672
state: BlockState = this.state,
5773
targetState: TargetState = this.targetState,
5874
pov: Vec3d = this.pov,
59-
concurrentResults: MutableSet<BuildResult> = this.concurrentResults
60-
): SimInfo? {
61-
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
62-
return SimInfo(pos, state, targetState, preProcessingInfo, pov, concurrentResults, this)
75+
simBuilder: suspend context(Dependable?) SimBuilder.() -> Unit
76+
) {
77+
SimInfo(
78+
pos, state, targetState,
79+
targetState.getProcessingInfo(pos) ?: return,
80+
pov, concurrentResults, this
81+
).takeIf { it.checkRequirements() }?.sim(dependable, simBuilder)
6382
}
83+
84+
@SimBuilderDsl
85+
context(dependable: Dependable)
86+
suspend fun ISimInfo.sim(
87+
simBuilder: suspend context(Dependable?) SimBuilder.() -> Unit
88+
) = sim(dependable, simBuilder)
6489
}
6590
}
6691

92+
@SimBuilderDsl
6793
data class SimInfo(
6894
override val pos: BlockPos,
6995
override val state: BlockState,
@@ -74,4 +100,6 @@ data class SimInfo(
74100
val automated: Automated
75101
) : ISimInfo, Automated by automated {
76102
override val dependencyStack = Stack<Dependable>()
77-
}
103+
}
104+
105+
class SimBuilder(simInfo: ISimInfo) : ISimInfo by simInfo

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ abstract class SimChecker<T : BuildResult> {
5252
}
5353

5454
fun ISimInfo.result(result: GenericResult) = addResult(result)
55-
5655
fun ISimInfo.result(result: T) = addResult(result)
5756

5857
private fun ISimInfo.addResult(result: BuildResult) {

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import com.lambda.interaction.construction.result.Dependable
2424
import com.lambda.interaction.construction.result.results.BreakResult
2525
import com.lambda.interaction.construction.result.results.GenericResult
2626
import com.lambda.interaction.construction.simulation.ISimInfo
27-
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.simInfo
27+
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.sim
28+
import com.lambda.interaction.construction.simulation.SimBuilder
29+
import com.lambda.interaction.construction.simulation.SimBuilderDsl
2830
import com.lambda.interaction.construction.simulation.SimChecker
29-
import com.lambda.interaction.construction.simulation.SimCheckerDsl
30-
import com.lambda.interaction.construction.simulation.SimInfo
31-
import com.lambda.interaction.construction.simulation.checks.PlaceChecker.Companion.checkPlacements
31+
import com.lambda.interaction.construction.simulation.checks.PlaceSim.Companion.simPlacement
3232
import com.lambda.interaction.construction.verify.TargetState
3333
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
3434
import com.lambda.interaction.material.StackSelection
@@ -44,7 +44,6 @@ import com.lambda.interaction.request.rotating.visibilty.lookAtBlock
4444
import com.lambda.util.BlockUtils.blockState
4545
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
4646
import com.lambda.util.BlockUtils.instantBreakable
47-
import com.lambda.util.BlockUtils.isEmpty
4847
import com.lambda.util.item.ItemStackUtils.inventoryIndex
4948
import com.lambda.util.item.ItemStackUtils.inventoryIndexOrSelected
5049
import com.lambda.util.world.raycast.RayCastUtils.blockResult
@@ -67,40 +66,39 @@ import net.minecraft.util.math.BlockPos
6766
import net.minecraft.util.math.Direction
6867
import kotlin.jvm.optionals.getOrNull
6968

70-
class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
69+
class BreakSim private constructor(simInfo: ISimInfo)
7170
: SimChecker<BreakResult>(), Dependable,
7271
ISimInfo by simInfo
7372
{
7473
override fun asDependent(buildResult: BuildResult) =
7574
BreakResult.Dependency(pos, buildResult)
7675

7776
companion object {
78-
@SimCheckerDsl
77+
@SimBuilderDsl
7978
context(automatedSafeContext: AutomatedSafeContext, dependable: Dependable?)
80-
suspend fun SimInfo.checkBreaks() =
81-
BreakChecker(this).run {
79+
suspend fun SimBuilder.simBreak() =
80+
BreakSim(this).run {
8281
checkDependent(dependable)
8382
automatedSafeContext.checkBreaks()
8483
}
8584
}
8685

87-
private suspend fun AutomatedSafeContext.checkBreaks(): Boolean {
86+
private suspend fun AutomatedSafeContext.checkBreaks() {
8887
if (breakConfig.avoidSupporting) player.supportingBlockPos.getOrNull()?.let { support ->
8988
if (support != pos) return@let
9089
result(BreakResult.PlayerOnTop(pos, state))
91-
return true
90+
return
9291
}
9392

9493
if (targetState.getState(pos).isAir && !state.fluidState.isEmpty && state.isReplaceable) {
9594
result(BreakResult.Submerge(pos, state))
96-
return simInfo(pos, state, TargetState.Solid(emptySet()))?.checkPlacements() ?: true
95+
sim(pos, state, TargetState.Solid(emptySet())) { simPlacement() }
96+
return
9797
}
9898

99-
if (state.isEmpty) return false
100-
101-
if (breakConfig.avoidLiquids && affectsFluids()) return true
99+
if (breakConfig.avoidLiquids && affectsFluids()) return
102100

103-
val (swapStack, stackSelection) = getSwapStack() ?: return true
101+
val (swapStack, stackSelection) = getSwapStack() ?: return
104102
val instant = instantBreakable(
105103
state, pos,
106104
if (breakConfig.swapMode.isEnabled()) swapStack else player.mainHandStack,
@@ -124,17 +122,17 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
124122
)
125123
result(BreakResult.Break(pos, breakContext))
126124
}
127-
return true
125+
return
128126
}
129127

130-
val validHits = scanShape(pov, shape, pos, Direction.entries.toSet(), preProcessing) ?: return true
128+
val validHits = scanShape(pov, shape, pos, Direction.entries.toSet(), preProcessing) ?: return
131129

132-
val bestHit = buildConfig.pointSelection.select(validHits) ?: return true
130+
val bestHit = buildConfig.pointSelection.select(validHits) ?: return
133131
val target = lookAt(bestHit.targetRotation, 0.001)
134132
val rotationRequest = RotationRequest(target, this)
135133

136134
val breakContext = BreakContext(
137-
bestHit.hit.blockResult ?: return true,
135+
bestHit.hit.blockResult ?: return,
138136
rotationRequest,
139137
swapStack.inventoryIndexOrSelected,
140138
stackSelection,
@@ -144,7 +142,7 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
144142
)
145143

146144
result(BreakResult.Break(pos, breakContext))
147-
return true
145+
return
148146
}
149147

150148
private fun AutomatedSafeContext.getSwapStack(): Pair<ItemStack, StackSelection>? {
@@ -276,7 +274,7 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
276274

277275
affectedFluids.forEach { (liquidPos, liquidState) ->
278276
result(BreakResult.Submerge(liquidPos, liquidState))
279-
simInfo(liquidPos, liquidState, TargetState.Solid(emptySet()))?.checkPlacements()
277+
sim(liquidPos, liquidState, TargetState.Solid(emptySet())) { simPlacement() }
280278
}
281279
result(BreakResult.BlockedByFluid(pos, state))
282280
return true

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PlaceChecker.kt renamed to src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PlaceSim.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import com.lambda.interaction.construction.result.Dependable
2424
import com.lambda.interaction.construction.result.results.GenericResult
2525
import com.lambda.interaction.construction.result.results.PlaceResult
2626
import com.lambda.interaction.construction.simulation.ISimInfo
27-
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.simInfo
27+
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.sim
28+
import com.lambda.interaction.construction.simulation.SimBuilder
29+
import com.lambda.interaction.construction.simulation.SimBuilderDsl
2830
import com.lambda.interaction.construction.simulation.SimChecker
29-
import com.lambda.interaction.construction.simulation.SimCheckerDsl
30-
import com.lambda.interaction.construction.simulation.SimInfo
31-
import com.lambda.interaction.construction.simulation.checks.BreakChecker.Companion.checkBreaks
32-
import com.lambda.interaction.construction.simulation.checks.PlaceChecker.RotatePlaceTest.Companion.rotatePlaceTest
31+
import com.lambda.interaction.construction.simulation.checks.BreakSim.Companion.simBreak
32+
import com.lambda.interaction.construction.simulation.checks.PlaceSim.RotatePlaceTest.Companion.rotatePlaceTest
3333
import com.lambda.interaction.construction.verify.TargetState
3434
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
3535
import com.lambda.interaction.material.StackSelection.Companion.select
@@ -69,24 +69,24 @@ import net.minecraft.util.math.BlockPos
6969
import net.minecraft.util.math.Direction
7070
import net.minecraft.util.shape.VoxelShapes
7171

72-
class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
72+
class PlaceSim private constructor(simInfo: ISimInfo)
7373
: SimChecker<PlaceResult>(), Dependable,
7474
ISimInfo by simInfo
7575
{
7676
override fun asDependent(buildResult: BuildResult) =
7777
PlaceResult.Dependency(pos, buildResult)
7878

7979
companion object {
80-
@SimCheckerDsl
8180
context(automatedSafeContext: AutomatedSafeContext, dependable: Dependable?)
82-
suspend fun SimInfo.checkPlacements() =
83-
PlaceChecker(this).run {
81+
@SimBuilderDsl
82+
suspend fun SimBuilder.simPlacement() =
83+
PlaceSim(this).run {
8484
checkDependent(dependable)
8585
automatedSafeContext.checkPlacements()
8686
}
8787
}
8888

89-
private suspend fun AutomatedSafeContext.checkPlacements(): Boolean =
89+
private suspend fun AutomatedSafeContext.checkPlacements() =
9090
supervisorScope {
9191
preProcessing.sides.forEach { side ->
9292
launch {
@@ -97,7 +97,6 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
9797
testBlock(pos, side, this@supervisorScope)
9898
}
9999
}
100-
true
101100
}
102101

103102
private suspend fun AutomatedSafeContext.testBlock(pos: BlockPos, side: Direction, supervisorScope: CoroutineScope) {
@@ -192,15 +191,15 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
192191

193192
val placeContext = PlaceContext(
194193
hitResult,
195-
RotationRequest(rotationRequest, this@PlaceChecker),
194+
RotationRequest(rotationRequest, this@PlaceSim),
196195
swapStack.inventoryIndex,
197196
pos,
198197
state,
199198
rotatePlaceTest.resultState,
200199
fakePlayer.isSneaking,
201200
false,
202201
rotatePlaceTest.currentDirIsValid,
203-
this@PlaceChecker
202+
this@PlaceSim
204203
)
205204

206205
result(PlaceResult.Place(pos, placeContext))
@@ -276,7 +275,7 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
276275
}
277276
.flatten()
278277
.forEach { support ->
279-
simInfo(support, blockState(support), TargetState.Empty)?.checkBreaks()
278+
sim(support, blockState(support), TargetState.Empty) { simBreak() }
280279
}
281280
result(PlaceResult.BlockedByEntity(pos, collidingEntities))
282281
}

0 commit comments

Comments
 (0)