Files
airdef/airdef-mod/src/sim/orders.rs
T

114 lines
4.5 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 {
/// 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).
pub fn order_move(&mut self, i: usize, to: DVec3) -> bool {
let Some(sam) = self.sams.get_mut(i) else { return false };
if !sam.alive() || !sam.kind.mobile() {
return false;
}
sam.dest = Some(to);
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.
pub(super) fn step_moves(&mut self, ground: &mut Ground, out: &mut Out) {
let now = self.tick;
for sam in &mut self.sams {
if !sam.alive() {
continue;
}
let spec = sam.kind.spec();
match sam.state {
SamState::Packing => {
// The launcher comes down for the march.
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;
}
}
SamState::Moving => {
let Some(to) = sam.dest else {
sam.state = SamState::Deploying;
sam.march = sam.kind.march_ticks();
continue;
};
let flat = horizontal(to - sam.pos);
if flat.length() < SPEED * 2.0 {
sam.pos = DVec3::new(to.x, sam.pos.y, to.z);
sam.dest = None;
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 * (1.0 - turning / std::f64::consts::PI).max(0.2);
let next = sam.pos + ahead * step;
let y = ground.at(next, now).unwrap_or(sam.pos.y);
sam.pos = DVec3::new(next.x, sam.pos.y + (y - sam.pos.y).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;
}
}
_ => {}
}
}
}
}