From 2ecaa9a30a61c98e7265e6be5013a9f76e30d034 Mon Sep 17 00:00:00 2001 From: Tanner Date: Tue, 17 Nov 2026 22:07:19 +0000 Subject: [PATCH] Avoid false path invalidation while airborne --- mosfet/bot.py | 6 +++- mosfet/path.py | 14 +++++++-- tests/test_door_navigation.py | 55 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/mosfet/bot.py b/mosfet/bot.py index f1ae69e..0c37d24 100644 --- a/mosfet/bot.py +++ b/mosfet/bot.py @@ -137,9 +137,13 @@ def _recover_invalid_path(g, now=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_start_edge = (not g.get('on_ground', True) or + bool(g.get('y_v'))) try: valid, invalid_node = path.Pathfinder(g).path_prefix_is_valid( - route, steps=2) + route, steps=2, start=start, + skip_start_edge=skip_start_edge) except path.AStarTimeout: valid, invalid_node = True, None if not valid: diff --git a/mosfet/path.py b/mosfet/path.py index c30b705..aa59af7 100644 --- a/mosfet/path.py +++ b/mosfet/path.py @@ -300,17 +300,25 @@ class Pathfinder(AStar): return self.check_parkour(start, offset) return False - def path_prefix_is_valid(self, route, steps=2): - """Check upcoming route nodes against the current loaded block state.""" - previous = None + def path_prefix_is_valid(self, route, steps=2, start=None, + skip_start_edge=False): + """Check upcoming route nodes without treating midair start as blocked.""" + previous = tuple(start) if start is not None else None + first_edge = True for point in route[:steps]: node = tuple(point) + if previous is not None and node == previous: + # A* includes its start in the reconstructed route. The bot + # may currently be jumping/falling through that cell. + continue if not self._can_stand_at(node): return False, node if (previous is not None and + not (first_edge and skip_start_edge) and not self._path_edge_is_valid(previous, node)): return False, node previous = node + first_edge = False return True, None def neighbors(self, node): diff --git a/tests/test_door_navigation.py b/tests/test_door_navigation.py index d44f2ce..bd4e585 100644 --- a/tests/test_door_navigation.py +++ b/tests/test_door_navigation.py @@ -82,6 +82,61 @@ class DoorNavigationTests(unittest.TestCase): self.assertFalse(valid) self.assertEqual(invalid_node, (2, 64, 0)) + def test_airborne_path_start_is_not_misreported_as_a_blocked_node(self): + class MidairStartChunks: + def get_block_at(self, x, y, z): + if (x, y, z) == (0, 63, 0): + return blocks.AIR + return blocks.STONE if y == 63 else blocks.AIR + + start = (0, 64, 0) + route = [start, (1, 64, 0), (2, 64, 0)] + chunks = MidairStartChunks() + finder = Pathfinder(Munch( + chunks=chunks, crawling=False, unopenable_doors=set())) + + valid, invalid_node = finder.path_prefix_is_valid( + route, steps=2, start=start, skip_start_edge=True) + + self.assertTrue(valid) + self.assertIsNone(invalid_node) + + # Real obstacles farther ahead in the unchanged route remain detectable. + chunks.get_block_at = lambda x, y, z: ( + blocks.STONE if (x, y, z) == (2, 64, 0) or y == 63 + else blocks.AIR) + valid, invalid_node = Pathfinder(Munch( + chunks=chunks, crawling=False, + unopenable_doors=set())).path_prefix_is_valid( + route, steps=3, start=start, skip_start_edge=True) + + self.assertFalse(valid) + self.assertEqual(invalid_node, (2, 64, 0)) + + def test_path_recovery_ignores_unsupported_airborne_start_cell(self): + class MidairStartChunks: + def get_block_at(self, x, y, z): + if (x, y, z) == (0, 63, 0): + return blocks.AIR + return blocks.STONE if y == 63 else blocks.AIR + + class FakeWorld: + def path_to_place(self, *_args): + raise AssertionError('unchanged path should not be replanned') + + route = [(0, 64, 0), (1, 64, 0), (2, 64, 0)] + g = Munch( + chunks=MidairStartChunks(), crawling=False, + unopenable_doors=set(), world=FakeWorld(), + path=list(route), pos=(0.5, 64.2, 0.5), + on_ground=False, y_v=1.0, path_validation_at=0.0, + navigation_recovery_target=None, navigation_retry_at=0.0) + + _recover_invalid_path(g, now=1.0) + + self.assertEqual(g.path, route) + self.assertIsNone(g.navigation_recovery_target) + def test_invalid_path_is_replanned_to_its_original_destination(self): class FakePathfinder: @staticmethod