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