Skip to content

Commit 3387261

Browse files
committed
sim cleanup and improvement
1 parent 196b1a3 commit 3387261

File tree

29 files changed

+913
-855
lines changed

29 files changed

+913
-855
lines changed

src/main/kotlin/com/lambda/config/groups/BreakSettings.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class BreakSettings(
7676
// Block
7777
override val avoidLiquids by c.setting("Avoid Liquids", true, "Avoids breaking blocks that would cause liquid to spill", visibility = vis).group(baseGroup, Group.General)
7878
override val avoidSupporting by c.setting("Avoid Supporting", true, "Avoids breaking the block supporting the player", visibility = vis).group(baseGroup, Group.General)
79-
override val breakWeakBlocks by c.setting("Break Weak Blocks", false, "Break blocks that dont have structural integrity (e.g: grass)", visibility = vis).group(baseGroup, Group.General)
8079
override val ignoredBlocks by c.setting("Ignored Blocks", allSigns, description = "Blocks that wont be broken", visibility = vis).group(baseGroup, Group.General)
8180

8281
// Tool

src/main/kotlin/com/lambda/interaction/construction/context/BreakContext.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ import kotlin.random.Random
4343
data class BreakContext(
4444
override val hitResult: BlockHitResult,
4545
override val rotationRequest: RotationRequest,
46-
override var hotbarIndex: Int,
47-
var itemSelection: StackSelection,
48-
var instantBreak: Boolean,
46+
override val hotbarIndex: Int,
47+
val itemSelection: StackSelection,
48+
val instantBreak: Boolean,
4949
override var cachedState: BlockState,
50-
val sortMode: BreakConfig.SortMode,
5150
private val automated: Automated
5251
) : BuildContext(), LogContext, Automated by automated {
5352
private val baseColor = Color(222, 0, 0, 25)
@@ -61,10 +60,10 @@ data class BreakContext(
6160
override fun compareTo(other: BuildContext): Int = runSafe {
6261
return when (other) {
6362
is BreakContext -> compareByDescending<BreakContext> {
64-
if (sortMode == BreakConfig.SortMode.Tool) it.hotbarIndex == HotbarManager.serverSlot
63+
if (breakConfig.sorter == BreakConfig.SortMode.Tool) it.hotbarIndex == HotbarManager.serverSlot
6564
else 0
6665
}.thenBy {
67-
when (sortMode) {
66+
when (breakConfig.sorter) {
6867
BreakConfig.SortMode.Tool,
6968
BreakConfig.SortMode.Closest -> player.eyePos.distance(it.hitResult.pos, it.cachedState.block)
7069
BreakConfig.SortMode.Farthest -> -player.eyePos.distance(it.hitResult.pos, it.cachedState.block)
@@ -104,7 +103,6 @@ data class BreakContext(
104103
value("Instant Break", instantBreak)
105104
value("Cached State", cachedState)
106105
value("Expected State", expectedState)
107-
value("Sort Mode", sortMode)
108106
}
109107
}
110108
}

src/main/kotlin/com/lambda/interaction/construction/processing/ProcessorRegistry.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import com.lambda.core.Loadable
2121
import com.lambda.interaction.construction.verify.TargetState
2222
import com.lambda.util.reflections.getInstances
2323
import net.minecraft.block.BlockState
24-
import net.minecraft.block.SlabBlock
25-
import net.minecraft.block.enums.SlabType
2624
import net.minecraft.state.property.Properties
2725
import net.minecraft.util.math.BlockPos
2826

@@ -108,7 +106,8 @@ object ProcessorRegistry : Loadable {
108106
override fun load() = "Loaded ${processors.size} pre processors"
109107

110108
fun TargetState.getProcessingInfo(pos: BlockPos) =
111-
if (this is TargetState.State) {
109+
if (this !is TargetState.State) PreProcessingInfo.DEFAULT
110+
else {
112111
val get: () -> PreProcessingInfo? = get@{
113112
val infoAccumulator = PreProcessingInfoAccumulator()
114113

@@ -122,15 +121,6 @@ object ProcessorRegistry : Loadable {
122121

123122
infoAccumulator.complete()
124123
}
125-
if (isExemptFromCache()) {
126-
get()
127-
} else {
128-
processorCache.getOrPut(blockState, get)
129-
}
130-
} else {
131-
PreProcessingInfo.DEFAULT
124+
processorCache.getOrPut(blockState, get)
132125
}
133-
134-
private fun TargetState.State.isExemptFromCache() =
135-
blockState.block is SlabBlock && blockState.get(Properties.SLAB_TYPE) == SlabType.DOUBLE
136126
}

src/main/kotlin/com/lambda/interaction/construction/result/Dependable.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

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

20-
import com.lambda.interaction.construction.simulation.SimInfo
20+
import com.lambda.interaction.construction.simulation.ISimInfo
2121

22-
interface Dependable {
23-
fun SimInfo.asDependent(buildResult: BuildResult): BuildResult
22+
interface Dependable : ISimInfo {
23+
fun asDependent(buildResult: BuildResult): BuildResult
2424
}

src/main/kotlin/com/lambda/interaction/construction/result/Rank.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ enum class Rank {
3131
ChunkNotLoaded,
3232
PlaceCantReplace,
3333
BreakPlayerOnTop,
34-
PlaceNotItemBlock,
3534

3635
// not solvable
3736
OutOfWorld,
@@ -50,7 +49,4 @@ enum class Rank {
5049
Done,
5150
Ignored,
5251
NoMatch;
53-
54-
val solvable: Boolean
55-
get() = ordinal < PlaceNotItemBlock.ordinal
5652
}

src/main/kotlin/com/lambda/interaction/construction/result/results/GenericResult.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package com.lambda.interaction.construction.result.results
2020
import baritone.api.pathing.goals.GoalNear
2121
import com.lambda.context.Automated
2222
import com.lambda.graphics.renderer.esp.ShapeBuilder
23-
import com.lambda.interaction.construction.context.BuildContext
2423
import com.lambda.interaction.construction.result.BuildResult
2524
import com.lambda.interaction.construction.result.ComparableResult
2625
import com.lambda.interaction.construction.result.Drawable
@@ -82,7 +81,6 @@ sealed class GenericResult : BuildResult() {
8281
*/
8382
data class WrongItemSelection(
8483
override val pos: BlockPos,
85-
val context: BuildContext,
8684
val neededSelection: StackSelection,
8785
val currentItem: ItemStack
8886
) : Drawable, Resolvable, GenericResult() {
@@ -102,13 +100,6 @@ sealed class GenericResult : BuildResult() {
102100
override fun ShapeBuilder.buildRenderer() {
103101
box(pos, color, color)
104102
}
105-
106-
override fun compareResult(other: ComparableResult<Rank>): Int {
107-
return when (other) {
108-
is WrongItemSelection -> context.compareTo(other.context)
109-
else -> super.compareResult(other)
110-
}
111-
}
112103
}
113104

114105
/**

src/main/kotlin/com/lambda/interaction/construction/result/results/PlaceResult.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,6 @@ sealed class PlaceResult : BuildResult() {
182182
override val rank = Rank.PlaceIllegalUsage
183183
}
184184

185-
/**
186-
* Represents the result of a place operation where the provided item does not match the expected item block type.
187-
*
188-
* @property pos The position of the block where the operation was attempted.
189-
* @property itemStack The item stack that was checked during the place operation.
190-
*/
191-
data class NotItemBlock(
192-
override val pos: BlockPos,
193-
val itemStack: ItemStack,
194-
) : PlaceResult() {
195-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
196-
override val rank = Rank.PlaceNotItemBlock
197-
}
198-
199185
data class Dependency(
200186
override val pos: BlockPos,
201187
override val dependency: BuildResult

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,45 @@ package com.lambda.interaction.construction.simulation
1919

2020
import com.lambda.context.AutomatedSafeContext
2121
import com.lambda.interaction.construction.blueprint.Blueprint
22-
import com.lambda.interaction.construction.processing.ProcessorRegistry.getProcessingInfo
2322
import com.lambda.interaction.construction.result.BuildResult
2423
import com.lambda.interaction.construction.result.results.PostSimResult
25-
import com.lambda.interaction.construction.simulation.checks.BreakChecks.checkBreaks
26-
import com.lambda.interaction.construction.simulation.checks.PlaceChecks.checkPlacements
27-
import com.lambda.interaction.construction.simulation.checks.PostProcessingChecks.checkPostProcessing
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
2828
import com.lambda.interaction.construction.simulation.checks.RequirementChecks.checkRequirements
2929
import com.lambda.util.BlockUtils.blockState
3030
import io.ktor.util.collections.*
3131
import kotlinx.coroutines.Dispatchers
32-
import kotlinx.coroutines.async
33-
import kotlinx.coroutines.awaitAll
32+
import kotlinx.coroutines.joinAll
33+
import kotlinx.coroutines.launch
3434
import kotlinx.coroutines.runBlocking
3535

3636
object BuildSimulator : SimChecker<PostSimResult>() {
37-
context(context: AutomatedSafeContext)
37+
context(automatedSafeContext: AutomatedSafeContext)
3838
fun Blueprint.simulate(): Set<BuildResult> = runBlocking(Dispatchers.Default) {
3939
val concurrentSet = ConcurrentSet<BuildResult>()
40-
with(context) {
40+
with(automatedSafeContext) {
4141
structure.entries
4242
.map { (pos, targetState) ->
43-
async {
44-
val preProcessing = targetState.getProcessingInfo(pos) ?: return@async
45-
val simInfo = SimInfo(
43+
launch {
44+
val simInfo = simInfo(
4645
pos,
4746
blockState(pos),
4847
targetState,
49-
preProcessing,
5048
concurrentSet
51-
)
49+
) ?: return@launch
5250
with(simInfo) {
5351
with(null) {
54-
checkRequirements()
55-
checkPostProcessing()
56-
checkPlacements()
57-
checkBreaks()
52+
if (checkRequirements() ||
53+
checkPostProcessing() ||
54+
checkPlacements() ||
55+
checkBreaks()) return@launch
56+
else result(PostSimResult.NoMatch(pos))
5857
}
5958
}
6059
}
61-
}.awaitAll()
60+
}.joinAll()
6261
}
6362

6463
return@runBlocking concurrentSet
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.interaction.construction.simulation
19+
20+
import com.lambda.context.Automated
21+
import com.lambda.interaction.construction.processing.PreProcessingInfo
22+
import com.lambda.interaction.construction.processing.ProcessorRegistry.getProcessingInfo
23+
import com.lambda.interaction.construction.result.BuildResult
24+
import com.lambda.interaction.construction.result.Dependable
25+
import com.lambda.interaction.construction.verify.TargetState
26+
import net.minecraft.block.BlockState
27+
import net.minecraft.util.math.BlockPos
28+
import java.util.*
29+
30+
interface ISimInfo : Automated {
31+
val pos: BlockPos
32+
val state: BlockState
33+
val targetState: TargetState
34+
val preProcessing: PreProcessingInfo
35+
val concurrentResults: MutableSet<BuildResult>
36+
val dependencyStack: Stack<Dependable>
37+
38+
companion object {
39+
@SimCheckerDsl
40+
fun Automated.simInfo(
41+
pos: BlockPos,
42+
state: BlockState,
43+
targetState: TargetState,
44+
concurrentResults: MutableSet<BuildResult>,
45+
): SimInfo? {
46+
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
47+
return SimInfo(pos, state, targetState, preProcessingInfo, concurrentResults, this)
48+
}
49+
50+
@SimCheckerDsl
51+
fun ISimInfo.simInfo(
52+
pos: BlockPos = this.pos,
53+
state: BlockState = this.state,
54+
targetState: TargetState = this.targetState,
55+
concurrentResults: MutableSet<BuildResult> = this.concurrentResults
56+
): SimInfo? {
57+
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
58+
return SimInfo(pos, state, targetState, preProcessingInfo, concurrentResults, this)
59+
}
60+
}
61+
}
62+
63+
data class SimInfo(
64+
override val pos: BlockPos,
65+
override val state: BlockState,
66+
override val targetState: TargetState,
67+
override val preProcessing: PreProcessingInfo,
68+
override val concurrentResults: MutableSet<BuildResult>,
69+
val automated: Automated
70+
) : ISimInfo, Automated by automated {
71+
override val dependencyStack = Stack<Dependable>()
72+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,28 @@ import com.lambda.interaction.construction.result.Dependable
2323
import com.lambda.interaction.construction.result.results.GenericResult
2424
import net.minecraft.util.math.Vec3d
2525

26+
@DslMarker
27+
annotation class SimCheckerDsl
28+
29+
@SimCheckerDsl
2630
abstract class SimChecker<T : BuildResult> {
2731
val SafeContext.eye: Vec3d get() = player.eyePos
2832

29-
fun SimInfo.checkDependent(caller: Dependable?) {
33+
protected fun ISimInfo.checkDependent(caller: Dependable?) {
3034
if (caller == null) {
3135
dependencyStack.clear()
3236
return
3337
}
3438
dependencyStack.push(caller)
3539
}
3640

37-
fun SimInfo.result(result: GenericResult) = addResult(result)
38-
fun SimInfo.result(result: T) = addResult(result)
39-
private fun SimInfo.addResult(result: BuildResult) {
40-
concurrentResults.add(
41+
fun ISimInfo.result(result: GenericResult) = addResult(result)
42+
43+
fun ISimInfo.result(result: T) = addResult(result)
44+
45+
private fun ISimInfo.addResult(result: BuildResult) {
46+
if (this@SimChecker is BuildSimulator) concurrentResults.add(result)
47+
else concurrentResults.add(
4148
dependencyStack
4249
.asReversed()
4350
.fold(result) { acc, dependable ->

0 commit comments

Comments
 (0)