diff --git a/mosfet/bot.py b/mosfet/bot.py index c32e93d..b3f1de7 100644 --- a/mosfet/bot.py +++ b/mosfet/bot.py @@ -654,8 +654,9 @@ def refresh_runtime(global_state, reload_managers=False): previous_threat_monitor.state, '__name__', 'watching') for attribute in ( 'next_scan_at', 'retry_at', 'safe_since', 'safety', - 'safe_sign', 'threats', 'resume_job_name', - 'job_suspended'): + 'safe_sign', 'threats', 'tracked_hostile_ids', + 'tracked_hostile_positions', 'next_refuge_report_at', + 'resume_job_name', 'job_suspended'): if hasattr(previous_threat_monitor, attribute): setattr(threat_monitor, attribute, getattr(previous_threat_monitor, attribute)) diff --git a/mosfet/jobs/check_threats.py b/mosfet/jobs/check_threats.py index 3386290..0d0cec7 100644 --- a/mosfet/jobs/check_threats.py +++ b/mosfet/jobs/check_threats.py @@ -9,6 +9,7 @@ class ThreatMonitor: THREAT_DISTANCE = 32 CLEAR_DISTANCE = 40 SAFE_WAIT = 10.0 + REFUGE_REPORT_INTERVAL = 60.0 SAFETY_SEARCH_DISTANCE = 80 @property @@ -78,6 +79,56 @@ class ThreatMonitor: threat.get('x', 0.0), threat.get('y', 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): world = self.g.world position = utils.pint(self.g.pos) @@ -134,6 +185,9 @@ class ThreatMonitor: return self.threats = threats + self.tracked_hostile_ids = set() + self.tracked_hostile_positions = {} + self._remember_hostiles(threats) descriptions = '; '.join(self._describe_threat(threat) for threat in threats) print('[threat] detected {} nearby hostile(s); pausing job: {}'.format( @@ -149,14 +203,16 @@ class ThreatMonitor: now = time.monotonic() if now >= self.next_scan_at: self.next_scan_at = now + self.CHECK_INTERVAL - threats = self._nearby_hostiles( - utils.pint(self.g.pos), self.THREAT_DISTANCE) + threats = self._visible_hostiles( + utils.pint(self.g.pos), self.CLEAR_DISTANCE) if threats: self.threats = threats + self._remember_hostiles(threats) if self.safety is not None and utils.pint(self.g.pos) == self.safety: self.g.path = [] self.safe_since = None + self.next_refuge_report_at = 0.0 self.next_scan_at = 0.0 self.state = self.waiting print('[threat] reached safety; waiting for patrol to leave', @@ -189,10 +245,18 @@ class ThreatMonitor: self.retry_at = 0.0 return - # Signs explicitly designate trusted refuges. Keep monitoring nearby - # threats so work only resumes after the patrol has moved away, but - # never ray-test or blacklist the sign's refuge. - if self._nearby_hostiles(position, self.CLEAR_DISTANCE): + # Wait only for hostile IDs actually seen in the open while watching + # or fleeing. Unseen underground mobs shouldn't keep the bot here. + visible = self._visible_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 return @@ -214,7 +278,10 @@ class ThreatMonitor: self.job_suspended = False self.safety = None self.safe_sign = None + self.tracked_hostile_ids = set() + self.tracked_hostile_positions = {} self.safe_since = None + self.next_refuge_report_at = 0.0 self.next_scan_at = time.monotonic() + self.CHECK_INTERVAL self.state = self.watching @@ -234,6 +301,9 @@ class ThreatMonitor: self.safety = None self.safe_sign = None self.threats = [] + self.tracked_hostile_ids = set() + self.tracked_hostile_positions = {} + self.next_refuge_report_at = 0.0 self.resume_job_name = None self.job_suspended = False self.suspending_job = False