From 142381d40ff26bddfd7c54fefd7aaef4e7e039c2 Mon Sep 17 00:00:00 2001 From: Tanner Date: Tue, 17 Nov 2026 16:05:59 +0000 Subject: [PATCH] Add sand safety inspection command --- README.md | 12 ++++-- mosfet/commands.py | 15 +++++++ mosfet/jobs/gather_sand.py | 19 ++------- mosfet/world.py | 77 +++++++++++++++++++++--------------- tests/test_indexed_search.py | 31 +++++++++++++++ 5 files changed, 103 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 42900eb..155e6dc 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,10 @@ mosfet sand origin The bot uses the nearest matching loaded sign within 200 blocks. A full bot name such as `mosfet1` can be used instead of the group name. Sand is harvested -in safe 3x3x1 sections, clearing one section before moving to the next. If no -matching sign is found, the job keeps its previous loaded origin, or uses the -bot's current block position when no loaded origin is set. +in safe 3x3x1 sections. After each section, pickup/cache/food/sleep maintenance +runs before the job starts the next one. If no matching sign is found, the job +keeps its previous loaded origin, or uses the bot's current block position when +no loaded origin is set. ### Cache labels @@ -221,7 +222,8 @@ and all bots will run the command. The current 26.3 command path handles public chat and direct messages, replying in the same channel. Verified commands are `!ping`, `!pos`, `!dist`, `!loaded`, -`!info x y z`, `!inv` (basic slot/count data), `1cache`, `1drain`, `1eat`, +`!info x y z`, `!safesand x y z`, `!inv` (basic slot/count data), `1cache`, +`1drain`, `1eat`, `1respawn`, `1sleep`, `1loiter`, addressed path movement such as `1goto x y z`, `1farm crops`, `1farm logs` (also available as `1farm wood`), `1gather sand`, and `1farm sand`. The command catalog below is retained from the legacy bot; @@ -287,6 +289,8 @@ These can be ran by anyone, all bots will reply. `!block x y z` - replies what block is at (x, y, z) +`!safesand x y z` - checks whether sand at the coordinate is safe to dig + ### Bot-specific Commands These will only run for the bot they are addressed to. Replace `1` with the last character of the bot's name. diff --git a/mosfet/commands.py b/mosfet/commands.py index c0900a6..bd3163b 100644 --- a/mosfet/commands.py +++ b/mosfet/commands.py @@ -126,6 +126,21 @@ class Commands: if command == 'nosquarebrackets': reply = 'don\'t literally put the [ ]' + ## !safesand x y z - report whether a sand block is safe to dig + if command == 'safesand': + coordinates = re.split(r'\s*,\s*|\s+', data.strip()) + if (len(coordinates) != 3 or + any(not re.fullmatch(r'-?\d+', value) + for value in coordinates)): + reply = 'no: use !safesand x y z (spaces or commas)' + else: + position = tuple(int(value) for value in coordinates) + safe, reason = self.g.world.sand_safety(position) + if safe and not self.g.world.work_position_allowed(position): + safe = False + reason = 'outside active work-sign boundaries' + reply = '{}: {}'.format('yes' if safe else 'no', reason) + ## !pos - replies with position and dimension if command == 'pos': reply = str(utils.pint(self.g.pos))[1:-1] + ', ' + self.g.dimension diff --git a/mosfet/jobs/gather_sand.py b/mosfet/jobs/gather_sand.py index 6845c37..a4c0ce1 100644 --- a/mosfet/jobs/gather_sand.py +++ b/mosfet/jobs/gather_sand.py @@ -67,19 +67,7 @@ class GatherSandStates: self.state = self.find_new_slice def _sand_is_safe(self, position): - world = self.g.world - if world.block_at(*position) != blocks.SAND: - return False - - below = world.block_at(*utils.padd(position, (0, -1, 0))) - below_two = world.block_at(*utils.padd(position, (0, -2, 0))) - above = world.block_at(*utils.padd(position, (0, 1, 0))) - if (below is None or below_two is None or above is None or - below in blocks.NON_SOLID_IDS or - below_two in blocks.NON_SOLID_IDS or - above not in blocks.NON_SOLID_IDS): - return False - return world.sand_adjacent_safe(position) + return self.g.world.sand_safety(position)[0] def find_new_slice(self): print('Finding a new 3x3x1 sand slice...') @@ -110,8 +98,9 @@ class GatherSandStates: self.state = self.nav_to_sand return - print('No more eligible sand in slice', self.sand_slice) - self.state = self.find_new_slice + print('Finished sand slice', self.sand_slice, + '; running pickup and maintenance') + self.state = self.cleanup def _sand_openings(self): world = self.g.world diff --git a/mosfet/world.py b/mosfet/world.py index e18dddb..c4e0619 100644 --- a/mosfet/world.py +++ b/mosfet/world.py @@ -789,27 +789,52 @@ class World: return False return True + 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) + state_id = self.block_at(*position) + if state_id is None: + return False, 'block is not loaded' + if state_id != blocks.SAND: + name = blocks.BLOCKS.get(state_id, 'unknown block') + return False, 'block is {}, not sand'.format(name) + + for depth in (1, 2): + support_pos = utils.padd(position, (0, -depth, 0)) + support = self.block_at(*support_pos) + if support is None: + return False, 'support block at {} is not loaded'.format( + support_pos) + if support in blocks.NON_SOLID_IDS: + name = blocks.BLOCKS.get(support, 'non-solid block') + return False, 'support block at {} is {} (needs solid support)'.format( + support_pos, name) + + above_pos = utils.padd(position, path.BLOCK_ABOVE) + above = self.block_at(*above_pos) + if above is None: + return False, 'headroom block at {} is not loaded'.format(above_pos) + if above not in blocks.NON_SOLID_IDS: + name = blocks.BLOCKS.get(above, 'solid block') + 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 = self.block_at(*adjacent_pos) + if adjacent in blocks.AVOID_IDS: + name = blocks.BLOCKS.get(adjacent, 'hazard block') + return False, 'unsafe adjacent {} at {}'.format( + name, adjacent_pos) + + return True, 'solid support below, headroom above, no adjacent hazards' + def find_sand(self, center, distance, player): sand = [] sand.extend(self.find_blocks(center, distance, [blocks.SAND], 25)) - safe_sand = [] - for s in sand: - # make sure it has solid below - if self.block_at(*utils.padd(s, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS: - continue - # make sure it has solid two below - prevent hanging sand - if self.block_at(*utils.padd(s, path.BLOCK_BELOW2)) in blocks.NON_SOLID_IDS: - continue - - # and walkable air above - if self.block_at(*utils.padd(s, path.BLOCK_ABOVE)) not in blocks.NON_SOLID_IDS: - continue - - if not self.sand_adjacent_safe(s): - continue - - safe_sand.append(s) + safe_sand = [sand for sand in sand + if self.sand_safety(sand)[0]] safe_sand.sort(key=lambda x: utils.phyp(player, x)) return safe_sand @@ -817,21 +842,9 @@ class World: def check_sand_slice(self, center): # Check whether a 3x3x1 sand section contains a safe block. for i in range(9): - s = utils.padd(center, utils.spiral(i)) - if self.block_at(*s) != blocks.SAND: - continue - # make sure it has solid below - if self.block_at(*utils.padd(s, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS: - continue - # make sure it has solid two below - prevent hanging sand - if self.block_at(*utils.padd(s, path.BLOCK_BELOW2)) in blocks.NON_SOLID_IDS: - continue - # and walkable air above - if self.block_at(*utils.padd(s, path.BLOCK_ABOVE)) not in blocks.NON_SOLID_IDS: - continue - if not self.sand_adjacent_safe(s): - continue - return True + position = utils.padd(center, utils.spiral(i)) + if self.sand_safety(position)[0]: + return True return False diff --git a/tests/test_indexed_search.py b/tests/test_indexed_search.py index 98d75d3..fdf48b8 100644 --- a/tests/test_indexed_search.py +++ b/tests/test_indexed_search.py @@ -6,6 +6,37 @@ from mosfet.world import World class IndexedSearchTests(unittest.TestCase): + def test_sand_safety_reports_support_headroom_and_adjacent_hazards(self): + sand = (0, 64, 0) + world_blocks = { + sand: blocks.SAND, + (0, 63, 0): blocks.STONE, + (0, 62, 0): blocks.STONE, + } + chunks = Munch(get_block_at=lambda *position: + world_blocks.get(tuple(position), blocks.AIR)) + world = World(Munch(chunks=chunks)) + + self.assertEqual(world.sand_safety(sand)[0], True) + + world_blocks[(0, 63, 0)] = blocks.AIR + safe, reason = world.sand_safety(sand) + self.assertFalse(safe) + self.assertIn('support block', reason) + + world_blocks[(0, 63, 0)] = blocks.STONE + world_blocks[(0, 65, 0)] = blocks.STONE + safe, reason = world.sand_safety(sand) + self.assertFalse(safe) + self.assertIn('headroom', reason) + + world_blocks[(0, 65, 0)] = blocks.AIR + water = next(iter(blocks.WATER_IDS)) + world_blocks[(1, 64, 0)] = water + safe, reason = world.sand_safety(sand) + self.assertFalse(safe) + self.assertIn('unsafe adjacent water', reason) + def test_sand_search_targets_safe_blocks_in_3x3_slice_only(self): sand_position = (1, 64, 1)