Return to work boundary after long cache trips
This commit is contained in:
1 parent
315bc212dc
commit
e019752ffd
2 files changed
+285
-4
No files matched your search
+166
-4
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user