From 5197c1ece70dd05173f75f5579ec7a6e6f0c4086 Mon Sep 17 00:00:00 2001 From: Tanner Date: Mon, 9 Nov 2026 17:47:16 +0000 Subject: [PATCH] Break crops while approaching within safe reach --- mosfet/jobs/gather_crop.py | 84 ++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/mosfet/jobs/gather_crop.py b/mosfet/jobs/gather_crop.py index b5b8cc9..7f80013 100644 --- a/mosfet/jobs/gather_crop.py +++ b/mosfet/jobs/gather_crop.py @@ -19,6 +19,7 @@ from mosfet.info import mobs class GatherCropStates: CROPS_PER_BATCH = 16 SECTION_MATURITY_WEIGHT = 2.0 + CROP_REACH_DISTANCE = 3.0 def idle(self): return None @@ -158,6 +159,31 @@ class GatherCropStates: self.active_section_maturity = (len(candidates), len(cached)) return crop, None, len(candidates) + def _finish_crop_travel(self): + if (self.crop_profile and + self.crop_profile.get('travel_started_at') is not None and + utils.pint(self.g.pos) == self.crop): + self.crop_profile['travel'] = ( + time.monotonic() - self.crop_profile.pop('travel_started_at')) + + def _crop_interaction_distance(self, target_block, hit_point): + position = self.g.pos + player = tuple( + getattr(position, axis) if hasattr(position, axis) else position[index] + for index, axis in enumerate(('x', 'y', 'z'))) + eye = (player[0], player[1] + 1.62, player[2]) + distance = utils.hypot(*(eye[i] - hit_point[i] for i in range(3))) + if distance > self.CROP_REACH_DISTANCE: + return None + if not self.g.world._has_cache_line_of_sight( + eye, hit_point, tuple(target_block)): + return None + return distance + + def _crop_break_distance(self): + point = tuple(value + 0.5 for value in self.crop) + return self._crop_interaction_distance(self.crop, point) + def find_new_crop(self): started_at = time.monotonic() print('Finding new crop...') @@ -270,15 +296,31 @@ class GatherCropStates: self.state = self.find_new_crop def going_to_crop(self): - if utils.pint(self.g.pos) == self.crop: - if self.crop_profile: - self.crop_profile['travel'] = ( - time.monotonic() - - self.crop_profile.pop('travel_started_at', time.monotonic())) - print('At the crop') + world = self.g.world + current_state = world.block_at(*self.crop) + if current_state != self.type_id: + print('Crop changed while approaching; selecting another crop:', + self.crop, current_state) + self.g.path = [] + self.g.look_at = None + self.state = self.find_new_crop + return + + position = utils.pint(self.g.pos) + distance = self._crop_break_distance() + if distance is not None: + if position == self.crop: + self._finish_crop_travel() + print('At the crop') + else: + print('Crop in reach while approaching ({:.2f} blocks):'.format( + distance), self.crop) + if self.crop_profile: + self.crop_profile['break_started_en_route'] = True self.state = self.break_crop def break_crop(self): + self._finish_crop_travel() current_state = self.g.world.block_at(*self.crop) mature_crops = { blocks.MATURE_WHEAT_ID, @@ -289,11 +331,18 @@ class GatherCropStates: if current_state != self.type_id or current_state not in mature_crops: print('Crop changed before break; selecting another crop:', self.crop, current_state) + self.g.path = [] + self.g.look_at = None self.state = self.find_new_crop return - print('Requesting crop break at {}: block={}, mode={}, held={}'.format( - self.crop, current_state, - self.g.get('game_mode'), self.g.get('holding'))) + distance = self._crop_break_distance() + if distance is None: + self.state = self.going_to_crop + return + print('Requesting crop break at {}: block={}, mode={}, held={}, ' + 'reach={:.2f}'.format( + self.crop, current_state, self.g.get('game_mode'), + self.g.get('holding'), distance)) break_started_at = time.monotonic() if self.g.game.break_block(self.crop): if self.crop_profile: @@ -306,9 +355,12 @@ class GatherCropStates: print('Break request skipped; current block is', self.g.world.block_at(*self.crop)) self.bad_crops.append(self.crop) + self.g.path = [] + self.g.look_at = None self.state = self.find_new_crop def wait_break(self): + self._finish_crop_travel() current_state = self.g.world.block_at(*self.crop) if current_state != self.type_id: now = time.monotonic() @@ -329,6 +381,7 @@ class GatherCropStates: self.state = self.find_new_crop def select_seed(self): + self._finish_crop_travel() crop_seeds = { 'wheat': items.WHEAT_SEEDS_ID, 'potatoes': items.POTATO_ID, @@ -368,6 +421,7 @@ class GatherCropStates: return def wait_select(self): + self._finish_crop_travel() if self.target_seed != self.g.holding: return if self.crop_profile: @@ -377,7 +431,18 @@ class GatherCropStates: self.state = self.place_crop def place_crop(self): + self._finish_crop_travel() + position = self.g.get('pos') + if position is not None and utils.pint(position) != self.crop: + return p = utils.padd(self.crop, path.BLOCK_BELOW) + if position is not None and self.g.get('world') is not None: + hit_point = (p[0] + 0.5, p[1] + 1.0, p[2] + 0.5) + distance = self._crop_interaction_distance(p, hit_point) + if distance is None: + # Keep the existing path moving until the support face is + # within reach and visible. + return if self.crop_profile: self.crop_profile['plant_started_at'] = time.monotonic() self.place_deadline = time.time() + 3.0 @@ -387,6 +452,7 @@ class GatherCropStates: self.state = self.wait_place def wait_place(self): + self._finish_crop_travel() w = self.g.world current_state = w.block_at(*self.crop) crop_name = blocks.BLOCKS.get(self.type_id)