Skip to content

Commit 4cd8e1d

Browse files
committed
Better support check and config cleanup
1 parent 86d7874 commit 4cd8e1d

File tree

9 files changed

+105
-75
lines changed

9 files changed

+105
-75
lines changed

common/src/main/kotlin/com/lambda/module/modules/movement/Pathfinder.kt

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.module.modules.movement
1919

2020
import com.lambda.config.groups.RotationSettings
2121
import com.lambda.context.SafeContext
22+
import com.lambda.event.events.MovementEvent
2223
import com.lambda.event.events.RenderEvent
2324
import com.lambda.event.events.RotationEvent
2425
import com.lambda.event.events.TickEvent
@@ -58,10 +59,15 @@ object Pathfinder : Module(
5859
description = "Get from A to B",
5960
defaultTags = setOf(ModuleTag.MOVEMENT)
6061
) {
61-
private val pathing = PathingSettings(this)
62-
private val rotation = RotationSettings(this)
62+
enum class Page {
63+
Pathing, Rotation
64+
}
65+
66+
private val page by setting("Page", Page.Pathing)
67+
private val pathing = PathingSettings(this) { page == Page.Pathing }
68+
private val rotation = RotationSettings(this) { page == Page.Rotation }
6369

64-
private val target = fastVectorOf(0, 78, 0)
70+
private val target = fastVectorOf(0, 91, -4)
6571
private var longPath = Path()
6672
private var shortPath = Path()
6773
private var currentTarget: Vec3d? = null
@@ -74,33 +80,17 @@ object Pathfinder : Module(
7480
integralError = Vec3d.ZERO
7581
lastError = Vec3d.ZERO
7682
calculating = false
83+
longPath = Path()
84+
shortPath = Path()
85+
currentTarget = null
7786
}
7887

7988
listen<TickEvent.Pre> {
8089
updateTargetNode()
8190

8291
if (calculating) return@listen
83-
calculating = true
8492

85-
runConcurrent {
86-
val long: Path
87-
val aStar = measureTimeMillis {
88-
long = findPathAStar(
89-
player.blockPos.toFastVec(),
90-
SimpleGoal(target),
91-
pathing
92-
)
93-
}
94-
val short: Path
95-
val thetaStar = measureTimeMillis {
96-
short = thetaStarClearance(long, pathing)
97-
}
98-
info("A* (Length: ${long.length.string} Nodes: ${long.moves.size} T: $aStar ms) and Theta* (Length: ${short.length.string} Nodes: ${short.moves.size} T: $thetaStar ms)")
99-
println("Long: $long | Short: $short")
100-
longPath = long
101-
shortPath = short
102-
// calculating = false
103-
}
93+
updatePaths()
10494
}
10595

10696
listen<RotationEvent.StrafeInput> { event ->
@@ -124,17 +114,22 @@ object Pathfinder : Module(
124114
}
125115

126116
onRotate {
127-
// val nextTarget = shortPath.moves.getOrNull(2)?.pos?.toBlockPos() ?: return@onRotate
128-
// val part = player.eyePos.rotationTo(Vec3d.ofBottomCenter(nextTarget))
129117
val currentTarget = currentTarget ?: return@onRotate
130118
val part = player.eyePos.rotationTo(currentTarget)
131119
val targetRotation = Rotation(part.yaw, player.pitch.toDouble())
132120

133121
lookAt(targetRotation).requestBy(rotation)
134122
}
135123

124+
listen<MovementEvent.Sprint> {
125+
if (shortPath.moves.isEmpty()) return@listen
126+
127+
player.isSprinting = pathing.allowSprint
128+
it.sprint = pathing.allowSprint
129+
}
130+
136131
listen<RenderEvent.StaticESP> { event ->
137-
longPath.render(event.renderer, Color.YELLOW)
132+
// longPath.render(event.renderer, Color.YELLOW)
138133
shortPath.render(event.renderer, Color.GREEN)
139134
event.renderer.buildFilled(Box(target.toBlockPos()), Color.PINK.setAlpha(0.25))
140135
}
@@ -149,19 +144,41 @@ object Pathfinder : Module(
149144
}
150145

151146
private fun SafeContext.updateTargetNode() {
152-
shortPath.moves.firstOrNull()?.let { firstNode ->
153-
val nodeVec = Vec3d.ofBottomCenter(firstNode.pos.toBlockPos())
154-
if (player.pos.distanceTo(nodeVec) < pathing.tolerance) {
147+
shortPath.moves.firstOrNull()?.let { current ->
148+
if (player.pos.distanceTo(current.bottomPos) < pathing.tolerance) {
155149
shortPath.moves.removeFirst()
156150
integralError = Vec3d.ZERO
157151
}
158-
val next = shortPath.moves.firstOrNull()?.pos?.toBlockPos() ?: return
159-
currentTarget = Vec3d.ofBottomCenter(next)
152+
currentTarget = shortPath.moves.firstOrNull()?.bottomPos
160153
} ?: run {
161154
currentTarget = null
162155
}
163156
}
164157

158+
private fun SafeContext.updatePaths() {
159+
runConcurrent {
160+
calculating = true
161+
val long: Path
162+
val aStar = measureTimeMillis {
163+
long = findPathAStar(
164+
player.blockPos.toFastVec(),
165+
SimpleGoal(target),
166+
pathing
167+
)
168+
}
169+
val short: Path
170+
val thetaStar = measureTimeMillis {
171+
short = thetaStarClearance(long, pathing)
172+
}
173+
info("A* (Length: ${long.length.string} Nodes: ${long.moves.size} T: $aStar ms) and Theta* (Length: ${short.length.string} Nodes: ${short.moves.size} T: $thetaStar ms)")
174+
println("Long: $long | Short: $short")
175+
short.moves.removeFirstOrNull()
176+
longPath = long
177+
shortPath = short
178+
// calculating = false
179+
}
180+
}
181+
165182
private fun SafeContext.calculatePID(target: Vec3d): Vec3d {
166183
val error = target.subtract(player.pos)
167184
integralError = integralError.add(error)

common/src/main/kotlin/com/lambda/pathing/Pathing.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object Pathing {
9797
isPathClear(
9898
startMove.pos.toBlockPos(),
9999
candidateMove.pos.toBlockPos(),
100-
config.pathClearanceCheckDistance
100+
config.clearancePrecition
101101
)
102102
) nextIndex++ else break
103103
}
@@ -109,6 +109,4 @@ object Pathing {
109109

110110
return cleanedPath
111111
}
112-
113-
114112
}

common/src/main/kotlin/com/lambda/pathing/PathingConfig.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@
1717

1818
package com.lambda.pathing
1919

20-
interface PathingConfig {
21-
val kP: Double
22-
val kI: Double
23-
val kD: Double
24-
val tolerance: Double
25-
val cutoffTimeout: Long
26-
val shortcutLength: Int
27-
val pathClearanceCheckDistance: Double
20+
import com.lambda.interaction.request.Priority
21+
import com.lambda.interaction.request.RequestConfig
2822

29-
val assumeJesus: Boolean
23+
abstract class PathingConfig(priority: Priority) : RequestConfig<PathRequest>(priority) {
24+
abstract val kP: Double
25+
abstract val kI: Double
26+
abstract val kD: Double
27+
abstract val tolerance: Double
28+
abstract val cutoffTimeout: Long
29+
abstract val shortcutLength: Int
30+
abstract val clearancePrecition: Double
31+
abstract val allowSprint: Boolean
32+
abstract val maxFallHeight: Double
33+
34+
abstract val assumeJesus: Boolean
35+
36+
override fun requestInternal(request: PathRequest) {
37+
PathingManager.registerRequest(this, request)
38+
}
3039
}

common/src/main/kotlin/com/lambda/pathing/PathingSettings.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@
1818
package com.lambda.pathing
1919

2020
import com.lambda.config.Configurable
21+
import com.lambda.interaction.request.Priority
2122

2223
class PathingSettings(
2324
c: Configurable,
25+
priority: Priority = 0,
2426
vis: () -> Boolean = { true }
25-
) : PathingConfig {
27+
) : PathingConfig(priority) {
2628
enum class Page {
27-
Execution, Misc
29+
Pathfinding, Movement, Misc
2830
}
2931

30-
private val page by c.setting("Pathing Page", Page.Execution, "Current page", vis)
32+
private val page by c.setting("Pathing Page", Page.Pathfinding, "Current page", vis)
3133

32-
override val kP by c.setting("P Gain", 0.5, 0.0..2.0, 0.01) { vis() && page == Page.Execution }
33-
override val kI by c.setting("I Gain", 0.0, 0.0..1.0, 0.01) { vis() && page == Page.Execution }
34-
override val kD by c.setting("D Gain", 0.2, 0.0..1.0, 0.01) { vis() && page == Page.Execution }
35-
override val tolerance by c.setting("Node Tolerance", 0.1, 0.01..1.0, 0.01) { vis() && page == Page.Execution }
36-
override val cutoffTimeout by c.setting("Cutoff Timeout", 50L, 1L..2000L, 10L) { vis() && page == Page.Execution }
37-
override val shortcutLength by c.setting("Shortcut Length", 10, 1..100, 1) { vis() && page == Page.Execution }
38-
override val pathClearanceCheckDistance by c.setting("Path Clearance Check Distance", 0.3, 0.0..1.0, 0.01) { vis() && page == Page.Execution }
34+
override val cutoffTimeout by c.setting("Cutoff Timeout", 500L, 1L..2000L, 10L, "Timeout of path calculation", " ms") { vis() && page == Page.Pathfinding }
35+
override val shortcutLength by c.setting("Shortcut Length", 10, 1..100, 1) { vis() && page == Page.Pathfinding }
36+
override val clearancePrecition by c.setting("Clearance Precition", 0.2, 0.0..1.0, 0.01) { vis() && page == Page.Pathfinding }
37+
override val maxFallHeight by c.setting("Max Fall Height", 3.0, 0.0..30.0, 0.5) { vis() && page == Page.Pathfinding }
38+
39+
override val kP by c.setting("P Gain", 0.5, 0.0..2.0, 0.01) { vis() && page == Page.Movement }
40+
override val kI by c.setting("I Gain", 0.0, 0.0..1.0, 0.01) { vis() && page == Page.Movement }
41+
override val kD by c.setting("D Gain", 0.2, 0.0..1.0, 0.01) { vis() && page == Page.Movement }
42+
override val tolerance by c.setting("Node Tolerance", 0.7, 0.01..2.0, 0.05) { vis() && page == Page.Movement }
43+
override val allowSprint by c.setting("Allow Sprint", true) { vis() && page == Page.Movement }
3944

4045
override val assumeJesus by c.setting("Assume Jesus", false) { vis() && page == Page.Misc }
4146
}

common/src/main/kotlin/com/lambda/pathing/move/Move.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package com.lambda.pathing.move
2020
import com.lambda.pathing.Path
2121
import com.lambda.task.Task
2222
import com.lambda.util.world.FastVector
23+
import com.lambda.util.world.toBlockPos
24+
import net.minecraft.util.math.Vec3d
2325

2426
abstract class Move : Comparable<Move>, Task<Unit>() {
2527
abstract val pos: FastVector
@@ -34,6 +36,8 @@ abstract class Move : Comparable<Move>, Task<Unit>() {
3436
// use updateable lazy and recompute on gCost change
3537
private val fCost get() = gCost + hCost
3638

39+
val bottomPos: Vec3d get() = Vec3d.ofBottomCenter(pos.toBlockPos())
40+
3741
override fun compareTo(other: Move) =
3842
fCost.compareTo(other.fCost)
3943

common/src/main/kotlin/com/lambda/pathing/move/MoveFinder.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ import com.lambda.pathing.goal.Goal
2323
import com.lambda.util.BlockUtils.blockState
2424
import com.lambda.util.BlockUtils.fluidState
2525
import com.lambda.util.world.FastVector
26+
import com.lambda.util.world.WorldUtils.hasSupport
2627
import com.lambda.util.world.WorldUtils.isPathClear
27-
import com.lambda.util.world.WorldUtils.playerFitsIn
28-
import com.lambda.util.world.WorldUtils.traversable
2928
import com.lambda.util.world.add
3029
import com.lambda.util.world.fastVectorOf
3130
import com.lambda.util.world.length
32-
import com.lambda.util.world.manhattanLength
3331
import com.lambda.util.world.offset
3432
import com.lambda.util.world.toBlockPos
35-
import com.lambda.util.world.y
3633
import net.minecraft.block.BlockState
3734
import net.minecraft.block.Blocks
3835
import net.minecraft.block.CampfireBlock
@@ -43,7 +40,6 @@ import net.minecraft.block.TrapdoorBlock
4340
import net.minecraft.enchantment.EnchantmentHelper
4441
import net.minecraft.enchantment.Enchantments
4542
import net.minecraft.entity.EquipmentSlot
46-
import net.minecraft.entity.ai.pathing.NavigationType
4743
import net.minecraft.entity.effect.StatusEffects
4844
import net.minecraft.item.Items
4945
import net.minecraft.registry.tag.BlockTags
@@ -79,14 +75,14 @@ object MoveFinder {
7975
if (nodeType == NodeType.BLOCKED) return null
8076

8177
val clear = when {
82-
height == 0 -> isPathClear(originBlockPos, checkingBlockPos)
78+
height == 0 -> isPathClear(originBlockPos, checkingBlockPos, config.clearancePrecition)
8379
height > 0 -> {
8480
val between = origin.pos.offset(0, height, 0)
85-
isPathClear(origin.pos, between, supportCheck = false) && isPathClear(between, checkingPos, supportCheck = false)
81+
isPathClear(origin.pos, between, config.clearancePrecition, false) && isPathClear(between, checkingPos, config.clearancePrecition, false) && hasSupport(checkingBlockPos)
8682
}
8783
else -> {
8884
val between = origin.pos.offset(direction.offsetX, 0, direction.offsetZ)
89-
isPathClear(origin.pos, between, supportCheck = false) && isPathClear(between, checkingPos, supportCheck = false)
85+
isPathClear(origin.pos, between, config.clearancePrecition, false) && isPathClear(between, checkingPos, config.clearancePrecition, false) && hasSupport(checkingBlockPos)
9086
}
9187
}
9288
if (!clear) return null

common/src/main/kotlin/com/lambda/pathing/move/SwimMove.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717

1818
package com.lambda.pathing.move
1919

20-
import com.lambda.context.SafeContext
2120
import com.lambda.util.world.FastVector
22-
import com.lambda.util.world.WorldUtils.traversable
23-
import com.lambda.util.world.toBlockPos
2421

2522
class SwimMove(
2623
override val pos: FastVector,

common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt renamed to common/src/main/kotlin/com/lambda/util/world/SearchDsl.kt

File renamed without changes.

common/src/main/kotlin/com/lambda/util/world/WorldUtils.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.util.world
2020
import com.lambda.context.SafeContext
2121
import com.lambda.util.BlockUtils.blockState
2222
import com.lambda.util.math.flooredBlockPos
23+
import net.fabricmc.loader.impl.lib.sat4j.core.Vec
2324
import net.minecraft.util.math.BlockPos
2425
import net.minecraft.util.math.Box
2526
import net.minecraft.util.math.Direction
@@ -60,9 +61,9 @@ object WorldUtils {
6061
var currentPos = start
6162

6263
(0 until steps).forEach { _ ->
63-
val blockPos = currentPos.flooredBlockPos
64-
val playerNotFitting = !playerFitsIn(blockPos)
65-
if (playerNotFitting || (supportCheck && !hasSupport(blockPos))) {
64+
val playerNotFitting = !playerFitsIn(currentPos)
65+
val hasNoSupport = !hasSupport(currentPos)
66+
if (playerNotFitting || (supportCheck && hasNoSupport)) {
6667
return false
6768
}
6869
currentPos = currentPos.add(stepDirection)
@@ -71,18 +72,21 @@ object WorldUtils {
7172
return playerFitsIn(end)
7273
}
7374

74-
fun SafeContext.playerFitsIn(pos: FastVector) =
75-
playerFitsIn(pos.toBlockPos())
75+
fun SafeContext.playerFitsIn(pos: BlockPos) =
76+
playerFitsIn(Vec3d.ofBottomCenter(pos))
7677

7778
fun SafeContext.playerFitsIn(pos: Vec3d) =
78-
world.isSpaceEmpty(pos.playerBox())
79+
world.isSpaceEmpty(pos.playerBox().contract(1.0E-6))
7980

80-
fun SafeContext.playerFitsIn(pos: BlockPos) =
81-
world.isSpaceEmpty(Vec3d.ofBottomCenter(pos).playerBox())
81+
fun SafeContext.hasSupport(pos: BlockPos) =
82+
hasSupport(Vec3d.ofBottomCenter(pos))
83+
84+
// private fun SafeContext.hasSupport(pos: BlockPos) =
85+
// blockState(pos.down()).isSideSolidFullSquare(world, pos.down(), Direction.UP)
8286

83-
private fun SafeContext.hasSupport(pos: BlockPos) =
84-
blockState(pos.down()).isSideSolidFullSquare(world, pos.down(), Direction.UP)
87+
fun SafeContext.hasSupport(pos: Vec3d) =
88+
!world.isSpaceEmpty(pos.playerBox().expand(1.0E-6))
8589

8690
fun Vec3d.playerBox(): Box =
87-
Box(x - 0.3, y, z - 0.3, x + 0.3, y + 1.8, z + 0.3).contract(1.0E-6)
91+
Box(x - 0.3, y, z - 0.3, x + 0.3, y + 1.8, z + 0.3)
8892
}

0 commit comments

Comments
 (0)