Allow refuge maintenance without resuming jobs

This commit is contained in:
Tanner committed 2026-11-14 18:14:54 +00:00
1 parent 9513dd3671
commit 4cd90a5d65
3 files changed
+128 -13

No files matched your search

+1
View File
@@ -656,6 +656,7 @@ def refresh_runtime(global_state, reload_managers=False):
'next_scan_at', 'retry_at', 'safe_since', 'safety', 'next_scan_at', 'retry_at', 'safe_since', 'safety',
'safe_sign', 'threats', 'tracked_hostile_ids', 'safe_sign', 'threats', 'tracked_hostile_ids',
'tracked_hostile_positions', 'next_refuge_report_at', 'tracked_hostile_positions', 'next_refuge_report_at',
'next_refuge_eat_at', 'next_refuge_sleep_at',
'resume_job_name', 'job_suspended'): 'resume_job_name', 'job_suspended'):
if hasattr(previous_threat_monitor, attribute): if hasattr(previous_threat_monitor, attribute):
setattr(threat_monitor, attribute, setattr(threat_monitor, attribute,
+89 -5
View File
@@ -10,6 +10,8 @@ class ThreatMonitor:
CLEAR_DISTANCE = 40 CLEAR_DISTANCE = 40
SAFE_WAIT = 10.0 SAFE_WAIT = 10.0
REFUGE_REPORT_INTERVAL = 60.0 REFUGE_REPORT_INTERVAL = 60.0
REFUGE_EAT_RETRY_INTERVAL = 5.0
REFUGE_SLEEP_RETRY_INTERVAL = 60.0
SAFETY_SEARCH_DISTANCE = 80 SAFETY_SEARCH_DISTANCE = 80
@property @property
@@ -129,6 +131,77 @@ class ThreatMonitor:
'; '.join(descriptions) if descriptions else 'no tracked hostiles'), '; '.join(descriptions) if descriptions else 'no tracked hostiles'),
flush=True) flush=True)
def _run_refuge_maintenance(self, now):
job = self.g.get('job')
if job is None:
return
food = getattr(job, 'eat_food_states', None)
if food is not None:
if food.state == food.done:
food.state = food.idle
self.next_refuge_eat_at = (
now + self.REFUGE_EAT_RETRY_INTERVAL)
food_level = self.g.get('food')
health = self.g.get('health')
needs_food = (
food_level is not None and
(food_level < 12 or
(health is not None and health < 20 and food_level < 18)))
if (food.state == food.idle and needs_food and
now >= self.next_refuge_eat_at):
food.state = food.init
if food.state != food.idle:
food.run()
if food.state == food.done:
food.state = food.idle
self.next_refuge_eat_at = (
now + self.REFUGE_EAT_RETRY_INTERVAL)
sleep = getattr(job, 'sleep_with_bed_states', None)
if sleep is not None:
if sleep.state == sleep.done:
sleep.state = sleep.idle
sleep.refuge_sign = None
self.next_refuge_sleep_at = (
now + self.REFUGE_SLEEP_RETRY_INTERVAL)
current_time = self.g.get('time')
sleep_due = (
self.g.get('dimension') == 'overworld' and
(self.g.get('weather') == 'thunderstorm' or
(current_time is not None and current_time >= 12550)))
if (sleep.state == sleep.idle and sleep_due and
self.safe_sign is not None and
now >= self.next_refuge_sleep_at):
sleep.refuge_sign = self.safe_sign
sleep.state = sleep.init
if sleep.state != sleep.idle:
sleep.run()
if sleep.state == sleep.done:
sleep.state = sleep.idle
sleep.refuge_sign = None
self.next_refuge_sleep_at = (
now + self.REFUGE_SLEEP_RETRY_INTERVAL)
def _refuge_sleep_in_progress(self):
job = self.g.get('job')
sleep = getattr(job, 'sleep_with_bed_states', None)
if sleep is None or self.safe_sign is None:
return False
if sleep.state in (sleep.idle, sleep.done) or sleep.area is None:
return False
return utils.phyp(
self.safe_sign['position'], sleep.area) <= 5
def _refuge_maintenance_in_progress(self):
job = self.g.get('job')
for name in ('eat_food_states', 'sleep_with_bed_states'):
machine = getattr(job, name, None) if job is not None else None
if (machine is not None and
machine.state not in (machine.idle, machine.done)):
return True
return False
def _find_safety(self): def _find_safety(self):
world = self.g.world world = self.g.world
position = utils.pint(self.g.pos) position = utils.pint(self.g.pos)
@@ -233,17 +306,19 @@ class ThreatMonitor:
def waiting(self): def waiting(self):
self._capture_job_change() self._capture_job_change()
now = time.monotonic() now = time.monotonic()
if now < self.next_scan_at: self._run_refuge_maintenance(now)
return
self.next_scan_at = now + self.CHECK_INTERVAL
position = utils.pint(self.g.pos) position = utils.pint(self.g.pos)
if position != self.safety: if (position != self.safety and
not self._refuge_sleep_in_progress()):
# If knockback or a correction moved us off the refuge, return to # If knockback or a correction moved us off the refuge, return to
# the same sign instead of searching for another one. # the same sign instead of searching for another one.
self.state = self.evacuating self.state = self.evacuating
self.retry_at = 0.0 self.retry_at = 0.0
return return
if now < self.next_scan_at:
return
self.next_scan_at = now + self.CHECK_INTERVAL
# Wait only for hostile IDs actually seen in the open while watching # Wait only for hostile IDs actually seen in the open while watching
# or fleeing. Unseen underground mobs shouldn't keep the bot here. # or fleeing. Unseen underground mobs shouldn't keep the bot here.
@@ -262,7 +337,8 @@ class ThreatMonitor:
if self.safe_since is None: if self.safe_since is None:
self.safe_since = now self.safe_since = now
elif now - self.safe_since >= self.SAFE_WAIT: elif (now - self.safe_since >= self.SAFE_WAIT and
not self._refuge_maintenance_in_progress()):
self.state = self.resuming self.state = self.resuming
def resuming(self): def resuming(self):
@@ -280,8 +356,14 @@ class ThreatMonitor:
self.safe_sign = None self.safe_sign = None
self.tracked_hostile_ids = set() self.tracked_hostile_ids = set()
self.tracked_hostile_positions = {} self.tracked_hostile_positions = {}
job = self.g.get('job')
sleep = getattr(job, 'sleep_with_bed_states', None)
if sleep is not None:
sleep.refuge_sign = None
self.safe_since = None self.safe_since = None
self.next_refuge_report_at = 0.0 self.next_refuge_report_at = 0.0
self.next_refuge_eat_at = 0.0
self.next_refuge_sleep_at = 0.0
self.next_scan_at = time.monotonic() + self.CHECK_INTERVAL self.next_scan_at = time.monotonic() + self.CHECK_INTERVAL
self.state = self.watching self.state = self.watching
@@ -304,6 +386,8 @@ class ThreatMonitor:
self.tracked_hostile_ids = set() self.tracked_hostile_ids = set()
self.tracked_hostile_positions = {} self.tracked_hostile_positions = {}
self.next_refuge_report_at = 0.0 self.next_refuge_report_at = 0.0
self.next_refuge_eat_at = 0.0
self.next_refuge_sleep_at = 0.0
self.resume_job_name = None self.resume_job_name = None
self.job_suspended = False self.job_suspended = False
self.suspending_job = False self.suspending_job = False
+38 -8
View File
@@ -89,7 +89,19 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
w = self.g.world w = self.g.world
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
if self.force_new_bed: refuge_sign = getattr(self, 'refuge_sign', None)
if refuge_sign is not None:
# Refuge maintenance may use existing beds near the sign only;
# never roam farther or place a bed while waiting for threats.
center = tuple(refuge_sign['position'])
indexed = w.find_blocks_indexed(center, blocks.BED_IDS, 5)
nearby = list(w.find_blocks_3d(
center, blocks.BED_IDS, distance=5, y_limit=5))
result = list(dict.fromkeys(nearby + indexed))
result = [bed for bed in result
if utils.phyp(center, bed) <= 5]
result.sort(key=lambda bed: utils.phyp(p, bed))
elif self.force_new_bed:
result = [] result = []
print('Skipping existing beds; placing a new bed as requested') print('Skipping existing beds; placing a new bed as requested')
else: else:
@@ -104,9 +116,13 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
print('Found beds by local scan; chunk index had none') print('Found beds by local scan; chunk index had none')
result = list(dict.fromkeys(self._bed_foot(bed) for bed in result)) result = list(dict.fromkeys(self._bed_foot(bed) for bed in result))
if refuge_sign is not None:
center = tuple(refuge_sign['position'])
result = [bed for bed in result
if utils.phyp(center, bed) <= 5]
self.beds = [] self.beds = []
for bed in result: for bed in result:
if bed in self.bad_beds: if refuge_sign is None and bed in self.bad_beds:
continue continue
if w.check_bed_occupied(bed): if w.check_bed_occupied(bed):
@@ -128,8 +144,11 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
navpath = w.path_to_place_faked(p, bed) navpath = w.path_to_place_faked(p, bed)
print('navpath:', navpath) print('navpath:', navpath)
if navpath is None: if navpath is None:
self.bad_beds.append(bed) if getattr(self, 'refuge_sign', None) is None:
print('Cant get to bed, blacklisting') self.bad_beds.append(bed)
print('Cant get to bed, blacklisting')
else:
print('Cant reach refuge bed this attempt; skipping')
continue continue
self.area = bed self.area = bed
@@ -142,6 +161,11 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
self.state = self.going_to_bed self.state = self.going_to_bed
return return
if getattr(self, 'refuge_sign', None) is not None:
print('No existing bed within 5 blocks of refuge sign; not placing one')
self.state = self.cleanup
return
print('No reachable beds; trying to place one') print('No reachable beds; trying to place one')
self.state = self.select_bed self.state = self.select_bed
@@ -160,7 +184,8 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
elif time.monotonic() >= self.opening_deadline: elif time.monotonic() >= self.opening_deadline:
print('Could not reach bed opening', self.opening, print('Could not reach bed opening', self.opening,
'; trying another bed') '; trying another bed')
self.bad_beds.append(self.area) if getattr(self, 'refuge_sign', None) is None:
self.bad_beds.append(self.area)
self.g.look_at = None self.g.look_at = None
self.state = self.choose_bed self.state = self.choose_bed
else: else:
@@ -359,8 +384,11 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
self.sleep_retry_at = time.monotonic() + 1.5 self.sleep_retry_at = time.monotonic() + 1.5
elif (not self.sleep_confirmed and elif (not self.sleep_confirmed and
time.monotonic() >= self.sleep_deadline): time.monotonic() >= self.sleep_deadline):
print('Bed use was not confirmed; blacklisting', self.area) if getattr(self, 'refuge_sign', None) is None:
self.bad_beds.append(self.area) print('Bed use was not confirmed; blacklisting', self.area)
self.bad_beds.append(self.area)
else:
print('Bed use was not confirmed; will retry later', self.area)
self.state = self.cleanup self.state = self.cleanup
def break_bed(self): def break_bed(self):
@@ -397,7 +425,8 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
self.g.world.block_at(*self.area) in blocks.BED_IDS): self.g.world.block_at(*self.area) in blocks.BED_IDS):
self.state = self.break_bed self.state = self.break_bed
return return
if self._maybe_return_to_work_boundary(self.cleanup): if (getattr(self, 'refuge_sign', None) is None and
self._maybe_return_to_work_boundary(self.cleanup)):
return return
self.g.look_at = None self.g.look_at = None
self.g.path = [] self.g.path = []
@@ -440,6 +469,7 @@ class SleepWithBedStates(WorkBoundaryReturnMixin):
self.work_policies = [] self.work_policies = []
self.sleep_deadline = 0 self.sleep_deadline = 0
self.sleep_attempt_started_at = None self.sleep_attempt_started_at = None
self.refuge_sign = None
def run(self): def run(self):
self._track_work_boundary_position() self._track_work_boundary_position()