diff --git a/domains/recreation/README.md b/domains/recreation/README.md index 7fd8d60..1911d1e 100644 --- a/domains/recreation/README.md +++ b/domains/recreation/README.md @@ -16,7 +16,7 @@ In the sim, **G** starts a pylon race and **T** a game of tag. The same key agai | `recreation_tag` | [tag/](tag) | Tag: the arena and its referee (who is it, touches, no tag-backs, leaving the arena, the clock). Depends only on `glam` | | `recreation_pilot` | [pilot/](pilot) | The computer pilot: hands that hold a bank and a climb rate, and steering to a point, along a line or onto another plane. Its [`Skill`](pilot/src/lib.rs) sets how hard it turns and how fast it flies | | `recreation_towers` | [towers/](towers) | Towers, a team game for drones flown by plan: the towers and their circles, capture, points, the end of the game, the valley (forest, lanes, lake) and what each team sees through the fog and the trees. Depends only on `glam`. See [plans/towers.md](../../plans/towers.md) | -| `recreation_towers_server` | [towers_server/](towers_server) | The game server of Towers (binary `towers_server`): an ArduPlane SITL in AGENT mode for each drone, the flight model (with the trees), the referee, and a seat for each team over TCP | +| `recreation_towers_server` | [towers_server/](towers_server) | The game server of Towers (binary `towers_server`): an ArduPlane SITL in AGENT mode for each drone, the flight model (with the trees), the referee, and a seat for each team over TCP, for a person or a computer commander | | `recreation_towers_view` | [towers_view/](towers_view) | The 3D view of a game of Towers (binary `towers_view`): the valley, the six drones, and the camera of each drone on the sensor feed of the sim (TCP 5601 to 5606) | | `recreation_games` | [games/](games) | A game in play: the countdown, the computer pilots (Kestrel the ace, Merlin the club pilot, Swift the rookie) and what they decide to do, their planes on the flight model, and the referee. Also a stand-in that flies your plane, which the acceptance tests use | @@ -164,9 +164,19 @@ QHOVER to 15 m, FBWA); once it flies on the wing, the ground switches it to AGEN plan for its order about once a second (a tower: an orbit 40 m out, at a height of its own) and the other drones as `ADSB_VEHICLE` traffic. A crashed drone is back at its base after 15 s. -Each team has a seat on TCP port 5955 (`recreation_towers::seat`, a JSON message a line): a -computer commander, or a person (`--blue person`, `--red person`; GO waits for them). A seat that -commands or watches a team gets only that team's map, four times a second; a spectator gets the +Each team has a seat on TCP port 5955 (`recreation_towers::seat`, a JSON message a line), and it is +one seat for a person and for a computer. A seat that commands is the team's ground station: it +gets the umpire's word, what its towers and base report, and the way to its drones (the air), makes +its own map from its drones' telemetry, flies the drones from it, and sends orders. The server decides and sends +no plan for any team. `--blue computer` (the default) makes the server start a computer commander +(`recreation_towers_ground::commander`, also the binary `towers_commander --team blue|red [--host H] +[--port P] [--idle]`) that takes the seat over loopback TCP, as `Join::Compute`; `nobody` starts one +that gives no orders; `person` keeps the seat for a person, and GO waits for them. One person for +each team may play. A seat changes hands during a game: a person who joins a team that a computer +commands takes the seat (the computer is relieved; the person's seat starts with the orders the +drones have, which the server sends once after the welcome), and when a person leaves, a second later +a computer commander takes the seat (`--on-leave computer`, the default; `--on-leave wait` leaves +the drones on their last orders until a person comes back). A seat that watches a team gets only that team's map, four times a second; a spectator gets the whole game. At the end the server prints `RESULT` lines (the outcome, drones in AGENT, refused commands, crashes, towers each team took). diff --git a/domains/recreation/towers/src/seat.rs b/domains/recreation/towers/src/seat.rs index 3ccf671..0896aa7 100644 --- a/domains/recreation/towers/src/seat.rs +++ b/domains/recreation/towers/src/seat.rs @@ -18,14 +18,27 @@ pub const PORT: u16 = 5955; /// What a seat is. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub enum Join { - /// Commands a team (a seat the server was told to keep for a person). + /// Commands a team, as a person at the commander app does. Command(Team), + /// Commands a team, as a computer does: the same seat, with the same messages. It is a seat that + /// gives way: a person who joins the team takes it from the computer. + Compute(Team), /// Watches a team: its map, no orders. Watch(Team), /// Watches the whole game, with no fog. Spectate, } +impl Join { + /// The team the seat commands, for a person or a computer. + pub fn commands(self) -> Option { + match self { + Join::Command(t) | Join::Compute(t) => Some(t), + Join::Watch(_) | Join::Spectate => None, + } + } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum ToServer { Join(Join), @@ -97,6 +110,10 @@ pub enum ToSeat { Umpire(Umpire), /// What the team's towers and base report, to a seat that commands. Sites(Sites), + /// The orders the team's drones have now (by drone number in the team: the order, and whether it + /// is flown low), once, right after the welcome of a seat that commands: a commander that takes + /// over a game goes on from them. + Orders(Vec<(Option, bool)>), Truth(Truth), /// The advisor of the team speaks, to a seat that commands it, when something matters. Advice(Advice), diff --git a/domains/recreation/towers_ground/Cargo.toml b/domains/recreation/towers_ground/Cargo.toml index e6fc362..e4d0cfa 100644 --- a/domains/recreation/towers_ground/Cargo.toml +++ b/domains/recreation/towers_ground/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" edition = "2024" description = "Recreation domain: the ground of Towers: the MAVLink side of a team's drones (AGENT mode, plans, standing orders, traffic) behind a Transport, as the commander app runs it and as the game server runs it for a computer team" +[[bin]] +name = "towers_commander" +path = "src/bin/towers_commander.rs" + [dependencies] recreation_towers = { path = "../towers" } simulation_sitl = { path = "../../simulation/sitl" } diff --git a/domains/recreation/towers_ground/src/bin/towers_commander.rs b/domains/recreation/towers_ground/src/bin/towers_commander.rs new file mode 100644 index 0000000..612ddea --- /dev/null +++ b/domains/recreation/towers_ground/src/bin/towers_commander.rs @@ -0,0 +1,26 @@ +//! A computer commander for one team of Towers, as a seat of its own: +//! `towers_commander --team blue|red [--host 127.0.0.1] [--port 5955] [--idle]`. It takes the +//! team's seat, flies its drones from the map its drones' telemetry makes, and gives them orders +//! once a second. If a person holds the seat it waits for it to be free; if a person joins the +//! team, it leaves. `--idle` sits in the seat and gives no orders. Ends when the game is over. + +use recreation_towers::Team; +use recreation_towers_ground::commander::{self, Brain}; +use std::sync::atomic::AtomicBool; + +fn main() { + let args: Vec = std::env::args().collect(); + let arg = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).cloned(); + let team = match arg("--team").as_deref() { + Some("blue") => Team::Blue, + Some("red") => Team::Red, + _ => { + eprintln!("usage: towers_commander --team blue|red [--host H] [--port P] [--idle]"); + std::process::exit(2); + } + }; + let host = arg("--host").unwrap_or_else(|| "127.0.0.1".into()); + let port: u16 = arg("--port").and_then(|p| p.parse().ok()).unwrap_or(recreation_towers::seat::PORT); + let brain = if args.iter().any(|a| a == "--idle") { Brain::Idle } else { Brain::Play }; + commander::run(&format!("{host}:{port}"), team, brain, None, &AtomicBool::new(false)); +} diff --git a/domains/recreation/towers_ground/src/commander.rs b/domains/recreation/towers_ground/src/commander.rs new file mode 100644 index 0000000..9f89a2b --- /dev/null +++ b/domains/recreation/towers_ground/src/commander.rs @@ -0,0 +1,73 @@ +//! The computer commander: a seat like a person's. It takes a `Command` seat on the game server +//! (as `Join::Compute`, a seat that gives way to a person), flies the team's drones from the +//! station of the seat (`station`: its own ground, the map from telemetry) and, once a second of +//! game time, asks [`recreation_towers::Commander`] for orders on the map the station built. It +//! knows only what a seat gets. + +use crate::seat::{Seat, Status}; +use crate::station::{self, Tally}; +use recreation_towers::seat::Join; +use recreation_towers::{Class, Commander, Team}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; + +/// How often the commander decides (s of the game). +pub const DECIDE_EVERY: f32 = 1.0; + +/// What the commander does with its seat. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Brain { + /// Plays: decides once a second. + Play, + /// Sits in the seat and gives no orders: its drones fly home (a game to practise in). + Idle, +} + +/// Takes the seat of `team` on the server at `addr` (`host:port`) and commands it until the game is +/// over, a person takes the seat, or `stop` is set. If the seat is held, it waits for it to be free. +pub fn run(addr: &str, team: Team, brain: Brain, tally: Option>, stop: &AtomicBool) { + let host = addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(); + let seat = Seat::connect(addr, Join::Compute(team)); + station::start(seat.clone(), host, tally); + let mut commander: Option<(Commander, f32)> = None; + while !stop.load(Ordering::SeqCst) { + std::thread::sleep(Duration::from_millis(50)); + let (status, rules, view) = { + let s = seat.state(); + (s.status.clone(), s.rules, s.view.clone()) + }; + match status { + // Relieved by a person, or refused for good. + Status::Refused(why) if !why.contains("has its commander") => break, + Status::Seated(_) => {} + _ => { + commander = None; + continue; + } + } + let (Some(rules), Some(view)) = (rules, view) else { continue }; + if view.outcome.is_some() { + break; + } + if brain == Brain::Idle || view.drones.len() != rules.drones || view.time <= 0.0 { + continue; + } + // The seat starts from the orders the drones have: they are on the map. + let (c, next) = commander.get_or_insert_with(|| { + let mut c = Commander::new(rules); + for d in &view.drones { + c.assign(d.k, d.order); + } + (c, f32::NEG_INFINITY) + }); + if view.time < *next { + continue; + } + *next = view.time + DECIDE_EVERY; + for (k, order) in c.decide(&view) { + seat.order(k, order, rules.class(k) == Class::Light); + } + } + seat.stop(); +} diff --git a/domains/recreation/towers_ground/src/lib.rs b/domains/recreation/towers_ground/src/lib.rs index b75489a..20a2c4a 100644 --- a/domains/recreation/towers_ground/src/lib.rs +++ b/domains/recreation/towers_ground/src/lib.rs @@ -5,11 +5,16 @@ //! of them. A light drone, a quadcopter, has no wing for AGENT to fly: it is switched to QLOITER and //! flown by the world's radio. //! -//! The commander app runs this for its team, and the game server for a computer's. It talks +//! The commander app runs this for its team, and so does a computer commander (`commander`): both +//! take a seat on the game server (`seat`) and fly the team's drones from its station (`station`). +//! It talks //! through a [`Transport`]: TCP to the simulated ground radio, or a serial line to a real one. //! What it knows of the game is a [`Situation`]. +pub mod commander; pub mod map; +pub mod seat; +pub mod station; use cognition_agent::Craft; use cognition_agent::link::{Link, LinkState, PositionReport, Sighting, Traffic}; diff --git a/domains/recreation/towers_ground/src/seat.rs b/domains/recreation/towers_ground/src/seat.rs new file mode 100644 index 0000000..98d7814 --- /dev/null +++ b/domains/recreation/towers_ground/src/seat.rs @@ -0,0 +1,221 @@ +//! A seat on the game server, for the commander app and for a computer commander: a TCP connection on threads of its own. It keeps the last +//! map the server sent and sends the orders the commander gives. If the connection is lost, or +//! the server is not up yet, it tries again. + +use recreation_towers::seat::{self, Join, ToSeat, ToServer}; +use recreation_towers::{Advice, Order, Rules, Sites, TeamView, Umpire}; +use std::io::{BufRead, BufReader, Write}; +use std::net::TcpStream; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; +use std::sync::mpsc::{Receiver, Sender, channel}; +use std::sync::{Arc, Mutex, MutexGuard}; +use std::time::{Duration, Instant}; + +#[derive(Clone, Debug, PartialEq)] +pub enum Status { + Connecting, + Seated(Join), + Refused(String), +} + +#[derive(Debug)] +pub struct State { + pub status: Status, + pub rules: Option, + pub view: Option, + /// What the server last said: the umpire's word, and what the team's towers and base report. The + /// ground station makes `view` from them and from the telemetry of its drones (station.rs). + pub umpire: Option, + pub sites: Option, + /// How many maps of the team the server sent a seat that commands (it sends none). + pub maps: usize, + /// The ports of the air in front of the team's drones, on the server's host: the way to them. + pub air: Vec, + /// The orders given, by drone number in the team: the order and whether it is flown low. + pub orders: Vec<(Option, bool)>, + /// What the advisor said lately, and when (the last few). + pub advice: Vec<(Advice, Instant)>, +} + +/// How many pieces of advice the seat keeps. +const ADVICE_KEPT: usize = 3; + +pub struct Seat { + addr: String, + join: Join, + state: Arc>, + orders: Mutex>, + /// Each (re)connection's number: a connection that is not the newest stops. + generation: Arc, + /// The drones that the app's own ground has put in AGENT mode (station.rs). + pub flown: AtomicUsize, + /// How many position reports the app has heard from its drones. + pub heard: AtomicUsize, + /// Set when the seat is given up for good: its station stops too. + dead: AtomicBool, +} + +impl Seat { + pub fn connect(addr: &str, join: Join) -> Arc { + let (tx, _) = channel(); + let seat = Arc::new(Seat { + addr: addr.to_string(), + join, + state: Arc::new(Mutex::new(State { + status: Status::Connecting, + rules: None, + view: None, + umpire: None, + sites: None, + maps: 0, + air: vec![], + orders: vec![], + advice: vec![], + })), + orders: Mutex::new(tx), + generation: Arc::new(AtomicU64::new(0)), + flown: AtomicUsize::new(0), + heard: AtomicUsize::new(0), + dead: AtomicBool::new(false), + }); + seat.reconnect(); + seat + } + + pub fn join(&self) -> Join { + self.join + } + + pub fn state(&self) -> MutexGuard<'_, State> { + self.state.lock().unwrap() + } + + /// Sends an order for the team's drone `drone`, flown `low` (along the lanes) or high. + pub fn order(&self, drone: usize, order: Order, low: bool) { + // The app is the team's ground station: the order is its own to carry out (station.rs). + { + let mut s = self.state(); + if s.orders.len() <= drone { + s.orders.resize(drone + 1, (None, false)); + } + s.orders[drone] = (Some(order), low); + } + let _ = self.orders.lock().unwrap().send(ToServer::Order { drone, order, low, standing: None }); + } + + /// Stops connecting: the connection that is open ends with its server, and is not made + /// again. (Before the server stops, so that it does not take the seat of the next game first.) + pub fn hang_up(&self) { + self.generation.fetch_add(1, Ordering::SeqCst); + } + + /// Gives the seat up for good: no connection, and the station stops. + pub fn stop(&self) { + self.dead.store(true, Ordering::SeqCst); + self.hang_up(); + } + + pub fn stopped(&self) -> bool { + self.dead.load(Ordering::SeqCst) + } + + /// Forgets the game and connects again (to a new game on the same server address). + pub fn reconnect(&self) { + let generation = self.generation.fetch_add(1, Ordering::SeqCst) + 1; + *self.state() = State { + status: Status::Connecting, + rules: None, + view: None, + umpire: None, + sites: None, + maps: 0, + air: vec![], + orders: vec![], + advice: vec![], + }; + let (tx, rx) = channel(); + *self.orders.lock().unwrap() = tx; + let (addr, join, state, current) = (self.addr.clone(), self.join, self.state.clone(), self.generation.clone()); + std::thread::spawn(move || run(&addr, join, &state, rx, ¤t, generation)); + } +} + +/// One connection, for as long as it is the newest: connects (again, if it is lost), takes the +/// seat, and reads the game. The orders go out on a thread of their own, to whichever stream is +/// open. +fn run(addr: &str, join: Join, state: &Mutex, orders: Receiver, current: &AtomicU64, generation: u64) { + let out: Arc>> = Arc::new(Mutex::new(None)); + let writer = out.clone(); + std::thread::spawn(move || { + while let Ok(msg) = orders.recv() { + if let Some(stream) = writer.lock().unwrap().as_mut() { + let _ = writeln!(stream, "{}", seat::encode(&msg)); + } + } + }); + while current.load(Ordering::SeqCst) == generation { + let Ok(stream) = TcpStream::connect(addr) else { + std::thread::sleep(Duration::from_millis(300)); + continue; + }; + let Ok(mut w) = stream.try_clone() else { continue }; + if writeln!(w, "{}", seat::encode(&ToServer::Join(join))).is_err() { + continue; + } + *out.lock().unwrap() = Some(w); + for line in BufReader::new(stream).lines().map_while(Result::ok) { + if current.load(Ordering::SeqCst) != generation { + if let Some(s) = out.lock().unwrap().take() { + let _ = s.shutdown(std::net::Shutdown::Both); + } + return; + } + let Ok(msg) = seat::decode::(&line) else { continue }; + let mut s = state.lock().unwrap(); + match msg { + ToSeat::Welcome { join, rules, air } => { + s.status = Status::Seated(join); + s.rules = Some(rules); + s.air = air; + } + ToSeat::Refused(why) => s.status = Status::Refused(why), + ToSeat::View(v) => { + if join.commands().is_some() { + s.maps += 1; + } + s.view = Some(v); + } + ToSeat::Umpire(u) => s.umpire = Some(u), + ToSeat::Sites(sites) => s.sites = Some(sites), + ToSeat::Orders(orders) => s.orders = orders, + ToSeat::Truth(_) => {} + ToSeat::Advice(a) => { + s.advice.push((a, Instant::now())); + let extra = s.advice.len().saturating_sub(ADVICE_KEPT); + s.advice.drain(..extra); + } + } + } + *out.lock().unwrap() = None; + // Hung up, or a newer connection: the state is the newer one's. + if current.load(Ordering::SeqCst) != generation { + return; + } + // The seat is refused for good if the computer commands the team. If a commander holds it, + // that is this app's own connection from before a new game: it lets go in a moment. + let refused = match &state.lock().unwrap().status { + Status::Refused(why) => Some(why.contains("has its commander")), + _ => None, + }; + match refused { + Some(false) => return, + Some(true) => { + std::thread::sleep(Duration::from_millis(400)); + continue; + } + None => {} + } + state.lock().unwrap().status = Status::Connecting; + std::thread::sleep(Duration::from_millis(300)); + } +} diff --git a/domains/recreation/towers_ground/src/station.rs b/domains/recreation/towers_ground/src/station.rs new file mode 100644 index 0000000..bb27ad4 --- /dev/null +++ b/domains/recreation/towers_ground/src/station.rs @@ -0,0 +1,254 @@ +//! A seat as the ground station of its team (the commander app's, and a computer commander's) (plans/unified-link.md): it puts its drones in AGENT +//! mode and sends each one its plan, its standing order and the traffic as MAVLink, through the air +//! in front of the drone, as the ground station of a real drone does. The game server only plays +//! the world; the app tells the drones what to do from what its team knows (the map) and from the +//! orders that the commander gave. + +use crate::map::{self, OwnReport, Seen}; +use crate::seat::{Seat, Status}; +use crate::{AGENT_MODE, Ground, QLOITER_MODE, Situation, Transport, drone_name, sitl_home}; +use glam::Vec2; +use recreation_towers::{Game, Order, Phase, Rules}; +use std::sync::Arc; +use std::sync::Mutex; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; + +/// How often the ground looks (wall time). +const LOOK_EVERY: Duration = Duration::from_millis(20); +/// ArduPlane's FBWA, the mode of a drone that has taken off and waits for AGENT. +const FBWA_MODE: u32 = 5; +/// A drone in FBWA flies on the wing at this speed (m/s) or more: it is past the transition (the +/// server's take-off counts the transition done at 15 m/s of airspeed). +const WING_SPEED: f32 = 15.0; +/// A drone is taken to have flown on in a straight line this long (s of the game) since it reported. +const DEAD_RECKONING: f32 = 1.5; + +/// The drones' time as the app can know it: the time of the last map, and the rate at which that time +/// runs (a game sped up runs ahead of the clock on the wall). +struct Clock { + map_time: f32, + at: Instant, + rate: f64, + last: f64, +} + +impl Clock { + fn new() -> Clock { + Clock { map_time: f32::NEG_INFINITY, at: Instant::now(), rate: 1.0, last: 0.0 } + } + + /// Game seconds to a wall second. + fn rate(&self) -> f64 { + self.rate + } + + /// The time now, given the time of the latest map. + fn now(&mut self, map_time: f32) -> f64 { + if map_time != self.map_time { + let wall = self.at.elapsed().as_secs_f64(); + if self.map_time.is_finite() && wall > 0.05 && map_time > self.map_time { + let rate = (map_time - self.map_time) as f64 / wall; + self.rate = (0.7 * self.rate + 0.3 * rate).clamp(0.5, 20.0); + } + (self.map_time, self.at) = (map_time, Instant::now()); + } + // Never ahead of the next map by more than a wall second (a game sped up sends a map every + // few game seconds: a cap in game time would stall the plans between maps), and never back. + let now = self.map_time as f64 + self.at.elapsed().as_secs_f64().min(1.0) * self.rate; + self.last = self.last.max(now); + self.last + } +} + +/// What the stations of a game report for the server's log: how the drones went into AGENT mode, +/// and what each said last. Shared by the stations of a game (a seat that is given up and taken +/// again makes a new ground: the counts keep the most). +#[derive(Default)] +pub struct Tally { + /// Drones that have been in AGENT mode, by team. + pub in_agent: [AtomicUsize; 2], + /// Commands that the drones refused, by team. + pub refused: [AtomicUsize; 2], + /// A line about each drone of the game, by number in the game. + pub lines: Mutex>, + /// Warnings of the stations, by team. + pub warnings: Mutex<[Vec; 2]>, +} + +impl Tally { + pub fn in_agent(&self) -> usize { + self.in_agent.iter().map(|n| n.load(Ordering::SeqCst)).sum() + } + + pub fn refused(&self) -> usize { + self.refused.iter().map(|n| n.load(Ordering::SeqCst)).sum() + } + + pub fn warnings(&self) -> Vec { + self.warnings.lock().unwrap().iter().flatten().cloned().collect() + } +} + +/// Starts the ground of the seat's team, on a thread of its own, for as long as the seat commands +/// (until [`Seat::stop`]). `tally`, if given, is told what the ground does. +pub fn start(seat: Arc, host: String, tally: Option>) { + std::thread::Builder::new() + .name("station".into()) + .spawn(move || run(&seat, &host, tally.as_deref())) + .expect("the station thread"); +} + +/// What the ground flies: its connection, for one set of air ports. +struct Flying { + air: Vec, + ground: Ground, + game: Game, + rules: Rules, + first: usize, + clock: Clock, + home: (f64, f64, f32), +} + +fn run(seat: &Seat, host: &str, tally: Option<&Tally>) { + let mut flying: Option = None; + let mut next_note = Instant::now(); + loop { + std::thread::sleep(LOOK_EVERY); + if seat.stopped() { + return; + } + let (status, rules, umpire, sites, air, orders) = { + let s = seat.state(); + (s.status.clone(), s.rules, s.umpire, s.sites.clone(), s.air.clone(), s.orders.clone()) + }; + let (Status::Seated(join), Some(rules), Some(umpire), Some(sites)) = (status, rules, umpire, sites) else { + flying = None; + seat.flown.store(0, Ordering::SeqCst); + continue; + }; + let Some(team) = join.commands() else { + flying = None; + seat.flown.store(0, Ordering::SeqCst); + continue; + }; + if air.len() != rules.drones { + continue; + } + if flying.as_ref().is_none_or(|f| f.air != air) { + // A new game, or the first look: connect to the drones through the air. + let n = rules.drones; + let first = team.index() * n; + let names: Vec = (0..2 * n).map(|i| drone_name(&rules, i)).collect(); + let indices: Vec = (first..first + n).collect(); + // Light drones have no wing for AGENT to fly: the world's radio flies them, in QLOITER. + let modes: Vec = (0..n) + .map(|k| if rules.class(k) == recreation_towers::Class::Light { QLOITER_MODE } else { AGENT_MODE }) + .collect(); + let transport = Transport::Tcp { host: host.to_string(), ports: air.clone() }; + let ground = Ground::connect(&transport, &names, &indices, &modes, sitl_home()); + flying = Some(Flying { + air, + ground, + game: Game::new(rules), + rules, + first, + clock: Clock::new(), + home: sitl_home(), + }); + } + let f = flying.as_mut().expect("a ground"); + let n = f.rules.drones; + // The map: the umpire's word and the reports of the sites, plus what the drones report of + // themselves and of the enemy in their telemetry. + let rate = f.clock.rate() as f32; + let (mut own, mut seen) = (vec![None; n], vec![]); + for (k, report) in own.iter_mut().enumerate() { + let (position, sightings) = f.ground.telemetry(k); + *report = position.map(|p| { + let (north, east) = cognition_plan::to_local(f.home.0, f.home.1, p.lat, p.lon); + let vel = Vec2::new(p.vel[0], p.vel[1]); + let age = p.heard.elapsed().as_secs_f32() * rate; + OwnReport { + // Where the drone has got to since it reported (it flies on): a plan from the + // report's place would put the drone's entry to a tower, and its lane, behind it. + at: Vec2::new(north, east) + vel * age.min(DEAD_RECKONING), + alt: p.alt, + vel, + age, + } + }); + seen.extend(sightings.iter().map(|s| { + let (north, east) = cognition_plan::to_local(f.home.0, f.home.1, s.lat, s.lon); + Seen { + enemy: s.enemy, + at: Vec2::new(north, east), + alt: s.alt_amsl - f.home.2, + vel: Vec2::new(s.speed * s.heading.to_radians().cos(), s.speed * s.heading.to_radians().sin()), + age: s.heard.elapsed().as_secs_f32() * rate, + } + })); + } + let view = map::build(&umpire, &sites, &own, &seen, &orders, &f.rules); + // The picture the ground plans from is the map: the drones where the team has them. + for d in &view.drones { + let g = &mut f.game.drones[f.first + d.k]; + (g.at, g.alt, g.vel, g.phase) = (d.at, d.alt, d.vel, d.phase); + } + let enemy_first = if team.index() == 0 { n } else { 0 }; + for g in &mut f.game.drones[enemy_first..enemy_first + n] { + g.phase = Phase::Ground; + } + for e in view.enemies.iter().filter(|e| e.seen) { + let g = &mut f.game.drones[enemy_first + e.k]; + (g.at, g.alt, g.vel, g.phase) = (e.at, e.alt, e.vel, Phase::Flying); + } + let mut given = vec![(Some(Order::Home), false); 2 * n]; + for (k, o) in orders.iter().enumerate().take(n) { + if o.0.is_some() { + given[f.first + k] = *o; + } + } + let wing: Vec = (0..2 * n) + .map(|i| { + let Some(k) = i.checked_sub(f.first).filter(|&k| k < n) else { return false }; + let d = &view.drones[k]; + // On the wing: in FBWA with the transition done (fast enough to fly), or already in + // AGENT or QLOITER. AGENT asked for in the middle of the transition stalls the drone. + let mode = f.ground.mode_of(k); + let up = d.phase == Phase::Flying; + up && match mode { + Some(FBWA_MODE) => d.vel.length() >= WING_SPEED, + Some(m) => [AGENT_MODE, QLOITER_MODE].contains(&m), + None => false, + } + }) + .collect(); + let situation = Situation { + now: f.clock.now(view.time), + game: &f.game, + orders: &given.iter().map(|o| o.0).collect::>(), + lows: &given.iter().map(|o| o.1).collect::>(), + standing: &vec![None; 2 * n], + wing, + }; + f.ground.tick(&situation); + seat.flown.store(f.ground.agent_drones(), Ordering::SeqCst); + seat.heard.store(f.ground.position_reports() as usize, Ordering::SeqCst); + if let Some(t) = tally { + let ti = team.index(); + t.in_agent[ti].fetch_max(f.ground.agent_drones(), Ordering::SeqCst); + t.refused[ti].fetch_max(f.ground.results().1 as usize, Ordering::SeqCst); + if next_note.elapsed() >= Duration::from_secs(1) { + next_note = Instant::now(); + let mut lines = t.lines.lock().unwrap(); + lines.resize(2 * n, String::new()); + for k in 0..n { + lines[f.first + k] = f.ground.describe(f.first + k); + } + t.warnings.lock().unwrap()[ti] = f.ground.verdict(); + } + } + seat.state().view = Some(view); + } +} diff --git a/domains/recreation/towers_ground/tests/commander.rs b/domains/recreation/towers_ground/tests/commander.rs new file mode 100644 index 0000000..e599e7b --- /dev/null +++ b/domains/recreation/towers_ground/tests/commander.rs @@ -0,0 +1,81 @@ +//! A computer commander is a seat like a person's: against a fake game server that speaks only the +//! seat protocol (no SITL), it takes the seat as `Join::Compute`, and from the umpire's word and +//! what its sites report it gives orders; it goes on from the orders the drones have, and it leaves +//! when it is relieved. + +use recreation_towers::seat::{self, Join, ToSeat, ToServer}; +use recreation_towers::{Game, Order, Rules, Team}; +use recreation_towers_ground::commander::{self, Brain}; +use std::io::{BufRead, BufReader, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc::channel; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +#[test] +fn a_computer_commander_takes_a_seat_gives_orders_from_the_sites_and_leaves_when_relieved() { + let rules = Rules::default(); + // Air ports for the team's drones that accept and say nothing: no telemetry at all. + let air: Vec = (0..rules.drones).map(|_| TcpListener::bind("127.0.0.1:0").unwrap()).collect(); + let ports: Vec = air.iter().map(|l| l.local_addr().unwrap().port()).collect(); + let held: Arc>> = Arc::default(); + for l in air { + let held = held.clone(); + std::thread::spawn(move || { + for s in l.incoming().flatten() { + held.lock().unwrap().push(s); + } + }); + } + let server = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port()); + let stop = Arc::new(AtomicBool::new(false)); + let (done_tx, done_rx) = channel(); + let (a, s) = (addr.clone(), stop.clone()); + std::thread::spawn(move || { + commander::run(&a, Team::Blue, Brain::Play, None, &s); + done_tx.send(()).unwrap(); + }); + let (stream, _) = server.accept().unwrap(); + let mut out = stream.try_clone().unwrap(); + let mut lines = BufReader::new(stream).lines().map_while(Result::ok); + let ToServer::Join(join) = seat::decode(&lines.next().unwrap()).unwrap() else { panic!("not a join") }; + assert_eq!(join, Join::Compute(Team::Blue)); + let said = |out: &mut TcpStream, m: ToSeat| writeln!(out, "{}", seat::encode(&m)).unwrap(); + said(&mut out, ToSeat::Welcome { join, rules, air: ports }); + // The drones have orders already: a person was commanding them. + said(&mut out, ToSeat::Orders(vec![(Some(Order::Hold), true); rules.drones])); + let mut game = Game::new(rules); + game.time = 5.0; + let (orders_tx, orders_rx) = channel(); + let reader = std::thread::spawn(move || { + for line in lines { + if let Ok(ToServer::Order { drone, order, low, .. }) = seat::decode::(&line) { + orders_tx.send((drone, order, low)).unwrap(); + } + } + }); + // Tell it the game until it has given every drone an order. + let end = Instant::now() + Duration::from_secs(10); + let mut given = std::collections::BTreeMap::new(); + while given.len() < rules.drones { + assert!(Instant::now() < end, "it gave orders to {given:?}"); + game.time += 0.25; + said(&mut out, ToSeat::Umpire(game.umpire())); + said(&mut out, ToSeat::Sites(game.sites(Team::Blue))); + while let Ok((drone, order, low)) = orders_rx.recv_timeout(Duration::from_millis(100)) { + given.insert(drone, (order, low)); + } + } + assert!(given.keys().all(|&k| k < rules.drones)); + for (k, (_, low)) in &given { + assert_eq!(*low, rules.class(*k) == recreation_towers::Class::Light, "drone {k} is flown low if it is light"); + } + // Relieved by a person: it leaves, and does not come back. + said(&mut out, ToSeat::Refused("relieved: a person took the seat".into())); + drop(out); + done_rx.recv_timeout(Duration::from_secs(5)).expect("the commander leaves a seat it is relieved of"); + stop.store(true, Ordering::SeqCst); + let _ = reader; +} diff --git a/domains/recreation/towers_server/src/main.rs b/domains/recreation/towers_server/src/main.rs index 7a85da5..01b77e2 100644 --- a/domains/recreation/towers_server/src/main.rs +++ b/domains/recreation/towers_server/src/main.rs @@ -1,5 +1,6 @@ //! The game server of Towers (see plans/towers.md): `cargo run --release -p -//! recreation_towers_server -- [--speedup N] [--blue computer|person] [--red computer|person]`. +//! recreation_towers_server -- [--speedup N] [--blue computer|person|nobody] [--red computer|person|nobody] +//! [--on-leave computer|wait]`. //! //! Each drone has an ArduPlane SITL of its own, flying the plane of its class (`recreation_towers::Class`: standard T1-S VTOLs, a heavy T2 Cruza and a light Seeker 3) (MAVLink system ids 1 to 10), with //! the AGENT mode and `ardupilot/towers.parm`. The SITL instances are 30 to 39 @@ -8,8 +9,21 @@ //! the drone's physics, and its radio takes the drone off at GO. Once a drone flies on the wing, the ground switches it to AGENT and flies its order by //! plan. The referee (`recreation_towers::Game`) steps with the physics. //! -//! Each team has a seat on TCP (`--seat-port`, 5955): a computer commander, or a person at the -//! commander app. A seat gets only its team's map. GO waits for the people, and for ArduPilot, +//! Each team has a seat on TCP (`--seat-port`, 5955), and the same seat for a person at the +//! commander app and for a computer commander: the team's ground station, which gets the umpire's +//! word, what its towers and base report and the way to its drones (the air), and makes its own map +//! from its drones' telemetry. The server decides and sends no plan for any team: for `computer` (the +//! default) it starts a computer commander (`recreation_towers_ground::commander`, the same as the +//! binary `towers_commander`) as a thread of its own that takes the seat over loopback TCP; for +//! `nobody` one that gives no orders. What the server does itself is the flight of the light quads +//! from the orders the seat gave, the take-off, the referee and the air. +//! +//! A seat changes hands during the game. A person who joins a team that a computer commands takes +//! the seat (the computer is relieved; the person's seat starts with the orders the drones have). When +//! a person leaves, the seat is free; a second later, with `--on-leave computer` (the default), a +//! computer commander takes it, and with `--on-leave wait` the drones fly their last orders until a +//! person comes back. `--blue person` still means that GO waits for a person. One person for each +//! team may play. A seat gets only its team's map. GO waits for the people, and for ArduPilot, //! which needs about a minute of its own time before it arms (`--warmup`). Needs the AGENT build: //! `SIM001_BIN`, else `.ardupilot-agent/build/sitl/bin`. @@ -21,8 +35,9 @@ mod world; use glam::Vec3; use recreation_games::Radio; -use recreation_towers::{Advisor, Class, Commander, Outcome, Rules, Team}; -use recreation_towers_ground as ground; +use recreation_towers::{Advisor, Class, Outcome, Rules, Team}; +use recreation_towers_ground::commander::{self, Brain}; +use recreation_towers_ground::station::Tally; use simulation_sitl::{Lockstep, launch, port_for, sensor_json}; use std::net::{TcpListener, UdpSocket}; use std::path::{Path, PathBuf}; @@ -33,8 +48,10 @@ use std::time::{Duration, Instant}; use world::World; const BASE_PORT: u16 = 9002; -/// The computer commanders decide once a second of game time. +/// The advisors look once a second of game time. const DECIDE_EVERY: f32 = 1.0; +/// A seat that a person left is free this long (s) before a computer takes it. +const GRACE: Duration = Duration::from_secs(1); /// The server stops if no drone has sent a packet after this long. const NO_PACKETS_AFTER: Duration = Duration::from_secs(60); /// The seats get the game four times a second. @@ -49,6 +66,8 @@ struct Args { people: [bool; 2], /// Teams nobody commands. nobody: [bool; 2], + /// A computer takes the seat that a person leaves. + on_leave_computer: bool, stop_with_stdin: bool, } @@ -65,6 +84,7 @@ fn args() -> Args { first_instance: arg("--first-instance").and_then(|s| s.parse().ok()).unwrap_or(30), people: [person("--blue"), person("--red")], nobody: [nobody("--blue"), nobody("--red")], + on_leave_computer: arg("--on-leave").is_none_or(|v| v != "wait"), stop_with_stdin: args.iter().any(|a| a == "--stop-with-stdin"), } } @@ -217,9 +237,32 @@ fn main() { .collect(); let air_ports: Arc> = Arc::new(air.iter().map(|c| c.port).collect()); // Clients find the seats at the port printed below (the next one up if this one is taken). + let port_cell = Arc::new(std::sync::atomic::AtomicU16::new(0)); + // The computer commanders are seats of their own, in this process, over loopback TCP. + let tally = Arc::new(Tally::default()); + let spawn_commander: Arc = { + let (port_cell, tally) = (port_cell.clone(), tally.clone()); + Arc::new(move |team, brain| { + let (addr, tally) = (format!("127.0.0.1:{}", port_cell.load(Ordering::SeqCst)), tally.clone()); + std::thread::Builder::new() + .name(format!("commander-{team:?}")) + .spawn(move || commander::run(&addr, team, brain, Some(tally), &AtomicBool::new(false))) + .expect("a commander thread"); + }) + }; + let take: Arc = { + let (spawn, on_leave) = (spawn_commander.clone(), a.on_leave_computer); + Arc::new(move |team| { + if on_leave { + spawn(team, Brain::Play); + } + }) + }; let seats = (a.seat_port..=a.seat_port.saturating_add(20)) .find_map(|port| { - let seats = seats::Seats::listen(port, world.clone(), a.people, air_ports.clone()).ok()?; + let free = seats::Free { grace: GRACE, take: take.clone() }; + let seats = seats::Seats::listen(port, world.clone(), a.people, air_ports.clone(), free).ok()?; + port_cell.store(seats.port(), Ordering::SeqCst); a.seat_port = port; Some(seats) }) @@ -249,26 +292,12 @@ fn main() { a.seat_port, a.seat_port ); - let names: Vec = (0..n).map(|i| ground::drone_name(&rules, i)).collect(); - // Light drones have no wing for AGENT to fly: they are flown by the radio, in QLOITER. - let modes: Vec = (0..n) - .map(|i| if rules.class(i % rules.drones) == Class::Light { ground::QLOITER_MODE } else { ground::AGENT_MODE }) - .collect(); - let home = ground::sitl_home(); - // The server is the ground of a team that no person commands; a person's app is its own. - let flown: Vec = (0..n).filter(|&i| !a.people[usize::from(i >= rules.drones)]).collect(); - let flown_modes: Vec = flown.iter().map(|&i| modes[i]).collect(); - let transport = - ground::Transport::Tcp { host: "127.0.0.1".into(), ports: flown.iter().map(|&i| air_ports[i]).collect() }; - let mut ground = ground::Ground::connect(&transport, &names, &flown, &flown_modes, home); // A person who commands a team has an advisor. let mut advisors: Vec<(Team, Advisor)> = Team::BOTH.into_iter().filter(|t| a.people[t.index()]).map(|t| (t, Advisor::new(rules))).collect(); - let mut commanders: Vec<(Team, Commander)> = Team::BOTH - .into_iter() - .filter(|t| !a.people[t.index()] && !a.nobody[t.index()]) - .map(|t| (t, Commander::new(rules))) - .collect(); + for t in Team::BOTH.into_iter().filter(|t| !a.people[t.index()]) { + spawn_commander(t, if a.nobody[t.index()] { Brain::Idle } else { Brain::Play }); + } let stop = Arc::new(AtomicBool::new(false)); if a.stop_with_stdin { @@ -309,18 +338,8 @@ fn main() { seats.advise(*team, &said); } } - for (team, c) in &mut commanders { - let view = w.game.view(*team); - for (k, order) in c.decide(&view) { - let i = w.game.drone(*team, k); - let low = w.low_by_default(i); - w.command(i, order, low); - } - } } } - let sent = ground.decide(&world.lock().unwrap().situation()); - ground.send(sent); if last_send.elapsed() >= SEND_EVERY { last_send = Instant::now(); let mut w = world.lock().unwrap(); @@ -356,7 +375,7 @@ fn main() { let d = &g.drones[i]; println!( " {} {:?} {:?}{} | at {:.0}, {:.0} {:.0} m, {:.0}, {:.0} m/s, heading {:.0}, roll {:.0}, airspeed {:.1}", - ground.describe(i), + tally.lines.lock().unwrap().get(i).cloned().unwrap_or_default(), d.phase, w.orders[i], if w.lows[i] { " LOW" } else { "" }, @@ -374,8 +393,8 @@ fn main() { if let Some(outcome) = w.game.outcome { w.game.look(); seats.send(&w); - let (in_agent, refused) = ground.results(); - for line in ground.verdict() { + let (in_agent, refused) = (tally.in_agent(), tally.refused()); + for line in tally.warnings() { println!("{line}"); } println!("game over: {outcome:?}, Blue {:.0} Red {:.0}", w.game.score[0], w.game.score[1]); @@ -410,7 +429,7 @@ fn main() { } if start.elapsed().as_secs() > a.timeout { eprintln!("timed out after {} s", a.timeout); - for line in ground.verdict() { + for line in tally.warnings() { eprintln!("{line}"); } drop(w); diff --git a/domains/recreation/towers_server/src/seats.rs b/domains/recreation/towers_server/src/seats.rs index 05423c1..4def790 100644 --- a/domains/recreation/towers_server/src/seats.rs +++ b/domains/recreation/towers_server/src/seats.rs @@ -1,41 +1,84 @@ -//! The seats, over TCP (`recreation_towers::seat`). A seat that commands a team is kept for a -//! person (`--blue person`), one at a time; any number may watch a team or the whole game. Each -//! gets what it may know, from the same moment of the game, four times a second. +//! The seats, over TCP (`recreation_towers::seat`). A team's seat that commands is one seat, for a +//! person (`Join::Command`) or a computer (`Join::Compute`), held by one client at a time; any number +//! may watch a team or the whole game. Each gets what it may know, from the same moment of the game, +//! four times a second. +//! +//! A seat changes hands during a game. A person who joins a team that a computer commands takes the +//! seat: the computer is told it is relieved and its connection is closed. When a person's client +//! leaves, the seat is free; if it is still free after a grace time, the server has a computer +//! commander take it (`Free::take`). A computer commander is a seat client like any other: the server +//! does not decide or send a plan for any team. use crate::world::World; use recreation_towers::seat::{self, Join, ToSeat, ToServer, Truth}; use recreation_towers::{Advice, Rules, Team}; use std::io::{BufRead, BufReader, Write}; -use std::net::{TcpListener, TcpStream}; +use std::net::{Shutdown, TcpListener, TcpStream}; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; +use std::time::Duration; struct Client { + id: u64, join: Join, stream: TcpStream, } +/// What the server does when a person's client leaves a seat. +#[derive(Clone)] +pub struct Free { + /// How long a seat may stay free before a computer takes it (a client that comes back at once, as + /// the app does after a lost connection, keeps it). + pub grace: Duration, + /// Has a computer commander take the seat of the team. + pub take: Arc, +} + pub struct Seats { clients: Arc>>, - /// The teams a person commands: their seats are not for a computer. + /// The teams a person commands: GO waits for them. people: [bool; 2], + port: u16, } /// The ports of the air in front of the drones, by drone number in the game. pub type AirPorts = Arc>; +/// Everything a seat's thread needs. +#[derive(Clone)] +struct Shared { + clients: Arc>>, + world: Arc>, + air: AirPorts, + free: Free, + next_id: Arc, +} + impl Seats { - /// Listens on `port`. `people[team]`: that team's seat is kept for a person. - pub fn listen(port: u16, world: Arc>, people: [bool; 2], air: AirPorts) -> std::io::Result { + /// Listens on `port` (0: any). `people[team]`: GO waits for a person to take that team's seat. + pub fn listen( + port: u16, + world: Arc>, + people: [bool; 2], + air: AirPorts, + free: Free, + ) -> std::io::Result { let listener = TcpListener::bind(("0.0.0.0", port))?; + let port = listener.local_addr()?.port(); let clients: Arc>> = Arc::new(Mutex::new(vec![])); - let c2 = clients.clone(); + let shared = Shared { clients: clients.clone(), world, air, free, next_id: Arc::new(AtomicU64::new(0)) }; std::thread::Builder::new().name("seats".into()).spawn(move || { for stream in listener.incoming().map_while(Result::ok) { - let (clients, world, air) = (c2.clone(), world.clone(), air.clone()); - std::thread::spawn(move || seat(stream, clients, world, people, air)); + let shared = shared.clone(); + std::thread::spawn(move || seat(stream, shared)); } })?; - Ok(Seats { clients, people }) + Ok(Seats { clients, people, port }) + } + + /// The port the seats are on. + pub fn port(&self) -> u16 { + self.port } /// Whether every team a person commands has its person. @@ -47,7 +90,7 @@ impl Seats { .all(|&t| clients.iter().any(|c| c.join == Join::Command(t))) } - /// Tells the seat that commands `team` what its advisor has to say. + /// Tells the seat of a person that commands `team` what its advisor has to say. pub fn advise(&self, team: Team, advice: &[Advice]) { let lines: Vec = advice.iter().map(|a| seat::encode(&ToSeat::Advice(a.clone()))).collect(); self.clients.lock().unwrap().retain_mut(|c| { @@ -58,8 +101,9 @@ impl Seats { /// Sends every seat what it may know of the game now; drops the seats that have gone. pub fn send(&self, world: &World) { let game = &world.game; - // A seat that watches a team gets its whole map. One that commands is the team's ground station - // and makes its own: it gets the umpire's word and what the team's towers and base report. + // A seat that watches a team gets its whole map. One that commands, a person's or a computer's, + // is the team's ground station and makes its own: it gets the umpire's word and what the team's + // towers and base report. let views = [game.view(Team::Blue), game.view(Team::Red)].map(|mut v| { for d in &mut v.drones { let i = game.drone(v.team, d.k); @@ -73,7 +117,7 @@ impl Seats { truth.attitudes = world.planes.iter().map(|p| p.rot.to_array()).collect(); let truth = seat::encode(&ToSeat::Truth(truth)); self.clients.lock().unwrap().retain_mut(|c| match c.join { - Join::Command(t) => { + Join::Command(t) | Join::Compute(t) => { writeln!(c.stream, "{umpire}").is_ok() && writeln!(c.stream, "{}", sites[t.index()]).is_ok() } Join::Watch(t) => writeln!(c.stream, "{}", views[t.index()]).is_ok(), @@ -83,13 +127,8 @@ impl Seats { } /// One seat's connection: what it is, then its orders. -fn seat( - stream: TcpStream, - clients: Arc>>, - world: Arc>, - people: [bool; 2], - air: AirPorts, -) { +fn seat(stream: TcpStream, shared: Shared) { + let Shared { clients, world, air, free, next_id } = shared; let Ok(mut out) = stream.try_clone() else { return }; let mut lines = BufReader::new(stream).lines().map_while(Result::ok); let Some(join) = lines.next().and_then(|l| seat::decode::(&l).ok()) else { @@ -97,30 +136,51 @@ fn seat( return; }; let ToServer::Join(join) = join else { return }; - let rules: Rules = world.lock().unwrap().game.rules; + let (rules, told): (Rules, Vec<_>) = { + let w = world.lock().unwrap(); + (w.game.rules, w.told.clone()) + }; + let id = next_id.fetch_add(1, Ordering::SeqCst); { let mut clients = clients.lock().unwrap(); - let refusal = match join { - Join::Command(t) if !people[t.index()] => Some(format!("{t:?} is commanded by the computer")), - Join::Command(t) if clients.iter().any(|c| c.join == join) => Some(format!("{t:?} has its commander")), - _ => None, - }; - if let Some(why) = refusal { - let _ = writeln!(out, "{}", seat::encode(&ToSeat::Refused(why))); - return; + if let Some(team) = join.commands() { + let holder = clients.iter().position(|c| c.join.commands() == Some(team)); + match (join, holder.map(|h| clients[h].join)) { + // A person takes the seat from a computer: the computer is relieved. + (Join::Command(_), Some(Join::Compute(_))) => { + let old = clients.remove(holder.expect("a holder")); + let mut s = &old.stream; + let _ = + writeln!(s, "{}", seat::encode(&ToSeat::Refused("relieved: a person took the seat".into()))); + let _ = old.stream.shutdown(Shutdown::Both); + } + (_, Some(_)) => { + let why = format!("{team:?} has its commander"); + let _ = writeln!(out, "{}", seat::encode(&ToSeat::Refused(why))); + return; + } + (_, None) => {} + } } let Ok(stream) = out.try_clone() else { return }; - // A seat that commands is its team's ground station: it gets the way to its drones. - let ports = match join { - Join::Command(t) => air[t.index() * rules.drones..(t.index() + 1) * rules.drones].to_vec(), - _ => vec![], + // A seat that commands is its team's ground station: it gets the way to its drones, and the + // orders they have. + let ports = match join.commands() { + Some(t) => air[t.index() * rules.drones..(t.index() + 1) * rules.drones].to_vec(), + None => vec![], }; if writeln!(out, "{}", seat::encode(&ToSeat::Welcome { join, rules, air: ports })).is_err() { return; } - clients.push(Client { join, stream }); + if let Some(t) = join.commands() { + let orders: Vec<_> = (0..rules.drones).map(|k| told[t.index() * rules.drones + k]).collect(); + if writeln!(out, "{}", seat::encode(&ToSeat::Orders(orders))).is_err() { + return; + } + } + clients.push(Client { id, join, stream }); } - let Join::Command(team) = join else { return }; + let Some(team) = join.commands() else { return }; for line in lines { if let Ok(ToServer::Order { drone, order, low, standing }) = seat::decode::(&line) && drone < rules.drones @@ -131,6 +191,226 @@ fn seat( w.command_standing(i, order, low, standing); } } - // The commander has gone: its seat is free again. - clients.lock().unwrap().retain(|c| c.join != join); + // The commander has gone: its seat is free again, unless it was relieved (it is not ours then). + let was_ours = { + let mut clients = clients.lock().unwrap(); + let n = clients.len(); + clients.retain(|c| c.id != id); + clients.len() < n + }; + if was_ours && matches!(join, Join::Command(_)) { + std::thread::sleep(free.grace); + let still_free = !clients.lock().unwrap().iter().any(|c| c.join.commands() == Some(team)); + let started = world.lock().unwrap().started(); + if still_free && started { + (free.take)(team); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use glam::Vec2; + use recreation_towers::{Order, Phase}; + use recreation_towers_ground::map; + use std::collections::BTreeSet; + use std::sync::mpsc::{Receiver, channel}; + use std::time::Instant; + + struct Client { + got: Arc>>, + out: TcpStream, + } + + impl Client { + fn join(port: u16, join: Join) -> Client { + let out = TcpStream::connect(("127.0.0.1", port)).unwrap(); + let mut w = out.try_clone().unwrap(); + writeln!(w, "{}", seat::encode(&ToServer::Join(join))).unwrap(); + let got = Arc::new(Mutex::new(vec![])); + let g = got.clone(); + let r = out.try_clone().unwrap(); + std::thread::spawn(move || { + for line in BufReader::new(r).lines().map_while(Result::ok) { + if let Ok(m) = seat::decode::(&line) { + g.lock().unwrap().push(m); + } + } + }); + Client { got, out } + } + + fn wait(&self, what: &str, f: impl Fn(&[ToSeat]) -> bool) { + let end = Instant::now() + Duration::from_secs(5); + while !f(&self.got.lock().unwrap()) { + assert!(Instant::now() < end, "timed out waiting for {what}: {:?}", self.got.lock().unwrap()); + std::thread::sleep(Duration::from_millis(10)); + } + } + + fn kinds(&self) -> BTreeSet<&'static str> { + self.got + .lock() + .unwrap() + .iter() + .map(|m| match m { + ToSeat::Welcome { .. } => "Welcome", + ToSeat::Refused(_) => "Refused", + ToSeat::View(_) => "View", + ToSeat::Umpire(_) => "Umpire", + ToSeat::Sites(_) => "Sites", + ToSeat::Orders(_) => "Orders", + ToSeat::Truth(_) => "Truth", + ToSeat::Advice(_) => "Advice", + }) + .collect() + } + + fn leave(self) { + let _ = self.out.shutdown(Shutdown::Both); + } + } + + fn server(people: [bool; 2], grace_ms: u64) -> (Seats, Arc>, Receiver) { + let rules = Rules::default(); + // GO is now: the game has started. + let world = Arc::new(Mutex::new(World::new(rules, 0.0))); + let (tx, rx) = channel(); + let tx = Mutex::new(tx); + let free = Free { + grace: Duration::from_millis(grace_ms), + take: Arc::new(move |t| { + let _ = tx.lock().unwrap().send(t); + }), + }; + let air = Arc::new((0..2 * rules.drones as u16).map(|i| 7000 + i).collect()); + let seats = Seats::listen(0, world.clone(), people, air, free).unwrap(); + (seats, world, rx) + } + + fn tick(seats: &Seats, world: &Arc>) { + let mut w = world.lock().unwrap(); + w.game.look(); + seats.send(&w); + } + + #[test] + fn a_computers_seat_gets_the_same_kind_of_messages_as_a_persons_and_a_map_from_telemetry_only() { + let (seats, world, _) = server([true, false], 50); + let rules = world.lock().unwrap().game.rules; + let n = rules.drones; + // Red's scout is out in the middle of the field, where Blue sees nothing. + { + let mut w = world.lock().unwrap(); + let d = &mut w.game.drones[n]; + (d.at, d.alt, d.phase) = (Vec2::new(0.0, 0.0), 60.0, Phase::Flying); + w.game.look(); + } + let person = Client::join(seats.port(), Join::Command(Team::Blue)); + let computer = Client::join(seats.port(), Join::Compute(Team::Red)); + let spectator = Client::join(seats.port(), Join::Spectate); + let blue_watcher = Client::join(seats.port(), Join::Watch(Team::Blue)); + for c in [&person, &computer, &spectator, &blue_watcher] { + c.wait("a welcome", |m| m.iter().any(|m| matches!(m, ToSeat::Welcome { .. }))); + } + tick(&seats, &world); + for c in [&person, &computer] { + c.wait("the umpire and the sites", |m| { + m.iter().any(|m| matches!(m, ToSeat::Umpire(_))) && m.iter().any(|m| matches!(m, ToSeat::Sites(_))) + }); + } + spectator.wait("the truth", |m| m.iter().any(|m| matches!(m, ToSeat::Truth(_)))); + assert_eq!(person.kinds(), computer.kinds(), "the same kind of messages"); + assert_eq!(person.kinds(), ["Orders", "Sites", "Umpire", "Welcome"].into()); + // The map of Blue's seat, made from what it got and no telemetry, has no enemy in it. + let (umpire, sites) = { + let got = person.got.lock().unwrap(); + ( + got.iter().find_map(|m| if let ToSeat::Umpire(u) = m { Some(*u) } else { None }).unwrap(), + got.iter().find_map(|m| if let ToSeat::Sites(s) = m { Some(s.clone()) } else { None }).unwrap(), + ) + }; + let view = map::build(&umpire, &sites, &vec![None; n], &[], &[], &rules); + let truth = spectator.got.lock().unwrap().iter().rev().find_map(|m| match m { + ToSeat::Truth(t) => Some(t.clone()), + _ => None, + }); + let truth = truth.unwrap(); + assert_eq!(truth.drones[n].phase, Phase::Flying, "the spectator sees Red's scout in the open"); + assert!(view.enemies.iter().all(|e| !e.seen), "the seat's map does not have it: {:?}", view.enemies); + assert!(!blue_watcher.kinds().contains("Truth")); + } + + #[test] + fn a_person_takes_the_seat_of_a_computer_and_goes_on_from_its_orders() { + let (seats, world, _) = server([false, false], 50); + let computer = Client::join(seats.port(), Join::Compute(Team::Blue)); + computer.wait("a welcome", |m| matches!(m.first(), Some(ToSeat::Welcome { .. }))); + let mut told = world.lock().unwrap().told.clone(); + told[1] = (Some(Order::Hold), true); + world.lock().unwrap().told = told; + let person = Client::join(seats.port(), Join::Command(Team::Blue)); + person.wait("the orders", |m| m.iter().any(|m| matches!(m, ToSeat::Orders(_)))); + let orders = person + .got + .lock() + .unwrap() + .iter() + .find_map(|m| if let ToSeat::Orders(o) = m { Some(o.clone()) } else { None }); + assert_eq!(orders.unwrap()[1], (Some(Order::Hold), true)); + computer + .wait("to be relieved", |m| m.iter().any(|m| matches!(m, ToSeat::Refused(w) if w.contains("relieved")))); + // The person has the seat now: another person is refused, and a computer waits. + let second = Client::join(seats.port(), Join::Command(Team::Blue)); + second.wait("a refusal", |m| { + m.iter().any(|m| matches!(m, ToSeat::Refused(w) if w.contains("has its commander"))) + }); + let late = Client::join(seats.port(), Join::Compute(Team::Blue)); + late.wait("a refusal", |m| { + m.iter().any(|m| matches!(m, ToSeat::Refused(w) if w.contains("has its commander"))) + }); + } + + #[test] + fn the_computer_takes_a_seat_a_person_leaves_unless_the_person_comes_back_at_once() { + let (seats, _world, rx) = server([true, false], 150); + let person = Client::join(seats.port(), Join::Command(Team::Blue)); + person.wait("a welcome", |m| matches!(m.first(), Some(ToSeat::Welcome { .. }))); + assert!(seats.people_seated()); + person.leave(); + // Back inside the grace time: the seat is not given to the computer. + std::thread::sleep(Duration::from_millis(30)); + let again = Client::join(seats.port(), Join::Command(Team::Blue)); + again.wait("a welcome", |m| matches!(m.first(), Some(ToSeat::Welcome { .. }))); + assert!(rx.recv_timeout(Duration::from_millis(400)).is_err(), "the person came back"); + again.leave(); + assert_eq!(rx.recv_timeout(Duration::from_secs(3)), Ok(Team::Blue), "the seat is free: a computer takes it"); + assert!(!seats.people_seated()); + } + + #[test] + fn two_people_one_for_each_team_can_join_the_same_server() { + let (seats, world, _) = server([true, true], 50); + let rules = world.lock().unwrap().game.rules; + let blue = Client::join(seats.port(), Join::Command(Team::Blue)); + let red = Client::join(seats.port(), Join::Command(Team::Red)); + for c in [&blue, &red] { + c.wait("a welcome", |m| m.iter().any(|m| matches!(m, ToSeat::Welcome { .. }))); + } + let air_of = |c: &Client| { + c.got + .lock() + .unwrap() + .iter() + .find_map(|m| if let ToSeat::Welcome { air, .. } = m { Some(air.clone()) } else { None }) + .unwrap() + }; + assert_eq!(air_of(&blue), (7000..7000 + rules.drones as u16).collect::>()); + assert_eq!(air_of(&red), (7000 + rules.drones as u16..7000 + 2 * rules.drones as u16).collect::>()); + tick(&seats, &world); + blue.wait("its sites", |m| m.iter().any(|m| matches!(m, ToSeat::Sites(s) if s.team == Team::Blue))); + red.wait("its sites", |m| m.iter().any(|m| matches!(m, ToSeat::Sites(s) if s.team == Team::Red))); + assert!(seats.people_seated()); + } } diff --git a/domains/recreation/towers_server/src/world.rs b/domains/recreation/towers_server/src/world.rs index f0b4bc4..63f748d 100644 --- a/domains/recreation/towers_server/src/world.rs +++ b/domains/recreation/towers_server/src/world.rs @@ -207,10 +207,6 @@ impl World { self.now() >= self.go } - pub fn on_the_wing(&self, i: usize) -> bool { - self.preflight[i].on_the_wing() && airborne(&self.planes[i]) - } - /// One physics step of drone `i` from its autopilot's outputs, then the referee (by drone 0). /// Returns what its radio sends. pub fn step_drone(&mut self, i: usize, pwm: &[u16; 16], dt: f32) -> Radio { @@ -259,6 +255,7 @@ impl World { /// The team gives drone `i` an order. The team does not know whether it is heard: the order is /// the team's, and the air carries it to the drone, or drops it (see `air`). + #[cfg(test)] pub fn command(&mut self, i: usize, order: Order, low: bool) { self.command_standing(i, order, low, None) } @@ -296,18 +293,6 @@ impl World { .collect() } - /// What the ground sees of the game: all of it. - pub fn situation(&self) -> recreation_towers_ground::Situation<'_> { - recreation_towers_ground::Situation { - now: self.now(), - game: &self.game, - orders: &self.orders, - lows: &self.lows, - standing: &self.standing, - wing: (0..self.orders.len()).map(|i| self.on_the_wing(i)).collect(), - } - } - /// The drone has no wing for the on-board executor to fly (a quadcopter), so its order is flown from /// here by its radio, and the standing order is carried out here too. pub fn flown_from_here(&self, i: usize) -> bool { @@ -349,6 +334,26 @@ impl World { self.crashes += 1; let (at, alt) = to_field(self.planes[i].pos); println!("crash: drone {i}: {} at {at} (north, east), {alt:.1} m", self.planes[i].crash_reason); + // What it was doing, and who was near: to tell a mid-air from a tree, a drone in a + // shadow from one in contact. + let here = glam::Vec3::new(at.x, at.y, alt); + let near = (0..self.game.drones.len()) + .filter(|&j| j != i && self.game.drones[j].phase == Referee::Flying) + .map(|j| { + let d = &self.game.drones[j]; + (j, glam::Vec3::new(d.at.x, d.at.y, d.alt).distance(here)) + }) + .min_by(|a, b| a.1.total_cmp(&b.1)); + println!( + "crash: drone {i}: t {:.0}s order {:?}{} speed {:.1} m/s, jammed {}, silent {:.1}s, nearest flying drone {}", + self.game.time, + self.orders[i], + if self.lows[i] { " LOW" } else { " HIGH" }, + self.planes[i].vel.length(), + self.game.jammed[i], + self.game.silent[i], + near.map_or("none".into(), |(j, d)| format!("{j} at {d:.0} m")), + ); } self.down_for[i] += dt; if self.down_for[i] > RESPAWN { diff --git a/domains/teleoperation/command/src/main.rs b/domains/teleoperation/command/src/main.rs index fa888bb..039804b 100644 --- a/domains/teleoperation/command/src/main.rs +++ b/domains/teleoperation/command/src/main.rs @@ -117,7 +117,7 @@ fn main() { let seat = seat::Seat::connect(&addr, join); // A seat that commands is the team's ground station: it flies the team's drones itself. if matches!(join, Join::Command(_)) { - station::start(seat.clone(), addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string()); + station::start(seat.clone(), addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(), None); } // The pictures from the team's drones: the 3D view of the game is on the server's machine. let host = addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(); diff --git a/domains/teleoperation/command/src/seat.rs b/domains/teleoperation/command/src/seat.rs index 9a8e95f..d74da63 100644 --- a/domains/teleoperation/command/src/seat.rs +++ b/domains/teleoperation/command/src/seat.rs @@ -1,208 +1,2 @@ -//! The app's seat on the game server: a TCP connection on threads of its own. It keeps the last -//! map the server sent and sends the orders the commander gives. If the connection is lost, or -//! the server is not up yet, it tries again. - -use recreation_towers::seat::{self, Join, ToSeat, ToServer}; -use recreation_towers::{Advice, Order, Rules, Sites, TeamView, Umpire}; -use std::io::{BufRead, BufReader, Write}; -use std::net::TcpStream; -use std::sync::atomic::AtomicUsize; -use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::mpsc::{Receiver, Sender, channel}; -use std::sync::{Arc, Mutex, MutexGuard}; -use std::time::{Duration, Instant}; - -#[derive(Clone, Debug, PartialEq)] -pub enum Status { - Connecting, - Seated(Join), - Refused(String), -} - -#[derive(Debug)] -pub struct State { - pub status: Status, - pub rules: Option, - pub view: Option, - /// What the server last said: the umpire's word, and what the team's towers and base report. The - /// ground station makes `view` from them and from the telemetry of its drones (station.rs). - pub umpire: Option, - pub sites: Option, - /// How many maps of the team the server sent a seat that commands (it sends none). - pub maps: usize, - /// The ports of the air in front of the team's drones, on the server's host: the way to them. - pub air: Vec, - /// The orders given, by drone number in the team: the order and whether it is flown low. - pub orders: Vec<(Option, bool)>, - /// What the advisor said lately, and when (the last few). - pub advice: Vec<(Advice, Instant)>, -} - -/// How many pieces of advice the seat keeps. -const ADVICE_KEPT: usize = 3; - -pub struct Seat { - addr: String, - join: Join, - state: Arc>, - orders: Mutex>, - /// Each (re)connection's number: a connection that is not the newest stops. - generation: Arc, - /// The drones that the app's own ground has put in AGENT mode (station.rs). - pub flown: AtomicUsize, - /// How many position reports the app has heard from its drones. - pub heard: AtomicUsize, -} - -impl Seat { - pub fn connect(addr: &str, join: Join) -> Arc { - let (tx, _) = channel(); - let seat = Arc::new(Seat { - addr: addr.to_string(), - join, - state: Arc::new(Mutex::new(State { - status: Status::Connecting, - rules: None, - view: None, - umpire: None, - sites: None, - maps: 0, - air: vec![], - orders: vec![], - advice: vec![], - })), - orders: Mutex::new(tx), - generation: Arc::new(AtomicU64::new(0)), - flown: AtomicUsize::new(0), - heard: AtomicUsize::new(0), - }); - seat.reconnect(); - seat - } - - pub fn join(&self) -> Join { - self.join - } - - pub fn state(&self) -> MutexGuard<'_, State> { - self.state.lock().unwrap() - } - - /// Sends an order for the team's drone `drone`, flown `low` (along the lanes) or high. - pub fn order(&self, drone: usize, order: Order, low: bool) { - // The app is the team's ground station: the order is its own to carry out (station.rs). - { - let mut s = self.state(); - if s.orders.len() <= drone { - s.orders.resize(drone + 1, (None, false)); - } - s.orders[drone] = (Some(order), low); - } - let _ = self.orders.lock().unwrap().send(ToServer::Order { drone, order, low, standing: None }); - } - - /// Stops connecting: the connection that is open ends with its server, and is not made - /// again. (Before the server stops, so that it does not take the seat of the next game first.) - pub fn hang_up(&self) { - self.generation.fetch_add(1, Ordering::SeqCst); - } - - /// Forgets the game and connects again (to a new game on the same server address). - pub fn reconnect(&self) { - let generation = self.generation.fetch_add(1, Ordering::SeqCst) + 1; - *self.state() = State { - status: Status::Connecting, - rules: None, - view: None, - umpire: None, - sites: None, - maps: 0, - air: vec![], - orders: vec![], - advice: vec![], - }; - let (tx, rx) = channel(); - *self.orders.lock().unwrap() = tx; - let (addr, join, state, current) = (self.addr.clone(), self.join, self.state.clone(), self.generation.clone()); - std::thread::spawn(move || run(&addr, join, &state, rx, ¤t, generation)); - } -} - -/// One connection, for as long as it is the newest: connects (again, if it is lost), takes the -/// seat, and reads the game. The orders go out on a thread of their own, to whichever stream is -/// open. -fn run(addr: &str, join: Join, state: &Mutex, orders: Receiver, current: &AtomicU64, generation: u64) { - let out: Arc>> = Arc::new(Mutex::new(None)); - let writer = out.clone(); - std::thread::spawn(move || { - while let Ok(msg) = orders.recv() { - if let Some(stream) = writer.lock().unwrap().as_mut() { - let _ = writeln!(stream, "{}", seat::encode(&msg)); - } - } - }); - while current.load(Ordering::SeqCst) == generation { - let Ok(stream) = TcpStream::connect(addr) else { - std::thread::sleep(Duration::from_millis(300)); - continue; - }; - let Ok(mut w) = stream.try_clone() else { continue }; - if writeln!(w, "{}", seat::encode(&ToServer::Join(join))).is_err() { - continue; - } - *out.lock().unwrap() = Some(w); - for line in BufReader::new(stream).lines().map_while(Result::ok) { - if current.load(Ordering::SeqCst) != generation { - if let Some(s) = out.lock().unwrap().take() { - let _ = s.shutdown(std::net::Shutdown::Both); - } - return; - } - let Ok(msg) = seat::decode::(&line) else { continue }; - let mut s = state.lock().unwrap(); - match msg { - ToSeat::Welcome { join, rules, air } => { - s.status = Status::Seated(join); - s.rules = Some(rules); - s.air = air; - } - ToSeat::Refused(why) => s.status = Status::Refused(why), - ToSeat::View(v) => { - if matches!(join, Join::Command(_)) { - s.maps += 1; - } - s.view = Some(v); - } - ToSeat::Umpire(u) => s.umpire = Some(u), - ToSeat::Sites(sites) => s.sites = Some(sites), - ToSeat::Truth(_) => {} - ToSeat::Advice(a) => { - s.advice.push((a, Instant::now())); - let extra = s.advice.len().saturating_sub(ADVICE_KEPT); - s.advice.drain(..extra); - } - } - } - *out.lock().unwrap() = None; - // Hung up, or a newer connection: the state is the newer one's. - if current.load(Ordering::SeqCst) != generation { - return; - } - // The seat is refused for good if the computer commands the team. If a commander holds it, - // that is this app's own connection from before a new game: it lets go in a moment. - let refused = match &state.lock().unwrap().status { - Status::Refused(why) => Some(why.contains("has its commander")), - _ => None, - }; - match refused { - Some(false) => return, - Some(true) => { - std::thread::sleep(Duration::from_millis(400)); - continue; - } - None => {} - } - state.lock().unwrap().status = Status::Connecting; - std::thread::sleep(Duration::from_millis(300)); - } -} +//! The app's seat on the game server: the library's (`recreation_towers_ground::seat`). +pub use recreation_towers_ground::seat::*; diff --git a/domains/teleoperation/command/src/station.rs b/domains/teleoperation/command/src/station.rs index 13eba4f..0dec5a3 100644 --- a/domains/teleoperation/command/src/station.rs +++ b/domains/teleoperation/command/src/station.rs @@ -1,182 +1,2 @@ -//! The app as the ground station of its team (plans/unified-link.md): it puts its drones in AGENT -//! mode and sends each one its plan, its standing order and the traffic as MAVLink, through the air -//! in front of the drone, as the ground station of a real drone does. The game server only plays -//! the world; the app tells the drones what to do from what its team knows (the map) and from the -//! orders that the commander gave. - -use crate::seat::{Seat, Status}; -use glam::Vec2; -use recreation_towers::seat::Join; -use recreation_towers::{Game, Order, Phase, Rules}; -use recreation_towers_ground::map::{self, OwnReport, Seen}; -use recreation_towers_ground::{AGENT_MODE, Ground, QLOITER_MODE, Situation, Transport, drone_name, sitl_home}; -use std::sync::Arc; -use std::sync::atomic::Ordering; -use std::time::{Duration, Instant}; - -/// How often the ground looks (wall time). -const LOOK_EVERY: Duration = Duration::from_millis(20); -/// ArduPlane's FBWA, the mode of a drone that has taken off and waits for AGENT. -const FBWA_MODE: u32 = 5; - -/// The drones' time as the app can know it: the time of the last map, and the rate at which that time -/// runs (a game sped up runs ahead of the clock on the wall). -struct Clock { - map_time: f32, - at: Instant, - rate: f64, - last: f64, -} - -impl Clock { - fn new() -> Clock { - Clock { map_time: f32::NEG_INFINITY, at: Instant::now(), rate: 1.0, last: 0.0 } - } - - /// Game seconds to a wall second. - fn rate(&self) -> f64 { - self.rate - } - - /// The time now, given the time of the latest map. - fn now(&mut self, map_time: f32) -> f64 { - if map_time != self.map_time { - let wall = self.at.elapsed().as_secs_f64(); - if self.map_time.is_finite() && wall > 0.05 && map_time > self.map_time { - let rate = (map_time - self.map_time) as f64 / wall; - self.rate = (0.7 * self.rate + 0.3 * rate).clamp(0.5, 20.0); - } - (self.map_time, self.at) = (map_time, Instant::now()); - } - // Never ahead of the next map by more than a second, and never back. - let now = self.map_time as f64 + (self.at.elapsed().as_secs_f64() * self.rate).min(1.0); - self.last = self.last.max(now); - self.last - } -} - -/// Starts the ground of the app's team, on a thread of its own, for as long as the seat commands. -pub fn start(seat: Arc, host: String) { - std::thread::Builder::new().name("station".into()).spawn(move || run(&seat, &host)).expect("the station thread"); -} - -/// What the ground flies: its connection, for one set of air ports. -struct Flying { - air: Vec, - ground: Ground, - game: Game, - rules: Rules, - first: usize, - clock: Clock, - home: (f64, f64, f32), -} - -fn run(seat: &Seat, host: &str) { - let mut flying: Option = None; - loop { - std::thread::sleep(LOOK_EVERY); - let (status, rules, umpire, sites, air, orders) = { - let s = seat.state(); - (s.status.clone(), s.rules, s.umpire, s.sites.clone(), s.air.clone(), s.orders.clone()) - }; - let (Status::Seated(Join::Command(team)), Some(rules), Some(umpire), Some(sites)) = - (status, rules, umpire, sites) - else { - flying = None; - seat.flown.store(0, Ordering::SeqCst); - continue; - }; - if air.len() != rules.drones { - continue; - } - if flying.as_ref().is_none_or(|f| f.air != air) { - // A new game, or the first look: connect to the drones through the air. - let n = rules.drones; - let first = team.index() * n; - let names: Vec = (0..2 * n).map(|i| drone_name(&rules, i)).collect(); - let indices: Vec = (first..first + n).collect(); - // Light drones have no wing for AGENT to fly: the world's radio flies them, in QLOITER. - let modes: Vec = (0..n) - .map(|k| if rules.class(k) == recreation_towers::Class::Light { QLOITER_MODE } else { AGENT_MODE }) - .collect(); - let transport = Transport::Tcp { host: host.to_string(), ports: air.clone() }; - let ground = Ground::connect(&transport, &names, &indices, &modes, sitl_home()); - flying = Some(Flying { - air, - ground, - game: Game::new(rules), - rules, - first, - clock: Clock::new(), - home: sitl_home(), - }); - } - let f = flying.as_mut().expect("a ground"); - let n = f.rules.drones; - // The map: the umpire's word and the reports of the sites, plus what the drones report of - // themselves and of the enemy in their telemetry. - let rate = f.clock.rate() as f32; - let (mut own, mut seen) = (vec![None; n], vec![]); - for (k, report) in own.iter_mut().enumerate() { - let (position, sightings) = f.ground.telemetry(k); - *report = position.map(|p| { - let (north, east) = cognition_plan::to_local(f.home.0, f.home.1, p.lat, p.lon); - OwnReport { - at: Vec2::new(north, east), - alt: p.alt, - vel: Vec2::new(p.vel[0], p.vel[1]), - age: p.heard.elapsed().as_secs_f32() * rate, - } - }); - seen.extend(sightings.iter().map(|s| { - let (north, east) = cognition_plan::to_local(f.home.0, f.home.1, s.lat, s.lon); - Seen { - enemy: s.enemy, - at: Vec2::new(north, east), - alt: s.alt_amsl - f.home.2, - vel: Vec2::new(s.speed * s.heading.to_radians().cos(), s.speed * s.heading.to_radians().sin()), - age: s.heard.elapsed().as_secs_f32() * rate, - } - })); - } - let view = map::build(&umpire, &sites, &own, &seen, &orders, &f.rules); - // The picture the ground plans from is the map: the drones where the team has them. - for d in &view.drones { - let g = &mut f.game.drones[f.first + d.k]; - (g.at, g.alt, g.vel, g.phase) = (d.at, d.alt, d.vel, d.phase); - } - let enemy_first = if team.index() == 0 { n } else { 0 }; - for g in &mut f.game.drones[enemy_first..enemy_first + n] { - g.phase = Phase::Ground; - } - for e in view.enemies.iter().filter(|e| e.seen) { - let g = &mut f.game.drones[enemy_first + e.k]; - (g.at, g.alt, g.vel, g.phase) = (e.at, e.alt, e.vel, Phase::Flying); - } - let mut given = vec![(Some(Order::Home), false); 2 * n]; - for (k, o) in orders.iter().enumerate().take(n) { - if o.0.is_some() { - given[f.first + k] = *o; - } - } - let wing: Vec = (0..2 * n) - .map(|i| { - let Some(k) = i.checked_sub(f.first).filter(|&k| k < n) else { return false }; - let flying = view.drones[k].phase == Phase::Flying; - flying && f.ground.mode_of(k).is_some_and(|m| [FBWA_MODE, AGENT_MODE, QLOITER_MODE].contains(&m)) - }) - .collect(); - let situation = Situation { - now: f.clock.now(view.time), - game: &f.game, - orders: &given.iter().map(|o| o.0).collect::>(), - lows: &given.iter().map(|o| o.1).collect::>(), - standing: &vec![None; 2 * n], - wing, - }; - f.ground.tick(&situation); - seat.flown.store(f.ground.agent_drones(), Ordering::SeqCst); - seat.heard.store(f.ground.position_reports() as usize, Ordering::SeqCst); - seat.state().view = Some(view); - } -} +//! The app as the ground station of its team: the library's (`recreation_towers_ground::station`). +pub use recreation_towers_ground::station::*; diff --git a/domains/teleoperation/command/src/ui.rs b/domains/teleoperation/command/src/ui.rs index 1418871..695f208 100644 --- a/domains/teleoperation/command/src/ui.rs +++ b/domains/teleoperation/command/src/ui.rs @@ -356,7 +356,7 @@ impl CommanderView { fn place_eye(&mut self) { let Some(rules) = self.seat.state().rules else { return }; let team = match self.seat.join() { - Join::Command(t) | Join::Watch(t) => t, + Join::Command(t) | Join::Compute(t) | Join::Watch(t) => t, Join::Spectate => Team::Blue, }; self.rig.limit = rules.field_radius * 1.3; @@ -672,13 +672,13 @@ impl Render for CommanderView { }; let join = self.seat.join(); let team = match join { - Join::Command(t) | Join::Watch(t) => Some(t), + Join::Command(t) | Join::Compute(t) | Join::Watch(t) => Some(t), Join::Spectate => None, }; let seat_text = match &status { Status::Connecting => "CONNECTING TO THE GAME SERVER...".to_string(), Status::Refused(why) => format!("REFUSED: {why}"), - Status::Seated(Join::Command(t)) => format!("{} COMMANDER", team_name(*t)), + Status::Seated(Join::Command(t) | Join::Compute(t)) => format!("{} COMMANDER", team_name(*t)), Status::Seated(Join::Watch(t)) => format!("WATCHING {}", team_name(*t)), Status::Seated(Join::Spectate) => "SPECTATOR".into(), }; diff --git a/expectations/09-towers/arduplane/01-arduplane.expect b/expectations/09-towers/arduplane/01-arduplane.expect index 3063409..404efbc 100644 --- a/expectations/09-towers/arduplane/01-arduplane.expect +++ b/expectations/09-towers/arduplane/01-arduplane.expect @@ -9,9 +9,17 @@ Feature: Playing Towers with ArduPilot drones for its order about once each second. To take a tower, a drone circles it 40 m out, inside its circle. - Each team has a [[seat]]. A computer commander or a person at the commander app sits in it. - A seat gets only the map of its team, four times each second. Thus the fog is the same for a - person and for a computer. Other seats can watch a team, or the whole game. + Each team has a [[seat]]. A computer commander or a person at the commander app sits in it: the + same seat, with the same messages. The seat is the team's ground station. The server tells it + the umpire's word and what the team's towers and base report, and the seat makes its own map + from what its drones report in their telemetry. Thus the fog is the same for a person and for a + computer: a computer commander knows of an enemy only what a person would. The server decides + and sends no plan for any team. Other seats can watch a team, or the whole game. + + The seat can change hands during a game. A person who joins a team that a computer commands takes + the seat, and goes on from the orders that the drones have. When a person leaves, a computer + commander takes the seat. One person for each team can play. (The server's own tests check this + with seat clients and no ArduPilot: `cargo test --release -p recreation_towers_server`.) Expectation: Every drone flies the game in AGENT mode When two computer teams play a game of Towers diff --git a/expectations/09-towers/arduplane/section.expect b/expectations/09-towers/arduplane/section.expect index 2722007..8bf0e80 100644 --- a/expectations/09-towers/arduplane/section.expect +++ b/expectations/09-towers/arduplane/section.expect @@ -3,6 +3,6 @@ Section: The game with ArduPlane drones # The server's own report, and a game sped up: no counterpart in the field. On: sim - Launch: start the Towers game server. From the top folder of the repository, run `cargo run --release -p recreation_towers_server -- --speedup 10`. The server starts ten ArduPlane SITL drones, and a computer commander for each team + Launch: start the Towers game server. From the top folder of the repository, run `cargo run --release -p recreation_towers_server -- --speedup 10`. The server starts ten ArduPlane SITL drones, and a computer commander for each team that takes the team's seat The game server flies a whole game with real ArduPlane flight controllers. Its report at the end tells you how the drones flew and what each team took. diff --git a/plans/actors.md b/plans/actors.md index ee560e6..15e0b34 100644 --- a/plans/actors.md +++ b/plans/actors.md @@ -92,6 +92,53 @@ server. 7. **The field.** The open items of `plans/unified-link.md`: the bench, a serial radio per drone, the field umpire. Then real and simulated drones in one game. +Results of step 3 (branch `actors-seats`): + +- `recreation_towers_ground` now holds the seat client (`seat`), the station (`station`: its own + `Ground` over the air ports, the map from telemetry) and the computer commander (`commander::run`, + binary `towers_commander --team blue|red [--host H] [--port P] [--idle]`). The commander app uses + the same `seat` and `station`. +- The server no longer decides or sends a plan for any team: its in-process `commanders` and its + truth-based `Ground` are gone. For a computer (or `nobody`) team it starts `commander::run` in a + thread that connects over loopback as `Join::Compute`; it keeps the light quads' sticks, take-off, + the referee and the air. +- Handover: a `Join::Command` (person) relieves a `Join::Compute` holder (`Refused("relieved...")` + and the connection is closed); the server sends `ToSeat::Orders` once after every welcome of a + seat that commands, and the new commander starts from them. When a person leaves, a computer takes + the seat after 1 s (`--on-leave computer`, default; `--on-leave wait` does not). +- Gate A: tests in `towers_server/src/seats.rs` (same messages for both seats; a Red scout in the + open is in the spectator's truth and not on the map built from a seat's messages; takeover; the + computer takes a freed seat; two people) and `towers_ground/tests/commander.rs` (a commander + against a fake server gives orders from the umpire's word and sites alone, leaves when relieved). +- Crashes on SITL (`towers_server --speedup 10 --timeout 200`, one game at a time; crashes per game, + 4 games each, batches interleaved with plain `actors-plan`; the server now prints a second line + for each crash with the time, order, low or high, jammed, silent time and the nearest flying drone): + + | Batch | Path | Crashes per game | Total | + | --- | --- | --- | --- | + | 1 | new, first version | 1, 2, 3, 3 | 9 | + | 1 | actors-plan | 0, 3, 0, 0 | 3 | + | 2 | new + AGENT only after the transition, clock cap in wall time | 3, 0, 2, 4 | 9 | + | 2 | actors-plan | 1, 1, 0, 5 | 7 | + | 3 | new + dead reckoning of the drones' reports | 1, 1, 0, 0 | 2 | + | 3 | actors-plan | 4, 7, 4, 2 | 17 | + + Plain `actors-plan` also crashes 0 to 7 drones per game here (12 games: 27 crashes), more in a batch + that ran while the machine was busy, so the acceptance test's "No drone crashes" is not a gate + that either path passes reliably. What was a defect of the new path: (1) the station asked for + AGENT as soon as a drone was in FBWA, in the middle of the transition (the server waits for 15 m/s): + two games lost a drone at its base at t = 10 s, "Hard impact" at 22 m/s, the other drone 10 m + away; fixed, no such crash in the 8 games after. (2) The station's clock was held to one game + second past the last map, so at 10x (a map each 2.5 game seconds) plans went out about 0.8 a + second instead of 1; now the cap is a wall second. (3) Plans were made from the position of the + last report; the station now carries it on by its velocity. In all 24 games the only crashes with another + drone within 30 m were the two take-off ones above (10 to 11 m) and one in plain `actors-plan` (24 m), so the executors' loss of the enemy's true positions as traffic does not show in + the crashes; they are tree strikes while taking a tower (6 to 25 m), in the open or in a shadow, + in both paths. +- Not done: the Orders echo is the server's `told` (what the drones last heard), not read from the + drones' telemetry; no GUI check of a takeover in the app; no full-SITL expectation for a takeover + (the arduplane driver only plays a computer-against-computer game). + Steps 1 and 3 do not depend on each other. Step 2 needs 1; step 4 needs 3; step 5 needs 1 and 3. ## Not settled