Refs #91, #114, #118, #135. Show the latest recorded unit decision in the native card (#153).
This commit is contained in:
+192
-80
@@ -66,7 +66,7 @@ pub(super) enum Drive {
|
||||
|
||||
/// Result of checking the vehicle's clearance at its next position.
|
||||
pub(super) enum Clearance {
|
||||
Clear,
|
||||
Clear(f64),
|
||||
Blocked,
|
||||
Pending,
|
||||
}
|
||||
@@ -105,20 +105,22 @@ impl Chassis {
|
||||
}
|
||||
|
||||
/// Every voxel whose square intersects the conservative footprint, including grazing corners.
|
||||
fn samples(self) -> Vec<(i32, i32)> {
|
||||
let r = self.radius.ceil() as i32;
|
||||
(-r..=r).flat_map(|x| (-r..=r).filter_map(move |z| {
|
||||
let dx = (f64::from(x.abs()) - 0.5).max(0.0);
|
||||
let dz = (f64::from(z.abs()) - 0.5).max(0.0);
|
||||
fn samples(self, pos: DVec3) -> impl Iterator<Item = (i32, i32)> {
|
||||
let min_x = (pos.x - self.radius).floor() as i32;
|
||||
let max_x = (pos.x + self.radius).floor() as i32;
|
||||
let min_z = (pos.z - self.radius).floor() as i32;
|
||||
let max_z = (pos.z + self.radius).floor() as i32;
|
||||
(min_x..=max_x).flat_map(move |x| (min_z..=max_z).filter_map(move |z| {
|
||||
let dx = (f64::from(x) - pos.x).max(pos.x - (f64::from(x) + 1.0)).max(0.0);
|
||||
let dz = (f64::from(z) - pos.z).max(pos.z - (f64::from(z) + 1.0)).max(0.0);
|
||||
(dx * dx + dz * dz <= self.radius * self.radius).then_some((x, z))
|
||||
})).collect()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Info {
|
||||
h: f32,
|
||||
ok: bool,
|
||||
}
|
||||
|
||||
enum Eval {
|
||||
@@ -127,6 +129,45 @@ enum Eval {
|
||||
Pending,
|
||||
}
|
||||
|
||||
/// Look below the reachable ceiling rather than lifting a vehicle onto an overhang.
|
||||
/// The heightmap still distinguishes exposed water from dry solid surfaces.
|
||||
fn support(ground: &mut Ground, x: i32, z: i32, near: f64, up: f64, down: f64, now: u64) -> Eval {
|
||||
let column = ground.column(x, z, 1024);
|
||||
if ground.known.get(&(x, z)).is_some_and(|&(_, tick)| tick == STALE) {
|
||||
return Eval::Pending;
|
||||
}
|
||||
let top = match column {
|
||||
None => return Eval::Pending,
|
||||
Some(None) => return Eval::Bad,
|
||||
Some(Some(top)) => top,
|
||||
};
|
||||
let low = (near - down).ceil() as i32 - 1;
|
||||
let high = (near + up).floor() as i32 - 1;
|
||||
let top = top as i32 - 1;
|
||||
if top < low {
|
||||
return Eval::Bad;
|
||||
}
|
||||
let mut section = None;
|
||||
match ground.world.road_block(&mut section, x, top, z, now) {
|
||||
None => return Eval::Pending,
|
||||
// The motion-blocking map includes the surface of an open body of water.
|
||||
Some(Some(true)) => {}
|
||||
Some(_) => return Eval::Bad,
|
||||
}
|
||||
if top <= high {
|
||||
return Eval::Ok((top + 1) as f32);
|
||||
}
|
||||
for y in (low..=high).rev() {
|
||||
match ground.world.road_block(&mut section, x, y, z, now) {
|
||||
Some(Some(true)) => return Eval::Ok((y + 1) as f32),
|
||||
Some(Some(false)) => {}
|
||||
None => return Eval::Pending,
|
||||
Some(None) => return Eval::Bad,
|
||||
}
|
||||
}
|
||||
Eval::Bad
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct Open(f32, (i32, i32));
|
||||
impl Eq for Open {}
|
||||
@@ -255,6 +296,7 @@ impl Route {
|
||||
self.lo = (self.start.0.min(goal.0) - margin, self.start.1.min(goal.1) - margin);
|
||||
self.hi = (self.start.0.max(goal.0) + margin, self.start.1.max(goal.1) + margin);
|
||||
self.g.clear();
|
||||
self.cells.clear();
|
||||
self.came.clear();
|
||||
self.open.clear();
|
||||
self.parked.clear();
|
||||
@@ -267,7 +309,7 @@ impl Route {
|
||||
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.cells.insert(self.start, Info { h: pos.y as f32 });
|
||||
self.g.insert(self.start, 0.0);
|
||||
let h = self.heur(self.start);
|
||||
self.open.push(Open(h, self.start));
|
||||
@@ -277,79 +319,60 @@ impl Route {
|
||||
(self.to_goal(c) * 1.1) as f32
|
||||
}
|
||||
|
||||
fn eval(&mut self, ground: &mut Ground, now: u64, c: (i32, i32)) -> Eval {
|
||||
fn eval(&mut self, ground: &mut Ground, now: u64, c: (i32, i32), near: f64, climb: f64) -> Eval {
|
||||
if let Some(i) = self.cells.get(&c) {
|
||||
return if i.ok { Eval::Ok(i.h) } else { Eval::Bad };
|
||||
return Eval::Ok(i.h);
|
||||
}
|
||||
if self.bad.contains(&c) {
|
||||
return Eval::Bad;
|
||||
}
|
||||
let p = self.centre(c);
|
||||
let (x, z) = (p.x.floor() as i32, p.z.floor() as i32);
|
||||
let verdict = match ground.column(x, z, 1024) {
|
||||
None => return Eval::Pending,
|
||||
Some(None) => Info { h: 0.0, ok: false },
|
||||
Some(Some(h)) => {
|
||||
// The height is the top of what blocks motion or holds a fluid: water has nothing solid under it.
|
||||
let top = DVec3::new(f64::from(x) + 0.5, h, f64::from(z) + 0.5);
|
||||
match ground.world.sweep(top + DVec3::Y * 0.5, top - DVec3::Y * 0.5, false, now) {
|
||||
world::Sweep::Hit { .. } => Info { h: h as f32, ok: true },
|
||||
world::Sweep::Clear => Info { h: h as f32, ok: false },
|
||||
world::Sweep::Unknown { pending: true, .. } | world::Sweep::Wait { .. } => return Eval::Pending,
|
||||
world::Sweep::Unknown { .. } => Info { h: h as f32, ok: false },
|
||||
}
|
||||
}
|
||||
};
|
||||
if verdict.ok {
|
||||
match self.clearance_at(ground, DVec3::new(f64::from(x) + 0.5, f64::from(verdict.h), f64::from(z) + 0.5), now) {
|
||||
Clearance::Clear => {}
|
||||
Clearance::Blocked => {
|
||||
let blocked = Info { h: verdict.h, ok: false };
|
||||
self.cells.insert(c, blocked);
|
||||
return Eval::Bad;
|
||||
}
|
||||
Clearance::Pending => return Eval::Pending,
|
||||
}
|
||||
let verdict = self.road_at(ground, self.centre(c).with_y(near), climb, climb.max(STEP_DOWN), now);
|
||||
if let Eval::Ok(h) = verdict {
|
||||
self.cells.insert(c, Info { h });
|
||||
}
|
||||
self.cells.insert(c, verdict);
|
||||
if verdict.ok { Eval::Ok(verdict.h) } else { Eval::Bad }
|
||||
verdict
|
||||
}
|
||||
|
||||
/// Check a chassis-sized footprint against sampled support columns and voxel headroom.
|
||||
/// Unknown columns or sections are pending so neither search nor motion assumes they are open.
|
||||
fn clearance_at(&self, ground: &mut Ground, pos: DVec3, now: u64) -> Clearance {
|
||||
let base = pos.y;
|
||||
/// Check support and headroom for the complete folded chassis. Unknown or just changed voxels
|
||||
/// remain pending so neither search nor motion assumes they are open.
|
||||
fn road_at(&self, ground: &mut Ground, pos: DVec3, climb: f64, drop: f64, now: u64) -> Eval {
|
||||
let (cx, cz) = (pos.x.floor() as i32, pos.z.floor() as i32);
|
||||
let base = match support(ground, cx, cz, pos.y, climb, drop, now) {
|
||||
Eval::Ok(h) => f64::from(h),
|
||||
other => return other,
|
||||
};
|
||||
let up = if self.truck { 2.0 } else { STEP_UP };
|
||||
let mut pending = false;
|
||||
for (dx, dz) in self.chassis.samples() {
|
||||
let x = pos.x.floor() as i32 + dx;
|
||||
let z = pos.z.floor() as i32 + dz;
|
||||
let Some(column) = ground.column(x, z, 1024) else { pending = true; continue };
|
||||
let Some(h) = column else { return Clearance::Blocked };
|
||||
let dh = h - base;
|
||||
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; }
|
||||
for (x, z) in self.chassis.samples(pos) {
|
||||
let h = if x == cx && z == cz {
|
||||
base
|
||||
} else {
|
||||
match support(ground, x, z, base, up, STEP_DOWN, now) {
|
||||
Eval::Ok(h) => f64::from(h),
|
||||
Eval::Pending => { pending = true; continue; }
|
||||
Eval::Bad => return Eval::Bad,
|
||||
}
|
||||
};
|
||||
let mut section = None;
|
||||
let top = h.max(base);
|
||||
for y in top as i32..(top + self.chassis.height).ceil() as i32 {
|
||||
match ground.world.road_block(&mut section, x, y, z, now) {
|
||||
Some(Some(false)) => {}
|
||||
None => { pending = true; break; }
|
||||
Some(_) => return Eval::Bad,
|
||||
}
|
||||
}
|
||||
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,
|
||||
world::Sweep::Unknown { .. } | world::Sweep::Hit { .. } => return Clearance::Blocked,
|
||||
}
|
||||
}
|
||||
if pending { Clearance::Pending } else { Clearance::Clear }
|
||||
if pending { Eval::Pending } else { Eval::Ok(base as f32) }
|
||||
}
|
||||
|
||||
/// Recheck the envelope at the live position, since obstacles may change after planning.
|
||||
pub(super) fn clearance(&self, ground: &mut Ground, pos: DVec3, now: u64) -> Clearance {
|
||||
self.clearance_at(ground, pos, now)
|
||||
match self.road_at(ground, pos, STEP_UP, STEP_DOWN, now) {
|
||||
Eval::Ok(h) => Clearance::Clear(f64::from(h)),
|
||||
Eval::Bad => Clearance::Blocked,
|
||||
Eval::Pending => Clearance::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
/// Relaxes the edge from the opened cell `c` to `nb`; parks it when the land between isn't known yet.
|
||||
@@ -360,8 +383,10 @@ impl Route {
|
||||
let (di, dj) = (nb.0 - c.0, nb.1 - c.1);
|
||||
let len = if di != 0 && dj != 0 { std::f64::consts::SQRT_2 } else { 1.0 };
|
||||
let dist = len * f64::from(self.cell);
|
||||
let slope = self.slope;
|
||||
let allow = |l: f64| (slope * l).max(STEP_UP);
|
||||
let Some(h0) = self.cells.get(&c).map(|i| f64::from(i.h)) else { return };
|
||||
let h1 = match self.eval(ground, now, nb) {
|
||||
let h1 = match self.eval(ground, now, nb, h0, allow(dist)) {
|
||||
Eval::Ok(h) => f64::from(h),
|
||||
Eval::Bad => return,
|
||||
Eval::Pending => return self.park(c, nb),
|
||||
@@ -369,7 +394,7 @@ impl Route {
|
||||
// A diagonal doesn't cut a corner of something in the way.
|
||||
if di != 0 && dj != 0 {
|
||||
for side in [(c.0 + di, c.1), (c.0, c.1 + dj)] {
|
||||
match self.eval(ground, now, side) {
|
||||
match self.eval(ground, now, side, h0, allow(f64::from(self.cell))) {
|
||||
Eval::Ok(h) if (f64::from(h) - h0).abs() <= (self.slope * f64::from(self.cell)).max(STEP_UP) => {}
|
||||
Eval::Pending => return self.park(c, nb),
|
||||
_ => return,
|
||||
@@ -379,13 +404,12 @@ impl Route {
|
||||
// Between the centres: a wall one block thick or a ditch there is what the centres don't show.
|
||||
let (a, b) = (self.centre(c), self.centre(nb));
|
||||
let mid = (a + b) * 0.5;
|
||||
let hm = match ground.column(mid.x.floor() as i32, mid.z.floor() as i32, 1024) {
|
||||
None => return self.park(c, nb),
|
||||
Some(None) => return,
|
||||
Some(Some(h)) => h,
|
||||
};
|
||||
let half = dist * 0.5;
|
||||
let allow = |l: f64| (self.slope * l).max(STEP_UP);
|
||||
let hm = match self.road_at(ground, mid.with_y(h0), allow(half), STEP_DOWN, now) {
|
||||
Eval::Pending => return self.park(c, nb),
|
||||
Eval::Bad => return,
|
||||
Eval::Ok(h) => f64::from(h),
|
||||
};
|
||||
if (hm - h0).abs() > allow(half) || (h1 - hm).abs() > allow(half) || hm - h0 < -STEP_DOWN * (half / 2.0).max(1.0) {
|
||||
return;
|
||||
}
|
||||
@@ -411,7 +435,15 @@ impl Route {
|
||||
/// Works on the search: at most `budget` cells.
|
||||
fn search(&mut self, ground: &mut Ground, now: u64, mut budget: usize) {
|
||||
if !self.goal_checked {
|
||||
match self.eval(ground, now, self.cell_of(self.dest)) {
|
||||
let x = self.dest.x.floor() as i32;
|
||||
let z = self.dest.z.floor() as i32;
|
||||
let goal = match ground.column(x, z, 1024) {
|
||||
// Orders made while flying above an exposed road still mean the ground below.
|
||||
Some(Some(top)) => self.road_at(ground, self.dest.with_y(self.dest.y.min(top)), STEP_UP, STEP_DOWN, now),
|
||||
Some(None) => Eval::Bad,
|
||||
None => Eval::Pending,
|
||||
};
|
||||
match goal {
|
||||
Eval::Bad => {
|
||||
self.failed = Some(Why::Goal);
|
||||
return;
|
||||
@@ -465,8 +497,7 @@ impl Route {
|
||||
}
|
||||
if self.open.is_empty() {
|
||||
if self.parked.is_empty() {
|
||||
let goal = self.cell_of(self.dest);
|
||||
self.failed = Some(if self.slack == 0.0 && !self.cells.get(&goal).is_none_or(|i| i.ok) { Why::Goal } else { Why::NoWay });
|
||||
self.failed = Some(Why::NoWay);
|
||||
} else {
|
||||
let since = *self.stuck_since.get_or_insert(now);
|
||||
if now.saturating_sub(since) > WAIT_MAP {
|
||||
@@ -526,13 +557,11 @@ impl Route {
|
||||
let c = self.cell_of(at);
|
||||
if c != self.cell_of(pos) {
|
||||
self.bad.insert(c);
|
||||
self.cells.insert(c, Info { h: 0.0, ok: false });
|
||||
} else {
|
||||
// In the cell itself: the stretch ahead, one cell on.
|
||||
let d = horizontal(at - pos).normalize_or(DVec3::X) * f64::from(self.cell);
|
||||
let c = self.cell_of(pos + d);
|
||||
self.bad.insert(c);
|
||||
self.cells.insert(c, Info { h: 0.0, ok: false });
|
||||
}
|
||||
self.replans += 1;
|
||||
if self.replans > REPLANS {
|
||||
@@ -737,7 +766,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn footprint_checks_voxels_grazed_between_their_centres() {
|
||||
let cells = Chassis { radius: 2.75, height: 3.0 }.samples();
|
||||
let cells: Vec<_> = Chassis { radius: 2.75, height: 3.0 }.samples(DVec3::new(0.5, 64.0, 0.5)).collect();
|
||||
assert!(cells.contains(&(3, 1)), "the body reaches the near corner of this voxel");
|
||||
assert!(cells.contains(&(-3, -1)));
|
||||
assert!(!cells.contains(&(3, 3)));
|
||||
@@ -761,6 +790,82 @@ mod tests {
|
||||
ground.world.serve(src, sections, now);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Overhang {
|
||||
lowered: bool,
|
||||
}
|
||||
|
||||
impl Overhang {
|
||||
fn spans(x: i32, z: i32) -> bool {
|
||||
(16..32).contains(&x) && (-120..120).contains(&z)
|
||||
}
|
||||
|
||||
fn answer(&self, ground: &mut Ground, tick: u64) {
|
||||
let columns = ground.take_wanted().into_iter().map(|(x, z)| {
|
||||
(x, z, if Self::spans(x, z) { 73 } else { 64 })
|
||||
}).collect();
|
||||
ground.answer(columns, tick);
|
||||
let sections = ground.world.take_wanted(tick);
|
||||
ground.world.serve(self, sections, tick);
|
||||
}
|
||||
}
|
||||
|
||||
impl world::WorldSource for Overhang {
|
||||
fn solid(&self, x: i32, y: i32, z: i32) -> Option<bool> {
|
||||
Some(y < 64 || Self::spans(x, z) && (y == 72 || self.lowered && y == 65))
|
||||
}
|
||||
}
|
||||
|
||||
fn march_under_overhang(lower_after_planning: bool) -> (Sim, bool, bool) {
|
||||
let mut src = Overhang::default();
|
||||
let mut sim = Sim::new(3);
|
||||
let mut ground = Ground::default();
|
||||
let mut out = Out::default();
|
||||
let id = sim.id();
|
||||
sim.sams.push(Sam::new(id, SamKind::S400, DVec3::new(0.5, 64.0, 0.5), 0.0));
|
||||
assert!(sim.order_move(0, DVec3::new(60.5, 64.0, 0.5)));
|
||||
let mut crossed = false;
|
||||
let mut replanned = false;
|
||||
for _ in 0..4000 {
|
||||
sim.tick += 1;
|
||||
src.answer(&mut ground, sim.tick);
|
||||
sim.step_moves(&mut ground, &mut out);
|
||||
let unit = &sim.sams[0];
|
||||
if lower_after_planning && !src.lowered && unit.pos.x > 5.0 {
|
||||
assert!(sim.routes.map.get(&id).is_some_and(|route| route.phase == Phase::Follow));
|
||||
src.lowered = true;
|
||||
// A new block under the deck changes the navigable road without changing the heightmap.
|
||||
let changed: Vec<_> = (-8..8).map(|z| (1, 4, z)).collect();
|
||||
ground.world.changed(1, false, &changed);
|
||||
}
|
||||
if src.lowered {
|
||||
replanned |= sim.routes.map.get(&id).is_some_and(|route| route.replans > 0);
|
||||
assert!(unit.pos.x + Chassis::for_kind(unit.kind).radius <= 16.0, "entered the changed ceiling at {:?}", unit.pos);
|
||||
}
|
||||
if Overhang::spans(unit.pos.x.floor() as i32, unit.pos.z.floor() as i32) {
|
||||
assert_eq!(unit.pos.y, 64.0);
|
||||
crossed = true;
|
||||
}
|
||||
if !unit.moving() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(sim, crossed, replanned)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vehicle_can_follow_the_lower_road_under_a_bridge_deck() {
|
||||
let (sim, crossed, _) = march_under_overhang(false);
|
||||
let unit = &sim.sams[0];
|
||||
assert!(crossed && !unit.moving() && unit.pos.x > 59.0, "ended at {:?}", unit.pos);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_low_ceiling_on_a_planned_road_stops_the_vehicle_and_replans() {
|
||||
let (sim, crossed, replanned) = march_under_overhang(true);
|
||||
assert!(replanned && !crossed && !sim.sams[0].moving(), "ended at {:?}", sim.sams[0].pos);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_supply_truck_builds_a_bridge_then_crosses_it() {
|
||||
struct Creek(HashSet<(i32, i32)>);
|
||||
@@ -885,6 +990,13 @@ mod tests {
|
||||
assert!(!s.moving() && s.pos.distance(DVec3::new(100.0, 64.0, 30.5)) < 3.0, "pos={:?} state={:?} messages={:?} route={route:?}", s.pos, s.state, out.messages);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_order_from_above_an_open_road_still_reaches_the_ground() {
|
||||
let (sim, _, _) = march(|_, _| 64, |_, _| 64, DVec3::new(30.5, 110.0, 0.5), 1500);
|
||||
let unit = &sim.sams[0];
|
||||
assert!(!unit.moving() && unit.pos.distance(DVec3::new(30.5, 64.0, 0.5)) < 1.0, "ended at {:?}", unit.pos);
|
||||
}
|
||||
|
||||
// A point-column route fits through this one-block slit, but the S-400 chassis does not.
|
||||
#[test]
|
||||
fn s400_rejects_a_one_block_corridor() {
|
||||
|
||||
Reference in New Issue
Block a user