Merge pull request #2377 from kacpherek/fix/raider-banner-reachable

🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed at this head; a re-review was asked hours ago, with no new objection since. Proven in cg19 union R1 bb29ce68e (union/cg19-r1: main c6c2ec5f9 + 83 PRs) Mac mini: 98 se.
This commit is contained in:
Ludovic Marie
2026-10-27 13:33:12 +01:00
committed by GitHub
2 changed files with 53 additions and 21 deletions
+25 -12
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,20 +858,33 @@ impl Game<'_> {
&& raids.get(id).is_some_and(|r| r.leader(s.wave).is_none())
&& !leaders.contains(&(id, s.wave))
{
let in_reach = |(i, _, p): &&(usize, u32, Vec3)| {
!taken.contains(i)
&& (p.x - position.x).abs() <= range
&& (p.z - position.z).abs() <= range
&& (p.y - position.y).abs() <= 8.0
};
let by_distance = |a: &&(usize, u32, Vec3), b: &&(usize, u32, Vec3)| {
a.2.distance_squared(position)
.total_cmp(&b.2.distance_squared(position))
};
// The unreachable cache only picks the navigation target;
// a banner next to the raider is still picked up.
let near = banners
.iter()
.filter(|(i, p)| {
!taken.contains(i)
&& (p.x - position.x).abs() <= range
&& (p.z - position.z).abs() <= range
&& (p.y - position.y).abs() <= 8.0
.filter(in_reach)
.filter(|(_, item, _)| {
!s.unreachable_banners
.iter()
.any(|&(b, until)| b == *item && now < until)
})
.min_by(|a, b| {
a.1.distance_squared(position)
.total_cmp(&b.1.distance_squared(position))
});
if let Some((i, p)) = near {
.min_by(by_distance);
let nearest = banners.iter().filter(in_reach).min_by(by_distance);
let adjacent =
nearest.filter(|(_, _, p)| p.distance_squared(position) < 1.414 * 1.414);
if let Some((i, item, p)) = adjacent.or(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
@@ -41,6 +41,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`.
@@ -116,7 +121,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);
@@ -339,23 +344,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);
}
}