diff --git a/mosfet/jobs/cache_items.py b/mosfet/jobs/cache_items.py index 80cca71..9eea57c 100644 --- a/mosfet/jobs/cache_items.py +++ b/mosfet/jobs/cache_items.py @@ -11,11 +11,12 @@ from mosfet.protocol.managers import ChunkNotLoadedException from mosfet import utils from mosfet import path +from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin from mosfet.info import blocks from mosfet.info import items from mosfet.info import mobs -class CacheItemsStates: +class CacheItemsStates(WorkBoundaryReturnMixin): def idle(self): return None @@ -92,154 +93,31 @@ class CacheItemsStates: return (tuple(sign['position']), sign.get('side'), sign.get('item_name')) - MAX_WORK_BOUNDARY_OVERSHOOT = 40 - - def _capture_work_policies(self): - world = self.g.get('world') - finder = getattr(world, 'sign_policies', None) - self.work_policies = (finder(self.g.get('name', '')) - if callable(finder) else []) - position = self.g.get('pos') - self.cache_start_position = ( - utils.pint(position) if position is not None and - position is not False else None) - self.cache_return_point = ( - self.cache_start_position - if self.cache_start_position is not None and - self._inside_work_policies(self.cache_start_position) else None) - self.cache_return_required = False - self.cache_trip_max_outside = ( - self._work_boundary_overshoot([self.cache_start_position]) - if self.work_policies and self.cache_start_position is not None - else 0.0) - self.return_retry_at = 0.0 - - def _inside_work_policies(self, position): - world = self.g.get('world') - return bool(self.work_policies) and all( - world._inside_sign_policy(position, policy) - for policy in self.work_policies) - - def _work_boundary_overshoot(self, positions): - world = self.g.world - return max(( - max(0.0, - world._sign_policy_distance(position, policy) - - policy['distance']) - for position in positions for policy in self.work_policies - ), default=0.0) - - def _return_candidates(self, world, from_position): - candidates = [] - for position in (self.cache_return_point, - self.cache_start_position): - if (position is not None and - self._inside_work_policies(position) and - position not in candidates): - candidates.append(position) - - standable = getattr(world, '_cache_opening_standable', None) - if callable(standable): - offsets = ((0, 0), (1, 0), (-1, 0), (0, 1), (0, -1), - (1, 1), (1, -1), (-1, 1), (-1, -1)) - for policy in self.work_policies: - anchor = tuple(policy['position']) - radius = max(0.0, policy['distance'] - 1.0) - dx = from_position[0] - anchor[0] - dz = from_position[2] - anchor[2] - if policy['shape'] == 'square': - base_x = anchor[0] + max(-radius, min(radius, dx)) - base_z = anchor[2] + max(-radius, min(radius, dz)) - else: - distance = utils.hypot(dx, dz) - scale = min(1.0, radius / distance) if distance else 0.0 - base_x = anchor[0] + dx * scale - base_z = anchor[2] + dz * scale - base = (int(round(base_x)), anchor[1], int(round(base_z))) - for dy in (0, -1, 1): - for ox, oz in offsets: - point = (base[0] + ox, base[1] + dy, - base[2] + oz) - if (point not in candidates and - self._inside_work_policies(point) and - standable(point)): - candidates.append(point) - - return sorted(candidates, key=lambda point: ( - utils.phyp(from_position, point), point)) - - def _find_valid_return_plan(self, world, from_position): - for point in self._return_candidates(world, from_position): - navpath = world.path_to_place(from_position, point) - if navpath is None: - continue - path_points = [tuple(from_position)] + [tuple(pos) - for pos in navpath] - if all(world._path_allowed_by_sign(path_points, policy) - for policy in self.work_policies): - return point, navpath - return None, None - def _accept_cache_trip(self, start, opening, navpath): if not self.work_policies: return True points = [tuple(start)] + [tuple(position) for position in navpath] - trip_max = max(self.cache_trip_max_outside, + trip_max = max(self.work_boundary_max_overshoot, self._work_boundary_overshoot(points)) - if (self.cache_return_required or + if (self.work_boundary_return_required or trip_max > self.MAX_WORK_BOUNDARY_OVERSHOOT): - return_point, _return_path = self._find_valid_return_plan( - self.g.world, tuple(opening)) + return_point, _return_path = self._find_work_boundary_return_plan( + self.g.world, tuple(opening), + (self.work_boundary_return_point, + self.work_boundary_start_position)) if return_point is None: print('Skipping cache opening outside work area; no valid return path:', opening) return False - self.cache_return_required = True - self.cache_return_point = return_point - self.cache_trip_max_outside = trip_max + if not self.work_boundary_return_required: + overshoot = trip_max + print('[work boundary] cache trip exceeds limit by {:.1f} ' + 'blocks; will return after caching'.format(overshoot)) + self.work_boundary_return_required = True + self.work_boundary_return_point = return_point + self.work_boundary_max_overshoot = trip_max return True - def return_to_work_area(self): - if not self.cache_return_required: - self.state = self.cleanup - return - now = time.monotonic() - if now < self.return_retry_at: - return - - current = utils.pint(self.g.pos) - if (self.cache_return_point is not None and - current == self.cache_return_point): - self.g.path = [] - self.g.look_at = None - self.cache_return_required = False - self.state = self.cleanup - return - - return_point, navpath = self._find_valid_return_plan( - self.g.world, current) - if navpath is None: - if self.return_retry_at == 0.0: - print('Could not replan a valid path back inside the work area; ' - 'will retry') - self.return_retry_at = now + 2.0 - return - - self.cache_return_point = return_point - self.return_retry_at = 0.0 - self.g.path = navpath - self.g.look_at = None - print('Returning inside work boundary to', return_point) - self.state = self.going_to_return_point - - def going_to_return_point(self): - if utils.pint(self.g.pos) == self.cache_return_point: - print('Back inside work boundary at', self.cache_return_point) - self.g.path = [] - self.g.look_at = None - self.cache_return_required = False - self.state = self.cleanup - def choose_throw_sign(self): print('Checking item-throw signs...') world = self.g.get('world') @@ -348,7 +226,7 @@ class CacheItemsStates: self.throw_sign = None self.throw_failed_slots = set() self.pending_throw = None - self._capture_work_policies() + self._capture_work_boundary() num_stacks = self.g.game.count_inventory_slots() has_override = 'minimum' in self.__dict__ @@ -756,8 +634,7 @@ class CacheItemsStates: self.state = self.cleanup def cleanup(self): - if self.cache_return_required: - self.state = self.return_to_work_area + if self._maybe_return_to_work_boundary(self.cleanup): return self.g.look_at = None self.state = self.done @@ -804,11 +681,6 @@ class CacheItemsStates: self.throw_failed_slots = set() self.pending_throw = None self.work_policies = [] - self.cache_start_position = None - self.cache_return_point = None - self.cache_return_required = False - self.cache_trip_max_outside = 0.0 - self.return_retry_at = 0.0 def run(self): self._refresh_useless_chests() diff --git a/mosfet/jobs/grab_supplies.py b/mosfet/jobs/grab_supplies.py index b3b8ac7..af6b640 100644 --- a/mosfet/jobs/grab_supplies.py +++ b/mosfet/jobs/grab_supplies.py @@ -14,8 +14,9 @@ from mosfet import path from mosfet.info import blocks from mosfet.info import items from mosfet.info import mobs +from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin -class GrabSuppliesStates: +class GrabSuppliesStates(WorkBoundaryReturnMixin): DEFAULT_SUPPLIES = { tuple(items.BED_IDS): (1, 1), (items.CHEST_ID,): (1, 1), @@ -29,6 +30,7 @@ class GrabSuppliesStates: return None def init(self): + self._capture_work_boundary() print('Started grab supplies states') self.checked_supply_chests = set() @@ -568,6 +570,8 @@ class GrabSuppliesStates: self.state = self.choose_supply_chest def cleanup(self): + if self._maybe_return_to_work_boundary(self.cleanup): + return self.state = self.done def done(self): @@ -598,7 +602,9 @@ class GrabSuppliesStates: self.failed_drop_slots = set() self.pending_drop_slot = None self.protected_ground_items_logged = set() + self.work_policies = [] def run(self): + self._track_work_boundary_position() self._refresh_useless_chests() self.state() diff --git a/mosfet/jobs/sleep_with_bed.py b/mosfet/jobs/sleep_with_bed.py index 74283d6..79075e3 100644 --- a/mosfet/jobs/sleep_with_bed.py +++ b/mosfet/jobs/sleep_with_bed.py @@ -15,8 +15,9 @@ from mosfet.info import blocks from mosfet.info import items from mosfet.info import mcdata from mosfet.info import mobs +from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin -class SleepWithBedStates: +class SleepWithBedStates(WorkBoundaryReturnMixin): def idle(self): return None @@ -24,6 +25,7 @@ class SleepWithBedStates: return self.g.get('weather') == 'thunderstorm' def init(self): + self._capture_work_boundary() self.sleep_confirmed = False self.my_bed = False self.area = None @@ -395,6 +397,8 @@ class SleepWithBedStates: self.g.world.block_at(*self.area) in blocks.BED_IDS): self.state = self.break_bed return + if self._maybe_return_to_work_boundary(self.cleanup): + return self.g.look_at = None self.g.path = [] self.g.yaw_target_override = None @@ -433,8 +437,10 @@ class SleepWithBedStates: self.sleep_retry_at = 0 self.force_new_bed = False self.sleep_confirmed = False + self.work_policies = [] self.sleep_deadline = 0 self.sleep_attempt_started_at = None def run(self): + self._track_work_boundary_position() self.state() diff --git a/mosfet/jobs/work_boundary.py b/mosfet/jobs/work_boundary.py new file mode 100644 index 0000000..ae3c72d --- /dev/null +++ b/mosfet/jobs/work_boundary.py @@ -0,0 +1,162 @@ +import time + +from mosfet import utils + + +class WorkBoundaryReturnMixin: + """Shared work-sign boundary tracking and return-path planning.""" + + MAX_WORK_BOUNDARY_OVERSHOOT = 40 + WORK_BOUNDARY_RETURN_RETRY = 2.0 + + def _capture_work_boundary(self): + world = self.g.get('world') + finder = getattr(world, 'sign_policies', None) + self.work_policies = (finder(self.g.get('name', '')) + if callable(finder) else []) + position = self.g.get('pos') + self.work_boundary_start_position = ( + utils.pint(position) if position is not None and + position is not False else None) + self.work_boundary_return_point = ( + self.work_boundary_start_position + if self.work_boundary_start_position is not None and + self._inside_work_policies(self.work_boundary_start_position) + else None) + self.work_boundary_return_required = False + self.work_boundary_max_overshoot = ( + self._work_boundary_overshoot( + [self.work_boundary_start_position]) + if self.work_policies and + self.work_boundary_start_position is not None else 0.0) + self.work_boundary_return_retry_at = 0.0 + + def _inside_work_policies(self, position): + return bool(self.work_policies) and all( + self.g.world._inside_sign_policy(position, policy) + for policy in self.work_policies) + + def _work_boundary_overshoot(self, positions): + world = self.g.world + return max(( + max(0.0, + world._sign_policy_distance(position, policy) - + policy['distance']) + for position in positions for policy in self.work_policies + ), default=0.0) + + def _track_work_boundary_position(self): + if not self.work_policies: + return + position = self.g.get('pos') + if position is None or position is False: + return + overshoot = self._work_boundary_overshoot([utils.pint(position)]) + self.work_boundary_max_overshoot = max( + self.work_boundary_max_overshoot, overshoot) + if (self.work_boundary_max_overshoot > + self.MAX_WORK_BOUNDARY_OVERSHOOT and + not self.work_boundary_return_required): + print('[work boundary] overshoot {:.1f} blocks exceeds {}; ' + 'will return after this task'.format( + self.work_boundary_max_overshoot, + self.MAX_WORK_BOUNDARY_OVERSHOOT)) + self.work_boundary_return_required = True + + def _work_boundary_return_candidates(self, world, from_position, + preferred_points=()): + candidates = [] + for position in preferred_points: + if (position is not None and + self._inside_work_policies(position) and + position not in candidates): + candidates.append(position) + + standable = getattr(world, '_cache_opening_standable', None) + if callable(standable): + offsets = ((0, 0), (1, 0), (-1, 0), (0, 1), (0, -1), + (1, 1), (1, -1), (-1, 1), (-1, -1)) + for policy in self.work_policies: + anchor = tuple(policy['position']) + radius = max(0.0, policy['distance'] - 1.0) + dx = from_position[0] - anchor[0] + dz = from_position[2] - anchor[2] + if policy['shape'] == 'square': + base_x = anchor[0] + max(-radius, min(radius, dx)) + base_z = anchor[2] + max(-radius, min(radius, dz)) + else: + distance = utils.hypot(dx, dz) + scale = min(1.0, radius / distance) if distance else 0.0 + base_x = anchor[0] + dx * scale + base_z = anchor[2] + dz * scale + base = (int(round(base_x)), anchor[1], int(round(base_z))) + for dy in (0, -1, 1): + for ox, oz in offsets: + point = (base[0] + ox, base[1] + dy, + base[2] + oz) + if (point not in candidates and + self._inside_work_policies(point) and + standable(point)): + candidates.append(point) + + return sorted(candidates, key=lambda point: ( + utils.phyp(from_position, point), point)) + + def _find_work_boundary_return_plan(self, world, from_position, + preferred_points=()): + for point in self._work_boundary_return_candidates( + world, from_position, preferred_points): + navpath = world.path_to_place(from_position, point) + if navpath is not None: + return point, navpath + return None, None + + def _maybe_return_to_work_boundary(self, resume_state): + if not getattr(self, 'work_boundary_return_required', False): + return False + + now = time.monotonic() + current = utils.pint(self.g.pos) + if (self.work_boundary_return_point is not None and + current == self.work_boundary_return_point): + self.g.path = [] + self.g.look_at = None + self.work_boundary_return_required = False + self.work_boundary_max_overshoot = 0.0 + self.work_boundary_return_retry_at = 0.0 + return False + + if now < self.work_boundary_return_retry_at: + return True + + preferred_points = (self.work_boundary_return_point, + self.work_boundary_start_position) + return_point, navpath = self._find_work_boundary_return_plan( + self.g.world, current, preferred_points) + if navpath is None: + if self.work_boundary_return_retry_at == 0.0: + print('[work boundary] no valid return path yet; will retry') + self.work_boundary_return_retry_at = ( + now + self.WORK_BOUNDARY_RETURN_RETRY) + return True + + self.work_boundary_return_point = return_point + self.work_boundary_return_retry_at = 0.0 + self.work_boundary_resume_state = resume_state + self.g.path = navpath + self.g.look_at = None + print('[work boundary] returning inside to', return_point) + self.state = self._work_boundary_returning + return True + + def _work_boundary_returning(self): + if utils.pint(self.g.pos) != self.work_boundary_return_point: + return + print('[work boundary] returned inside at', + self.work_boundary_return_point) + self.g.path = [] + self.g.look_at = None + self.work_boundary_return_required = False + self.work_boundary_max_overshoot = 0.0 + self.work_boundary_return_retry_at = 0.0 + self.state = self.work_boundary_resume_state