Monitor and dodge projectiles beyond combat range
This commit is contained in:
1 parent
0722924263
commit
0ea7b855be
1 file changed
+108
-49
+108
-49
@@ -7,8 +7,8 @@ from mosfet.info import blocks, items, mobs
|
||||
|
||||
|
||||
class CombatStates:
|
||||
ACQUIRE_RADIUS = 16.0
|
||||
ATTACK_REACH = 3.8
|
||||
ACQUIRE_RADIUS = 32.0
|
||||
ATTACK_REACH = 3.0
|
||||
ATTACK_INTERVAL = 0.7
|
||||
PATH_RETRY_INTERVAL = 1.0
|
||||
PATH_SEARCH_TIMEOUT = 0.1
|
||||
@@ -42,6 +42,7 @@ class CombatStates:
|
||||
def init(self):
|
||||
self.target_id = None
|
||||
self.path_target = None
|
||||
self.monitor_only = False
|
||||
self.next_path_at = 0.0
|
||||
self.next_attack_at = 0.0
|
||||
self.failed_paths = 0
|
||||
@@ -132,6 +133,7 @@ class CombatStates:
|
||||
def find_target(self):
|
||||
search_started = time.monotonic()
|
||||
candidates = []
|
||||
ranged_watch_candidates = []
|
||||
for entity_id, mob in self._target_entities().items():
|
||||
if entity_id in self.skipped_ids:
|
||||
continue
|
||||
@@ -146,16 +148,23 @@ class CombatStates:
|
||||
distance = self._distance(mob)
|
||||
if distance <= self.ACQUIRE_RADIUS:
|
||||
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
|
||||
self.monitor_only = False
|
||||
if not candidates and ranged_watch_candidates:
|
||||
candidates = ranged_watch_candidates
|
||||
self.monitor_only = True
|
||||
if not candidates:
|
||||
noun = 'players' if self.target_players else 'mobs'
|
||||
self._log('no {} within 16 blocks (scan {:.3f}s)'.format(
|
||||
noun, search_elapsed))
|
||||
self._log('no {} or ranged threats in loaded entities '
|
||||
'(scan {:.3f}s)'.format(noun, search_elapsed))
|
||||
self.cleanup()
|
||||
return
|
||||
|
||||
distance, self.target_id, target = min(candidates)
|
||||
self.monitor_only = distance > self.ACQUIRE_RADIUS
|
||||
self.failed_paths = 0
|
||||
self.path_target = None
|
||||
self.next_path_at = 0.0
|
||||
@@ -204,13 +213,27 @@ class CombatStates:
|
||||
yaw_delta = (float(yaw) - desired_yaw + 180.0) % 360.0 - 180.0
|
||||
return abs(yaw_delta) <= 40.0
|
||||
|
||||
def _incoming_projectile(self):
|
||||
def _incoming_projectile(self, target=None):
|
||||
player = self.g.pos
|
||||
now = time.monotonic()
|
||||
best = None
|
||||
for entity_id, projectile in self.g.get('mobs', {}).items():
|
||||
if projectile.get('type') not in mobs.RANGED_PROJECTILE_IDS:
|
||||
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)
|
||||
relative = (player.x - px, player.y + 0.9 - py, player.z - pz)
|
||||
velocity_time = projectile.get('velocity_updated_at')
|
||||
@@ -290,7 +313,8 @@ class CombatStates:
|
||||
self.g.sprinting = False
|
||||
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:
|
||||
return False
|
||||
if self.evading and self.g.path:
|
||||
@@ -298,8 +322,9 @@ class CombatStates:
|
||||
if now < self.next_evasion_at:
|
||||
return False
|
||||
|
||||
projectile = self._incoming_projectile()
|
||||
ranged_weapon = self._has_ranged_weapon(target)
|
||||
projectile = self._incoming_projectile(target)
|
||||
ranged_weapon = (not projectile_only and
|
||||
self._has_ranged_weapon(target))
|
||||
facing = ranged_weapon and self._aiming_at_player(target)
|
||||
aiming_with_sight = facing and self._line_of_sight(target)
|
||||
if projectile is None and not aiming_with_sight:
|
||||
@@ -333,44 +358,52 @@ class CombatStates:
|
||||
deadline = time.monotonic() + self.EVASION_PATH_TIMEOUT
|
||||
routes = []
|
||||
seen = set()
|
||||
for advance in (2.0, 2.75):
|
||||
for lateral, side in ((2.0, self.evasion_side),
|
||||
(3.5, -self.evasion_side)):
|
||||
x = math.floor(
|
||||
player.x + toward[0] * advance +
|
||||
perpendicular[0] * lateral * side)
|
||||
z = math.floor(
|
||||
player.z + toward[1] * advance +
|
||||
perpendicular[1] * lateral * side)
|
||||
destination = self._grounded_approach_block(
|
||||
x, z, start[1])
|
||||
if destination is None or destination == start or destination in seen:
|
||||
continue
|
||||
seen.add(destination)
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
break
|
||||
route = self.g.world.path_to_place(
|
||||
start, destination, timeout=remaining)
|
||||
if not route or len(route) <= 1:
|
||||
continue
|
||||
cover = False
|
||||
check = getattr(
|
||||
self.g.world, '_has_cache_line_of_sight', None)
|
||||
if callable(check):
|
||||
source = (target.x, target.y + 1.5, target.z)
|
||||
endpoint = (destination[0] + 0.5,
|
||||
destination[1] + 1.0,
|
||||
destination[2] + 0.5)
|
||||
cover = not check(source, endpoint, destination)
|
||||
after_distance = math.hypot(
|
||||
target.x - (destination[0] + 0.5),
|
||||
target.z - (destination[2] + 0.5))
|
||||
progress = toward_length - after_distance
|
||||
if progress <= 0.4:
|
||||
continue
|
||||
routes.append((0 if cover else 1, -progress, len(route),
|
||||
destination, route, cover, progress))
|
||||
maneuvers = (
|
||||
((2.0, 2.0, 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(
|
||||
player.x + toward[0] * forward_step +
|
||||
perpendicular[0] * lateral * side)
|
||||
z = math.floor(
|
||||
player.z + toward[1] * forward_step +
|
||||
perpendicular[1] * lateral * side)
|
||||
destination = self._grounded_approach_block(
|
||||
x, z, start[1])
|
||||
if destination is None or destination == start or destination in seen:
|
||||
continue
|
||||
seen.add(destination)
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
break
|
||||
route = self.g.world.path_to_place(
|
||||
start, destination, timeout=remaining)
|
||||
if not route or len(route) <= 1:
|
||||
continue
|
||||
cover = False
|
||||
check = getattr(
|
||||
self.g.world, '_has_cache_line_of_sight', None)
|
||||
if callable(check):
|
||||
source = (target.x, target.y + 1.5, target.z)
|
||||
endpoint = (destination[0] + 0.5,
|
||||
destination[1] + 1.0,
|
||||
destination[2] + 0.5)
|
||||
cover = not check(source, endpoint, destination)
|
||||
after_distance = math.hypot(
|
||||
target.x - (destination[0] + 0.5),
|
||||
target.z - (destination[2] + 0.5))
|
||||
progress = toward_length - after_distance
|
||||
if advance and progress <= 0.4:
|
||||
continue
|
||||
rank = (0 if cover else 1, -progress if advance else 0,
|
||||
len(route))
|
||||
routes.append((rank, destination, route, cover, progress))
|
||||
|
||||
if not routes:
|
||||
self.next_evasion_at = now + 0.5
|
||||
@@ -379,8 +412,7 @@ class CombatStates:
|
||||
reason, self.EVASION_PATH_TIMEOUT))
|
||||
return False
|
||||
|
||||
(_cover_rank, _progress_rank, _length, destination, route,
|
||||
has_cover, advance) = min(routes)
|
||||
(_rank, destination, route, has_cover, advance) = min(routes)
|
||||
self.g.path = route
|
||||
self.path_target = None
|
||||
self.evading = True
|
||||
@@ -476,7 +508,27 @@ class CombatStates:
|
||||
distance = self._distance(target)
|
||||
now = time.monotonic()
|
||||
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.target_id = None
|
||||
self.g.path = []
|
||||
@@ -484,6 +536,13 @@ class CombatStates:
|
||||
self.state = self.find_target
|
||||
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:
|
||||
self._stop_rush()
|
||||
if (distance > self.ATTACK_REACH and now >= self.next_rush_at):
|
||||
|
||||
Reference in new issue
Block a user