@@ -8,6 +8,7 @@ mod debug;
88pub mod goals;
99pub mod mining;
1010pub mod moves;
11+ pub mod rel_block_pos;
1112pub mod simulation;
1213pub mod world;
1314
@@ -35,6 +36,7 @@ use bevy_ecs::schedule::IntoSystemConfigs;
3536use bevy_tasks:: { AsyncComputeTaskPool , Task } ;
3637use futures_lite:: future;
3738use parking_lot:: RwLock ;
39+ use rel_block_pos:: RelBlockPos ;
3840use tracing:: { debug, error, info, trace, warn} ;
3941
4042use self :: debug:: debug_render_path_with_particles;
@@ -321,8 +323,9 @@ pub async fn calculate_path(opts: CalculatePathOpts) -> Option<PathFoundEvent> {
321323
322324 let goto_id = opts. goto_id_atomic . fetch_add ( 1 , atomic:: Ordering :: SeqCst ) + 1 ;
323325
324- let cached_world = CachedWorld :: new ( opts. world_lock ) ;
325- let successors = |pos : BlockPos | {
326+ let origin = opts. start ;
327+ let cached_world = CachedWorld :: new ( opts. world_lock , origin) ;
328+ let successors = |pos : RelBlockPos | {
326329 call_successors_fn ( & cached_world, & opts. mining_cache , opts. successors_fn , pos)
327330 } ;
328331
@@ -345,10 +348,10 @@ pub async fn calculate_path(opts: CalculatePathOpts) -> Option<PathFoundEvent> {
345348 } ;
346349
347350 let astar:: Path { movements, partial } = a_star (
348- opts . start ,
349- |n| opts. goal . heuristic ( n) ,
351+ RelBlockPos :: get_origin ( origin ) ,
352+ |n| opts. goal . heuristic ( n. apply ( origin ) ) ,
350353 successors,
351- |n| opts. goal . success ( n) ,
354+ |n| opts. goal . success ( n. apply ( origin ) ) ,
352355 timeout,
353356 ) ;
354357 let end_time = Instant :: now ( ) ;
@@ -368,7 +371,7 @@ pub async fn calculate_path(opts: CalculatePathOpts) -> Option<PathFoundEvent> {
368371
369372 debug ! ( "Path:" ) ;
370373 for movement in & movements {
371- debug ! ( " {}" , movement. target) ;
374+ debug ! ( " {}" , movement. target. apply ( origin ) ) ;
372375 }
373376
374377 path = movements. into_iter ( ) . collect :: < VecDeque < _ > > ( ) ;
@@ -394,10 +397,19 @@ pub async fn calculate_path(opts: CalculatePathOpts) -> Option<PathFoundEvent> {
394397 break ;
395398 }
396399
400+ // replace the RelBlockPos types with BlockPos
401+ let mapped_path = path
402+ . into_iter ( )
403+ . map ( |movement| astar:: Movement {
404+ target : movement. target . apply ( origin) ,
405+ data : movement. data ,
406+ } )
407+ . collect ( ) ;
408+
397409 Some ( PathFoundEvent {
398410 entity : opts. entity ,
399411 start : opts. start ,
400- path : Some ( path ) ,
412+ path : Some ( mapped_path ) ,
401413 is_partial,
402414 successors_fn : opts. successors_fn ,
403415 allow_mining : opts. allow_mining ,
@@ -448,21 +460,27 @@ pub fn path_found_listener(
448460 let world_lock = instance_container
449461 . get ( instance_name)
450462 . expect ( "Entity tried to pathfind but the entity isn't in a valid world" ) ;
463+ let origin = event. start ;
451464 let successors_fn: moves:: SuccessorsFn = event. successors_fn ;
452- let cached_world = CachedWorld :: new ( world_lock) ;
465+ let cached_world = CachedWorld :: new ( world_lock, origin ) ;
453466 let mining_cache = MiningCache :: new ( if event. allow_mining {
454467 Some ( inventory. inventory_menu . clone ( ) )
455468 } else {
456469 None
457470 } ) ;
458- let successors = |pos : BlockPos | {
471+ let successors = |pos : RelBlockPos | {
459472 call_successors_fn ( & cached_world, & mining_cache, successors_fn, pos)
460473 } ;
461474
462475 if let Some ( first_node_of_new_path) = path. front ( ) {
463- if successors ( last_node_of_current_path. target )
476+ let last_target_of_current_path =
477+ RelBlockPos :: from_origin ( origin, last_node_of_current_path. target ) ;
478+ let first_target_of_new_path =
479+ RelBlockPos :: from_origin ( origin, first_node_of_new_path. target ) ;
480+
481+ if successors ( last_target_of_current_path)
464482 . iter ( )
465- . any ( |edge| edge. movement . target == first_node_of_new_path . target )
483+ . any ( |edge| edge. movement . target == first_target_of_new_path )
466484 {
467485 debug ! ( "combining old and new paths" ) ;
468486 debug ! (
@@ -655,17 +673,19 @@ pub fn check_for_path_obstruction(
655673 . expect ( "Entity tried to pathfind but the entity isn't in a valid world" ) ;
656674
657675 // obstruction check (the path we're executing isn't possible anymore)
658- let cached_world = CachedWorld :: new ( world_lock) ;
676+ let origin = executing_path. last_reached_node ;
677+ let cached_world = CachedWorld :: new ( world_lock, origin) ;
659678 let mining_cache = MiningCache :: new ( if pathfinder. allow_mining {
660679 Some ( inventory. inventory_menu . clone ( ) )
661680 } else {
662681 None
663682 } ) ;
664683 let successors =
665- |pos : BlockPos | call_successors_fn ( & cached_world, & mining_cache, successors_fn, pos) ;
684+ |pos : RelBlockPos | call_successors_fn ( & cached_world, & mining_cache, successors_fn, pos) ;
666685
667686 if let Some ( obstructed_index) = check_path_obstructed (
668- executing_path. last_reached_node ,
687+ origin,
688+ RelBlockPos :: from_origin ( origin, executing_path. last_reached_node ) ,
669689 & executing_path. path ,
670690 successors,
671691 ) {
@@ -873,18 +893,21 @@ pub fn stop_pathfinding_on_instance_change(
873893/// Checks whether the path has been obstructed, and returns Some(index) if it
874894/// has been. The index is of the first obstructed node.
875895pub fn check_path_obstructed < SuccessorsFn > (
876- mut current_position : BlockPos ,
896+ origin : BlockPos ,
897+ mut current_position : RelBlockPos ,
877898 path : & VecDeque < astar:: Movement < BlockPos , moves:: MoveData > > ,
878899 successors_fn : SuccessorsFn ,
879900) -> Option < usize >
880901where
881- SuccessorsFn : Fn ( BlockPos ) -> Vec < astar:: Edge < BlockPos , moves:: MoveData > > ,
902+ SuccessorsFn : Fn ( RelBlockPos ) -> Vec < astar:: Edge < RelBlockPos , moves:: MoveData > > ,
882903{
883904 for ( i, movement) in path. iter ( ) . enumerate ( ) {
905+ let movement_target = RelBlockPos :: from_origin ( origin, movement. target ) ;
906+
884907 let mut found_obstruction = false ;
885908 for edge in successors_fn ( current_position) {
886- if edge. movement . target == movement . target {
887- current_position = movement . target ;
909+ if edge. movement . target == movement_target {
910+ current_position = movement_target ;
888911 found_obstruction = false ;
889912 break ;
890913 } else {
@@ -903,8 +926,8 @@ pub fn call_successors_fn(
903926 cached_world : & CachedWorld ,
904927 mining_cache : & MiningCache ,
905928 successors_fn : SuccessorsFn ,
906- pos : BlockPos ,
907- ) -> Vec < astar:: Edge < BlockPos , moves:: MoveData > > {
929+ pos : RelBlockPos ,
930+ ) -> Vec < astar:: Edge < RelBlockPos , moves:: MoveData > > {
908931 let mut edges = Vec :: with_capacity ( 16 ) ;
909932 let mut ctx = PathfinderCtx {
910933 edges : & mut edges,
0 commit comments