Recover around invalid navigation paths

This commit is contained in:
Tanner committed 2026-11-17 18:12:55 +00:00
1 parent 5f5dde1a62
commit c2b595a70d
4 files changed
+162

No files matched your search

+66
View File
@@ -121,6 +121,67 @@ def missing_movement_chunks(g):
if g.chunks.get_chunk(x, z) is None)
PATH_VALIDATION_INTERVAL = 0.25
PATH_RECOVERY_RETRY_INTERVAL = 1.0
def _recover_invalid_path(g, now=None):
"""Replan the remaining route if live block updates invalidated it."""
now = time.monotonic() if now is None else now
route = list(g.get('path') or ())
if route:
# A job or command may have replaced a route while recovery was pending.
if g.get('navigation_recovery_target') is not None:
g.navigation_recovery_target = None
g.navigation_retry_at = 0.0
if now >= g.get('path_validation_at', 0.0):
g.path_validation_at = now + PATH_VALIDATION_INTERVAL
start = utils.pint(g.pos)
skip_first_edge = (not g.get('on_ground', True) or
bool(g.get('y_v')))
try:
valid, invalid_node = path.Pathfinder(g).path_prefix_is_valid(
start, route, steps=2,
skip_start_edge=skip_first_edge)
except path.AStarTimeout:
valid, invalid_node = True, None
if not valid:
target = tuple(route[-1])
print('[movement] path blocked at {}; rerouting to {}'.format(
invalid_node, target), flush=True)
g.path = []
g.navigation_recovery_target = target
g.navigation_retry_at = 0.0
target = g.get('navigation_recovery_target')
if target is None or now < g.get('navigation_retry_at', 0.0):
return
position = utils.pint(g.pos)
if position == tuple(target):
g.navigation_recovery_target = None
g.navigation_retry_at = 0.0
return
try:
navpath = g.world.path_to_place(position, tuple(target))
except path.AStarTimeout:
navpath = None
if navpath:
g.path = navpath
g.navigation_recovery_target = None
g.navigation_retry_at = 0.0
print('[movement] rerouted to {}'.format(target), flush=True)
else:
g.path = []
g.navigation_retry_at = now + PATH_RECOVERY_RETRY_INTERVAL
if now >= g.get('navigation_retry_log_at', 0.0):
print('[movement] no route to {}; will retry'.format(target),
flush=True)
g.navigation_retry_log_at = now + 5.0
def tick(global_state):
g = global_state
p = g.pos
@@ -169,6 +230,8 @@ def tick(global_state):
print('[movement] status={}'.format(status), flush=True)
g.movement_status = status
_recover_invalid_path(g)
door_pause = (g.game.handle_path_doors()
if g.get('game') and
hasattr(g.game, 'handle_path_doors') else False)
@@ -442,6 +505,9 @@ def start_automation(global_state, initialize=True):
g.time_update_received_at = None
g.clock_rate = 1.0
g.path = []
g.navigation_recovery_target = None
g.navigation_retry_at = 0.0
g.path_validation_at = 0.0
g.look_at = None
g.y_v = 0.0
g.y_a = 0.0
+2
View File
@@ -348,6 +348,8 @@ class JobStates(LegacyJobStates):
if self.g.get('window'):
self.g.game.close_window()
self.g.path = []
self.g.navigation_recovery_target = None
self.g.navigation_retry_at = 0.0
self.g.look_at = None
self.g.yaw_target_override = None
if self.sleep_with_bed_states.state == self.sleep_with_bed_states.sleep_bed:
+31
View File
@@ -272,6 +272,37 @@ class Pathfinder(AStar):
return True
def _can_stand_at(self, node):
node = tuple(node)
if not self.bair(node) or self.bavoid(node):
return False
if not self.g.get('crawling'):
above = utils.padd(node, BLOCK_ABOVE)
if not self.bair(above) or self.bavoid(above):
return False
below = utils.padd(node, BLOCK_BELOW)
below_state = self.chunks.get_block_at(*below)
return (below_state is not None and not self.bair(below) and
not self.bavoid(below))
def path_prefix_is_valid(self, start, route, steps=2,
skip_start_edge=False):
"""Check upcoming route nodes against the current loaded block state."""
previous = tuple(start)
first_edge = True
for point in route[:steps]:
node = tuple(point)
if node == previous:
continue
if not self._can_stand_at(node):
return False, node
if not (first_edge and skip_start_edge):
if node not in self.neighbors(previous):
return False, node
previous = node
first_edge = False
return True, None
def neighbors(self, node):
if time.monotonic() - self.start_time > 2.0:
raise AStarTimeout