1717
1818package com.lambda.module.modules.movement
1919
20- import com.lambda.config.groups.RotationSettings
2120import com.lambda.context.SafeContext
2221import com.lambda.event.events.MovementEvent
2322import com.lambda.event.events.RenderEvent
@@ -40,19 +39,26 @@ import com.lambda.pathing.dstar.DStarLite
4039import com.lambda.pathing.dstar.LazyGraph
4140import com.lambda.pathing.goal.SimpleGoal
4241import com.lambda.pathing.move.MoveFinder.moveOptions
42+ import com.lambda.pathing.move.NodeType
43+ import com.lambda.pathing.move.TraverseMove
4344import com.lambda.threading.runConcurrent
4445import com.lambda.threading.runSafe
4546import com.lambda.util.Communication.info
4647import com.lambda.util.Formatting.string
4748import com.lambda.util.math.setAlpha
4849import com.lambda.util.player.MovementUtils.buildMovementInput
4950import com.lambda.util.player.MovementUtils.mergeFrom
51+ import com.lambda.util.world.FastVector
5052import com.lambda.util.world.fastVectorOf
5153import com.lambda.util.world.toBlockPos
5254import com.lambda.util.world.toFastVec
55+ import com.lambda.util.world.x
56+ import com.lambda.util.world.y
57+ import com.lambda.util.world.z
5358import net.minecraft.util.math.Box
5459import net.minecraft.util.math.Vec3d
5560import java.awt.Color
61+ import kotlin.math.abs
5662import kotlin.math.cos
5763import kotlin.math.sin
5864import kotlin.system.measureTimeMillis
@@ -62,15 +68,21 @@ object Pathfinder : Module(
6268 description = " Get from A to B" ,
6369 defaultTags = setOf(ModuleTag .MOVEMENT )
6470) {
65- enum class Page {
66- Pathing , Rotation
67- }
71+ private val pathing = PathingSettings (this )
72+
73+ private fun heuristic (u : FastVector ): Double =
74+ (abs(u.x) + abs(u.y) + abs(u.z)).toDouble()
6875
69- private val page by setting(" Page" , Page .Pathing )
70- private val pathing = PathingSettings (this ) { page == Page .Pathing }
71- private val rotation = RotationSettings (this ) { page == Page .Rotation }
76+ private fun heuristic (u : FastVector , v : FastVector ): Double =
77+ (abs(u.x - v.x) + abs(u.y - v.y) + abs(u.z - v.z)).toDouble()
7278
7379 private val target = fastVectorOf(0 , 91 , - 4 )
80+ private val graph = LazyGraph { origin ->
81+ runSafe {
82+ moveOptions(origin, ::heuristic, pathing).associate { it.pos to it.cost }
83+ } ? : emptyMap()
84+ }
85+ private val dStar = DStarLite (graph, fastVectorOf(0 , 0 , 0 ), target, ::heuristic)
7486 private var coarsePath = Path ()
7587 private var refinedPath = Path ()
7688 private var currentTarget: Vec3d ? = null
@@ -86,6 +98,9 @@ object Pathfinder : Module(
8698 coarsePath = Path ()
8799 refinedPath = Path ()
88100 currentTarget = null
101+ graph.clear()
102+ dStar.initialize()
103+ dStar.updateStart(player.pos.toFastVec())
89104 }
90105
91106 listen<TickEvent .Pre > {
@@ -96,7 +111,15 @@ object Pathfinder : Module(
96111 updatePaths()
97112 }
98113
114+ // listen<WorldEvent.BlockUpdate.Client> {
115+ // val pos = it.pos.toFastVec()
116+ // graph.markDirty(pos)
117+ // info("Updated block at ${it.pos} to ${it.newState.block.name.string} rescheduled D*Lite.")
118+ // }
119+
99120 listen<RotationEvent .StrafeInput > { event ->
121+ if (! pathing.moveAlongPath) return @listen
122+
100123 currentTarget?.let { target ->
101124 event.strafeYaw = player.eyePos.rotationTo(target).yaw
102125 val adjustment = calculatePID(target)
@@ -117,68 +140,93 @@ object Pathfinder : Module(
117140 }
118141
119142 onRotate {
143+ if (! pathing.moveAlongPath) return @onRotate
144+
120145 val currentTarget = currentTarget ? : return @onRotate
121146 val part = player.eyePos.rotationTo(currentTarget)
122147 val targetRotation = Rotation (part.yaw, player.pitch.toDouble())
123148
124- lookAt(targetRotation).requestBy(rotation)
149+ lookAt(targetRotation).requestBy(pathing. rotation)
125150 }
126151
127152 listen<MovementEvent .Sprint > {
153+ if (! pathing.moveAlongPath) return @listen
128154 if (refinedPath.moves.isEmpty()) return @listen
129155
130156 player.isSprinting = pathing.allowSprint
131157 it.sprint = pathing.allowSprint
132158 }
133159
134160 listen<RenderEvent .StaticESP > { event ->
135- // longPath .render(event.renderer, Color.YELLOW)
136- refinedPath.render(event.renderer, Color .GREEN )
137- event.renderer.buildFilled(Box (target.toBlockPos()), Color .PINK .setAlpha(0.25 ))
161+ if (pathing.renderCoarsePath) coarsePath .render(event.renderer, Color .YELLOW )
162+ if (pathing.renderRefinedPath) refinedPath.render(event.renderer, Color .GREEN )
163+ if (pathing.renderGoal) event.renderer.buildFilled(Box (target.toBlockPos()), Color .PINK .setAlpha(0.25 ))
138164 }
139165 }
140166
141167 private fun SafeContext.updateTargetNode () {
142- refinedPath.moves.firstOrNull()?.let { current ->
168+ currentTarget = refinedPath.moves.firstOrNull()?.let { current ->
143169 if (player.pos.distanceTo(current.bottomPos) < pathing.tolerance) {
144170 refinedPath.moves.removeFirst()
145171 integralError = Vec3d .ZERO
146172 }
147- currentTarget = refinedPath.moves.firstOrNull()?.bottomPos
148- } ? : run {
149- currentTarget = null
173+ refinedPath.moves.firstOrNull()?.bottomPos
150174 }
151175 }
152176
153177 private fun SafeContext.updatePaths () {
154178 val goal = SimpleGoal (target)
179+ val start = player.blockPos.toFastVec()
155180 when (pathing.algorithm) {
156- PathingConfig .PathingAlgorithm .A_STAR -> {
157- runConcurrent {
158- calculating = true
159- val long: Path
160- val aStar = measureTimeMillis {
161- long = findPathAStar(player.blockPos.toFastVec(), goal, pathing)
162- }
163- val short: Path
164- val thetaStar = measureTimeMillis {
165- short = if (pathing.pathRefining) {
166- thetaStarClearance(long, pathing)
167- } else long
168- }
169- info(" A* (Length: ${long.length().string} Nodes: ${long.size} T: $aStar ms) and Theta* (Length: ${short.length().string} Nodes: ${short.size} T: $thetaStar ms)" )
170- println (" Long: $long | Short: $short " )
171- short.moves.removeFirstOrNull()
172- coarsePath = long
173- refinedPath = short
174- // calculating = false
175- }
181+ PathingConfig .PathingAlgorithm .A_STAR -> updateAStar(start, goal)
182+ PathingConfig .PathingAlgorithm .D_STAR_LITE -> updateDStar()
183+ }
184+ }
185+
186+ private fun SafeContext.updateAStar (start : FastVector , goal : SimpleGoal ) {
187+ runConcurrent {
188+ calculating = true
189+ val long: Path
190+ val aStar = measureTimeMillis {
191+ long = findPathAStar(start, goal, pathing)
192+ }
193+ val short: Path
194+ val thetaStar = measureTimeMillis {
195+ short = if (pathing.refinePath) {
196+ thetaStarClearance(long, pathing)
197+ } else long
176198 }
177- PathingConfig .PathingAlgorithm .D_STAR_LITE -> {
178- runConcurrent {
199+ info(" A* (Length: ${long.length().string} Nodes: ${long.size} T: $aStar ms) and \u03b8 * (Length: ${short.length().string} Nodes: ${short.size} T: $thetaStar ms)" )
200+ println (" Long: $long | Short: $short " )
201+ short.moves.removeFirstOrNull()
202+ coarsePath = long
203+ refinedPath = short
204+ // calculating = false
205+ }
206+ }
179207
180- }
208+ private fun SafeContext.updateDStar () {
209+ runConcurrent {
210+ calculating = true
211+ val long: Path
212+ val dStar = measureTimeMillis {
213+ // if (start dist dstar.start > 3) dstar.updateStart(start)
214+ dStar.computeShortestPath()
215+ val nodes = dStar.getPath().map { TraverseMove (it, 0.0 , NodeType .OPEN , 0.0 , 0.0 ) }
216+ long = Path (ArrayDeque (nodes))
217+ }
218+ val short: Path
219+ val thetaStar = measureTimeMillis {
220+ short = if (pathing.refinePath) {
221+ thetaStarClearance(long, pathing)
222+ } else long
181223 }
224+ info(" Lazy D* Lite (Length: ${long.length().string} Nodes: ${long.size} Graph Size: ${graph.size} T: $dStar ms) and \u03b8 * (Length: ${short.length().string} Nodes: ${short.size} T: $thetaStar ms)" )
225+ println (" Long: $long | Short: $short " )
226+ short.moves.removeFirstOrNull()
227+ coarsePath = long
228+ refinedPath = short
229+ // calculating = false
182230 }
183231 }
184232
0 commit comments