Skip to content

Commit b0892a3

Browse files
committed
Faster pathing
1 parent 0944e8d commit b0892a3

File tree

4 files changed

+27
-41
lines changed

4 files changed

+27
-41
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package com.lambda.interaction.construction
22

3-
import com.lambda.context.SafeContext
43
import com.lambda.interaction.construction.verify.TargetState
5-
import com.lambda.module.modules.client.TaskFlow
64
import com.lambda.util.BlockUtils.blockPos
7-
import com.lambda.util.BlockUtils.blockState
8-
import com.lambda.util.Communication.info
95
import com.lambda.util.extension.Structure
6+
import com.lambda.util.math.VecUtils.blockPos
107
import net.minecraft.structure.StructureTemplate
11-
import net.minecraft.util.math.BlockBox
12-
import net.minecraft.util.math.BlockPos
13-
import net.minecraft.util.math.Box
8+
import net.minecraft.util.math.*
149

1510
abstract class Blueprint {
1611
abstract val structure: Structure
1712

18-
open fun isDone(ctx: SafeContext) =
19-
structure.all { (pos, targetState) ->
20-
with(ctx) {
21-
targetState.matches(pos.blockState(world), pos, world)
22-
}
23-
}
13+
private val bounds: BlockBox by lazy {
14+
val maxX = structure.keys.maxOf { it.x }
15+
val maxY = structure.keys.maxOf { it.y }
16+
val maxZ = structure.keys.maxOf { it.z }
17+
val minX = structure.keys.minOf { it.x }
18+
val minY = structure.keys.minOf { it.y }
19+
val minZ = structure.keys.minOf { it.z }
20+
BlockBox(minX, minY, minZ, maxX, maxY, maxZ)
21+
}
22+
23+
fun getClosestPointTo(target: Vec3d): Vec3d {
24+
val d = MathHelper.clamp(target.x, bounds.minX.toDouble(), bounds.maxX.toDouble())
25+
val e = MathHelper.clamp(target.y, bounds.minY.toDouble(), bounds.maxY.toDouble())
26+
val f = MathHelper.clamp(target.z, bounds.minZ.toDouble(), bounds.maxZ.toDouble())
27+
return Vec3d(d, e, f)
28+
}
29+
30+
fun isOutOfBounds(vec3d: Vec3d): Boolean = !bounds.contains(vec3d.blockPos)
2431

2532
companion object {
2633
fun emptyStructure(): Structure = emptyMap()

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildGoal.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.lambda.interaction.construction.simulation
22

33
import baritone.api.pathing.goals.Goal
4-
import com.lambda.util.Communication.info
54
import com.lambda.util.world.fastVectorOf
65

76
class BuildGoal(private val sim: Simulation) : Goal {
87
override fun isInGoal(x: Int, y: Int, z: Int): Boolean {
98
val vec = fastVectorOf(x, y, z)
10-
// info("Checking goal at $x, $y, $z")
119
val simu = sim.simulate(vec)
1210
return simu.any { it.rank.ordinal < 4 }
1311
}
1412

1513
override fun heuristic(x: Int, y: Int, z: Int): Double {
16-
val bestRank = sim.simulate(fastVectorOf(x, y, z)).minOrNull()?.rank?.ordinal ?: 10000
14+
val bestRank = sim.simulate(fastVectorOf(x, y, z)).minOrNull()?.rank?.ordinal ?: 100000
1715
val score = 1 / (bestRank.toDouble() + 1)
1816
// info("Calculating heuristic at $x, $y, $z with score $score")
1917
return score

common/src/main/kotlin/com/lambda/interaction/construction/simulation/Simulation.kt

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.lambda.context.SafeContext
44
import com.lambda.interaction.construction.Blueprint
55
import com.lambda.interaction.construction.result.BuildResult
66
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
7+
import com.lambda.module.modules.client.TaskFlow
8+
import com.lambda.task.Task
79
import com.lambda.threading.runSafe
810
import com.lambda.util.Communication.info
911
import com.lambda.util.world.FastVector
@@ -18,35 +20,16 @@ import net.minecraft.util.math.Vec3d
1820
data class Simulation(val blueprint: Blueprint) {
1921
private val cache: MutableMap<FastVector, Set<BuildResult>> = mutableMapOf()
2022
private fun FastVector.toView(): Vec3d = toVec3d().add(0.5, 0.62, 0.5)
21-
// private lateinit var player: ClientPlayerEntity
22-
23-
// init {
24-
// runSafe {
25-
// this@Simulation.player = player
26-
// BlockPos.iterateOutwards(player.blockPos, 5, 5, 5).forEach { pos ->
27-
// val vec = pos.toFastVec()
28-
// info("Preloading simulation at $vec")
29-
// simulate(vec)
30-
// }
31-
// }
32-
// }
33-
34-
// val best: FastVector? get() = cache.filter { (_, results) ->
35-
// results.isNotEmpty() && results.any { it.rank.ordinal < 4 }
36-
// }.keys.minByOrNull { it.toVec3d().distanceTo(player.pos) }
37-
38-
// fun best() = cache.filter { (_, results) ->
39-
// results.isNotEmpty() && results.any { it.rank.ordinal < 3 }
40-
// }.keys
4123

4224
fun simulate(pos: FastVector): Set<BuildResult> {
4325
return cache.computeIfAbsent(pos) {
26+
val view = pos.toView()
4427
runSafe {
28+
if (blueprint.isOutOfBounds(view) && blueprint.getClosestPointTo(view).distanceTo(view) > 10.0) return@computeIfAbsent emptySet()
4529
if (!playerFitsIn(Vec3d.ofBottomCenter(pos.toBlockPos()))) return@computeIfAbsent emptySet()
4630
}
47-
blueprint.simulate(pos.toView(), reach = 3.5)
31+
blueprint.simulate(view, reach = TaskFlow.interact.reach - 1)
4832
}
49-
// return blueprint.simulate(pos.toView(), reach = 3.5)
5033
}
5134

5235
private fun SafeContext.playerFitsIn(pos: Vec3d): Boolean {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ class BuildTask @Ta5kBuilder constructor(
8383

8484
val result = results.minOrNull() ?: return@listener
8585
when {
86-
!result.rank.solvable -> {
87-
success(Unit)
88-
}
86+
!result.rank.solvable -> success(Unit)
8987
result is BuildResult.NotVisible -> {
9088
if (pathing) BaritoneUtils.setGoalAndPath(
9189
BuildGoal(blueprint.simulation())

0 commit comments

Comments
 (0)