Skip to content

Commit 66675f2

Browse files
committed
Structure slicing for HWT
1 parent 518283f commit 66675f2

File tree

3 files changed

+56
-57
lines changed

3 files changed

+56
-57
lines changed
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
package com.lambda.interaction.construction
22

33
import com.lambda.context.SafeContext
4-
import com.lambda.threading.runSafe
54
import com.lambda.util.primitives.extension.Structure
6-
import net.minecraft.util.math.BlockPos
75
import net.minecraft.util.math.Vec3i
86

97
data class DynamicBlueprint(
108
val init: SafeContext.(Structure) -> Structure = { emptyMap() },
11-
val onTick: SafeContext.(Structure) -> Structure = { it },
12-
val onDone: SafeContext.(Structure) -> Structure? = { null },
9+
val update: SafeContext.(Structure) -> Structure = { it },
1310
) : Blueprint() {
14-
fun onTick(ctx: SafeContext) {
15-
structure = ctx.onTick(structure)
16-
}
17-
18-
fun onDone(ctx: SafeContext): Boolean {
19-
structure = ctx.onDone(structure) ?: return true
20-
return false
11+
fun update(ctx: SafeContext) {
12+
structure = ctx.update(structure)
2113
}
2214

2315
fun create(ctx: SafeContext) {
@@ -37,15 +29,10 @@ data class DynamicBlueprint(
3729
fun blueprintOnTick(
3830
init: SafeContext.(Structure) -> Structure = { emptyMap() },
3931
onTick: SafeContext.(Structure) -> Structure
40-
) = DynamicBlueprint(init = init, onTick = onTick)
41-
42-
fun blueprintOnDone(
43-
init: SafeContext.(Structure) -> Structure = { emptyMap() },
44-
onDone: SafeContext.(Structure) -> Structure
45-
) = DynamicBlueprint(init = init, onDone = onDone)
32+
) = DynamicBlueprint(init = init, update = onTick)
4633

4734
fun Structure.toBlueprint(
4835
onTick: SafeContext.(Structure) -> Structure
49-
) = DynamicBlueprint(init = { emptyMap() }, onTick = onTick)
36+
) = DynamicBlueprint(init = { emptyMap() }, update = onTick)
5037
}
5138
}

common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.lambda.module.modules.player
22

33
import baritone.api.pathing.goals.GoalNear
4-
import com.lambda.interaction.construction.DynamicBlueprint
5-
import com.lambda.interaction.construction.DynamicBlueprint.Companion.blueprintOnDone
6-
import com.lambda.interaction.construction.DynamicBlueprint.Companion.offset
4+
import com.lambda.interaction.construction.StaticBlueprint.Companion.toBlueprint
75
import com.lambda.interaction.construction.verify.TargetState
86
import com.lambda.module.Module
97
import com.lambda.module.tag.ModuleTag
108
import com.lambda.task.Task
119
import com.lambda.task.tasks.BuildStructure.Companion.buildStructure
1210
import com.lambda.util.BaritoneUtils.primary
11+
import com.lambda.util.Communication.info
1312
import com.lambda.util.KeyCode
1413
import com.lambda.util.player.MovementUtils.direction
1514
import com.lambda.util.primitives.extension.Structure
@@ -27,11 +26,11 @@ object HighwayTools : Module(
2726
defaultKeybind = KeyCode.X
2827
) {
2928
private val height by setting("Height", 4, 1..10, 1)
30-
private val width by setting("Width", 6, 1..100, 1)
31-
private val rimHeight by setting("Rim Height", 1, 1..6, 1)
29+
private val width by setting("Width", 6, 1..30, 1)
30+
private val rimHeight by setting("Rim Height", 0, 1..6, 1)
3231
private val cornerBlock by setting("Corner Block", false, description = "Include corner blocks in the highway")
3332
private val material = Blocks.OBSIDIAN
34-
private val distance by setting("Distance", -1, -1..1000000, 100, description = "Distance to build the highway (negative for infinite)")
33+
private val distance by setting("Distance", -1, -1..1000000, 1, description = "Distance to build the highway (negative for infinite)")
3534
// ToDo: Fix block setting
3635
// private val material by setting("Material", Blocks.OBSIDIAN, description = "Material to build the highway with")
3736

@@ -45,23 +44,43 @@ object HighwayTools : Module(
4544
onEnable {
4645
direction = player.direction()
4746
startPos = player.blockPos
48-
val task = getTask()
49-
task.start(null)
50-
runningTask = task
47+
currentPos = startPos
48+
buildSlice()
49+
}
50+
onDisable {
51+
runningTask?.cancel()
52+
runningTask = null
53+
distanceMoved = 0
5154
}
52-
onDisable { runningTask?.cancel() }
5355
}
5456

55-
private fun getTask() =
57+
private fun buildSlice() {
58+
val blueprint = generateHighway()
59+
.map { it.key.add(currentPos) to it.value }
60+
.toMap()
61+
.toBlueprint()
62+
5663
buildStructure {
57-
blueprintOnDone({ generateHighway() }) { last ->
64+
blueprint
65+
}.apply {
66+
runningTask = this
67+
primary.customGoalProcess.setGoalAndPath(GoalNear(currentPos, 1))
68+
onSuccess { _, _ ->
5869
distanceMoved++
5970

6071
val vec = Vec3i(direction.offsetX, 0, direction.offsetZ)
6172
currentPos = currentPos.add(vec)
62-
offset(vec).invoke(this, last)
73+
74+
if (distanceMoved < distance || distance < 0) {
75+
buildSlice()
76+
} else {
77+
this@HighwayTools.info("Highway built")
78+
disable()
79+
}
6380
}
81+
start(null)
6482
}
83+
}
6584

6685
private fun generateHighway(): Structure {
6786
val structure = mutableMapOf<BlockPos, TargetState>()
@@ -140,6 +159,6 @@ object HighwayTools : Module(
140159
// ).associateWith { TargetState.Support(Direction.UP) }
141160
}
142161

143-
return structure.map { it.key.add(startPos) to it.value }.toMap()
162+
return structure
144163
}
145164
}

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.task.tasks
22

3-
import baritone.api.pathing.goals.GoalNear
43
import com.lambda.context.SafeContext
54
import com.lambda.event.events.TickEvent
65
import com.lambda.event.listener.SafeListener.Companion.listener
@@ -23,13 +22,10 @@ import com.lambda.interaction.visibilty.VisibilityChecker.mostCenter
2322
import com.lambda.interaction.visibilty.VisibilityChecker.scanVisibleSurfaces
2423
import com.lambda.module.modules.client.TaskFlow
2524
import com.lambda.task.Task
26-
import com.lambda.util.BaritoneUtils.primary
2725
import com.lambda.util.BlockUtils
2826
import com.lambda.util.BlockUtils.blockState
2927
import com.lambda.util.BlockUtils.instantBreakable
30-
import com.lambda.util.Communication.info
3128
import com.lambda.util.math.VecUtils.distSq
32-
import com.lambda.util.primitives.extension.Structure
3329
import com.lambda.util.world.raycast.RayCastUtils.blockResult
3430
import net.minecraft.block.OperatorBlock
3531
import net.minecraft.block.pattern.CachedBlockPosition
@@ -57,39 +53,20 @@ class BuildStructure @Ta5kBuilder constructor(
5753
private val useRayCast: Boolean = TaskFlow.interactionSettings.useRayCast,
5854
) : Task<Unit>() {
5955
private var lastTask: Task<*>? = null
60-
private var doneBlueprint: Structure? = null
6156

6257
override fun SafeContext.onStart() {
6358
(blueprint as? DynamicBlueprint)?.create(this)
6459
}
6560

6661
init {
6762
listener<TickEvent.Pre> {
68-
(blueprint as? DynamicBlueprint)?.onTick(this)
63+
(blueprint as? DynamicBlueprint)?.update(this)
6964

7065
if (finishOnDone && blueprint.structure.isEmpty()) {
7166
failure("Structure is empty")
7267
return@listener
7368
}
7469

75-
doneBlueprint?.entries?.take(3)?.lastOrNull()?.let {
76-
primary.customGoalProcess.setGoalAndPath(GoalNear(it.key.up(), 0))
77-
}
78-
79-
if (finishOnDone && blueprint.isDone(this)) {
80-
doneBlueprint = blueprint.structure
81-
if (blueprint is DynamicBlueprint) {
82-
if (!blueprint.onDone(this)) {
83-
return@listener
84-
}
85-
}
86-
87-
this@BuildStructure.info("Structure is done")
88-
cancelSubTasks()
89-
success(Unit)
90-
return@listener
91-
}
92-
9370
val results = blueprint.structure.entries.fold(mutableSetOf<BuildResult>()) { acc, (pos, target) ->
9471
checkRequirements(pos, target)?.let {
9572
acc.add(it)
@@ -125,7 +102,15 @@ class BuildStructure @Ta5kBuilder constructor(
125102
res
126103

127104
results.minOrNull()?.let { result ->
128-
if (result !is Resolvable) return@listener
105+
if (result !is Resolvable) {
106+
if (result is BuildResult.Done) {
107+
checkDone()
108+
} else {
109+
failure("Failed to resolve build result: $result")
110+
return@listener
111+
}
112+
return@listener
113+
}
129114
if (lastTask?.isCompleted == false) return@listener
130115

131116
lastTask = result.resolve
@@ -137,6 +122,14 @@ class BuildStructure @Ta5kBuilder constructor(
137122
}
138123
}
139124

125+
private fun SafeContext.checkDone() {
126+
if (!finishOnDone) return
127+
128+
cancelSubTasks()
129+
success(Unit)
130+
return
131+
}
132+
140133
private fun SafeContext.checkRequirements(pos: BlockPos, target: TargetState): BuildResult? {
141134
/* the chunk is not loaded */
142135
if (!world.isChunkLoaded(pos)) {

0 commit comments

Comments
 (0)