@@ -608,7 +608,7 @@ export class ThePathfinder {
608608 this . currentExecutionId ++
609609 const myExecutionId = this . currentExecutionId
610610
611- let currentIndex = 0
611+ let currentIndex = 0
612612 const localPath = path . path
613613
614614 this . currentIndex = currentIndex
@@ -619,27 +619,35 @@ export class ThePathfinder {
619619
620620 log ( '[ExecID: %d] Perform started. Entry: %d, Initial Path Length: %d' , myExecutionId , entry , localPath . length )
621621
622+ // NEW: Track the length of the path and cache the optimized sequence
623+ let lastPathLength = - 1
624+ let optSequence : Move [ ] = [ ]
625+
622626 while ( currentIndex < localPath . length ) {
623627 if ( this . currentExecutionId !== myExecutionId ) {
624628 log ( '[ExecID: %d] Execution superseded before move start.' , myExecutionId )
625629 return
626630 }
627631
628- // ON-THE-FLY OPTIMIZATION
629- // Calculate the absolute best optimized route for the remaining path right now.
630- const optimizer = new Optimizer ( this . bot , this . world , this . optimizers )
631- optimizer . loadPath ( localPath . slice ( currentIndex ) )
632- const optSequence = await optimizer . compute ( )
632+ // ACTIVE MONITOR: Only re-optimize if the path was edited in-place (or if it's the first run)
633+ if ( localPath . length !== lastPathLength ) {
634+ log ( '[ExecID: %d] Path modification detected (Length %d -> %d). Optimizing remaining slice...' , myExecutionId , Math . max ( 0 , lastPathLength ) , localPath . length )
635+ const optimizer = new Optimizer ( this . bot , this . world , this . optimizers )
636+ optimizer . loadPath ( localPath . slice ( currentIndex ) )
637+ optSequence = await optimizer . compute ( )
638+ lastPathLength = localPath . length // Update monitor
639+ }
633640
634- // Grab the very first move of our freshly optimized slice
635- let move = optSequence [ 0 ]
641+ // Grab the optimized move that matches our current unoptimized position
642+ let move = optSequence . find ( m => m . entryPos . distanceTo ( localPath [ currentIndex ] . entryPos ) < 0.1 )
636643 if ( ! move ) {
644+ log ( '[ExecID: %d] Warning: Optimized move not found for index %d. Falling back to unoptimized.' , myExecutionId , currentIndex )
637645 move = localPath [ currentIndex ]
638646 }
639647
640648 const executor = movements . get ( move . moveType . constructor as BuildableMoveProvider )
641649 if ( executor == null ) throw new Error ( 'No executor for movement type ' + move . moveType . constructor . name )
642-
650+
643651 this . currentMove = move
644652 this . currentExecutor = executor
645653
0 commit comments