//! 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 and a fixed optical post don't drive. pub fn mobile(self) -> bool { !matches!(self, SamKind::Depot | SamKind::Optical) } } impl Sam { /// A reload truck's job: the system or depot it is going to. pub fn job(&self) -> Option { self.job } /// Packing up, driving or deploying: not fighting. pub fn moving(&self) -> bool { matches!(self.state, SamState::Packing | SamState::Moving | SamState::Deploying) } } fn finish_move(sam: &mut Sam, routes: &mut route::Routes, index: usize, now: u64, why: Option, out: &mut Out) { if let Some(why) = why { let report = sam.job.take().is_none_or(|job| { routes.unreachable.insert((sam.id, job), now + route::UNREACHABLE_FOR).is_none() }); if report { out.messages.push(format!("{} №{}: {}", sam.kind.name(), index + 1, why.text())); } } routes.map.remove(&sam.id); sam.dest = None; sam.state = SamState::Deploying; sam.march = sam.kind.march_ticks(); } 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 { if !self.order_move_near(i, to, 0.0) { return false; } self.sams[i].job = None; true } /// 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, 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 { finish_move(sam, routes, n, now, None, out); continue; }; let route = routes.map.entry(sam.id).or_insert_with(|| route::Route::new(sam.pos, to, 0.0, sam.kind, now)); let (go, last) = match route.drive(ground, now, sam.pos, share) { route::Drive::Wait(why) => { if sam.job.is_none() && 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) => { if why == route::Why::NoWay && route.approach_works() { continue; } if matches!(why, route::Why::NoWay | route::Why::Stuck) && route.try_works(ground, sam.pos, now, out) { continue; } finish_move(sam, routes, n, now, Some(why), out); continue; } route::Drive::Go(p, last) => (p, last), }; let flat = horizontal(go - sam.pos); if last && flat.length() < SPEED * 2.0 { let why = if route.work_approach() { if route.try_works(ground, sam.pos, now, out) { continue; } Some(route::Why::NoWay) } else { // A partial route already made measurable progress. A delivery // continues from here when the next stretch of terrain is ready. (route.partial && sam.job.is_none()).then_some(route::Why::Partial) }; finish_move(sam, routes, n, now, why, out); 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(); if sam.kind == SamKind::Tzm && turning > 0.45 { sam.yaw = sam.heading; continue; } let step = SPEED * sam.drive_speed() * (1.0 - turning / std::f64::consts::PI).max(0.2); let next = sam.pos + ahead * step; // The column and chassis clearance ahead: a wall, low ceiling or a change after planning // turns the vehicle back to the planner. Unknown voxels remain a wait, never a drive. let Some(y) = ground.at(next, now) else { if route.wait_ahead() { finish_move(sam, routes, n, now, Some(route::Why::NoMap), out); } continue; }; match route.clearance(ground, next, now) { route::Clearance::Clear => {} route::Clearance::Pending => { if route.wait_ahead() { finish_move(sam, routes, n, now, Some(route::Why::NoMap), out); } continue; } route::Clearance::Blocked => { route.blocked_ahead(next, sam.pos); 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; } } _ => {} } } } }