Skip to content

Commit c813e30

Browse files
committed
Break result optimization
1 parent 69d31f2 commit c813e30

File tree

25 files changed

+674
-140
lines changed

25 files changed

+674
-140
lines changed

common/src/main/java/com/lambda/mixin/render/DebugHudMixin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.mixin.render;
22

3+
import com.lambda.task.RootTask;
34
import com.lambda.util.DebugInfoHud;
45
import net.minecraft.client.gui.hud.DebugHud;
56
import org.spongepowered.asm.mixin.Mixin;
@@ -15,4 +16,9 @@ public class DebugHudMixin {
1516
private void onGetRightText(CallbackInfoReturnable<List<String>> cir) {
1617
DebugInfoHud.addDebugInfo(cir.getReturnValue());
1718
}
19+
20+
@Inject(method = "getLeftText", at = @At(value = "TAIL"))
21+
private void onGetLeftText(CallbackInfoReturnable<List<String>> cir) {
22+
RootTask.INSTANCE.addInfo(cir.getReturnValue());
23+
}
1824
}

common/src/main/kotlin/com/lambda/config/InteractionSettings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class InteractionSettings(
1010
override val reach by c.setting("Reach", 5.0, 0.1..10.0, 0.1, "Players reach / range", "", vis)
1111
override val resolution by c.setting("Resolution", 10, 1..100, 1, "Raycast resolution", "", vis)
1212
override val rayCastMask by c.setting("Raycast Mask", RayCastMask.BOTH, "What to raycast against", vis)
13+
override val ignoreRayCast by c.setting("Ignore Raycast", true, "Ignore raycast when looking at blocks", vis)
1314
}

common/src/main/kotlin/com/lambda/event/events/RotationEvent.kt

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,6 @@ abstract class RotationEvent : Event {
3535
this.context = context
3636
}
3737
}
38-
39-
fun lookAtEntity(
40-
rotationConfig: IRotationConfig,
41-
interactionConfig: InteractionConfig,
42-
entity: Entity
43-
) {
44-
runSafe {
45-
findRotation(rotationConfig, interactionConfig, listOf(entity.boundingBox)) {
46-
entityResult?.entity == entity
47-
}?.let { rotationContext ->
48-
context = rotationContext
49-
}
50-
}
51-
}
52-
53-
fun lookAtBlock(
54-
blockPos: BlockPos,
55-
rotationConfig: IRotationConfig = TaskFlow.rotationSettings,
56-
interactionConfig: InteractionConfig = TaskFlow.interactionSettings,
57-
sides: Set<Direction> = emptySet()
58-
) = runSafe {
59-
val state = blockPos.blockState(world)
60-
val voxelShape = state.getOutlineShape(world, blockPos)
61-
val boundingBoxes = voxelShape.boundingBoxes.map { it.offset(blockPos) }
62-
findRotation(rotationConfig, interactionConfig, boundingBoxes, sides) {
63-
blockResult?.blockPos == blockPos && (blockResult?.side in sides || sides.isEmpty())
64-
}?.let {
65-
context = it
66-
return@runSafe it
67-
}
68-
return@runSafe null
69-
}
7038
}
7139

7240
class Post(val context: RotationContext) : RotationEvent()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ interface InteractionConfig {
1414
val resolution: Int
1515

1616
val rayCastMask: RayCastMask
17+
18+
val ignoreRayCast: Boolean
1719
}

common/src/main/kotlin/com/lambda/interaction/construction/Blueprint.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import net.minecraft.util.math.Box
1313
abstract class Blueprint {
1414
abstract val structure: Structure
1515

16-
open fun isDone(safeContext: SafeContext) =
16+
open fun isDone(ctx: SafeContext) =
1717
structure.all { (pos, targetState) ->
18-
with(safeContext) {
18+
with(ctx) {
1919
targetState.matches(pos.blockState(world), pos, world)
2020
}
2121
}

common/src/main/kotlin/com/lambda/interaction/construction/DynamicBlueprint.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ data class DynamicBlueprint(
88
val initial: Structure = emptyMap(),
99
val update: SafeContext.(Structure) -> Structure,
1010
) : Blueprint() {
11-
fun update(safeContext: SafeContext) =
12-
safeContext.update(structure)
11+
fun update(ctx: SafeContext) =
12+
ctx.update(structure)
1313

1414
override val structure: Structure by lazy { initial }
1515

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
package com.lambda.interaction.construction.context
22

3+
import com.lambda.context.SafeContext
4+
import com.lambda.interaction.rotation.RotationContext
5+
import com.lambda.util.world.raycast.RayCastUtils.distanceTo
36
import net.minecraft.block.BlockState
47
import net.minecraft.util.Hand
58
import net.minecraft.util.hit.BlockHitResult
6-
import net.minecraft.util.math.BlockPos
79
import net.minecraft.util.math.Direction
810
import net.minecraft.util.math.Vec3d
911

1012
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,
13+
val pov: Vec3d,
14+
val result: BlockHitResult,
15+
val rotation: RotationContext,
16+
val checkedState: BlockState,
17+
val hand: Hand,
18+
val instantBreak: Boolean,
1919
) : BuildContext, ComparableContext {
20-
// override val resultingPos = hitPos
21-
// override fun toBlockHitResult() = BlockHitResult(hitVec, side, hitPos, false)
20+
override val distance: Double by lazy {
21+
result.distanceTo(pov)
22+
}
23+
24+
fun exposedSides(ctx: SafeContext) =
25+
Direction.entries.filter {
26+
ctx.world.isAir(result.blockPos.offset(it))
27+
}
2228

23-
// val expectedBlockState = blockState.fluidState.blockState
29+
override val expectedState = checkedState.fluidState.blockState
2430

2531
override fun compareTo(other: ComparableContext): Int {
2632
return when (other) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.lambda.interaction.construction.context
22

3-
import net.minecraft.util.hit.BlockHitResult
4-
import net.minecraft.util.math.BlockPos
3+
import net.minecraft.block.BlockState
54

65
interface BuildContext {
76
val distance: Double
7+
val expectedState: BlockState
88
// val resultingPos: BlockPos
9-
// fun toBlockHitResult(): BlockHitResult
109
}
Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package com.lambda.interaction.construction.result
22

33
import com.lambda.interaction.construction.context.BreakContext
4-
import com.lambda.task.tasks.BreakBlock.Companion.uncheckedBreak
4+
import com.lambda.interaction.material.ContainerManager.findBestAvailableTool
5+
import com.lambda.interaction.material.StackSelection.Companion.select
6+
import com.lambda.interaction.material.StackSelection.Companion.selectStack
7+
import com.lambda.interaction.material.container.CreativeContainer.transfer
8+
import com.lambda.interaction.material.container.MainHandContainer
9+
import com.lambda.task.Task
10+
import com.lambda.task.Task.Companion.emptyTask
11+
import com.lambda.task.tasks.BreakBlock.Companion.breakBlock
12+
import com.lambda.task.tasks.GoalTask.Companion.moveToBlock
13+
import net.minecraft.block.BlockState
14+
import net.minecraft.item.Item
15+
import net.minecraft.util.math.BlockPos
16+
import net.minecraft.util.math.Direction
517

618
sealed class BreakResult : BuildResult() {
719

820
/**
921
* Represents a successful break. All checks have been passed.
1022
* @param context The context of the break.
1123
*/
12-
data class Success(val context: BreakContext) : Resolvable, BreakResult() {
24+
data class Success(
25+
override val blockPos: BlockPos,
26+
val context: BreakContext
27+
) : Resolvable, BreakResult() {
1328
override val rank = Rank.BREAK_SUCCESS
1429

15-
override val resolve = uncheckedBreak(context.hitPos)
30+
override val resolve = breakBlock(context)
1631

1732
override fun compareTo(other: ComparableResult<Rank>): Int {
1833
return when (other) {
@@ -21,4 +36,150 @@ sealed class BreakResult : BuildResult() {
2136
}
2237
}
2338
}
39+
40+
/**
41+
* Represents a break out of reach.
42+
* @param blockPos The position of the block that is out of reach.
43+
* @param distance The distance to the hit vector.
44+
*/
45+
data class OutOfReach(
46+
override val blockPos: BlockPos,
47+
val distance: Double
48+
) : Resolvable, BreakResult() {
49+
override val rank = Rank.BREAK_OUT_OF_REACH
50+
51+
override val resolve = moveToBlock(blockPos)
52+
53+
override fun compareTo(other: ComparableResult<Rank>): Int {
54+
return when (other) {
55+
is OutOfReach -> distance.compareTo(other.distance)
56+
else -> super.compareTo(other)
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Represents a break configuration where the hit side is not exposed to air.
63+
* @param blockPos The position of the block that is not exposed.
64+
* @param side The side that is not exposed.
65+
*/
66+
data class NotExposed(
67+
override val blockPos: BlockPos,
68+
val side: Direction
69+
) : Resolvable, BreakResult() {
70+
override val rank = Rank.BREAK_NOT_EXPOSED
71+
72+
override val resolve = emptyTask()
73+
74+
override fun compareTo(other: ComparableResult<Rank>): Int {
75+
return when (other) {
76+
is NotExposed -> blockPos.compareTo(other.blockPos)
77+
else -> super.compareTo(other)
78+
}
79+
}
80+
}
81+
82+
/**
83+
* The checked break configuration hits on a side not in the player direction.
84+
* @param blockPos The position of the block that is not exposed.
85+
* @param side The side that is not exposed.
86+
*/
87+
data class NotVisible(
88+
override val blockPos: BlockPos,
89+
val side: Direction,
90+
val distance: Double
91+
) : Resolvable, BreakResult() {
92+
override val rank = Rank.BREAK_NOT_VISIBLE
93+
94+
override val resolve = emptyTask()
95+
96+
override fun compareTo(other: ComparableResult<Rank>): Int {
97+
return when (other) {
98+
is NotVisible -> distance.compareTo(other.distance)
99+
else -> super.compareTo(other)
100+
}
101+
}
102+
}
103+
104+
/**
105+
* The equipped item is not suitable for breaking blocks.
106+
* @param blockState The block state that is being broken.
107+
* @param badItem The item that is being used.
108+
*/
109+
data class ItemCantMine(
110+
override val blockPos: BlockPos,
111+
val blockState: BlockState,
112+
val badItem: Item
113+
) : Resolvable, BreakResult() {
114+
override val rank = Rank.BREAK_ITEM_CANT_MINE
115+
override val resolve = findBestAvailableTool(blockState)
116+
?.select()
117+
?.transfer(MainHandContainer)
118+
?.solve ?: run {
119+
selectStack {
120+
isItem(badItem).not()
121+
}.transfer(MainHandContainer).solve
122+
}
123+
124+
override fun compareTo(other: ComparableResult<Rank>): Int {
125+
return when (other) {
126+
is ItemCantMine -> badItem.name.string.compareTo(other.badItem.name.string)
127+
else -> super.compareTo(other)
128+
}
129+
}
130+
}
131+
132+
/**
133+
* Player has an inefficient tool equipped.
134+
* @param bestTool The best tool for the block state.
135+
*/
136+
data class WrongTool(
137+
override val blockPos: BlockPos,
138+
val context: BreakContext,
139+
val bestTool: Item
140+
) : Resolvable, BreakResult() {
141+
override val rank = Rank.BREAK_WRONG_TOOL
142+
143+
override val resolve: Task<*> =
144+
bestTool.select().transfer(MainHandContainer).solve
145+
146+
override fun compareTo(other: ComparableResult<Rank>): Int {
147+
return when (other) {
148+
is WrongTool -> context.compareTo(other.context)
149+
else -> super.compareTo(other)
150+
}
151+
}
152+
}
153+
154+
/**
155+
* The block is a liquid and first has to be submerged.
156+
* @param blockPos The position of the block that is a liquid.
157+
*/
158+
data class Submerge(
159+
override val blockPos: BlockPos,
160+
val blockState: BlockState,
161+
val submerge: Set<BuildResult>
162+
) : BreakResult() {
163+
override val rank = Rank.BREAK_SUBMERGE
164+
}
165+
166+
/**
167+
* The block is blocked by another liquid block that first has to be submerged.
168+
*/
169+
data class BlockedByLiquid(
170+
override val blockPos: BlockPos,
171+
val blockState: BlockState
172+
) : BreakResult() {
173+
override val rank = Rank.BREAK_IS_BLOCKED_BY_LIQUID
174+
}
175+
176+
/**
177+
* The player is standing on the block.
178+
*/
179+
data class PlayerOnTop(
180+
override val blockPos: BlockPos,
181+
val blockState: BlockState
182+
) : BreakResult() {
183+
override val rank = Rank.BREAK_PLAYER_ON_TOP
184+
}
24185
}

0 commit comments

Comments
 (0)