Files
minecraft-bot/mosfet/jobs/gather_sand.py
T

250 lines
9.0 KiB
Python

"""Gather naturally generated sand using the protocol-777 world APIs."""
import time
from mosfet import utils
from mosfet.info import blocks
from mosfet.info import items
class GatherSandStates:
SEARCH_DISTANCE = 200
BREAK_CONFIRM_TIMEOUT = 15.0
BREAK_REACH = 4.5
NAVIGATION_STALL_TIMEOUT = 20.0
NAVIGATION_PROGRESS_EPSILON = 0.15
def _path_head_distance(self):
if not self.g.path:
return None
waypoint = self.g.path[0]
return utils.hypot(
self.g.pos.x - (waypoint[0] + 0.5),
self.g.pos.z - (waypoint[2] + 0.5))
def _begin_navigation(self):
self.navigation_path_head = (
tuple(self.g.path[0]) if self.g.path else None)
self.navigation_best_distance = self._path_head_distance()
self.navigation_last_progress_at = time.monotonic()
def _clear_navigation(self):
self.navigation_path_head = None
self.navigation_best_distance = None
self.navigation_last_progress_at = None
def _navigation_stalled(self):
now = time.monotonic()
path_head = tuple(self.g.path[0]) if self.g.path else None
distance = self._path_head_distance()
if path_head != self.navigation_path_head:
self.navigation_path_head = path_head
self.navigation_best_distance = distance
self.navigation_last_progress_at = now
elif (distance is not None and
(self.navigation_best_distance is None or
distance <= self.navigation_best_distance -
self.NAVIGATION_PROGRESS_EPSILON)):
self.navigation_best_distance = distance
self.navigation_last_progress_at = now
return (self.navigation_last_progress_at is not None and
now - self.navigation_last_progress_at >=
self.NAVIGATION_STALL_TIMEOUT)
def idle(self):
return None
def init(self):
if (not self.g.get('sand_origin_from_sign') and
(not self.g.sand_origin or
self.g.chunks.get_block_at(*self.g.sand_origin) is None)):
self.g.sand_origin = utils.pint(self.g.pos)
self.state = self.select_shovel
def select_shovel(self):
if not self.g.game.select_item(items.SHOVEL_IDS):
print('No shovel found; gathering sand without one')
self.state = self.find_new_slice
def _sand_is_safe(self, position):
return self.g.world.sand_safety(position)[0]
def find_new_slice(self):
print('Finding a new 3x3x1 sand slice...')
started = time.monotonic()
self.prev_layer, sand_slice = self.g.world.find_sand_slice(
self.g.sand_origin, self.SEARCH_DISTANCE, 10,
self.bad_slices, self.prev_layer)
if sand_slice is None:
print('No safe sand slices remain')
self.state = self.cleanup
return
self.sand_slice = tuple(sand_slice)
self.bad_slices.append(self.sand_slice)
self.bad_sand.clear()
print('Selected sand slice {} (search {:.3f}s)'.format(
self.sand_slice, time.monotonic() - started))
self.state = self.find_new_sand
def find_new_sand(self):
world = self.g.world
center = utils.pint(self.g.pos)
for sand in world.find_sand(self.sand_slice, 2, center):
if (sand not in self.bad_sand and
world.work_position_allowed(sand)):
self.sand = tuple(sand)
self.tried_openings = set()
self.state = self.nav_to_sand
return
print('Finished sand slice', self.sand_slice,
'; running pickup and maintenance')
self.state = self.cleanup
def _sand_openings(self):
world = self.g.world
openings = []
for direction in ((0, 0, -1), (0, 0, 1),
(-1, 0, 0), (1, 0, 0)):
for height in (1, 0):
opening = (self.sand[0] + direction[0],
self.sand[1] + height,
self.sand[2] + direction[2])
if opening in self.tried_openings:
continue
if not world.work_position_allowed(opening):
continue
feet = world.block_at(*opening)
head = world.block_at(*utils.padd(opening, (0, 1, 0)))
floor = world.block_at(*utils.padd(opening, (0, -1, 0)))
if (feet not in blocks.NON_SOLID_IDS or
head not in blocks.NON_SOLID_IDS or floor is None or
floor in blocks.NON_SOLID_IDS):
continue
openings.append(opening)
return openings
def nav_to_sand(self):
world = self.g.world
if not self._sand_is_safe(self.sand):
self.state = self.find_new_sand
return
position = utils.pint(self.g.pos)
routes = []
for opening in self._sand_openings():
navpath = ([opening] if position == opening else
world.path_to_place(position, opening))
if navpath:
routes.append((len(navpath), opening, navpath))
if routes:
_, self.opening, navpath = min(routes)
self.g.path = navpath
self.g.look_at = self.sand
self._begin_navigation()
print('Going to sand', self.sand, 'via', self.opening)
self.state = self.going_to_sand
else:
print('Cannot path to sand; skipping', self.sand)
self.bad_sand.add(self.sand)
self.state = self.find_new_sand
def going_to_sand(self):
if self.g.world.block_at(*self.sand) not in blocks.SAND_IDS:
self.g.path = []
self._clear_navigation()
self.state = self.find_new_sand
elif utils.pint(self.g.pos) == self.opening:
self.g.path = []
self._clear_navigation()
self.state = self.dig_sand
elif self._navigation_stalled():
print('Navigation stalled to sand {}; trying another opening'.format(
self.sand))
self.g.path = []
self.g.look_at = None
self._clear_navigation()
self.tried_openings.add(self.opening)
self.state = self.nav_to_sand
def _set_break_deadline(self):
game_deadline = getattr(self.g.game, 'break_deadline', None)
self.break_deadline = (
game_deadline + 5.0 if game_deadline is not None else
time.monotonic() + self.BREAK_CONFIRM_TIMEOUT)
def dig_sand(self):
if self.g.world.block_at(*self.sand) not in blocks.SAND_IDS:
self.g.look_at = None
self.state = self.find_new_sand
return
position = self.g.pos
eye = (position.x, position.y + 1.62, position.z)
hit_point = tuple(value + 0.5 for value in self.sand)
if (utils.phyp(eye, hit_point) > self.BREAK_REACH or
not self.g.world._has_cache_line_of_sight(
eye, hit_point, self.sand)):
self.tried_openings.add(self.opening)
self.state = self.nav_to_sand
return
breaking = self.g.get('breaking')
if breaking:
if tuple(breaking) == self.sand:
self._set_break_deadline()
self.state = self.wait_break
return
self.g.look_at = self.sand
if self.g.game.break_block(self.sand):
self._set_break_deadline()
self.state = self.wait_break
else:
print('Break request rejected; skipping', self.sand)
self.bad_sand.add(self.sand)
self.state = self.find_new_sand
def wait_break(self):
if self.break_deadline is None:
self._set_break_deadline()
if self.g.world.block_at(*self.sand) not in blocks.SAND_IDS:
print('Sand break confirmed at', self.sand)
self.g.look_at = None
self.state = self.find_new_sand
elif time.monotonic() > self.break_deadline:
print('Sand did not break; skipping', self.sand)
if self.g.get('breaking') == self.sand:
self.g.game.cancel_break()
self.g.look_at = None
self.bad_sand.add(self.sand)
self.state = self.find_new_sand
def cleanup(self):
self.g.path = []
self.g.look_at = None
self._clear_navigation()
self.state = self.done
def done(self):
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.sand = None
self.sand_slice = None
self.bad_slices = []
self.prev_layer = 0
self.bad_sand = set()
self.tried_openings = set()
self.opening = None
self.break_deadline = None
self.navigation_path_head = None
self.navigation_best_distance = None
self.navigation_last_progress_at = None
def run(self):
self.state()