Break crops while approaching within safe reach
This commit is contained in:
1 parent
281e90a59f
commit
5197c1ece7
1 file changed
+75
-9
@@ -19,6 +19,7 @@ from mosfet.info import mobs
|
|||||||
class GatherCropStates:
|
class GatherCropStates:
|
||||||
CROPS_PER_BATCH = 16
|
CROPS_PER_BATCH = 16
|
||||||
SECTION_MATURITY_WEIGHT = 2.0
|
SECTION_MATURITY_WEIGHT = 2.0
|
||||||
|
CROP_REACH_DISTANCE = 3.0
|
||||||
|
|
||||||
def idle(self):
|
def idle(self):
|
||||||
return None
|
return None
|
||||||
@@ -158,6 +159,31 @@ class GatherCropStates:
|
|||||||
self.active_section_maturity = (len(candidates), len(cached))
|
self.active_section_maturity = (len(candidates), len(cached))
|
||||||
return crop, None, len(candidates)
|
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):
|
def find_new_crop(self):
|
||||||
started_at = time.monotonic()
|
started_at = time.monotonic()
|
||||||
print('Finding new crop...')
|
print('Finding new crop...')
|
||||||
@@ -270,15 +296,31 @@ class GatherCropStates:
|
|||||||
self.state = self.find_new_crop
|
self.state = self.find_new_crop
|
||||||
|
|
||||||
def going_to_crop(self):
|
def going_to_crop(self):
|
||||||
if utils.pint(self.g.pos) == self.crop:
|
world = self.g.world
|
||||||
if self.crop_profile:
|
current_state = world.block_at(*self.crop)
|
||||||
self.crop_profile['travel'] = (
|
if current_state != self.type_id:
|
||||||
time.monotonic() -
|
print('Crop changed while approaching; selecting another crop:',
|
||||||
self.crop_profile.pop('travel_started_at', time.monotonic()))
|
self.crop, current_state)
|
||||||
print('At the crop')
|
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
|
self.state = self.break_crop
|
||||||
|
|
||||||
def break_crop(self):
|
def break_crop(self):
|
||||||
|
self._finish_crop_travel()
|
||||||
current_state = self.g.world.block_at(*self.crop)
|
current_state = self.g.world.block_at(*self.crop)
|
||||||
mature_crops = {
|
mature_crops = {
|
||||||
blocks.MATURE_WHEAT_ID,
|
blocks.MATURE_WHEAT_ID,
|
||||||
@@ -289,11 +331,18 @@ class GatherCropStates:
|
|||||||
if current_state != self.type_id or current_state not in mature_crops:
|
if current_state != self.type_id or current_state not in mature_crops:
|
||||||
print('Crop changed before break; selecting another crop:',
|
print('Crop changed before break; selecting another crop:',
|
||||||
self.crop, current_state)
|
self.crop, current_state)
|
||||||
|
self.g.path = []
|
||||||
|
self.g.look_at = None
|
||||||
self.state = self.find_new_crop
|
self.state = self.find_new_crop
|
||||||
return
|
return
|
||||||
print('Requesting crop break at {}: block={}, mode={}, held={}'.format(
|
distance = self._crop_break_distance()
|
||||||
self.crop, current_state,
|
if distance is None:
|
||||||
self.g.get('game_mode'), self.g.get('holding')))
|
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()
|
break_started_at = time.monotonic()
|
||||||
if self.g.game.break_block(self.crop):
|
if self.g.game.break_block(self.crop):
|
||||||
if self.crop_profile:
|
if self.crop_profile:
|
||||||
@@ -306,9 +355,12 @@ class GatherCropStates:
|
|||||||
print('Break request skipped; current block is',
|
print('Break request skipped; current block is',
|
||||||
self.g.world.block_at(*self.crop))
|
self.g.world.block_at(*self.crop))
|
||||||
self.bad_crops.append(self.crop)
|
self.bad_crops.append(self.crop)
|
||||||
|
self.g.path = []
|
||||||
|
self.g.look_at = None
|
||||||
self.state = self.find_new_crop
|
self.state = self.find_new_crop
|
||||||
|
|
||||||
def wait_break(self):
|
def wait_break(self):
|
||||||
|
self._finish_crop_travel()
|
||||||
current_state = self.g.world.block_at(*self.crop)
|
current_state = self.g.world.block_at(*self.crop)
|
||||||
if current_state != self.type_id:
|
if current_state != self.type_id:
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
@@ -329,6 +381,7 @@ class GatherCropStates:
|
|||||||
self.state = self.find_new_crop
|
self.state = self.find_new_crop
|
||||||
|
|
||||||
def select_seed(self):
|
def select_seed(self):
|
||||||
|
self._finish_crop_travel()
|
||||||
crop_seeds = {
|
crop_seeds = {
|
||||||
'wheat': items.WHEAT_SEEDS_ID,
|
'wheat': items.WHEAT_SEEDS_ID,
|
||||||
'potatoes': items.POTATO_ID,
|
'potatoes': items.POTATO_ID,
|
||||||
@@ -368,6 +421,7 @@ class GatherCropStates:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def wait_select(self):
|
def wait_select(self):
|
||||||
|
self._finish_crop_travel()
|
||||||
if self.target_seed != self.g.holding:
|
if self.target_seed != self.g.holding:
|
||||||
return
|
return
|
||||||
if self.crop_profile:
|
if self.crop_profile:
|
||||||
@@ -377,7 +431,18 @@ class GatherCropStates:
|
|||||||
self.state = self.place_crop
|
self.state = self.place_crop
|
||||||
|
|
||||||
def place_crop(self):
|
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)
|
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:
|
if self.crop_profile:
|
||||||
self.crop_profile['plant_started_at'] = time.monotonic()
|
self.crop_profile['plant_started_at'] = time.monotonic()
|
||||||
self.place_deadline = time.time() + 3.0
|
self.place_deadline = time.time() + 3.0
|
||||||
@@ -387,6 +452,7 @@ class GatherCropStates:
|
|||||||
self.state = self.wait_place
|
self.state = self.wait_place
|
||||||
|
|
||||||
def wait_place(self):
|
def wait_place(self):
|
||||||
|
self._finish_crop_travel()
|
||||||
w = self.g.world
|
w = self.g.world
|
||||||
current_state = w.block_at(*self.crop)
|
current_state = w.block_at(*self.crop)
|
||||||
crop_name = blocks.BLOCKS.get(self.type_id)
|
crop_name = blocks.BLOCKS.get(self.type_id)
|
||||||
|
|||||||
Reference in new issue
Block a user