Add sand safety inspection command

This commit is contained in:
Tanner committed 2026-11-17 16:25:22 +00:00
1 parent c3a045d17a
commit 142381d40f
5 files changed
+102 -50

No files matched your search

+8 -4
View File
@@ -87,9 +87,10 @@ mosfet sand origin
The bot uses the nearest matching loaded sign within 200 blocks. A full bot 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 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 in safe 3x3x1 sections. After each section, pickup/cache/food/sleep maintenance
matching sign is found, the job keeps its previous loaded origin, or uses the runs before the job starts the next one. If no matching sign is found, the job
bot's current block position when no loaded origin is set. keeps its previous loaded origin, or uses the bot's current block position when
no loaded origin is set.
### Cache labels ### 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 The current 26.3 command path handles public chat and direct messages, replying
in the same channel. Verified commands are `!ping`, `!pos`, `!dist`, `!loaded`, 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`, `1respawn`, `1sleep`, `1loiter`, addressed path movement such as `1goto x y z`,
`1farm crops`, `1farm logs` (also available as `1farm wood`), `1gather sand`, `1farm crops`, `1farm logs` (also available as `1farm wood`), `1gather sand`,
and `1farm sand`. The command catalog below is retained from the legacy bot; 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) `!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 ### 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. These will only run for the bot they are addressed to. Replace `1` with the last character of the bot's name.
+15
View File
@@ -126,6 +126,21 @@ class Commands:
if command == 'nosquarebrackets': if command == 'nosquarebrackets':
reply = 'don\'t literally put the [ ]' 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 ## !pos - replies with position and dimension
if command == 'pos': if command == 'pos':
reply = str(utils.pint(self.g.pos))[1:-1] + ', ' + self.g.dimension reply = str(utils.pint(self.g.pos))[1:-1] + ', ' + self.g.dimension
+4 -15
View File
@@ -67,19 +67,7 @@ class GatherSandStates:
self.state = self.find_new_slice self.state = self.find_new_slice
def _sand_is_safe(self, position): def _sand_is_safe(self, position):
world = self.g.world return self.g.world.sand_safety(position)[0]
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)
def find_new_slice(self): def find_new_slice(self):
print('Finding a new 3x3x1 sand slice...') print('Finding a new 3x3x1 sand slice...')
@@ -110,8 +98,9 @@ class GatherSandStates:
self.state = self.nav_to_sand self.state = self.nav_to_sand
return return
print('No more eligible sand in slice', self.sand_slice) print('Finished sand slice', self.sand_slice,
self.state = self.find_new_slice '; running pickup and maintenance')
self.state = self.cleanup
def _sand_openings(self): def _sand_openings(self):
world = self.g.world world = self.g.world
+44 -31
View File
@@ -789,27 +789,52 @@ class World:
return False return False
return True 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): def find_sand(self, center, distance, player):
sand = [] sand = []
sand.extend(self.find_blocks(center, distance, [blocks.SAND], 25)) sand.extend(self.find_blocks(center, distance, [blocks.SAND], 25))
safe_sand = [] safe_sand = [sand for sand in sand
for s in sand: if self.sand_safety(sand)[0]]
# 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.sort(key=lambda x: utils.phyp(player, x)) safe_sand.sort(key=lambda x: utils.phyp(player, x))
return safe_sand return safe_sand
@@ -817,20 +842,8 @@ class World:
def check_sand_slice(self, center): def check_sand_slice(self, center):
# Check whether a 3x3x1 sand section contains a safe block. # Check whether a 3x3x1 sand section contains a safe block.
for i in range(9): for i in range(9):
s = utils.padd(center, utils.spiral(i)) position = utils.padd(center, utils.spiral(i))
if self.block_at(*s) != blocks.SAND: if self.sand_safety(position)[0]:
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 return True
return False return False
+31
View File
@@ -6,6 +6,37 @@ from mosfet.world import World
class IndexedSearchTests(unittest.TestCase): 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): def test_sand_search_targets_safe_blocks_in_3x3_slice_only(self):
sand_position = (1, 64, 1) sand_position = (1, 64, 1)