57a5426e65
build / mod (push) Successful in 4m13s
#101: radars locate sites from launches, /airdef sites, /airdef strike; sites launch slower (15/25/50 ticks) so a strike can come in time. #48: M opens the map on the network's picture, with an editor. #47: guard, sector, reserve and salvo per system. #94: vehicles route round slopes and water and say when they cannot get through. #96: reverb, and a menu switch for the acoustics. #92: generated files are validated before the swap, repo map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
188 lines
8.8 KiB
Rust
188 lines
8.8 KiB
Rust
//! Move orders: a unit packs up (its launcher down, radar silent), drives
|
|
//! over the land to the point it was given, and deploys there; it fights
|
|
//! again only once deployed. The reload trucks drive the same way.
|
|
use super::*;
|
|
|
|
/// Blocks a tick a vehicle drives (about 25 km/h).
|
|
const SPEED: f64 = 0.35;
|
|
|
|
impl SamKind {
|
|
/// Ticks to pack up for the march, and to deploy again.
|
|
pub fn march_ticks(self) -> u32 {
|
|
match self.spec().family {
|
|
Family::Vertical => 200,
|
|
Family::Trainable => 120,
|
|
Family::Static => 160,
|
|
Family::Turret | Family::Gun => 40,
|
|
Family::Light => 40,
|
|
Family::Truck => 10,
|
|
}
|
|
}
|
|
/// A depot doesn't drive.
|
|
pub fn mobile(self) -> bool {
|
|
self != SamKind::Depot
|
|
}
|
|
}
|
|
|
|
impl Sam {
|
|
/// A reload truck's job: the system or depot it is going to.
|
|
pub fn job(&self) -> Option<u32> {
|
|
self.job
|
|
}
|
|
/// Packing up, driving or deploying: not fighting.
|
|
pub fn moving(&self) -> bool {
|
|
matches!(self.state, SamState::Packing | SamState::Moving | SamState::Deploying)
|
|
}
|
|
}
|
|
|
|
impl Sim {
|
|
/// Orders unit `i` to drive to `to`. False for one that can't (a depot, a wreck, failed running gear).
|
|
pub fn order_move(&mut self, i: usize, to: DVec3) -> bool {
|
|
self.order_move_near(i, to, 0.0)
|
|
}
|
|
|
|
/// The same, where arriving within `slack` blocks of `to` will do (a truck stops beside its job).
|
|
pub fn order_move_near(&mut self, i: usize, to: DVec3, slack: f64) -> bool {
|
|
let Some(sam) = self.sams.get_mut(i) else { return false };
|
|
if !sam.alive() || !sam.kind.mobile() || sam.drive_speed() <= 0.0 {
|
|
return false;
|
|
}
|
|
sam.dest = Some(to);
|
|
let route = route::Route::new(sam.pos, to, slack, sam.kind.climbs(), self.tick);
|
|
self.routes.map.insert(sam.id, route);
|
|
sam.target = None;
|
|
sam.gun_target = None;
|
|
if sam.state != SamState::Moving {
|
|
sam.state = SamState::Packing;
|
|
sam.march = sam.kind.march_ticks();
|
|
}
|
|
true
|
|
}
|
|
|
|
/// Packing, driving and deploying, once a tick. The way is searched on the ground the game has told of (a
|
|
/// few hundred cells a tick for all the vehicles); a vehicle stands while it waits, and stops with a word
|
|
/// when there is no way.
|
|
pub(super) fn step_moves(&mut self, ground: &mut Ground, out: &mut Out) {
|
|
let now = self.tick;
|
|
let planners = self.routes.map.values().filter(|r| r.searching()).count().max(1);
|
|
let share = route::WORK_PER_TICK / planners;
|
|
let Sim { sams, routes, .. } = self;
|
|
for (n, sam) in sams.iter_mut().enumerate() {
|
|
if !sam.alive() {
|
|
routes.map.remove(&sam.id);
|
|
continue;
|
|
}
|
|
let spec = sam.kind.spec();
|
|
// The march and the packing drive the mount themselves: no speed carries over into or out of them.
|
|
if matches!(sam.state, SamState::Packing | SamState::Moving | SamState::Deploying) {
|
|
sam.halt_mount();
|
|
}
|
|
match sam.state {
|
|
SamState::Packing => {
|
|
// The launcher comes down for the march; the way is searched meanwhile.
|
|
sam.pitch += (0.0 - sam.pitch) * 0.06;
|
|
sam.yaw = slew(sam.yaw, sam.heading, 0.04);
|
|
sam.march = sam.march.saturating_sub(1);
|
|
if sam.march == 0 {
|
|
sam.state = SamState::Moving;
|
|
} else if let Some(r) = routes.map.get_mut(&sam.id)
|
|
&& r.searching()
|
|
{
|
|
let _ = r.drive(ground, now, sam.pos, share);
|
|
}
|
|
}
|
|
SamState::Moving => {
|
|
// The running gear failed on the way: it stops where it is and deploys there.
|
|
if sam.drive_speed() <= 0.0 {
|
|
sam.dest = None;
|
|
}
|
|
let Some(to) = sam.dest else {
|
|
routes.map.remove(&sam.id);
|
|
sam.state = SamState::Deploying;
|
|
sam.march = sam.kind.march_ticks();
|
|
continue;
|
|
};
|
|
let route = routes.map.entry(sam.id).or_insert_with(|| route::Route::new(sam.pos, to, 0.0, sam.kind.climbs(), now));
|
|
let (go, last) = match route.drive(ground, now, sam.pos, share) {
|
|
route::Drive::Wait(why) => {
|
|
if route.waited(now) > 40 && !route.told {
|
|
route.told = true;
|
|
out.messages.push(format!("{} №{}: {}", sam.kind.name(), n + 1, why.text()));
|
|
}
|
|
continue;
|
|
}
|
|
route::Drive::Fail(why) => {
|
|
out.messages.push(format!("{} №{}: {}", sam.kind.name(), n + 1, why.text()));
|
|
// A truck leaves that job alone for a while, and takes another.
|
|
if let Some(job) = sam.job.take() {
|
|
routes.unreachable.insert((sam.id, job), now + route::UNREACHABLE_FOR);
|
|
}
|
|
routes.map.remove(&sam.id);
|
|
sam.dest = None;
|
|
sam.state = SamState::Deploying;
|
|
sam.march = sam.kind.march_ticks();
|
|
continue;
|
|
}
|
|
route::Drive::Go(p, last) => (p, last),
|
|
};
|
|
let flat = horizontal(go - sam.pos);
|
|
if last && flat.length() < SPEED * 2.0 {
|
|
let partial = route.partial;
|
|
sam.pos = DVec3::new(go.x, sam.pos.y, go.z);
|
|
sam.dest = None;
|
|
routes.map.remove(&sam.id);
|
|
if partial {
|
|
out.messages.push(format!("{} №{}: {}", sam.kind.name(), n + 1, route::Why::Partial.text()));
|
|
}
|
|
sam.state = SamState::Deploying;
|
|
sam.march = sam.kind.march_ticks();
|
|
out.sound("minecraft:block.piston.contract", sam.pos, 2.0, 0.6);
|
|
continue;
|
|
}
|
|
// Turn towards it, then drive: slower while turning, over the land's height.
|
|
sam.heading = slew(sam.heading, heading_of(flat), 0.06);
|
|
let ahead = DQuat::from_rotation_y(sam.heading) * DVec3::NEG_Z;
|
|
let turning = angle_diff(sam.heading, heading_of(flat)).abs();
|
|
let step = SPEED * sam.drive_speed() * (1.0 - turning / std::f64::consts::PI).max(0.2);
|
|
let next = sam.pos + ahead * step;
|
|
// The column ahead: a wall or a drop the grid missed turns the vehicle back to the planner.
|
|
let Some(y) = ground.at(next, now) else {
|
|
if route.wait_ahead() {
|
|
out.messages.push(format!("{} №{}: {}", sam.kind.name(), n + 1, route::Why::NoMap.text()));
|
|
routes.map.remove(&sam.id);
|
|
sam.dest = None;
|
|
sam.state = SamState::Deploying;
|
|
sam.march = sam.kind.march_ticks();
|
|
}
|
|
continue;
|
|
};
|
|
route.ahead_ok();
|
|
let rise = y - sam.pos.y;
|
|
if rise > route::STEP_UP || rise < -route::STEP_DOWN {
|
|
route.blocked_ahead(next, sam.pos);
|
|
continue;
|
|
}
|
|
sam.pos = DVec3::new(next.x, sam.pos.y + rise.clamp(-0.5, 0.5), next.z);
|
|
sam.yaw = sam.heading;
|
|
if now % 4 == 0 {
|
|
out.particles("minecraft:campfire_cosy_smoke", sam.pos - ahead * 2.0 * spec.scale, 0.3, DVec3::new(0.0, 0.03, 0.0), 0.01, 1);
|
|
}
|
|
if now % 30 == 0 {
|
|
out.sound("minecraft:entity.minecart.riding", sam.pos, 0.6, 0.5);
|
|
}
|
|
}
|
|
SamState::Deploying => {
|
|
sam.pitch += (spec.rest_pitch - sam.pitch) * 0.05;
|
|
sam.march = sam.march.saturating_sub(1);
|
|
if sam.march == 0 {
|
|
sam.state = SamState::Search;
|
|
sam.pitch = spec.rest_pitch;
|
|
sam.yaw = sam.heading;
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|