Fix raid gameplay, finite ammunition and truck routing

Check launch terrain, reserve ballistic targets for marked objectives, tune in-game reach and fallback fire, and support bounded terrain work on supply routes.

Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
2026-11-25 18:53:17 +03:00
parent 61c4fa5cee
commit aa842d8a1f
18 changed files with 606 additions and 257 deletions
+177 -4
View File
@@ -171,6 +171,10 @@ pub(super) struct Route {
near: ((i32, i32), f64),
expanded: usize,
goal_checked: bool,
truck: bool,
work_approach: bool,
work_wait: u32,
works: u8,
phase: Phase,
pts: Vec<DVec3>,
next: usize,
@@ -206,6 +210,10 @@ impl Route {
near: ((0, 0), f64::MAX),
expanded: 0,
goal_checked: slack > 0.0,
truck: kind == SamKind::Tzm && slack > 0.0,
work_approach: false,
work_wait: 0,
works: 0,
phase: Phase::Search,
pts: Vec::new(),
next: 0,
@@ -231,7 +239,12 @@ impl Route {
horizontal(self.centre(c) - self.dest).length()
}
fn is_goal(&self, c: (i32, i32)) -> bool {
if self.slack > 0.0 { self.to_goal(c) <= self.slack } else { c == self.cell_of(self.dest) }
if self.slack == 0.0 {
return c == self.cell_of(self.dest);
}
let Some(info) = self.cells.get(&c) else { return false };
let p = self.centre(c);
DVec3::new(p.x, f64::from(info.h), p.z).distance(self.dest) <= self.slack
}
/// Searches afresh from `pos`, keeping the cells found bad.
@@ -252,6 +265,8 @@ impl Route {
self.expanded = 0;
self.near = (self.start, f64::MAX);
self.partial = false;
self.work_approach = false;
self.work_wait = 0;
self.cells.insert(self.start, Info { h: pos.y as f32, ok: true });
self.g.insert(self.start, 0.0);
let h = self.heur(self.start);
@@ -311,10 +326,18 @@ impl Route {
let Some(column) = ground.column(x, z, 1024) else { pending = true; continue };
let Some(h) = column else { return Clearance::Blocked };
let dh = h - base;
if dh > STEP_UP || dh < -STEP_DOWN {
let up = if self.truck { 2.0 } else { STEP_UP };
if dh > up || dh < -STEP_DOWN {
return Clearance::Blocked;
}
let top = DVec3::new(f64::from(x) + 0.5, h + 0.05, f64::from(z) + 0.5);
if self.truck && dx == 0 && dz == 0 {
match ground.world.sweep(top, top - DVec3::Y * 0.55, false, now) {
world::Sweep::Hit { .. } => {}
world::Sweep::Clear => return Clearance::Blocked,
_ => { pending = true; continue; }
}
}
match ground.world.sweep(top, top + DVec3::Y * self.chassis.height, false, now) {
world::Sweep::Clear => {}
world::Sweep::Unknown { pending: true, .. } | world::Sweep::Wait { .. } => pending = true,
@@ -542,13 +565,117 @@ impl Route {
if self.phase == Phase::Search {
return Drive::Wait(Why::Map);
}
let reach = f64::from(self.cell) * 0.7;
let reach = if self.truck { 0.65 } else { f64::from(self.cell) * 0.7 };
while self.next + 1 < self.pts.len() && horizontal(self.pts[self.next] - pos).length() < reach {
self.next += 1;
}
Drive::Go(self.pts[self.next], self.next + 1 >= self.pts.len())
}
/// Let an automatic delivery approach the nearest reachable spot before trying local roadwork.
pub(super) fn approach_works(&mut self) -> bool {
if !self.truck || self.works >= 2 || self.near.0 == self.start
|| self.to_goal(self.start) - self.near.1 < f64::from(self.cell)
{
return false;
}
self.failed = None;
self.finish(self.near.0, true);
self.work_approach = true;
true
}
pub(super) fn work_approach(&self) -> bool {
self.work_approach
}
/// Make a short local crossing only after route search or motion finds a blockage.
/// Returns true while waiting for terrain data or after requesting the edits.
pub(super) fn try_works(&mut self, ground: &mut Ground, pos: DVec3, now: u64, out: &mut Out) -> bool {
if !self.truck || self.works >= 2 || self.work_wait >= 60 {
return false;
}
self.work_wait += 1;
let direction = horizontal(self.dest - pos);
let (dx, dz) = if direction.x.abs() >= direction.z.abs() {
(if direction.x >= 0.0 { 1 } else { -1 }, 0)
} else {
(0, if direction.z >= 0.0 { 1 } else { -1 })
};
let coord = |row: i32, side: i32| (
pos.x.floor() as i32 + dx * row + dz * side,
pos.z.floor() as i32 + dz * row + dx * side,
);
let width = self.chassis.radius.ceil() as i32;
let start = pos.y.round() as i32;
let (ex, ez) = coord(10, 0);
let Some(Some(end)) = ground.column(ex, ez, 1024) else {
ground.want_chunk((ex >> 4, ez >> 4));
return true;
};
let end = end.round() as i32;
if (end - start).abs() > 2 || !(-63..=320).contains(&start) || !(-63..=320).contains(&end) {
return false;
}
let mut commands = Vec::new();
for row in 1..=10 {
let level = start + ((end - start) as f64 * f64::from(row) / 10.0).round() as i32;
let mut deck = false;
for side in -width..=width {
let (x, z) = coord(row, side);
let Some(Some(top)) = ground.column(x, z, 1024) else {
ground.want_chunk((x >> 4, z >> 4));
return true;
};
let top = top.round() as i32;
if top > level + 2 {
return false;
}
let p = DVec3::new(f64::from(x) + 0.5, f64::from(level), f64::from(z) + 0.5);
match ground.world.sweep(p + DVec3::Y * 0.25, p - DVec3::Y * 0.25, false, now) {
world::Sweep::Hit { .. } if top >= level => {}
world::Sweep::Hit { .. } | world::Sweep::Clear => deck = true,
_ => return true,
}
if top > level {
// Only shallow natural cover is cleared. Vanilla fills preserve other materials.
for block in ["short_grass", "fern", "snow", "dirt", "grass_block", "sand", "gravel"] {
commands.push(format!("fill {x} {level} {z} {x} {} {z} minecraft:air replace minecraft:{block}", top - 1));
}
}
}
if deck {
let (ax, az) = coord(row, -width);
let (bx, bz) = coord(row, width);
let y = level - 1;
for block in ["air", "water"] {
commands.push(format!("fill {ax} {y} {az} {bx} {y} {bz} minecraft:cobblestone replace minecraft:{block}"));
}
}
}
if commands.is_empty() || out.commands.len() + commands.len() > 192 {
return false;
}
out.commands.extend(commands);
let centre = pos + DVec3::new(f64::from(dx * 5), 0.0, f64::from(dz * 5));
ground.forget(centre, 9);
let (cx, cz) = (centre.x.floor() as i32, centre.z.floor() as i32);
let mut chunks = Vec::new();
for x in (cx - 9).div_euclid(16)..=(cx + 9).div_euclid(16) {
for z in (cz - 9).div_euclid(16)..=(cz + 9).div_euclid(16) {
chunks.push((x, z));
}
}
ground.world.columns_changed(&chunks);
self.works += 1;
self.replans = 0;
self.failed = None;
self.bad.clear();
self.cells.clear();
self.restart(pos);
true
}
pub(super) fn searching(&self) -> bool {
self.phase == Phase::Search && self.failed.is_none()
}
@@ -634,7 +761,53 @@ mod tests {
ground.world.serve(src, sections, now);
}
/// Drives a Patriot from the origin to `to` over land of the given heights (water: a height over a lower floor).
#[test]
fn a_supply_truck_builds_a_bridge_then_crosses_it() {
struct Creek(HashSet<(i32, i32)>);
impl world::WorldSource for Creek {
fn solid(&self, x: i32, y: i32, z: i32) -> Option<bool> {
let floor = if (16..20).contains(&x) && !self.0.contains(&(x, z)) { 60 } else { 64 };
Some(y < floor)
}
}
let mut creek = Creek(HashSet::new());
let mut sim = Sim::new(5);
let from = DVec3::new(0.5, 64.0, 0.5);
let to = DVec3::new(80.5, 64.0, 0.5);
let id = sim.id();
sim.sams.push(Sam::new(id, SamKind::Tzm, from, 0.0));
sim.sams[0].job = Some(999);
assert!(sim.order_move_near(0, to, HANDOVER - 0.75));
let mut ground = Ground::default();
let mut out = Out::default();
for _ in 0..2000 {
sim.tick += 1;
let columns = ground.take_wanted().into_iter().map(|(x, z)| (x, z, 64)).collect();
ground.answer(columns, sim.tick);
let sections = ground.world.take_wanted(sim.tick);
ground.world.serve(&creek, sections, sim.tick);
sim.step_moves(&mut ground, &mut out);
for command in out.commands.drain(..) {
let words: Vec<&str> = command.split_whitespace().collect();
if words[7] == "minecraft:cobblestone" && words[9] == "minecraft:water" {
let [ax, ay, az, bx, by, bz] = std::array::from_fn(|i| words[i + 1].parse::<i32>().unwrap());
assert_eq!((ay, by), (63, 63));
for x in ax.min(bx)..=ax.max(bx) {
for z in az.min(bz)..=az.max(bz) {
if (16..20).contains(&x) { creek.0.insert((x, z)); }
}
}
}
}
if !sim.sams[0].moving() { break; }
}
assert!(!creek.0.is_empty(), "the truck built no bridge");
assert!(sim.sams[0].pos.distance(to) <= HANDOVER, "{:?} {:?}", sim.sams[0].pos, out.messages);
assert!(out.messages.is_empty(), "{:?}", out.messages);
}
/// Drives a Patriot from the origin to `to` over land of the given heights.
fn march_kind<H: Fn(i32, i32) -> i32, B: Fn(i32, i32) -> i32>(kind: SamKind, height: H, floor: B, to: DVec3, ticks: u32) -> (Sim, Out, u32) {
let src = Src { height, floor };
let mut sim = Sim::new(3);