Return to work boundary after long cache trips

This commit is contained in:
Tanner committed 2026-11-10 21:26:28 +00:00
1 parent 315bc212dc
commit e019752ffd
2 files changed
+285 -4

No files matched your search

+166 -4
View File
@@ -92,6 +92,154 @@ 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,
self._work_boundary_overshoot(points))
if (self.cache_return_required or
trip_max > self.MAX_WORK_BOUNDARY_OVERSHOOT):
return_point, _return_path = self._find_valid_return_plan(
self.g.world, tuple(opening))
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
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')
@@ -110,7 +258,8 @@ class CacheItemsStates:
for opening in finder(sign, center, 3):
opening_position = tuple(opening['position'])
navpath = world.path_to_place(center, opening_position)
if not navpath:
if (not navpath or not self._accept_cache_trip(
center, opening_position, navpath)):
continue
self.throw_sign = sign
self.throw_failed_slots = set()
@@ -199,6 +348,7 @@ class CacheItemsStates:
self.throw_sign = None
self.throw_failed_slots = set()
self.pending_throw = None
self._capture_work_policies()
num_stacks = self.g.game.count_inventory_slots()
has_override = 'minimum' in self.__dict__
@@ -274,7 +424,8 @@ class CacheItemsStates:
if self._opening_in_bad_region(w, opening_position):
continue
navpath = w.path_to_place(p, opening_position)
if navpath:
if (navpath and
self._accept_cache_trip(p, opening_position, navpath)):
print('Going to labeled chest', container['labels'],
self.area)
self.opening = opening_position
@@ -313,7 +464,8 @@ class CacheItemsStates:
if self._opening_in_bad_region(w, opening_position):
continue
navpath = w.path_to_place(p, opening_position)
if navpath:
if (navpath and
self._accept_cache_trip(p, opening_position, navpath)):
print('navpath to trapped chest:', navpath)
self.opening = opening_position
self.open_interaction = opening
@@ -372,7 +524,8 @@ class CacheItemsStates:
if self._opening_in_bad_region(w, opening_position):
continue
navpath = w.path_to_place(p, opening_position)
if navpath:
if (navpath and
self._accept_cache_trip(p, opening_position, navpath)):
self.opening = opening_position
self.open_interaction = opening
break
@@ -603,6 +756,9 @@ class CacheItemsStates:
self.state = self.cleanup
def cleanup(self):
if self.cache_return_required:
self.state = self.return_to_work_area
return
self.g.look_at = None
self.state = self.done
if (self.g.get('job') and
@@ -647,6 +803,12 @@ class CacheItemsStates:
self.throw_sign = None
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()
+119
View File
@@ -7,6 +7,7 @@ from minecraft.networking.types import BlockFace
from mosfet.info import items
from mosfet.jobs.cache_items import CacheItemsStates
from mosfet.world import World
class CacheItemsTests(unittest.TestCase):
@@ -164,6 +165,124 @@ class CacheItemsTests(unittest.TestCase):
state.select_chest()
self.assertIs(state.state.__func__, state.cleanup.__func__)
def test_long_cache_trip_requires_a_valid_return_inside_work_boundary(self):
start = (0, 64, 0)
opening = (51, 64, 0)
policy = {'position': start, 'distance': 10, 'shape': 'square'}
class FakeWorld:
@staticmethod
def _sign_policy_distance(position, boundary):
return World._sign_policy_distance(position, boundary)
@staticmethod
def _inside_sign_policy(position, boundary):
return World._inside_sign_policy(position, boundary)
@staticmethod
def _path_allowed_by_sign(points, boundary):
return World._path_allowed_by_sign(points, boundary)
@staticmethod
def _cache_opening_standable(_position):
return True
@staticmethod
def sign_policies(_bot_name):
return [policy]
@staticmethod
def path_to_place(origin, target):
return [tuple(origin), tuple(target)]
state = CacheItemsStates(Munch(
world=FakeWorld(), name='mosfet2', pos=start))
state._capture_work_policies()
self.assertTrue(state._accept_cache_trip(
start, opening, [start, opening]))
self.assertTrue(state.cache_return_required)
self.assertTrue(state._inside_work_policies(
state.cache_return_point))
self.assertEqual(state.cache_trip_max_outside, 41)
def test_cache_trip_exactly_40_beyond_boundary_does_not_trigger_return(self):
start = (0, 64, 0)
opening = (50, 64, 0)
policy = {'position': start, 'distance': 10, 'shape': 'square'}
class FakeWorld:
@staticmethod
def _sign_policy_distance(position, boundary):
return World._sign_policy_distance(position, boundary)
@staticmethod
def _inside_sign_policy(position, boundary):
return World._inside_sign_policy(position, boundary)
@staticmethod
def sign_policies(_bot_name):
return [policy]
state = CacheItemsStates(Munch(
world=FakeWorld(), name='mosfet2', pos=start))
state._capture_work_policies()
self.assertTrue(state._accept_cache_trip(
start, opening, [start, opening]))
self.assertFalse(state.cache_return_required)
self.assertEqual(state.cache_trip_max_outside, 40)
def test_cache_replans_return_path_after_finishing_chests(self):
start = (0, 64, 0)
opening = (51, 64, 0)
return_point = (9, 64, 0)
policy = {'position': start, 'distance': 10, 'shape': 'square'}
path_calls = []
class FakeWorld:
@staticmethod
def _sign_policy_distance(position, boundary):
return World._sign_policy_distance(position, boundary)
@staticmethod
def _inside_sign_policy(position, boundary):
return World._inside_sign_policy(position, boundary)
@staticmethod
def _path_allowed_by_sign(points, boundary):
return World._path_allowed_by_sign(points, boundary)
@staticmethod
def _cache_opening_standable(_position):
return True
@staticmethod
def path_to_place(origin, target):
path_calls.append((tuple(origin), tuple(target)))
return [tuple(origin), tuple(target)]
g = Munch(world=FakeWorld(), name='mosfet2', pos=opening,
path=[], look_at=None)
state = CacheItemsStates(g)
state.work_policies = [policy]
state.cache_return_point = return_point
state.cache_return_required = True
state.cleanup()
self.assertIs(state.state.__func__, state.return_to_work_area.__func__)
state.return_to_work_area()
self.assertIn((opening, state.cache_return_point), path_calls)
self.assertTrue(state._inside_work_policies(state.cache_return_point))
self.assertIs(state.state.__func__, state.going_to_return_point.__func__)
self.assertEqual(g.path[-1], state.cache_return_point)
g.pos = state.cache_return_point
state.going_to_return_point()
self.assertFalse(state.cache_return_required)
self.assertIs(state.state.__func__, state.cleanup.__func__)
def test_wanted_single_stack_is_not_considered_transferable(self):
wanted_id = items.WHEAT_SEEDS_ID
old_wanted = items.WANTED_ITEMS