Skip to content

Commit 9bae188

Browse files
committed
Added HighwayTools
1 parent 08859f6 commit 9bae188

File tree

13 files changed

+274
-53
lines changed

13 files changed

+274
-53
lines changed

common/src/main/kotlin/com/lambda/command/commands/TransferCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object TransferCommand : LambdaCommand(
1515
) {
1616
override fun CommandBuilder.create() {
1717
execute {
18-
Items.OBSIDIAN.select().transfer(MainHandContainer).solve.start(null)
18+
Items.OBSIDIAN.select().transfer(MainHandContainer)?.solve?.start(null)
1919
}
2020
}
2121
}
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package com.lambda.interaction.construction
22

33
import com.lambda.context.SafeContext
4+
import com.lambda.threading.runSafe
45
import com.lambda.util.primitives.extension.Structure
56
import net.minecraft.util.math.Vec3i
67

78
data class DynamicBlueprint(
8-
val initial: Structure = emptyMap(),
9-
val update: SafeContext.(Structure) -> Structure,
9+
val init: SafeContext.(Structure) -> Structure = { emptyMap() },
10+
val onTick: SafeContext.(Structure) -> Structure = { it },
11+
val onDone: SafeContext.(Structure) -> Structure? = { null }
1012
) : Blueprint() {
11-
fun update(ctx: SafeContext) =
12-
ctx.update(structure)
13+
fun onTick(ctx: SafeContext) {
14+
structure = ctx.onTick(structure)
15+
}
16+
17+
fun onDone(ctx: SafeContext): Boolean {
18+
structure = ctx.onDone(structure) ?: return true
19+
return false
20+
}
1321

14-
override val structure: Structure by lazy { initial }
22+
fun create(ctx: SafeContext) {
23+
structure = ctx.init(structure)
24+
}
25+
26+
override var structure: Structure = emptyMap()
27+
private set
1528

1629
companion object {
1730
fun offset(offset: Vec3i): SafeContext.(Structure) -> Structure = {
@@ -20,8 +33,18 @@ data class DynamicBlueprint(
2033
}.toMap()
2134
}
2235

36+
fun blueprintOnTick(
37+
init: SafeContext.(Structure) -> Structure = { emptyMap() },
38+
onTick: SafeContext.(Structure) -> Structure
39+
) = DynamicBlueprint(init, onTick = onTick)
40+
41+
fun blueprintOnDone(
42+
init: SafeContext.(Structure) -> Structure = { emptyMap() },
43+
onDone: SafeContext.(Structure) -> Structure
44+
) = DynamicBlueprint(init, onDone = onDone)
45+
2346
fun Structure.toBlueprint(
24-
update: SafeContext.(Structure) -> Structure
25-
) = DynamicBlueprint(this, update)
47+
onTick: SafeContext.(Structure) -> Structure
48+
) = DynamicBlueprint({ emptyMap() }, onTick)
2649
}
2750
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ sealed class BreakResult : BuildResult() {
7575
?.solve ?: run {
7676
selectStack {
7777
isItem(badItem).not()
78-
}.transfer(MainHandContainer).solve
78+
}.transfer(MainHandContainer)?.solve ?: emptyTask() // ToDo: Should throw error
7979
}
8080

8181
override fun compareTo(other: ComparableResult<Rank>): Int {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ abstract class BuildResult : ComparableResult<Rank> {
132132
override val rank = Rank.WRONG_ITEM
133133

134134
override val resolve: Task<*> =
135-
neededItem.select().transfer(MainHandContainer).solve
135+
neededItem.select().transfer(MainHandContainer)?.solve ?: emptyTask() // ToDo: Should throw error
136136

137137
override fun compareTo(other: ComparableResult<Rank>): Int {
138138
return when (other) {

common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ object ContainerManager : Loadable {
7272

7373
fun StackSelection.transfer(destination: MaterialContainer) =
7474
findContainerWithSelection(this)?.transfer(this, destination)
75-
?: throw NoContainerFound(this)
7675

7776
fun findContainer(
7877
block: (MaterialContainer) -> Boolean

common/src/main/kotlin/com/lambda/module/modules/debug/BuildTest.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.lambda.module.modules.debug
2+
3+
import com.lambda.interaction.construction.DynamicBlueprint
4+
import com.lambda.interaction.construction.DynamicBlueprint.Companion.blueprintOnDone
5+
import com.lambda.interaction.construction.DynamicBlueprint.Companion.offset
6+
import com.lambda.interaction.construction.verify.TargetState
7+
import com.lambda.module.Module
8+
import com.lambda.module.tag.ModuleTag
9+
import com.lambda.task.tasks.BuildStructure.Companion.buildStructure
10+
import com.lambda.util.KeyCode
11+
import com.lambda.util.player.MovementUtils.direction
12+
import com.lambda.util.primitives.extension.Structure
13+
import com.lambda.util.world.StructureUtils.generateDirectionalTube
14+
import net.minecraft.block.Blocks
15+
import net.minecraft.util.math.BlockPos
16+
import net.minecraft.util.math.EightWayDirection
17+
import net.minecraft.util.math.Vec3i
18+
import kotlin.math.roundToInt
19+
20+
object HighwayTools : Module(
21+
name = "HighwayTools",
22+
description = "Auto highway builder",
23+
defaultTags = setOf(ModuleTag.BUILDING),
24+
defaultKeybind = KeyCode.X
25+
) {
26+
private val height by setting("Height", 4, 1..10, 1)
27+
private val width by setting("Width", 6, 1..100, 1)
28+
private val rimHeight by setting("Rim Height", 1, 1..6, 1)
29+
private val cornerBlock by setting("Corner Block", false, description = "Include corner blocks in the highway")
30+
private val material = Blocks.OBSIDIAN
31+
private val distance by setting("Distance", -1, -1..Int.MAX_VALUE, 100, description = "Distance to build the highway (negative for infinite)")
32+
// ToDo: Fix block setting
33+
// private val material by setting("Material", Blocks.OBSIDIAN, description = "Material to build the highway with")
34+
35+
private var direction = EightWayDirection.NORTH
36+
private var startPos = BlockPos.ORIGIN
37+
38+
init {
39+
onEnable {
40+
direction = player.direction()
41+
startPos = player.blockPos
42+
highwayTask.start(null)
43+
}
44+
onDisable { highwayTask.cancel() }
45+
}
46+
47+
private val highwayTask = buildStructure {
48+
blueprintOnDone({ generateHighway() }) { last ->
49+
val vec = Vec3i(direction.offsetX, 0, direction.offsetZ)
50+
offset(vec).invoke(this, last)
51+
}
52+
}
53+
54+
private fun generateHighway(): Structure {
55+
val structure = mutableMapOf<BlockPos, TargetState>()
56+
val orthogonal = EightWayDirection.entries[(direction.ordinal + 2).mod(8)]
57+
val center = (width / 2.0).roundToInt()
58+
59+
// Area to clear
60+
structure += generateDirectionalTube(
61+
orthogonal,
62+
width,
63+
height,
64+
-center,
65+
-1,
66+
).associateWith { TargetState.Air }
67+
68+
// Highway
69+
structure += generateDirectionalTube(
70+
orthogonal,
71+
width,
72+
1,
73+
-center,
74+
-1,
75+
).associateWith { TargetState.Block(material) }
76+
77+
// Left rim
78+
structure += generateDirectionalTube(
79+
orthogonal,
80+
1,
81+
rimHeight,
82+
-center + width - 1,
83+
0,
84+
).associateWith { TargetState.Block(material) }
85+
86+
// Right rim
87+
structure += generateDirectionalTube(
88+
orthogonal,
89+
1,
90+
rimHeight,
91+
-center,
92+
0,
93+
).associateWith { TargetState.Block(material) }
94+
95+
if (!cornerBlock) {
96+
structure -= generateDirectionalTube(
97+
orthogonal,
98+
1,
99+
1,
100+
-center + width - 1,
101+
-1,
102+
)
103+
104+
structure -= generateDirectionalTube(
105+
orthogonal,
106+
1,
107+
1,
108+
-center,
109+
-1,
110+
)
111+
112+
// Remove the left corner
113+
// structure += generateDirectionalTube(
114+
// orthogonal,
115+
// 1,
116+
// 1,
117+
// -center + width - 1,
118+
// -1,
119+
// ).associateWith { TargetState.Support(Direction.UP) }
120+
121+
// Remove the right corner
122+
// structure += generateDirectionalTube(
123+
// orthogonal,
124+
// 1,
125+
// 1,
126+
// -center,
127+
// -1,
128+
// ).associateWith { TargetState.Support(Direction.UP) }
129+
}
130+
131+
return structure.map { it.key.add(startPos) to it.value }.toMap()
132+
}
133+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.module.modules.player
22

33
import com.lambda.interaction.construction.DynamicBlueprint
4+
import com.lambda.interaction.construction.DynamicBlueprint.Companion.blueprintOnTick
45
import com.lambda.interaction.construction.verify.TargetState
56
import com.lambda.module.Module
67
import com.lambda.module.tag.ModuleTag
@@ -33,7 +34,7 @@ object Nuker : Module(
3334
pathing = false,
3435
finishOnDone = false
3536
) {
36-
DynamicBlueprint { _ ->
37+
blueprintOnTick { _ ->
3738
val selection = BlockPos.iterateOutwards(player.blockPos, range.x, range.y, range.z)
3839
.asSequence()
3940
.map { it.blockPos }

common/src/main/kotlin/com/lambda/module/tag/ModuleTag.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ data class ModuleTag(override val name: String) : Nameable {
2424
val WORLD = ModuleTag("World")
2525
val MISC = ModuleTag("Misc")
2626
val CLIENT = ModuleTag("Client")
27+
val BUILDING = ModuleTag("Building")
2728

2829
// Do something with this ?
2930
val HIDDEN = ModuleTag("Hidden")
@@ -33,6 +34,6 @@ data class ModuleTag(override val name: String) : Nameable {
3334
val AUTOMATION = ModuleTag("Automation")
3435
val DEBUG = ModuleTag("Debug")
3536

36-
val defaults = listOf(COMBAT, MOVEMENT, RENDER, PLAYER, WORLD, DEBUG, CLIENT)
37+
val defaults = listOf(COMBAT, MOVEMENT, RENDER, PLAYER, WORLD, DEBUG, CLIENT, BUILDING)
3738
}
3839
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import net.minecraft.util.math.Direction
4242
import net.minecraft.util.math.Vec3d
4343
import kotlin.math.pow
4444

45-
class BuildStructure(
45+
class BuildStructure @Ta5kBuilder constructor(
4646
private val blueprint: Blueprint,
4747
private val collectDrops: Boolean = false,
4848
private val skipWeakBlocks: Boolean = false,
@@ -53,25 +53,34 @@ class BuildStructure(
5353
) : Task<Unit>() {
5454
private var lastResult: BuildResult? = null
5555

56+
override fun SafeContext.onStart() {
57+
(blueprint as? DynamicBlueprint)?.create(this)
58+
}
59+
5660
init {
5761
listener<TickEvent.Pre> {
58-
val structure = when (blueprint) {
59-
is DynamicBlueprint -> blueprint.update(this)
60-
else -> blueprint.structure
61-
}
62+
(blueprint as? DynamicBlueprint)?.onTick(this)
6263

63-
if (finishOnDone && structure.isEmpty()) {
64+
if (finishOnDone && blueprint.structure.isEmpty()) {
6465
failure("Structure is empty")
6566
return@listener
6667
}
6768

6869
if (finishOnDone && blueprint.isDone(this)) {
70+
if (blueprint is DynamicBlueprint) {
71+
if (!blueprint.onDone(this)) {
72+
this@BuildStructure.info("Structure moved")
73+
return@listener
74+
}
75+
}
76+
6977
this@BuildStructure.info("Structure is done")
78+
cancelSubTasks()
7079
success(Unit)
7180
return@listener
7281
}
7382

74-
val results = structure.entries.fold(mutableSetOf<BuildResult>()) { acc, (pos, target) ->
83+
val results = blueprint.structure.entries.fold(mutableSetOf<BuildResult>()) { acc, (pos, target) ->
7584
checkRequirements(pos, target)?.let {
7685
acc.add(it)
7786
return@fold acc

0 commit comments

Comments
 (0)