Skip to content

Commit 6b27bcc

Browse files
committed
Dynamic blueprints and structure building prototype
1 parent 1978c74 commit 6b27bcc

File tree

26 files changed

+415
-126
lines changed

26 files changed

+415
-126
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/vao/VAO.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import com.lambda.graphics.gl.VaoUtils.bufferData
1919
import com.lambda.graphics.gl.VaoUtils.unbindIndexBuffer
2020
import com.lambda.graphics.gl.VaoUtils.unbindVertexArray
2121
import com.lambda.graphics.gl.VaoUtils.unbindVertexBuffer
22-
import com.lambda.threading.mainThread
23-
import com.lambda.threading.runOnGameThread
22+
import com.lambda.threading.runGameScheduled
2423
import com.mojang.blaze3d.systems.RenderSystem.drawElements
2524
import org.lwjgl.opengl.GL30C.*
2625
import java.awt.Color
@@ -50,7 +49,7 @@ class VAO(
5049
val stride = attribGroup.stride
5150
objectSize = stride * drawMode.indicesCount
5251

53-
runGameConcurrent {
52+
runGameScheduled {
5453
vertices = byteBuffer(objectSize * 256 * 4)
5554
verticesPointer = address(vertices)
5655
verticesPosition = verticesPointer
@@ -198,7 +197,7 @@ class VAO(
198197
}
199198

200199
fun destroy() {
201-
runOnGameThread {
200+
runGameScheduled {
202201
glDeleteBuffers(ibo)
203202
glDeleteBuffers(vbo)
204203
glDeleteVertexArrays(vao)

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.lambda.interaction.rotation.Rotation.Companion.slerp
1515
import com.lambda.interaction.rotation.RotationContext
1616
import com.lambda.interaction.rotation.RotationMode
1717
import com.lambda.module.modules.client.Baritone
18-
import com.lambda.threading.runGameConcurrent
18+
import com.lambda.threading.runGameScheduled
1919
import com.lambda.threading.runSafe
2020
import com.lambda.util.math.MathUtils.lerp
2121
import com.lambda.util.math.MathUtils.toRadian
@@ -50,7 +50,7 @@ object RotationManager : Loadable {
5050
val packet = event.packet
5151
if (packet !is PlayerPositionLookS2CPacket) return@listener
5252

53-
runGameConcurrent {
53+
runGameScheduled {
5454
reset(Rotation(packet.yaw, packet.pitch))
5555
}
5656
}
Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
package com.lambda.interaction.construction
22

3+
import com.lambda.context.SafeContext
34
import com.lambda.interaction.construction.verify.TargetState
5+
import com.lambda.util.BlockUtils.blockPos
6+
import com.lambda.util.primitives.extension.Structure
47
import net.minecraft.structure.StructureTemplate
58
import net.minecraft.util.math.BlockBox
69
import net.minecraft.util.math.BlockPos
710
import net.minecraft.util.math.Box
811

9-
data class Blueprint(
10-
private val structure: Map<BlockPos, TargetState>,
11-
var offset: BlockPos? = null,
12-
) {
13-
val origin: BlockPos
14-
get() = offset ?: structure.keys.firstOrNull() ?: BlockPos.ORIGIN
15-
16-
private val modifications = mutableMapOf<BlockPos, TargetState>()
17-
val structureMap: Map<BlockPos, TargetState>
18-
get() {
19-
offset?.let {
20-
val offsetMap = mutableMapOf<BlockPos, TargetState>()
21-
for ((pos, state) in structure) {
22-
offsetMap[pos.add(it)] = state
23-
}
24-
return offsetMap + modifications
25-
}
12+
abstract class Blueprint {
13+
abstract val structure: Structure
2614

27-
return structure + modifications
15+
fun isDone(safeContext: SafeContext) =
16+
structure.all { (pos, targetState) ->
17+
with(safeContext) {
18+
targetState.matches(world.getBlockState(pos), pos, world)
19+
}
2820
}
2921

3022
companion object {
31-
fun Box.from(targetState: TargetState) =
32-
Blueprint(BlockPos.stream(this).map { BlockPos(it) }.toList().associateWith { targetState })
33-
34-
fun BlockBox.from(targetState: TargetState) =
35-
Blueprint(BlockPos.stream(this).map { BlockPos(it) }.toList().associateWith { targetState })
36-
37-
fun BlockPos.from(targetState: TargetState) =
38-
Blueprint(setOf(this).associateWith { targetState })
23+
fun Box.toStructure(targetState: TargetState): Structure =
24+
BlockPos.stream(this)
25+
.map { it.blockPos }
26+
.toList()
27+
.associateWith { targetState }
28+
29+
fun BlockBox.toStructure(targetState: TargetState): Structure =
30+
BlockPos.stream(this)
31+
.map { it.blockPos }
32+
.toList()
33+
.associateWith { targetState }
34+
35+
fun BlockPos.toStructure(targetState: TargetState): Structure =
36+
setOf(this)
37+
.associateWith { targetState }
3938

4039
// fun Schematic.fromSchematic() =
41-
// Blueprint(this.blockMap.map { it.key to TargetState.BlockState(it.value) }.toMap())
42-
43-
fun StructureTemplate.fromStructureTemplate() =
44-
Blueprint(
45-
blockInfoLists
46-
.flatMap { it.all }
47-
.associate { it.pos to TargetState.State(it.state) }
48-
)
40+
// this.blockMap.map { it.key to TargetState.BlockState(it.value) }.toMap()
41+
42+
fun StructureTemplate.toStructure(): Structure =
43+
blockInfoLists
44+
.flatMap { it.all }
45+
.associate { it.pos to TargetState.State(it.state) }
4946
}
5047
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lambda.interaction.construction
2+
3+
import com.lambda.context.SafeContext
4+
import com.lambda.util.primitives.extension.Structure
5+
import net.minecraft.util.math.Vec3i
6+
7+
data class DynamicBlueprint(
8+
val initial: Structure = emptyMap(),
9+
val update: SafeContext.(Structure) -> Structure,
10+
) : Blueprint() {
11+
fun update(safeContext: SafeContext) =
12+
safeContext.update(structure)
13+
14+
override val structure: Structure by lazy { initial }
15+
16+
companion object {
17+
fun offset(offset: Vec3i): SafeContext.(Structure) -> Structure = {
18+
it.map { (pos, state) ->
19+
pos.add(offset) to state
20+
}.toMap()
21+
}
22+
23+
fun Structure.toBlueprint(
24+
update: SafeContext.(Structure) -> Structure
25+
) = DynamicBlueprint(this, update)
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.lambda.interaction.construction
2+
3+
import com.lambda.util.primitives.extension.Structure
4+
5+
data class StaticBlueprint(
6+
override val structure: Structure
7+
) : Blueprint() {
8+
companion object {
9+
fun Structure.toBlueprint() = StaticBlueprint(this)
10+
}
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.lambda.interaction.construction.context
2+
3+
import net.minecraft.block.BlockState
4+
import net.minecraft.util.Hand
5+
import net.minecraft.util.hit.BlockHitResult
6+
import net.minecraft.util.math.BlockPos
7+
import net.minecraft.util.math.Direction
8+
import net.minecraft.util.math.Vec3d
9+
10+
data class BreakContext(
11+
// val hitVec: Vec3d,
12+
val hitPos: BlockPos,
13+
val exposedSides: Int,
14+
// val side: Direction,
15+
override val distance: Double,
16+
// var hand: Hand,
17+
// val pointOfView: Vec3d,
18+
// val blockState: BlockState,
19+
) : BuildContext, ComparableContext {
20+
// override val resultingPos = hitPos
21+
// override fun toBlockHitResult() = BlockHitResult(hitVec, side, hitPos, false)
22+
23+
// val expectedBlockState = blockState.fluidState.blockState
24+
25+
override fun compareTo(other: ComparableContext): Int {
26+
return when (other) {
27+
is BreakContext -> compareByDescending<BreakContext> {
28+
it.exposedSides
29+
}.thenBy {
30+
it.distance
31+
}.compare(this, other)
32+
else -> 1
33+
}
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.lambda.interaction.construction.context
2+
3+
import net.minecraft.util.hit.BlockHitResult
4+
import net.minecraft.util.math.BlockPos
5+
6+
interface BuildContext {
7+
val distance: Double
8+
// val resultingPos: BlockPos
9+
// fun toBlockHitResult(): BlockHitResult
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lambda.interaction.construction.context
2+
3+
interface ComparableContext : Comparable<ComparableContext> {
4+
override fun compareTo(other: ComparableContext): Int {
5+
return 0
6+
}
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lambda.interaction.construction.result
2+
3+
import com.lambda.interaction.construction.context.BreakContext
4+
import com.lambda.task.buildChain
5+
import com.lambda.task.tasks.BreakBlock.Companion.breakBlock
6+
7+
sealed class BreakResult : BuildResult() {
8+
9+
/**
10+
* Represents a successful break. All checks have been passed.
11+
* @param context The context of the break.
12+
*/
13+
data class Success(val context: BreakContext) : Resolvable, BreakResult() {
14+
override val rank = Rank.BREAK_SUCCESS
15+
16+
override val resolve = buildChain {
17+
breakBlock(context.hitPos)
18+
}
19+
20+
override fun compareTo(other: ComparableResult<Rank>): Int {
21+
return when (other) {
22+
is Success -> context.compareTo(other.context)
23+
else -> super.compareTo(other)
24+
}
25+
}
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.interaction.construction.result
2+
3+
abstract class BuildResult : ComparableResult<Rank> {
4+
5+
/**
6+
* The build action is done.
7+
*/
8+
data object Done : BuildResult() {
9+
override val rank = Rank.DONE
10+
}
11+
12+
/**
13+
* The build action is ignored.
14+
*/
15+
data object Ignored : BuildResult() {
16+
override val rank = Rank.IGNORED
17+
}
18+
}

0 commit comments

Comments
 (0)