diff --git a/mosfet/jobs/plant_tree.py b/mosfet/jobs/plant_tree.py index 95a32c3..3112dde 100644 --- a/mosfet/jobs/plant_tree.py +++ b/mosfet/jobs/plant_tree.py @@ -17,10 +17,52 @@ from mosfet.info import mcdata from mosfet.info import mobs class PlantTreeStates: + NAVIGATION_STALL_TIMEOUT = 8.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): + self.failed_openings = set() if self.g.chopped_tree: self.state = self.check_feet else: @@ -80,13 +122,16 @@ class PlantTreeStates: p = utils.pint(self.g.pos) for opening in w.find_tree_openings(p)[::-1]: - if not w.work_position_allowed(opening): + if (opening in self.failed_openings or + not w.work_position_allowed(opening)): continue print('trying sapling opening', opening) navpath = w.path_to_place(p, opening) if navpath: self.g.path = navpath + self.g.look_at = None self.area = opening + self._begin_navigation() self.state = self.going_to_area return else: # for @@ -95,10 +140,20 @@ class PlantTreeStates: def going_to_area(self): if utils.pint(self.g.pos) == self.area: + self._clear_navigation() self.state = self.cleanup + elif self._navigation_stalled(): + print('No path progress leaving planted sapling at {}; trying ' + 'another opening'.format(self.area)) + self.failed_openings.add(self.area) + self.g.path = [] + self.g.look_at = None + self._clear_navigation() + self.state = self.find_open_spot def cleanup(self): self.g.look_at = None + self.g.path = [] self.state = self.done def done(self): @@ -111,6 +166,10 @@ class PlantTreeStates: self.wait_time = 0 self.area = None + self.failed_openings = set() + self.navigation_path_head = None + self.navigation_best_distance = None + self.navigation_last_progress_at = None def run(self): self.state()