Return support jobs to work boundary
This commit is contained in:
1 parent
72a03220c3
commit
9513dd3671
4 files changed
+193
-147
No files matched your search
+17
-145
@@ -11,11 +11,12 @@ from mosfet.protocol.managers import ChunkNotLoadedException
|
||||
|
||||
from mosfet import utils
|
||||
from mosfet import path
|
||||
from mosfet.jobs.work_boundary import WorkBoundaryReturnMixin
|
||||
from mosfet.info import blocks
|
||||
from mosfet.info import items
|
||||
from mosfet.info import mobs
|
||||
|
||||
class CacheItemsStates:
|
||||
class CacheItemsStates(WorkBoundaryReturnMixin):
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
@@ -92,154 +93,31 @@ 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,
|
||||
trip_max = max(self.work_boundary_max_overshoot,
|
||||
self._work_boundary_overshoot(points))
|
||||
if (self.cache_return_required or
|
||||
if (self.work_boundary_return_required or
|
||||
trip_max > self.MAX_WORK_BOUNDARY_OVERSHOOT):
|
||||
return_point, _return_path = self._find_valid_return_plan(
|
||||
self.g.world, tuple(opening))
|
||||
return_point, _return_path = self._find_work_boundary_return_plan(
|
||||
self.g.world, tuple(opening),
|
||||
(self.work_boundary_return_point,
|
||||
self.work_boundary_start_position))
|
||||
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
|
||||
if not self.work_boundary_return_required:
|
||||
overshoot = trip_max
|
||||
print('[work boundary] cache trip exceeds limit by {:.1f} '
|
||||
'blocks; will return after caching'.format(overshoot))
|
||||
self.work_boundary_return_required = True
|
||||
self.work_boundary_return_point = return_point
|
||||
self.work_boundary_max_overshoot = 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')
|
||||
@@ -348,7 +226,7 @@ class CacheItemsStates:
|
||||
self.throw_sign = None
|
||||
self.throw_failed_slots = set()
|
||||
self.pending_throw = None
|
||||
self._capture_work_policies()
|
||||
self._capture_work_boundary()
|
||||
|
||||
num_stacks = self.g.game.count_inventory_slots()
|
||||
has_override = 'minimum' in self.__dict__
|
||||
@@ -756,8 +634,7 @@ class CacheItemsStates:
|
||||
self.state = self.cleanup
|
||||
|
||||
def cleanup(self):
|
||||
if self.cache_return_required:
|
||||
self.state = self.return_to_work_area
|
||||
if self._maybe_return_to_work_boundary(self.cleanup):
|
||||
return
|
||||
self.g.look_at = None
|
||||
self.state = self.done
|
||||
@@ -804,11 +681,6 @@ class CacheItemsStates:
|
||||
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