diff --git a/mosfet/bot.py b/mosfet/bot.py index 671f266..060576b 100644 --- a/mosfet/bot.py +++ b/mosfet/bot.py @@ -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 diff --git a/mosfet/job.py b/mosfet/job.py index cbcb1bb..4a39742 100644 --- a/mosfet/job.py +++ b/mosfet/job.py @@ -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: diff --git a/mosfet/path.py b/mosfet/path.py index e76c10e..3e3c8ae 100644 --- a/mosfet/path.py +++ b/mosfet/path.py @@ -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 diff --git a/tests/test_door_navigation.py b/tests/test_door_navigation.py index e56e965..1604ea5 100644 --- a/tests/test_door_navigation.py +++ b/tests/test_door_navigation.py @@ -1,6 +1,8 @@ import unittest +from unittest.mock import patch from munch import Munch +from mosfet.bot import _recover_invalid_path from mosfet.game import Game from mosfet.info import blocks from mosfet.path import Pathfinder @@ -42,6 +44,67 @@ class DoorNavigationTests(unittest.TestCase): finder = Pathfinder(g) self.assertFalse(finder.bair((1, 64, 0))) + def test_path_prefix_detects_new_obstacles_and_missing_support(self): + class TerrainChunks: + def __init__(self): + self.states = {} + + def get_block_at(self, x, y, z): + position = (x, y, z) + if position in self.states: + return self.states[position] + return blocks.STONE if y == 63 else blocks.AIR + + chunks = TerrainChunks() + g = Munch(chunks=chunks, crawling=False, unopenable_doors=set()) + route = [(1, 64, 0), (2, 64, 0), (3, 64, 0)] + + valid, invalid_node = Pathfinder(g).path_prefix_is_valid( + (0, 64, 0), route) + self.assertTrue(valid) + self.assertIsNone(invalid_node) + + chunks.states[(2, 64, 0)] = blocks.STONE + valid, invalid_node = Pathfinder(g).path_prefix_is_valid( + (0, 64, 0), route) + self.assertFalse(valid) + self.assertEqual(invalid_node, (2, 64, 0)) + + chunks.states.clear() + chunks.states[(2, 63, 0)] = blocks.AIR + valid, invalid_node = Pathfinder(g).path_prefix_is_valid( + (0, 64, 0), route) + self.assertFalse(valid) + self.assertEqual(invalid_node, (2, 64, 0)) + + def test_invalid_path_is_replanned_to_its_original_destination(self): + class FakePathfinder: + @staticmethod + def path_prefix_is_valid(*_args, **_kwargs): + return False, (1, 64, 0) + + class FakeWorld: + def __init__(self): + self.calls = [] + + def path_to_place(self, start, goal): + self.calls.append((start, goal)) + return [start, (1, 64, 0), goal] + + world = FakeWorld() + g = Munch( + path=[(1, 64, 0), (2, 64, 0), (5, 64, 0)], + pos=(0, 64, 0), on_ground=True, y_v=0, + path_validation_at=0, navigation_recovery_target=None, + navigation_retry_at=0, world=world) + + with patch('mosfet.bot.path.Pathfinder', return_value=FakePathfinder()): + _recover_invalid_path(g, now=1.0) + + self.assertEqual(world.calls, [((0, 64, 0), (5, 64, 0))]) + self.assertEqual(g.path, [(0, 64, 0), (1, 64, 0), (5, 64, 0)]) + self.assertIsNone(g.navigation_recovery_target) + def test_pressure_plate_opened_door_is_not_toggled_or_closed(self): door = (1, 64, 0) after = (2, 64, 0)