Monitor and dodge projectiles beyond combat range
This commit is contained in:
1 parent
0722924263
commit
0ea7b855be
1 file changed
+78
-19
+78
-19
@@ -7,8 +7,8 @@ from mosfet.info import blocks, items, mobs
|
|||||||
|
|
||||||
|
|
||||||
class CombatStates:
|
class CombatStates:
|
||||||
ACQUIRE_RADIUS = 16.0
|
ACQUIRE_RADIUS = 32.0
|
||||||
ATTACK_REACH = 3.8
|
ATTACK_REACH = 3.0
|
||||||
ATTACK_INTERVAL = 0.7
|
ATTACK_INTERVAL = 0.7
|
||||||
PATH_RETRY_INTERVAL = 1.0
|
PATH_RETRY_INTERVAL = 1.0
|
||||||
PATH_SEARCH_TIMEOUT = 0.1
|
PATH_SEARCH_TIMEOUT = 0.1
|
||||||
@@ -42,6 +42,7 @@ class CombatStates:
|
|||||||
def init(self):
|
def init(self):
|
||||||
self.target_id = None
|
self.target_id = None
|
||||||
self.path_target = None
|
self.path_target = None
|
||||||
|
self.monitor_only = False
|
||||||
self.next_path_at = 0.0
|
self.next_path_at = 0.0
|
||||||
self.next_attack_at = 0.0
|
self.next_attack_at = 0.0
|
||||||
self.failed_paths = 0
|
self.failed_paths = 0
|
||||||
@@ -132,6 +133,7 @@ class CombatStates:
|
|||||||
def find_target(self):
|
def find_target(self):
|
||||||
search_started = time.monotonic()
|
search_started = time.monotonic()
|
||||||
candidates = []
|
candidates = []
|
||||||
|
ranged_watch_candidates = []
|
||||||
for entity_id, mob in self._target_entities().items():
|
for entity_id, mob in self._target_entities().items():
|
||||||
if entity_id in self.skipped_ids:
|
if entity_id in self.skipped_ids:
|
||||||
continue
|
continue
|
||||||
@@ -146,16 +148,23 @@ class CombatStates:
|
|||||||
distance = self._distance(mob)
|
distance = self._distance(mob)
|
||||||
if distance <= self.ACQUIRE_RADIUS:
|
if distance <= self.ACQUIRE_RADIUS:
|
||||||
candidates.append((distance, entity_id, mob))
|
candidates.append((distance, entity_id, mob))
|
||||||
|
elif self._has_ranged_weapon(mob):
|
||||||
|
ranged_watch_candidates.append((distance, entity_id, mob))
|
||||||
|
|
||||||
search_elapsed = time.monotonic() - search_started
|
search_elapsed = time.monotonic() - search_started
|
||||||
|
self.monitor_only = False
|
||||||
|
if not candidates and ranged_watch_candidates:
|
||||||
|
candidates = ranged_watch_candidates
|
||||||
|
self.monitor_only = True
|
||||||
if not candidates:
|
if not candidates:
|
||||||
noun = 'players' if self.target_players else 'mobs'
|
noun = 'players' if self.target_players else 'mobs'
|
||||||
self._log('no {} within 16 blocks (scan {:.3f}s)'.format(
|
self._log('no {} or ranged threats in loaded entities '
|
||||||
noun, search_elapsed))
|
'(scan {:.3f}s)'.format(noun, search_elapsed))
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
return
|
return
|
||||||
|
|
||||||
distance, self.target_id, target = min(candidates)
|
distance, self.target_id, target = min(candidates)
|
||||||
|
self.monitor_only = distance > self.ACQUIRE_RADIUS
|
||||||
self.failed_paths = 0
|
self.failed_paths = 0
|
||||||
self.path_target = None
|
self.path_target = None
|
||||||
self.next_path_at = 0.0
|
self.next_path_at = 0.0
|
||||||
@@ -204,13 +213,27 @@ class CombatStates:
|
|||||||
yaw_delta = (float(yaw) - desired_yaw + 180.0) % 360.0 - 180.0
|
yaw_delta = (float(yaw) - desired_yaw + 180.0) % 360.0 - 180.0
|
||||||
return abs(yaw_delta) <= 40.0
|
return abs(yaw_delta) <= 40.0
|
||||||
|
|
||||||
def _incoming_projectile(self):
|
def _incoming_projectile(self, target=None):
|
||||||
player = self.g.pos
|
player = self.g.pos
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
best = None
|
best = None
|
||||||
for entity_id, projectile in self.g.get('mobs', {}).items():
|
for entity_id, projectile in self.g.get('mobs', {}).items():
|
||||||
if projectile.get('type') not in mobs.RANGED_PROJECTILE_IDS:
|
if projectile.get('type') not in mobs.RANGED_PROJECTILE_IDS:
|
||||||
continue
|
continue
|
||||||
|
spawned_at = projectile.get('spawned_at')
|
||||||
|
spawn = tuple(projectile.get('spawn_' + axis)
|
||||||
|
for axis in ('x', 'y', 'z'))
|
||||||
|
if (target is not None and
|
||||||
|
entity_id not in self.seen_projectile_ids and
|
||||||
|
self._has_ranged_weapon(target) and
|
||||||
|
self._aiming_at_player(target) and
|
||||||
|
spawned_at is not None and
|
||||||
|
0.0 <= now - spawned_at <= self.RECENT_SHOT_WINDOW and
|
||||||
|
all(value is not None for value in spawn) and
|
||||||
|
utils.phyp((target.x, target.y + 1.0, target.z), spawn)
|
||||||
|
<= self.PROJECTILE_SOURCE_RADIUS):
|
||||||
|
self.seen_projectile_ids.add(entity_id)
|
||||||
|
return projectile, 0.0, 0.0
|
||||||
px, py, pz = self._coordinates(projectile)
|
px, py, pz = self._coordinates(projectile)
|
||||||
relative = (player.x - px, player.y + 0.9 - py, player.z - pz)
|
relative = (player.x - px, player.y + 0.9 - py, player.z - pz)
|
||||||
velocity_time = projectile.get('velocity_updated_at')
|
velocity_time = projectile.get('velocity_updated_at')
|
||||||
@@ -290,7 +313,8 @@ class CombatStates:
|
|||||||
self.g.sprinting = False
|
self.g.sprinting = False
|
||||||
self._log('rush window ended')
|
self._log('rush window ended')
|
||||||
|
|
||||||
def _try_evasion(self, target, now):
|
def _try_evasion(self, target, now, advance=True,
|
||||||
|
projectile_only=False):
|
||||||
if self.rushing and now < self.rush_until:
|
if self.rushing and now < self.rush_until:
|
||||||
return False
|
return False
|
||||||
if self.evading and self.g.path:
|
if self.evading and self.g.path:
|
||||||
@@ -298,8 +322,9 @@ class CombatStates:
|
|||||||
if now < self.next_evasion_at:
|
if now < self.next_evasion_at:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
projectile = self._incoming_projectile()
|
projectile = self._incoming_projectile(target)
|
||||||
ranged_weapon = self._has_ranged_weapon(target)
|
ranged_weapon = (not projectile_only and
|
||||||
|
self._has_ranged_weapon(target))
|
||||||
facing = ranged_weapon and self._aiming_at_player(target)
|
facing = ranged_weapon and self._aiming_at_player(target)
|
||||||
aiming_with_sight = facing and self._line_of_sight(target)
|
aiming_with_sight = facing and self._line_of_sight(target)
|
||||||
if projectile is None and not aiming_with_sight:
|
if projectile is None and not aiming_with_sight:
|
||||||
@@ -333,14 +358,21 @@ class CombatStates:
|
|||||||
deadline = time.monotonic() + self.EVASION_PATH_TIMEOUT
|
deadline = time.monotonic() + self.EVASION_PATH_TIMEOUT
|
||||||
routes = []
|
routes = []
|
||||||
seen = set()
|
seen = set()
|
||||||
for advance in (2.0, 2.75):
|
maneuvers = (
|
||||||
for lateral, side in ((2.0, self.evasion_side),
|
((2.0, 2.0, self.evasion_side),
|
||||||
(3.5, -self.evasion_side)):
|
(2.0, 3.5, -self.evasion_side),
|
||||||
|
(2.75, 2.0, -self.evasion_side),
|
||||||
|
(2.75, 3.5, self.evasion_side)) if advance else
|
||||||
|
((0.0, 2.5, self.evasion_side),
|
||||||
|
(0.0, 3.5, -self.evasion_side),
|
||||||
|
(0.0, 4.0, self.evasion_side))
|
||||||
|
)
|
||||||
|
for forward_step, lateral, side in maneuvers:
|
||||||
x = math.floor(
|
x = math.floor(
|
||||||
player.x + toward[0] * advance +
|
player.x + toward[0] * forward_step +
|
||||||
perpendicular[0] * lateral * side)
|
perpendicular[0] * lateral * side)
|
||||||
z = math.floor(
|
z = math.floor(
|
||||||
player.z + toward[1] * advance +
|
player.z + toward[1] * forward_step +
|
||||||
perpendicular[1] * lateral * side)
|
perpendicular[1] * lateral * side)
|
||||||
destination = self._grounded_approach_block(
|
destination = self._grounded_approach_block(
|
||||||
x, z, start[1])
|
x, z, start[1])
|
||||||
@@ -367,10 +399,11 @@ class CombatStates:
|
|||||||
target.x - (destination[0] + 0.5),
|
target.x - (destination[0] + 0.5),
|
||||||
target.z - (destination[2] + 0.5))
|
target.z - (destination[2] + 0.5))
|
||||||
progress = toward_length - after_distance
|
progress = toward_length - after_distance
|
||||||
if progress <= 0.4:
|
if advance and progress <= 0.4:
|
||||||
continue
|
continue
|
||||||
routes.append((0 if cover else 1, -progress, len(route),
|
rank = (0 if cover else 1, -progress if advance else 0,
|
||||||
destination, route, cover, progress))
|
len(route))
|
||||||
|
routes.append((rank, destination, route, cover, progress))
|
||||||
|
|
||||||
if not routes:
|
if not routes:
|
||||||
self.next_evasion_at = now + 0.5
|
self.next_evasion_at = now + 0.5
|
||||||
@@ -379,8 +412,7 @@ class CombatStates:
|
|||||||
reason, self.EVASION_PATH_TIMEOUT))
|
reason, self.EVASION_PATH_TIMEOUT))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
(_cover_rank, _progress_rank, _length, destination, route,
|
(_rank, destination, route, has_cover, advance) = min(routes)
|
||||||
has_cover, advance) = min(routes)
|
|
||||||
self.g.path = route
|
self.g.path = route
|
||||||
self.path_target = None
|
self.path_target = None
|
||||||
self.evading = True
|
self.evading = True
|
||||||
@@ -476,7 +508,27 @@ class CombatStates:
|
|||||||
distance = self._distance(target)
|
distance = self._distance(target)
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
if distance > self.ACQUIRE_RADIUS:
|
if distance > self.ACQUIRE_RADIUS:
|
||||||
self._log_target_end('target left the 16-block radius')
|
if self._has_ranged_weapon(target):
|
||||||
|
if not self.monitor_only:
|
||||||
|
self._log('target outside 32 blocks; monitoring ranged '
|
||||||
|
'threats without pursuing')
|
||||||
|
self._stop_rush()
|
||||||
|
self.monitor_only = True
|
||||||
|
self.g.path = []
|
||||||
|
self.path_target = None
|
||||||
|
self.evading = False
|
||||||
|
if self.evading and self.g.path:
|
||||||
|
return
|
||||||
|
if self.evading:
|
||||||
|
self.evading = False
|
||||||
|
if self._try_evasion(
|
||||||
|
target, now, advance=False, projectile_only=True):
|
||||||
|
return
|
||||||
|
self.g.path = []
|
||||||
|
self.path_target = None
|
||||||
|
return
|
||||||
|
|
||||||
|
self._log_target_end('target left the 32-block radius')
|
||||||
self._stop_rush()
|
self._stop_rush()
|
||||||
self.target_id = None
|
self.target_id = None
|
||||||
self.g.path = []
|
self.g.path = []
|
||||||
@@ -484,6 +536,13 @@ class CombatStates:
|
|||||||
self.state = self.find_target
|
self.state = self.find_target
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if self.monitor_only:
|
||||||
|
self._log('target back within 32 blocks; resuming pursuit')
|
||||||
|
self.monitor_only = False
|
||||||
|
self.g.path = []
|
||||||
|
self.path_target = None
|
||||||
|
self.next_path_at = 0.0
|
||||||
|
|
||||||
if self.rushing and now >= self.rush_until:
|
if self.rushing and now >= self.rush_until:
|
||||||
self._stop_rush()
|
self._stop_rush()
|
||||||
if (distance > self.ATTACK_REACH and now >= self.next_rush_at):
|
if (distance > self.ATTACK_REACH and now >= self.next_rush_at):
|
||||||
|
|||||||
Reference in new issue
Block a user