Add sand safety inspection command
This commit is contained in:
1 parent
c3a045d17a
commit
142381d40f
5 files changed
+103
-51
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+45
-32
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user