Log tracked hostiles while waiting at refuge
This commit is contained in:
1 parent
1fad756db7
commit
b75ad65de8
2 files changed
+79
-8
No files matched your search
+3
-2
@@ -654,8 +654,9 @@ def refresh_runtime(global_state, reload_managers=False):
|
|||||||
previous_threat_monitor.state, '__name__', 'watching')
|
previous_threat_monitor.state, '__name__', 'watching')
|
||||||
for attribute in (
|
for attribute in (
|
||||||
'next_scan_at', 'retry_at', 'safe_since', 'safety',
|
'next_scan_at', 'retry_at', 'safe_since', 'safety',
|
||||||
'safe_sign', 'threats', 'resume_job_name',
|
'safe_sign', 'threats', 'tracked_hostile_ids',
|
||||||
'job_suspended'):
|
'tracked_hostile_positions', 'next_refuge_report_at',
|
||||||
|
'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,
|
||||||
getattr(previous_threat_monitor, attribute))
|
getattr(previous_threat_monitor, attribute))
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class ThreatMonitor:
|
|||||||
THREAT_DISTANCE = 32
|
THREAT_DISTANCE = 32
|
||||||
CLEAR_DISTANCE = 40
|
CLEAR_DISTANCE = 40
|
||||||
SAFE_WAIT = 10.0
|
SAFE_WAIT = 10.0
|
||||||
|
REFUGE_REPORT_INTERVAL = 60.0
|
||||||
SAFETY_SEARCH_DISTANCE = 80
|
SAFETY_SEARCH_DISTANCE = 80
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -78,6 +79,56 @@ class ThreatMonitor:
|
|||||||
threat.get('x', 0.0), threat.get('y', 0.0),
|
threat.get('x', 0.0), threat.get('y', 0.0),
|
||||||
threat.get('z', 0.0))
|
threat.get('z', 0.0))
|
||||||
|
|
||||||
|
def _remember_hostiles(self, threats):
|
||||||
|
for threat in threats:
|
||||||
|
entity_id = threat.get('entity_id')
|
||||||
|
if entity_id is None:
|
||||||
|
continue
|
||||||
|
self.tracked_hostile_ids.add(entity_id)
|
||||||
|
self.tracked_hostile_positions[entity_id] = (
|
||||||
|
threat.get('type'), threat.get('x'),
|
||||||
|
threat.get('y'), threat.get('z'))
|
||||||
|
|
||||||
|
def _report_refuge_hostiles(self, now, nearby_ids):
|
||||||
|
if now < self.next_refuge_report_at:
|
||||||
|
return
|
||||||
|
self.next_refuge_report_at = now + self.REFUGE_REPORT_INTERVAL
|
||||||
|
entities = self.g.get('mobs', {})
|
||||||
|
bot_position = self.g.pos
|
||||||
|
descriptions = []
|
||||||
|
for entity_id in sorted(self.tracked_hostile_ids):
|
||||||
|
entity = entities.get(entity_id)
|
||||||
|
if entity is not None:
|
||||||
|
self.tracked_hostile_positions[entity_id] = (
|
||||||
|
entity.get('type'), entity.get('x'),
|
||||||
|
entity.get('y'), entity.get('z'))
|
||||||
|
description = self._describe_threat(entity)
|
||||||
|
else:
|
||||||
|
mob_type, x, y, z = self.tracked_hostile_positions.get(
|
||||||
|
entity_id, (None, None, None, None))
|
||||||
|
name = mobs.MOB_NAMES.get(
|
||||||
|
mob_type, 'unknown type {}'.format(mob_type))
|
||||||
|
if x is None:
|
||||||
|
description = '{}#{} (not currently loaded)'.format(
|
||||||
|
name, entity_id)
|
||||||
|
else:
|
||||||
|
description = '{}#{} last seen at ({:.1f}, {:.1f}, {:.1f})'.format(
|
||||||
|
name, entity_id, x, y, z)
|
||||||
|
coordinates = self.tracked_hostile_positions.get(
|
||||||
|
entity_id, (None, None, None, None))[1:]
|
||||||
|
if coordinates[0] is None:
|
||||||
|
distance_status = 'distance unknown'
|
||||||
|
else:
|
||||||
|
distance = utils.phyp(bot_position, coordinates)
|
||||||
|
distance_status = '{:.1f} blocks away'.format(distance)
|
||||||
|
range_status = ('within 40 blocks' if entity_id in nearby_ids
|
||||||
|
else 'outside 40 blocks or not loaded')
|
||||||
|
descriptions.append('{} [{}; {}]'.format(
|
||||||
|
description, distance_status, range_status))
|
||||||
|
print('[threat] refuge tracking: {}'.format(
|
||||||
|
'; '.join(descriptions) if descriptions else 'no tracked hostiles'),
|
||||||
|
flush=True)
|
||||||
|
|
||||||
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)
|
||||||
@@ -134,6 +185,9 @@ class ThreatMonitor:
|
|||||||
return
|
return
|
||||||
|
|
||||||
self.threats = threats
|
self.threats = threats
|
||||||
|
self.tracked_hostile_ids = set()
|
||||||
|
self.tracked_hostile_positions = {}
|
||||||
|
self._remember_hostiles(threats)
|
||||||
descriptions = '; '.join(self._describe_threat(threat)
|
descriptions = '; '.join(self._describe_threat(threat)
|
||||||
for threat in threats)
|
for threat in threats)
|
||||||
print('[threat] detected {} nearby hostile(s); pausing job: {}'.format(
|
print('[threat] detected {} nearby hostile(s); pausing job: {}'.format(
|
||||||
@@ -149,14 +203,16 @@ class ThreatMonitor:
|
|||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
if now >= self.next_scan_at:
|
if now >= self.next_scan_at:
|
||||||
self.next_scan_at = now + self.CHECK_INTERVAL
|
self.next_scan_at = now + self.CHECK_INTERVAL
|
||||||
threats = self._nearby_hostiles(
|
threats = self._visible_hostiles(
|
||||||
utils.pint(self.g.pos), self.THREAT_DISTANCE)
|
utils.pint(self.g.pos), self.CLEAR_DISTANCE)
|
||||||
if threats:
|
if threats:
|
||||||
self.threats = threats
|
self.threats = threats
|
||||||
|
self._remember_hostiles(threats)
|
||||||
|
|
||||||
if self.safety is not None and utils.pint(self.g.pos) == self.safety:
|
if self.safety is not None and utils.pint(self.g.pos) == self.safety:
|
||||||
self.g.path = []
|
self.g.path = []
|
||||||
self.safe_since = None
|
self.safe_since = None
|
||||||
|
self.next_refuge_report_at = 0.0
|
||||||
self.next_scan_at = 0.0
|
self.next_scan_at = 0.0
|
||||||
self.state = self.waiting
|
self.state = self.waiting
|
||||||
print('[threat] reached safety; waiting for patrol to leave',
|
print('[threat] reached safety; waiting for patrol to leave',
|
||||||
@@ -189,10 +245,18 @@ class ThreatMonitor:
|
|||||||
self.retry_at = 0.0
|
self.retry_at = 0.0
|
||||||
return
|
return
|
||||||
|
|
||||||
# Signs explicitly designate trusted refuges. Keep monitoring nearby
|
# Wait only for hostile IDs actually seen in the open while watching
|
||||||
# threats so work only resumes after the patrol has moved away, but
|
# or fleeing. Unseen underground mobs shouldn't keep the bot here.
|
||||||
# never ray-test or blacklist the sign's refuge.
|
visible = self._visible_hostiles(position, self.CLEAR_DISTANCE)
|
||||||
if self._nearby_hostiles(position, self.CLEAR_DISTANCE):
|
self._remember_hostiles(visible)
|
||||||
|
nearby = self._nearby_hostiles(position, self.CLEAR_DISTANCE)
|
||||||
|
nearby_by_id = {threat.entity_id: threat for threat in nearby
|
||||||
|
if threat.get('entity_id') is not None}
|
||||||
|
for entity_id in self.tracked_hostile_ids.intersection(nearby_by_id):
|
||||||
|
self._remember_hostiles([nearby_by_id[entity_id]])
|
||||||
|
nearby_ids = set(nearby_by_id)
|
||||||
|
self._report_refuge_hostiles(now, nearby_ids)
|
||||||
|
if self.tracked_hostile_ids.intersection(nearby_ids):
|
||||||
self.safe_since = None
|
self.safe_since = None
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -214,7 +278,10 @@ class ThreatMonitor:
|
|||||||
self.job_suspended = False
|
self.job_suspended = False
|
||||||
self.safety = None
|
self.safety = None
|
||||||
self.safe_sign = None
|
self.safe_sign = None
|
||||||
|
self.tracked_hostile_ids = set()
|
||||||
|
self.tracked_hostile_positions = {}
|
||||||
self.safe_since = None
|
self.safe_since = None
|
||||||
|
self.next_refuge_report_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
|
||||||
|
|
||||||
@@ -234,6 +301,9 @@ class ThreatMonitor:
|
|||||||
self.safety = None
|
self.safety = None
|
||||||
self.safe_sign = None
|
self.safe_sign = None
|
||||||
self.threats = []
|
self.threats = []
|
||||||
|
self.tracked_hostile_ids = set()
|
||||||
|
self.tracked_hostile_positions = {}
|
||||||
|
self.next_refuge_report_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
|
||||||
|
|||||||
Reference in new issue
Block a user