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
+103 -51

No files matched your search

+45 -32
View File
@@ -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