Raiders skip ominous banners they cannot reach

A captainless raider walked towards the nearest fallen ominous banner
even when no path led to it. Check that a path reaches the banner before
going for it, and skip a banner without one for 600 ticks, like vanilla.
This commit is contained in:
Kacpherek
2026-10-26 19:32:41 +01:00
parent 68db8847e7
commit 97f8bca9da
2 changed files with 39 additions and 15 deletions
+11 -6
View File
@@ -762,7 +762,7 @@ impl Game<'_> {
}
positions.push((m.id, m.position, s.active_raid));
}
let banners: Vec<(usize, Vec3)> = self
let banners: Vec<(usize, u32, Vec3)> = self
.dropped
.iter()
.enumerate()
@@ -771,7 +771,7 @@ impl Game<'_> {
&& e.stack.data().is_some_and(|d| d.ominous_banner)
&& e.pickup_delay == 0
})
.map(|(i, e)| (i, e.position))
.map(|(i, e)| (i, e.id, e.position))
.collect();
let mut taken: Vec<usize> = Vec::new();
let mut collectors: Vec<(usize, u32)> = Vec::new();
@@ -858,18 +858,23 @@ impl Game<'_> {
{
let near = banners
.iter()
.filter(|(i, p)| {
.filter(|(i, item, p)| {
!taken.contains(i)
&& !s
.unreachable_banners
.iter()
.any(|&(b, until)| b == *item && now < until)
&& (p.x - position.x).abs() <= range
&& (p.z - position.z).abs() <= range
&& (p.y - position.y).abs() <= 8.0
})
.min_by(|a, b| {
a.1.distance_squared(position)
.total_cmp(&b.1.distance_squared(position))
a.2.distance_squared(position)
.total_cmp(&b.2.distance_squared(position))
});
if let Some((i, p)) = near {
if let Some((i, item, p)) = near {
s.banner_at = Some([p.x, p.y, p.z]);
s.banner_item = *item;
if p.distance_squared(position) < 1.414 * 1.414 {
// pickUpItem: the banner becomes the wave's leader.
taken.push(*i);
+28 -9
View File
@@ -38,6 +38,11 @@ pub struct State {
pub in_village: bool,
/// A dropped ominous banner this raider may fetch.
pub banner_at: Option<[f64; 3]>,
/// The item entity of `banner_at`.
pub banner_item: u32,
/// Banners with no path to them, and until when (game time):
/// `unreachableBannerCache`.
pub unreachable_banners: Vec<(u32, i64)>,
/// Beds around the raid's centre.
pub homes: Option<std::sync::Arc<Vec<[i32; 3]>>>,
/// A ravager carrying a raider, set every tick from `Extra::rider`.
@@ -113,7 +118,7 @@ pub fn register(m: &mut Mob) {
}
m.extra.raider = Some(Box::default());
m.ai.goals.add(4, LongDistancePatrol::new(0.7, 0.595));
m.ai.goals.add(1, ObtainBanner);
m.ai.goals.add(1, ObtainBanner::default());
m.ai.goals.add(3, PathfindToRaid);
m.ai.goals.add(4, MoveThroughVillage::new(1.05, 1.0));
m.ai.goals.add(5, Celebration);
@@ -336,23 +341,37 @@ fn close_to_center(p: [i32; 3], at: Vec3, distance: f64) -> bool {
/// `Raider.ObtainRaidLeaderBannerGoal`: walk to a fallen ominous banner when
/// the wave has no living leader (the game finds the banner and picks it up).
#[derive(Clone, Debug)]
pub struct ObtainBanner;
#[derive(Clone, Debug, Default)]
pub struct ObtainBanner {
path_to_banner: Option<super::path::Path>,
}
impl Goal for ObtainBanner {
goal_clone!();
fn flags(&self) -> u8 {
MOVE
}
fn can_use(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
state(m).is_some_and(|s| s.banner_at.is_some())
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
let Some((at, item)) = state(m).and_then(|s| s.banner_at.map(|at| (at, s.banner_item)))
else {
return false;
};
// A banner with no path to it is skipped for 600 ticks.
let path = m.create_path_to_entity(cx.w, at, 1);
if path.as_ref().is_some_and(|p| p.reached) {
self.path_to_banner = path;
return true;
}
let now = cx.w.game_time;
let s = state_mut(m);
s.unreachable_banners.retain(|&(_, until)| now < until);
s.unreachable_banners.push((item, now + 600));
false
}
fn can_continue(&mut self, m: &mut Mob, _cx: &mut Cx) -> bool {
!m.ai.nav.is_done() && state(m).is_some_and(|s| s.banner_at.is_some())
}
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
if let Some(at) = state(m).and_then(|s| s.banner_at) {
m.nav_move_to(cx.w, at, 1.15);
}
fn start(&mut self, m: &mut Mob, _cx: &mut Cx) {
m.nav_move_to_path(self.path_to_banner.take(), 1.15);
}
}