diff --git a/mosfet/world.py b/mosfet/world.py index c4e0619..33bdc3f 100644 --- a/mosfet/world.py +++ b/mosfet/world.py @@ -789,6 +789,18 @@ class World: return False return True + def _sand_has_immediate_support(self, position): + support_pos = utils.padd(position, path.BLOCK_BELOW) + support = self.block_at(*support_pos) + if support is None: + return False, 'support under sand at {} is not loaded'.format( + support_pos) + if support in blocks.NON_SOLID_IDS: + name = blocks.BLOCKS.get(support, 'non-solid block') + return False, 'sand at {} is unsupported: {} immediately below at {}'.format( + position, name, support_pos) + return True, None + def sand_safety(self, position): """Return whether a sand block is safe to dig and the reason.""" position = tuple(int(value) for value in position) @@ -819,15 +831,28 @@ class World: return False, 'block above at {} is {} (needs headroom)'.format( above_pos, name) - for direction in path.CHECK_DIRECTIONS: - adjacent_pos = utils.padd(position, direction) + adjacent_positions = [ + utils.padd(position, direction) + for direction in path.CHECK_DIRECTIONS + ] + adjacent_positions.append(utils.padd(position, path.BLOCK_BELOW)) + for adjacent_pos in adjacent_positions: adjacent = self.block_at(*adjacent_pos) + if adjacent is None: + return False, 'adjacent block at {} is not loaded'.format( + adjacent_pos) if adjacent in blocks.AVOID_IDS: name = blocks.BLOCKS.get(adjacent, 'hazard block') return False, 'unsafe adjacent {} at {}'.format( name, adjacent_pos) + if adjacent == blocks.SAND: + supported, reason = self._sand_has_immediate_support( + adjacent_pos) + if not supported: + return False, reason - return True, 'solid support below, headroom above, no adjacent hazards' + return (True, 'solid support below, headroom above, adjacent sand ' + 'supported, no hazards') def find_sand(self, center, distance, player): sand = [] diff --git a/tests/test_indexed_search.py b/tests/test_indexed_search.py index fdf48b8..abd5e5b 100644 --- a/tests/test_indexed_search.py +++ b/tests/test_indexed_search.py @@ -37,6 +37,16 @@ class IndexedSearchTests(unittest.TestCase): self.assertFalse(safe) self.assertIn('unsafe adjacent water', reason) + world_blocks[(1, 64, 0)] = blocks.SAND + safe, reason = world.sand_safety(sand) + self.assertFalse(safe) + self.assertIn('unsupported', reason) + + # A sand block immediately below this neighboring sand counts as its + # support; don't inspect the rest of that lower sand column. + world_blocks[(1, 63, 0)] = blocks.SAND + self.assertTrue(world.sand_safety(sand)[0]) + def test_sand_search_targets_safe_blocks_in_3x3_slice_only(self): sand_position = (1, 64, 1)