use crate::game::*; use crate::iso::*; use crate::player::*; use crate::world::*; use bevy::app::AppExit; use bevy::prelude::*; use bevy::render::view::window::screenshot::{Screenshot, ScreenshotCaptured, save_to_disk}; use bevy::time::TimeUpdateStrategy; use std::io::Write; use std::process::{Child, ChildStdin, Command, Stdio}; use std::sync::{Arc, Mutex}; use std::time::Duration; pub const FPS: f32 = 30.0; pub struct DemoPlugin { pub record: Option, pub script: bool, } #[derive(Clone)] pub enum Step { Walk(Vec2), Road { leg: usize, forward: bool, journey: bool, until: f32 }, Press, Pick(&'static str), Settle, Wait(f32), Camp, Lantern, Eat, Inv, Shot(&'static str), Tp(Vec2), Hour(f32), } #[derive(Resource)] struct Script { steps: Vec, i: usize, t: f32, road_i: Option, auto_t: f32, path: Vec, } fn find_path(map: &Map, from: Vec2, to: Vec2) -> Vec { use std::collections::{HashMap, VecDeque}; let (a, b) = (cell(from), cell(to)); let mut prev: HashMap = HashMap::new(); let mut q = VecDeque::from([a]); prev.insert(a, a); while let Some(c) = q.pop_front() { if c == b { break; } for d in [IVec2::X, IVec2::NEG_X, IVec2::Y, IVec2::NEG_Y] { let n = c + d; if prev.contains_key(&n) || (n - a).abs().max_element() > 40 { continue; } if n != b && !map.walkable(n.as_vec2() + Vec2::splat(0.5)) { continue; } prev.insert(n, c); q.push_back(n); } } if !prev.contains_key(&b) { return vec![to]; } let mut cells = vec![b]; let mut c = b; while c != a { c = prev[&c]; cells.push(c); } cells.reverse(); let mut out: Vec = cells.iter().skip(1).map(|c| c.as_vec2() + Vec2::splat(0.5)).collect(); out.pop(); out.push(to); out } const AUTO: &[&str] = &["Light the lantern", "Throw them a ration", "Help him", "Offer him bread", "Point the way", "Press on", "Make camp", "Continue", "Leave", "Walk on", "Run for it"]; fn pick(g: &Game, prefs: &[&str]) -> Option { let (t, node) = g.dialog?; let d = crate::content::talk(t, node, g); if d.choices.is_empty() { return Some(0); } for p in prefs { if let Some(i) = d.choices.iter().position(|c| c.label.starts_with(p)) { return Some(i); } } None } #[derive(Resource)] struct Recorder { pipe: Option>>, child: Option, done_at: Option, frame: u32, } impl Plugin for DemoPlugin { fn build(&self, app: &mut App) { if self.script { app.insert_resource(Scripted) .insert_resource(Script { steps: if std::env::var("COURIER_TEST").is_ok() { test_script() } else { script() }, i: 0, t: 0.0, road_i: None, auto_t: 0.0, path: Vec::new() }) .add_systems(PreUpdate, drive); } if let Some(path) = &self.record { app.insert_resource(TimeUpdateStrategy::ManualDuration(Duration::from_secs_f32(1.0 / FPS))); let mut child = Command::new("ffmpeg") .args(["-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgba", "-s", "1280x720", "-r", "30", "-i", "-", "-c:v", "libx264", "-preset", "medium", "-crf", "20", "-pix_fmt", "yuv420p", path]) .stdin(Stdio::piped()) .spawn() .expect("ffmpeg"); let stdin = child.stdin.take().unwrap(); app.insert_resource(Recorder { pipe: Some(Arc::new(Mutex::new(stdin))), child: Some(child), done_at: None, frame: 0 }) .add_systems(Last, record); } } } fn record(mut commands: Commands, off: Res, mut rec: ResMut, script: Option>, mut exit: MessageWriter) { rec.frame += 1; let finished = script.map(|s| s.i >= s.steps.len()).unwrap_or(rec.frame > 30 * 20); if finished && rec.done_at.is_none() { rec.done_at = Some(rec.frame); } if let Some(d) = rec.done_at { if rec.frame > d + 20 { rec.pipe = None; if let Some(mut c) = rec.child.take() { let _ = c.wait(); } exit.write(AppExit::Success); } return; } if rec.frame < 5 { return; } let Some(pipe) = rec.pipe.clone() else { return }; commands.spawn(Screenshot::image(off.0.clone())).observe(move |ev: On| { if let Ok(img) = ev.image.clone().try_into_dynamic() { let rgba = img.to_rgba8(); if rgba.width() == 1280 && rgba.height() == 720 { let _ = pipe.lock().unwrap().write_all(rgba.as_raw()); } } }); } fn drive( mut commands: Commands, time: Res