diff --git a/domains/cognition/towers/examples/advice.rs b/domains/cognition/towers/examples/advice.rs new file mode 100644 index 0000000..8118e4d --- /dev/null +++ b/domains/cognition/towers/examples/advice.rs @@ -0,0 +1,29 @@ +//! What advice is worth, and what lateness costs: a commander with the lookahead as its advisor, +//! at several delays, against a commander with no advisor, on paired seeds, both sides: +//! `cargo run --release -p cognition_towers --example advice [SEEDS] [DELAY...]`. +//! The table in `plans/actors.md` (step 4) is read off its output. + +use cognition_towers::{Captain, Comparison, compare_captains}; +use recreation_towers::{Commander, Lookahead, Rules}; + +fn main() -> Result<(), String> { + let mut args = std::env::args().skip(1); + let n: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(100); + let mut delays: Vec = args.filter_map(|s| s.parse().ok()).collect(); + if delays.is_empty() { + delays = vec![0.0, 2.0, 5.0, 10.0, 30.0]; + } + let rules = Rules::default(); + let seeds: Vec = (1..=n).collect(); + println!("advice: the lookahead as an advisor against no advisor, {n} seeds ({} games each)\n", 2 * n); + println!("| delay | margin | won | lost | drawn |\n|---|---|---|---|---|"); + for delay in delays { + let advised = || -> Box { + Box::new(Commander::new(rules).with_lookahead(false).with_advisor(Lookahead::new(rules), delay)) + }; + let alone = || -> Box { Box::new(Commander::new(rules).with_lookahead(false)) }; + let Comparison { margin, error, won, lost, drawn, .. } = compare_captains(rules, &advised, &alone, &seeds)?; + println!("| {delay} s | {margin:+.2} ± {error:.2} | {won} | {lost} | {drawn} |"); + } + Ok(()) +} diff --git a/domains/cognition/towers/src/lib.rs b/domains/cognition/towers/src/lib.rs index 95493de..ee8df48 100644 --- a/domains/cognition/towers/src/lib.rs +++ b/domains/cognition/towers/src/lib.rs @@ -25,8 +25,8 @@ use glam::Vec2; use recreation_towers::Commander; use recreation_towers::flight::{ORBIT_RADIUS, low_height, orbit_height}; use recreation_towers::{ - Autopilot, Command, CommanderTuning, Directive, Game, Instruction, OnBoard, Order, Outcome, Phase, Queued, Rules, - Standing, Team, TeamView, Unit, + Advise, Autopilot, Command, CommanderTuning, Directive, Game, Instruction, OnBoard, Order, Outcome, Phase, Queued, + Rules, Standing, Team, TeamView, Unit, }; use serde::{Deserialize, Serialize}; use std::io::{BufRead, BufReader, Write}; @@ -74,6 +74,11 @@ pub trait Captain { fn unit(&self, _team: Team, _k: usize) -> Box { Box::new(Autopilot) } + + /// Gives the captain an advisor (see [`Command::advised_by`]); whether it takes advice. + fn advised_by(&mut self, _advisor: Box, _delay: f32) -> bool { + false + } } impl Captain for &mut C { @@ -92,12 +97,26 @@ impl Captain for &mut C { fn unit(&self, team: Team, k: usize) -> Box { (**self).unit(team, k) } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + (**self).advised_by(advisor, delay) + } } impl Captain for recreation_towers::Commander { fn decide(&mut self, view: &TeamView) -> Result, String> { Ok(recreation_towers::Commander::decide(self, view)) } + + /// The queues of the proposals that it took. + fn queue(&mut self, _view: &TeamView, orders: &[(usize, Order)]) -> Vec<(usize, Vec)> { + orders.iter().map(|&(k, _)| (k, self.advised_queue(k))).filter(|(_, q)| !q.is_empty()).collect() + } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + self.add_advisor(advisor, delay); + true + } } /// A captain as a [`Command`]: its orders go out with the queues it prepares for them. @@ -115,6 +134,10 @@ impl Command for Directing { }) .collect()) } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + self.0.advised_by(advisor, delay) + } } /// Who plays: a [`Command`] for each team and a [`Unit`] for each drone, the drones of Blue first. @@ -147,6 +170,23 @@ impl<'a> Roster<'a> { Roster { blue: Box::new(Directing(blue)), red: Box::new(Directing(red)), units } } + /// Gives `team`'s commander an advisor, whose proposals reach it `delay` s of game time after the + /// map it was shown (the arena has no threads: the delay is in game time, so a game is + /// reproducible for its seed). Fails if the commander takes no advice. + pub fn with_advisor( + mut self, + team: Team, + advisor: impl Advise + 'static, + delay: f32, + ) -> Result, String> { + let command = if team == Team::Blue { &mut self.blue } else { &mut self.red }; + if command.advised_by(Box::new(advisor), delay) { + Ok(self) + } else { + Err(format!("the {team:?} commander takes no advice")) + } + } + /// Replaces the unit of drone `k` of `team`. pub fn with_unit(mut self, rules: &Rules, team: Team, k: usize, unit: impl Unit + 'a) -> Roster<'a> { self.units[team.index() * rules.drones + k] = Box::new(unit); @@ -922,6 +962,31 @@ mod tests { assert_eq!(by_roster, play_with(rules, &mut commander(), &mut commander(), 4).unwrap()); } + #[test] + fn a_roster_gives_a_team_an_advisor_that_is_late_in_game_time() { + use recreation_towers::Lookahead; + let rules = Rules::default(); + let bare = || Commander::new(rules).with_lookahead(false); + let thinking = || Commander::new(rules); + let play = |roster: Result, String>| play_roster(rules, roster.unwrap(), 6).unwrap(); + // No delay: the lookahead as an advisor is the commander that thinks ahead. + let at_once = Roster::of_captains(&rules, bare(), bare()).with_advisor(Team::Blue, Lookahead::new(rules), 0.0); + let by_default = Roster::of_captains(&rules, thinking(), bare()); + assert_eq!(play(at_once), play(Ok(by_default))); + // Late, it is another game, and the same game each time. + let late = || Roster::of_captains(&rules, bare(), bare()).with_advisor(Team::Blue, Lookahead::new(rules), 7.0); + assert_eq!(play(late()), play(late())); + // A commander that takes no advice says so. + struct Deaf; + impl Command for Deaf { + fn decide(&mut self, _: &TeamView) -> Result, String> { + Ok(vec![]) + } + } + let deaf = Roster::new(&rules, Deaf, bare(), |_, _| Box::new(Autopilot)); + assert!(deaf.with_advisor(Team::Blue, Lookahead::new(rules), 0.0).is_err()); + } + /// Plays Blue's commander against Red's, with `unit(team, k)` for each drone. fn mixed(seed: u64, unit: impl FnMut(Team, usize) -> Box) -> TowersResult { let rules = Rules::default(); 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/actors.rs b/domains/recreation/towers/src/actors.rs index 71a2580..fa3ea5b 100644 --- a/domains/recreation/towers/src/actors.rs +++ b/domains/recreation/towers/src/actors.rs @@ -37,23 +37,43 @@ pub trait Command { /// The directives for the drones whose order it changes. A drone it does not name goes on /// with what it was told. fn decide(&mut self, view: &TeamView) -> Result, String>; + + /// Gives the command an advisor, whose proposals reach it `delay` s of game time after the map + /// it is shown. Returns whether the command takes advice at all (by default it does not). + fn advised_by(&mut self, _advisor: Box, _delay: f32) -> bool { + false + } } impl Command for &mut C { fn decide(&mut self, view: &TeamView) -> Result, String> { (**self).decide(view) } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + (**self).advised_by(advisor, delay) + } } impl Command for Box { fn decide(&mut self, view: &TeamView) -> Result, String> { (**self).decide(view) } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + (**self).advised_by(advisor, delay) + } } impl Command for crate::Commander { fn decide(&mut self, view: &TeamView) -> Result, String> { - Ok(crate::Commander::decide(self, view).into_iter().map(|(k, o)| Directive::new(k, o)).collect()) + let orders = crate::Commander::decide(self, view); + Ok(orders.into_iter().map(|(k, o)| Directive::with_queue(k, o, self.advised_queue(k))).collect()) + } + + fn advised_by(&mut self, advisor: Box, delay: f32) -> bool { + self.add_advisor(advisor, delay); + true } } diff --git a/domains/recreation/towers/src/advice.rs b/domains/recreation/towers/src/advice.rs new file mode 100644 index 0000000..fa8d1f5 --- /dev/null +++ b/domains/recreation/towers/src/advice.rs @@ -0,0 +1,128 @@ +//! Advisors that propose moves to a commander (plans/actors.md): the [`Advise`] trait, the +//! [`Proposal`] it makes, and the first advisor, the [`Lookahead`]. Nothing here does I/O: the +//! arena, the game server and a seat in another process all drive the same trait. +//! +//! An advisor sees what a seat gets, the team's map ([`TeamView`]), and what the commander has in +//! hand ([`Hand`]: the orders it means to give, the drones it may still change, the towers a +//! drone must stay away from). It may keep what state it likes, a belief about the enemy for one. +//! It answers with proposals; it does not give orders. The commander decides whether to take one +//! (`Commander::advise`), and an advisor that is slow is only late: its proposals reach the +//! commander some seconds after the map they were made from. + +use crate::lookahead::{self, HORIZON}; +use crate::{Belief, Order, Queued, Rules, Team, TeamView}; + +/// A proposal: one order for one drone, with what the advisor expects to gain by it. +#[derive(Clone, Debug, PartialEq)] +pub struct Proposal { + /// The drone, by its number in the team. + pub drone: usize, + pub order: Order, + /// The queue that goes with the order (what the drone does out of contact). + pub queue: Vec, + /// The points the advisor expects the order to gain over what the commander means to do. + pub gain: f32, + /// The game time (s) of the map the proposal was made from. + pub made: f32, + /// The game time (s) after which the proposal is stale and is not taken. + pub until: f32, + /// What the advisor says to a person who is shown the proposal. + pub why: String, +} + +/// What a commander has in hand when an advisor looks: the part of its own state that an advisor +/// needs and that the map does not show. A seat has it, for the seat is the commander. +#[derive(Clone, Debug, PartialEq)] +pub struct Hand { + /// The order each drone is to fly, by its number in the team. + pub orders: Vec>, + /// The drones that an order may be given to (in contact, up, not on their way to charge). + pub free: Vec, + /// For each drone, the towers it must stay away from (it gave them up). + pub away: Vec>, +} + +/// Looks at the team's map and proposes moves. It may be slow; it is called when the map is shown +/// to it, and what it answers reaches the commander later. +pub trait Advise: std::fmt::Debug { + /// The proposals on this map, if any. Called with each map the advisor is shown (the + /// commander shows it one each second); it decides for itself how often it thinks. + fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec; + + /// A copy of the advisor, with its state (a commander can be cloned). + fn clone_box(&self) -> Box; +} + +impl Clone for Box { + fn clone(&self) -> Box { + self.clone_box() + } +} + +/// A lookahead thinks ahead this often (s)... +pub const LOOK_EVERY: f32 = 5.0; +/// ...and not before this time in the game (s): at the start the belief is only a prior. +pub const LOOK_AFTER: f32 = 10.0; + +/// The first advisor: the lookahead. Every [`LOOK_EVERY`] s it plays the game forward +/// [`HORIZON`] s in a small model (`lookahead`), with the enemy drones guessed from its own +/// [`Belief`], and proposes the one change of orders that gains most. Its proposal is good for +/// the horizon it looked over. +#[derive(Clone, Debug)] +pub struct Lookahead { + rules: Rules, + belief: Belief, + /// The game time of the last look. + last: f32, + /// For measuring: the belief is never updated, so the model knows nothing of the enemy. + blind: bool, +} + +impl Lookahead { + pub fn new(rules: Rules) -> Lookahead { + Lookahead { rules, belief: Belief::new(rules, Team::Blue), last: f32::NEG_INFINITY, blind: false } + } + + /// A lookahead whose belief is never updated (for measuring what the belief is worth). + pub fn blind(mut self, blind: bool) -> Lookahead { + self.blind = blind; + self + } +} + +impl Advise for Lookahead { + fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec { + if self.belief.team() != view.team { + self.belief = Belief::new(self.rules, view.team); + } + if !self.blind { + self.belief.update(view); + } + if view.time < LOOK_AFTER || view.time - self.last < LOOK_EVERY { + return vec![]; + } + self.last = view.time; + let Some((drone, tower, gain)) = + lookahead::best_change(&self.rules, view, &hand.orders, &hand.free, &hand.away, &self.belief) + else { + return vec![]; + }; + vec![Proposal { + drone, + order: Order::Take(tower), + queue: vec![], + gain, + made: view.time, + until: view.time + HORIZON, + why: format!( + "Send drone {} to the {} tower: about {gain:.0} points over the next {HORIZON:.0} s.", + drone + 1, + view.towers[tower].name + ), + }] + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} diff --git a/domains/recreation/towers/src/commander.rs b/domains/recreation/towers/src/commander.rs index c6f4465..b79dfb3 100644 --- a/domains/recreation/towers/src/commander.rs +++ b/domains/recreation/towers/src/commander.rs @@ -5,7 +5,7 @@ //! strategy that improves commanders may change; the rules, the referee, the fog and the view //! are elsewhere. -use crate::{Kind, Order, Phase, Rules, TeamView}; +use crate::{Advise, Hand, Kind, Lookahead, Order, Phase, Proposal, Queued, Rules, TeamView}; use serde::{Deserialize, Serialize}; /// Declares the tunable numbers: for each, a constant (its default) and a field of @@ -75,10 +75,8 @@ const CRUISE: f32 = 16.0; /// this margin (s); it goes back to work when the battery is this much full. const REFUEL_MARGIN: f32 = 15.0; const BACK_TO_WORK: f32 = 0.9; -/// A commander that thinks ahead does so this often (s). -const LOOK_EVERY: f32 = 5.0; -/// ...and not before this time in the game (s): at the start the belief is only a prior. -const LOOK_AFTER: f32 = 10.0; +/// A commander takes a proposal of an advisor only if it gains more than this many points. +pub const ADVICE_MARGIN: f32 = 2.0; /// A computer in a team's seat. #[derive(Clone, Debug)] @@ -95,10 +93,18 @@ pub struct Commander { recharging: Vec, /// The game time of the last decision. last: Option, - /// Thinks ahead: a belief about the enemy, and when the model last changed an order. - look: Option<(crate::Belief, f32)>, - /// For measuring: the belief is never updated, so the model knows nothing of the enemy. - blind: bool, + /// The advisors that look at the map each time it decides, each with the delay (s of game + /// time) between the map it is shown and the moment its proposals reach the commander. A + /// commander with none does not think ahead. + advisors: Vec<(Box, f32)>, + /// Proposals on their way: when each arrives (game time, s). + late: Vec<(f32, Proposal)>, + /// Proposals in hand, not yet taken and not yet stale. + proposals: Vec, + /// A proposal must gain more than this many points to be taken. + advice_margin: f32, + /// The queue that goes with the order a proposal gave a drone, while it still flies it. + queues: Vec)>>, } impl Commander { @@ -116,20 +122,67 @@ impl Commander { away: vec![None; n], recharging: vec![false; n], last: None, - look: None, - blind: std::env::var("SIM001_LOOKAHEAD").as_deref() == Ok("blind"), + advisors: vec![], + late: vec![], + proposals: vec![], + advice_margin: ADVICE_MARGIN, + queues: vec![None; n], } .with_lookahead(std::env::var("SIM001_LOOKAHEAD").as_deref() != Ok("0")) } - /// Thinks ahead or not (crate::lookahead): once in a while a commander that does rolls a model of - /// the game forward from what it believes about the enemy, and changes an order if that is - /// better. A commander thinks ahead unless `SIM001_LOOKAHEAD` is `0`. + /// Thinks ahead or not: with the [`Lookahead`] advisor, at no delay, or with no advisor at all + /// (this takes every advisor away). A commander has the lookahead unless `SIM001_LOOKAHEAD` is + /// `0`; with it set to `blind`, the lookahead never updates its belief. pub fn with_lookahead(mut self, on: bool) -> Commander { - self.look = on.then(|| (crate::Belief::new(self.rules, crate::Team::Blue), f32::NEG_INFINITY)); + self.advisors.clear(); + self.late.clear(); + if on { + let blind = std::env::var("SIM001_LOOKAHEAD").as_deref() == Ok("blind"); + self.advisors.push((Box::new(Lookahead::new(self.rules).blind(blind)), 0.0)); + } + self + } + + /// Adds an advisor. Its proposals reach the commander `delay` s of game time after the map it + /// was shown (the map of each decision), so a slow advisor is a late one. + pub fn with_advisor(mut self, advisor: impl Advise + 'static, delay: f32) -> Commander { + self.advisors.push((Box::new(advisor), delay.max(0.0))); + self + } + + /// Adds an advisor that is already boxed (see [`Commander::with_advisor`]). + pub fn add_advisor(&mut self, advisor: Box, delay: f32) { + self.advisors.push((advisor, delay.max(0.0))); + } + + /// How many advisors the commander has (none: it does not think ahead). + pub fn advisors(&self) -> usize { + self.advisors.len() + } + + /// A proposal must gain more than `points` to be taken (default [`ADVICE_MARGIN`]). + pub fn with_advice_margin(mut self, points: f32) -> Commander { + self.advice_margin = points; self } + /// Puts proposals in hand, as if they had just arrived: from an advisor outside this process, or + /// one that a person accepted. The commander takes them at its next decision, or lets them go + /// if they are stale or do not fit then. + pub fn advise(&mut self, proposals: impl IntoIterator) { + self.proposals.extend(proposals); + } + + /// The queue that goes with drone `k`'s order: the one a proposal gave it, while it flies that + /// order; else none. + pub fn advised_queue(&self, k: usize) -> Vec { + match (self.queues.get(k), self.order(k)) { + (Some(Some((o, q))), Some(now)) if *o == now => q.clone(), + _ => vec![], + } + } + pub fn tuning(&self) -> CommanderTuning { self.tuning } @@ -303,22 +356,9 @@ impl Commander { .min_by(|&&a, &&b| view.towers[a].at.distance(at).total_cmp(&view.towers[b].at.distance(at))); *order = spare.map(|&t| Order::Take(t)); } - if let Some((belief, last_look)) = &mut self.look { - if belief.team() != view.team { - *belief = crate::Belief::new(self.rules, view.team); - } - if !self.blind { - belief.update(view); - } - if view.time >= LOOK_AFTER && view.time - *last_look >= LOOK_EVERY { - *last_look = view.time; - let free: Vec = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect(); - if let Some((k, t)) = - crate::lookahead::best_change(&self.rules, view, &self.orders, &free, &away, belief) - { - self.orders[k] = Some(Order::Take(t)); - } - } + if !self.advisors.is_empty() || !self.late.is_empty() || !self.proposals.is_empty() { + let free: Vec = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect(); + self.take_advice(view, free, away); } for (k, order) in charge.into_iter().enumerate() { if order.is_some() { @@ -327,6 +367,61 @@ impl Commander { } self.orders.iter().enumerate().filter(|(k, _)| linked[*k]).filter_map(|(k, o)| o.map(|o| (k, o))).collect() } + + /// Shows the advisors the map, collects the proposals that have reached the commander, and takes + /// the best one that can be taken: not stale, gaining more than the margin, and still fitting + /// (see [`Commander::fits`]). A proposal that is stale or does not fit is dropped; one that gains too + /// little waits for its time to run out. + fn take_advice(&mut self, view: &TeamView, free: Vec, away: Vec>) { + let hand = Hand { orders: self.orders.clone(), free, away }; + for (advisor, delay) in &mut self.advisors { + for p in advisor.advise(view, &hand) { + self.late.push((view.time + *delay, p)); + } + } + let now = view.time; + let (arrived, waiting): (Vec<_>, Vec<_>) = + std::mem::take(&mut self.late).into_iter().partition(|(at, _)| *at <= now); + self.late = waiting; + self.proposals.extend(arrived.into_iter().map(|(_, p)| p)); + self.proposals.retain(|p| p.until >= now); + let mut best: Option = None; + let mut keep = vec![true; self.proposals.len()]; + for (i, p) in self.proposals.iter().enumerate() { + if !self.fits(p, view, &hand) { + keep[i] = false; + } else if p.gain > self.advice_margin && best.is_none_or(|b| p.gain > self.proposals[b].gain) { + best = Some(i); + } + } + if let Some(b) = best { + let p = self.proposals[b].clone(); + self.orders[p.drone] = Some(p.order); + self.queues[p.drone] = Some((p.order, p.queue.clone())); + for (i, q) in self.proposals.iter().enumerate() { + keep[i] &= q.drone != p.drone; + } + } + let mut keep = keep.into_iter(); + self.proposals.retain(|_| keep.next().unwrap_or(false)); + } + + /// Whether a proposal still makes sense: its drone is one of the team's and free, the order + /// is not the one the drone has, a tower it sends the drone to is not one the drone gave up, and + /// (for a proposal made on an earlier map) is not one the team holds now. + fn fits(&self, p: &Proposal, view: &TeamView, hand: &Hand) -> bool { + if p.drone >= self.orders.len() || !hand.free[p.drone] || hand.orders[p.drone] == Some(p.order) { + return false; + } + match p.order { + Order::Take(t) => { + t < view.towers.len() + && !hand.away[p.drone][t] + && !(p.made < view.time && view.towers[t].holder == Some(view.team)) + } + _ => true, + } + } } #[cfg(test)] @@ -596,4 +691,156 @@ mod tests { let mut c = Commander::new(g.rules); assert_ne!(decide(&mut c, &g)[0], Some(Order::Home)); } + + /// An advisor that proposes the same thing on every map. + #[derive(Clone, Debug)] + struct Fixed(Proposal); + + impl Advise for Fixed { + fn advise(&mut self, view: &TeamView, _: &Hand) -> Vec { + vec![Proposal { made: view.time, until: view.time + 5.0, ..self.0.clone() }] + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + } + + fn proposal(drone: usize, order: Order, gain: f32) -> Proposal { + Proposal { drone, order, queue: vec![], gain, made: 0.0, until: 10.0, why: "test".into() } + } + + fn bare(g: &Game) -> Commander { + Commander::new(g.rules).with_lookahead(false) + } + + #[test] + fn a_proposal_with_enough_gain_is_taken() { + let g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let mut c = bare(&g); + c.advise([proposal(0, red_home, 5.0)]); + c.decide(&g.view(Team::Blue)); + assert_eq!(c.order(0), Some(red_home)); + } + + #[test] + fn a_proposal_that_gains_little_is_not_taken() { + let g = Game::new(Rules::default()); + let mut c = bare(&g); + c.advise([proposal(0, Order::Take(g.tower("Red home")), ADVICE_MARGIN)]); + let advised = c.decide(&g.view(Team::Blue)); + assert_eq!(advised, bare(&g).decide(&g.view(Team::Blue))); + } + + #[test] + fn a_stale_proposal_is_not_taken() { + let mut g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let mut c = bare(&g); + c.decide(&g.view(Team::Blue)); + let before = c.order(0); + g.step(10.0); + c.advise([Proposal { made: 0.0, until: 5.0, ..proposal(0, red_home, 9.0) }]); + c.decide(&g.view(Team::Blue)); + assert_eq!(c.order(0), before); + } + + #[test] + fn a_proposal_for_a_drone_that_is_not_free_is_dropped() { + let mut g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let i = g.drone(Team::Blue, 0); + g.drones[i].phase = Phase::Down; + let mut c = bare(&g); + c.advise([proposal(0, Order::Hold, 9.0), proposal(99, red_home, 9.0)]); + c.decide(&g.view(Team::Blue)); + assert_ne!(c.order(0), Some(Order::Hold)); + // It is not kept for later either: the drone is up again, and the proposal is gone. + g.drones[i].phase = Phase::Ground; + c.decide(&g.view(Team::Blue)); + assert_ne!(c.order(0), Some(Order::Hold)); + } + + #[test] + fn a_proposal_made_on_an_old_map_is_not_taken_for_a_tower_that_the_team_holds_now() { + let mut g = Game::new(Rules::default()); + let west = g.tower("west"); + let mut c = bare(&g); + c.decide(&g.view(Team::Blue)); + g.step(1.0); + g.towers[west].holder = Some(Team::Blue); + g.towers[west].control = 1.0; + g.look(); + c.advise([proposal(1, Order::Take(west), 9.0)]); + c.decide(&g.view(Team::Blue)); + assert_ne!(c.order(1), Some(Order::Take(west))); + } + + #[test] + fn the_best_proposal_is_taken_first_and_one_at_a_decision() { + let g = Game::new(Rules::default()); + let (a, b) = (Order::Take(g.tower("Red home")), Order::Take(g.tower("east"))); + let mut c = bare(&g); + c.advise([proposal(0, a, 4.0), proposal(0, b, 6.0), proposal(2, Order::Hold, 3.0)]); + c.decide(&g.view(Team::Blue)); + assert_eq!((c.order(0), c.order(2) == Some(Order::Hold)), (Some(b), false)); + c.decide(&g.view(Team::Blue)); + assert_eq!(c.order(2), Some(Order::Hold)); + } + + #[test] + fn a_taken_proposal_gives_its_queue_to_the_drone() { + let g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let mut c = bare(&g); + let q = vec![Queued::done(Order::Home)]; + c.advise([Proposal { queue: q.clone(), ..proposal(0, red_home, 5.0) }]); + c.decide(&g.view(Team::Blue)); + assert_eq!(c.advised_queue(0), q); + assert!(c.advised_queue(1).is_empty()); + } + + #[test] + fn an_advisor_that_is_late_is_taken_when_its_proposal_arrives() { + let mut g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let mut c = bare(&g).with_advisor(Fixed(proposal(0, red_home, 5.0)), 3.0); + for _ in 0..3 { + c.decide(&g.view(Team::Blue)); + assert_ne!(c.order(0), Some(red_home), "at {}", g.time); + g.step(1.0); + } + c.decide(&g.view(Team::Blue)); + assert_eq!(c.order(0), Some(red_home)); + } + + #[test] + fn an_advisor_with_no_delay_is_taken_at_once_and_one_that_is_later_than_its_proposal_lasts_is_not() { + let mut g = Game::new(Rules::default()); + let red_home = Order::Take(g.tower("Red home")); + let mut now = bare(&g).with_advisor(Fixed(proposal(0, red_home, 5.0)), 0.0); + now.decide(&g.view(Team::Blue)); + assert_eq!(now.order(0), Some(red_home)); + // Good for 5 s, and 8 s late: stale when it arrives. + let mut late = bare(&g).with_advisor(Fixed(proposal(0, red_home, 5.0)), 8.0); + for _ in 0..30 { + late.decide(&g.view(Team::Blue)); + assert_ne!(late.order(0), Some(red_home), "at {}", g.time); + g.step(1.0); + } + } + + #[test] + fn the_lookahead_as_an_advisor_is_the_commander_that_thinks_ahead() { + let mut g = Game::new(Rules::default()); + let mut inside = Commander::new(g.rules); + let mut advised = bare(&g).with_advisor(Lookahead::new(g.rules), 0.0); + for _ in 0..120 { + let view = g.view(Team::Blue); + let (a, b) = (inside.decide(&view), advised.decide(&view)); + assert_eq!(a, b, "at {}", g.time); + g.step(1.0); + } + } } diff --git a/domains/recreation/towers/src/lib.rs b/domains/recreation/towers/src/lib.rs index 10e5411..1a96792 100644 --- a/domains/recreation/towers/src/lib.rs +++ b/domains/recreation/towers/src/lib.rs @@ -14,6 +14,7 @@ //! Positions are on the field: north and east of its centre (m), and a height above it (m). mod actors; +mod advice; mod advisor; mod belief; mod commander; @@ -28,9 +29,10 @@ pub mod svg; mod view; pub use actors::{Autopilot, Command, Directive, OnBoard, Unit}; +pub use advice::{Advise, Hand, Lookahead, Proposal}; pub use advisor::{Advice, AdviceKind, Advisor}; pub use belief::Belief; -pub use commander::{Commander, CommanderTuning}; +pub use commander::{ADVICE_MARGIN, Commander, CommanderTuning}; pub use edge::{EdgeObs, EnemySeen, TowerSeen}; pub use filter::LinkFilter; pub use instruction::{Instruction, MAX_QUEUE, Queued, Standing, When, queue_of}; diff --git a/domains/recreation/towers/src/lookahead.rs b/domains/recreation/towers/src/lookahead.rs index 75e7e3b..3e037ab 100644 --- a/domains/recreation/towers/src/lookahead.rs +++ b/domains/recreation/towers/src/lookahead.rs @@ -15,8 +15,6 @@ const CRUISE: f32 = 16.0; /// How far ahead the model looks (s), and its step (s). pub const HORIZON: f32 = 45.0; const STEP: f32 = 1.0; -/// A change of orders must be worth this many points to be made. -const MARGIN: f32 = 2.0; /// How many futures the search draws for the enemy. const FUTURES: usize = 8; /// The value of each point of progress on a tower's bar, in points a second held for a minute. @@ -149,9 +147,10 @@ fn toward(from: Vec2, to: Vec2, step: f32) -> Vec2 { if d.length() <= step { to } else { from + d.normalize() * step } } -/// The one change to `orders` that the model likes best: drone `k` goes to tower `t` instead. `None` -/// if no change is worth [`MARGIN`] points. `free` says which drones may be given a new order, and -/// `away` which towers a drone must stay away from. +/// The one change to `orders` that the model likes best: drone `k` goes to tower `t` instead, and +/// the points it expects to gain by it: `(k, t, gain)`. `None` if there is no change to try. Whether +/// the gain is worth making is for the commander to say. `free` says which drones may be given a new +/// order, and `away` which towers a drone must stay away from. pub fn best_change( rules: &Rules, view: &TeamView, @@ -159,7 +158,7 @@ pub fn best_change( free: &[bool], away: &[Vec], belief: &Belief, -) -> Option<(usize, usize)> { +) -> Option<(usize, usize, f32)> { let towers = view.towers.len(); let base = rules.base(view.team); // A drone is not sent where its battery cannot last the flight, the capture and the way home. @@ -194,7 +193,8 @@ pub fn best_change( } let (best, total) = gain.iter().copied().enumerate().max_by(|a, b| a.1.total_cmp(&b.1)).filter(|&(_, g)| g.is_finite())?; - (total / FUTURES as f32 > MARGIN).then(|| candidates[best]) + let (k, t) = candidates[best]; + Some((k, t, total / FUTURES as f32)) } #[cfg(test)] @@ -292,11 +292,11 @@ mod tests { let change = best_change(&rules, &view, &orders, &free, &no_away(&view), &belief); let centre = g.tower("centre"); // A free tower: the east, or the centre, which is worth more. Not the west. - assert!(matches!(change, Some((0, t)) if t == east || t == centre), "{change:?}"); + assert!(matches!(change, Some((0, t, gain)) if (t == east || t == centre) && gain > 2.0), "{change:?}"); } #[test] - fn it_leaves_the_orders_alone_when_nothing_is_better() { + fn it_has_no_change_to_try_when_no_drone_is_free() { let (rules, g) = setup(); let view = g.view(Team::Blue); let belief = Belief::new(rules, Team::Blue); diff --git a/domains/recreation/towers/src/seat.rs b/domains/recreation/towers/src/seat.rs index 22525dd..5a77a12 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), @@ -102,6 +115,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/tests/acceptance.rs b/domains/recreation/towers/tests/acceptance.rs index 41ccf56..d5d0ff6 100644 --- a/domains/recreation/towers/tests/acceptance.rs +++ b/domains/recreation/towers/tests/acceptance.rs @@ -10,8 +10,8 @@ use glam::Vec2; use recreation_towers::flight::{self, Plan}; use recreation_towers::map::LaneName; use recreation_towers::{ - Advice, AdviceKind, Advisor, Belief, Commander, Game, Instruction, Link, LinkFilter, Order, Outcome, Phase, Queued, - Rules, Standing, Team, When, + Advice, AdviceKind, Advisor, Belief, Commander, Game, Instruction, Link, LinkFilter, Order, Outcome, Phase, + Proposal, Queued, Rules, Standing, Team, When, }; use std::path::Path; @@ -42,6 +42,8 @@ struct World { /// What Blue believes about where the Red drones are, and the time it was last updated. belief: Belief, belief_at: f32, + /// The proposal that a step put in the commander's hand, in the spec's words. + proposed: String, } impl World { @@ -58,6 +60,7 @@ impl World { told: Instruction::default(), belief: Belief::new(rules, Team::Blue), belief_at: 0.0, + proposed: "none".into(), } } @@ -121,6 +124,22 @@ impl World { b } + /// An advisor of Blue proposes that the first Blue drone takes the Red home tower, for `gain` + /// points: from the map of `ago` seconds before, and good for `good_for` seconds after it. + fn propose(&mut self, gain: f32, good_for: f32, ago: f32) { + let red_home = Order::Take(self.game.tower("Red home")); + let made = self.game.time - ago; + let p = + Proposal { drone: 0, order: red_home, queue: vec![], gain, made, until: made + good_for, why: "".into() }; + self.blue.advise([p]); + } + + /// The computer commander of Blue decides on the map now. + fn blue_decides(&mut self) { + let view = self.game.view(Team::Blue); + self.blue.decide(&view); + } + /// The advisor looks at the Blue map. fn look(&mut self) { let view = self.game.view(Team::Blue); @@ -594,6 +613,31 @@ fn bindings() -> Bindings { let view = w.game.view(Team::Blue); w.blue.decide(&view); }) + // The advisors of the computer commander: proposals that it takes or not. + .observe("the proposal in the hand of the Blue commander", |w| w.proposed.clone()) + .arrange("the proposal in the hand of the Blue commander", |w, v| { + w.proposed = v.as_str().unwrap_or("none").to_string(); + match v.as_str() { + Some("the Red home tower for 5 points") => w.propose(5.0, 60.0, 0.0), + Some("the Red home tower for 1 point") => w.propose(1.0, 60.0, 0.0), + Some("the Red home tower for 5 points, good for 5 seconds") => w.propose(5.0, 5.0, 0.0), + Some("the Red home tower for 5 points, from the map of 3 seconds before") => w.propose(5.0, 60.0, 3.0), + _ => {} + } + }) + .on("the computer commander of Blue weighs the proposal", |w| w.blue_decides()) + .on("the computer commander of Blue weighs the proposal 10 seconds later", |w| { + w.run(10.0); + w.blue_decides(); + }) + .observe("the advisor of the computer commander of Blue", |w| { + if w.blue.advisors() == 0 { "none" } else { "the lookahead" } + }) + .arrange("the advisor of the computer commander of Blue", |w, v| { + let on = v.as_str() != Some("none"); + let blue = std::mem::replace(&mut w.blue, Commander::new(w.game.rules)); + w.blue = blue.with_lookahead(on); + }) // Blue's advisor. .observe("the advice of Blue", |w| w.advice_said()) .on("the advisor of Blue looks", |w| w.look()) @@ -941,3 +985,78 @@ mod queue { assert_eq!(w.say(w.told.order), "take the east tower"); } } + +/// The advice expectations (`18-advice.expect`) played by hand: the suite's coverage reaches only some +/// of them (the last two are only arranged and visited there), and all must hold. +mod advice { + use super::*; + + fn red_home(w: &World) -> Option { + Some(Order::Take(w.game.tower("Red home"))) + } + + #[test] + fn a_proposal_that_gains_enough_is_taken() { + let mut w = World::new(); + w.propose(5.0, 60.0, 0.0); + w.blue_decides(); + assert_eq!(w.blue.order(0), red_home(&w)); + } + + #[test] + fn a_proposal_that_gains_little_is_not_taken() { + let mut w = World::new(); + w.propose(1.0, 60.0, 0.0); + w.blue_decides(); + assert_eq!(w.say(w.blue.order(0)), "take the west tower"); + } + + #[test] + fn a_stale_proposal_is_not_taken() { + let mut w = World::new(); + w.propose(5.0, 5.0, 0.0); + w.run(10.0); + w.blue_decides(); + assert_ne!(w.blue.order(0), red_home(&w)); + assert_eq!(w.say(w.blue.order(0)), "take the centre tower"); + } + + #[test] + fn a_proposal_for_a_tower_that_the_team_holds_now_is_not_taken() { + let mut w = World::new(); + w.blue_holds(2); + w.hold("west", &Value::from("neutral")); + w.hold("Red home", &Value::from("held by Blue")); + w.propose(5.0, 60.0, 3.0); + w.blue_decides(); + assert_eq!(w.say(w.blue.order(0)), "take the west tower"); + } + + #[test] + fn a_commander_with_no_advisor_does_not_think_ahead() { + assert_eq!(sits_at_west(false), Some(Order::Take(World::new().game.tower("west")))); + // The same fight, with the lookahead for an advisor, is not stayed in. + assert_ne!(sits_at_west(true), Some(Order::Take(World::new().game.tower("west")))); + } + + fn sits_at_west(lookahead: bool) -> Option { + let mut w = World::new(); + w.blue = Commander::new(w.game.rules).with_lookahead(lookahead); + let west = w.game.tower("west"); + w.hold("west", &Value::from("held by Red")); + w.blue.assign(0, Some(Order::Take(west))); + for k in 0..2 { + let i = w.game.drone(Team::Red, k); + let at = w.game.towers[west].at; + w.put(i, at, Some(ALT)); + } + w.place_blue(&Value::from("190 m from the centre tower")); + for _ in 0..15 { + w.blue_decides(); + w.run(1.0); + } + w.blue_decides(); + assert_eq!(w.blue.advisors(), usize::from(lookahead)); + w.blue.order(0) + } +} 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..b749e0c --- /dev/null +++ b/domains/recreation/towers_ground/src/commander.rs @@ -0,0 +1,74 @@ +//! 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`] (as a [`Command`]) for directives, and sends each with its queue, 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, Command, 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; + let Ok(directives) = Command::decide(c, &view) else { continue }; + for d in directives { + seat.order_with_queue(d.drone, d.order, rules.class(d.drone) == Class::Light, d.queue); + } + } + seat.stop(); +} diff --git a/domains/recreation/towers_ground/src/lib.rs b/domains/recreation/towers_ground/src/lib.rs index d9243cf..565fc70 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..9507bed --- /dev/null +++ b/domains/recreation/towers_ground/src/seat.rs @@ -0,0 +1,227 @@ +//! 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) { + self.order_with_queue(drone, order, low, vec![]); + } + + /// Sends an order with the queue that goes with it: what the drone does, in order, if it is out + /// of contact. + pub fn order_with_queue(&self, drone: usize, order: Order, low: bool, queue: Vec) { + // 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, queue }); + } + + /// 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 fceadeb..35a14d5 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, queue }) = seat::decode::(&line) && drone < rules.drones @@ -131,6 +191,226 @@ fn seat( w.command_queue(i, order, low, recreation_towers::queue_of(standing, queue)); } } - // 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 cffd6d6..61df843 100644 --- a/domains/recreation/towers_server/src/world.rs +++ b/domains/recreation/towers_server/src/world.rs @@ -218,10 +218,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 { @@ -270,11 +266,13 @@ 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) } /// The team gives drone `i` an order and a standing order. + #[cfg(test)] pub fn command_standing(&mut self, i: usize, order: Order, low: bool, standing: Option) { self.command_queue(i, order, low, queue_of(standing, vec![])) } @@ -316,18 +314,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 { @@ -374,6 +360,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 310e3b5..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, queue: vec![] }); - } - - /// 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/expectations/09-towers/rules/05-commander.expect b/expectations/09-towers/rules/05-commander.expect index 70f16a4..34e295f 100644 --- a/expectations/09-towers/rules/05-commander.expect +++ b/expectations/09-towers/rules/05-commander.expect @@ -23,7 +23,8 @@ Feature: The computer commander The enemy drones in the model are guesses from its belief. It tries to send each free drone to each other tower. It makes the change that gives most points, if the gain is more than 2 points. Thus it does not send a drone to a tower where the enemy has more drones than the - team can bring. + team can bring. This is the first [[advisor]] of the commander: [[Advice for the commander]] + tells how a commander takes advice, and what it does with no advisor. Expectation: At the start, the commander sends a drone to each neutral tower Each tower goes to the drone that wins it soonest. diff --git a/expectations/09-towers/rules/18-advice.expect b/expectations/09-towers/rules/18-advice.expect new file mode 100644 index 0000000..90dd3f0 --- /dev/null +++ b/expectations/09-towers/rules/18-advice.expect @@ -0,0 +1,69 @@ +Feature: Advice for the commander + A commander can have an [[advisor]] that helps it. The advisor looks at the map of the team, + like the commander, and sends a [[proposal]] when it sees a better move: an order for a drone, the points that it expects + to gain by it, and the time after which it is stale. The advisor does not give orders. The + commander decides. + + The computer commander takes a proposal when it gains more than 2 points, when it is not + stale, and when it still fits: the drone is free to take an order, and the tower is not one + that the team holds now. Of the proposals that it can take, it takes the one that gains most. + A proposal that does not fit is dropped. + + The first advisor is the lookahead. Every 5 s it plays the game forward 45 s in a small model. + The enemy drones in the model are guesses from its [[belief]]. It tries to send each free + drone to each other tower, and proposes the change that gives most points. Its proposal is + good for 45 s. A commander with no advisor does not think ahead. A commander thinks ahead + with the lookahead by default. + + An advisor can be slow. Its proposals then reach the commander some seconds after the map + that it looked at. A proposal that arrives after its time is stale, and the commander does + not take it. + + Expectation: A proposal that gains enough is taken + The advisor proposes that the first Blue drone takes the Red home tower, for 5 points. At the + start the commander sends the first drone to the west tower. + + Given the proposal in the hand of the Blue commander is the Red home tower for 5 points + When the computer commander of Blue weighs the proposal + Then the order of the first Blue drone is take the Red home tower + + Expectation: A proposal that gains little is not taken + The commander takes a proposal only when it gains more than 2 points. + + Given the proposal in the hand of the Blue commander is the Red home tower for 1 point + When the computer commander of Blue weighs the proposal + Then the order of the first Blue drone is take the west tower + + Expectation: A stale proposal is not taken + The proposal is good for 5 s. The commander decides 10 s later, and does what it does with no + proposal: at that time it sends the first drone to the centre tower. + + Given the proposal in the hand of the Blue commander is the Red home tower for 5 points, good for 5 seconds + When the computer commander of Blue weighs the proposal 10 seconds later + Then the order of the first Blue drone is take the centre tower + + Expectation: A proposal for a tower that the team holds now is not taken + The advisor looked at the map when the Red home tower was Red's. When the commander decides, + Blue holds it. + + Given the towers held by Blue are 2 + And the west tower is neutral + And the Red home tower is held by Blue + And the proposal in the hand of the Blue commander is the Red home tower for 5 points, from the map of 3 seconds before + When the computer commander of Blue weighs the proposal + Then the order of the first Blue drone is take the west tower + + Expectation: A commander with no advisor does not think ahead + Overrides: The commander does not keep a drone at a tower that the enemy holds with two drones + The west tower is Red's, and two Red drones circle it. A commander that thinks ahead sends + the first drone to a free tower, as in [[The computer commander]]. A commander with no + advisor does not. + + Given the advisor of the computer commander of Blue is none + And the west tower is held by Red + And the control of the west tower is -100 % + And the Red drones in the west circle are 2 + And the order of the first Blue drone is take the west tower + And the first Blue drone is 190 m from the centre tower + When the computer commander of Blue plays for 15 seconds + Then the order of the first Blue drone is take the west tower diff --git a/expectations/09-towers/rules/section.expect b/expectations/09-towers/rules/section.expect index 705e32c..067ec1a 100644 --- a/expectations/09-towers/rules/section.expect +++ b/expectations/09-towers/rules/section.expect @@ -12,7 +12,7 @@ Section: The rules of Towers [[Radio shadows]] tells you how a tower blocks the radio of the enemy. [[The mesh]] tells you how a friend relays the radio. [[The battery]] tells you what limits the time that a drone flies. [[Kinds of tower]] tells you how a fort, a spire and a depot differ. [[Standing orders]] tell you what a drone does when it is out of contact. [[The queue of a drone]] tells you how it works down a list of them. [[The mountains]] tells you what stands at the edge of the field. - [[The computer commander]] tells you how the computer plays a team. + [[The computer commander]] tells you how the computer plays a team. [[Advice for the commander]] tells you how an advisor helps it. Background: Follows: the launcher has just opened diff --git a/expectations/09-towers/section.expect b/expectations/09-towers/section.expect index 1fd399b..577666b 100644 --- a/expectations/09-towers/section.expect +++ b/expectations/09-towers/section.expect @@ -271,6 +271,19 @@ Vocabulary: Shows: a drone is down: the app shows an ADVISOR line that names the drone that is down Shows: the team is behind: the app shows an ADVISOR line with the score and the time left Shows: none: the ADVISOR line says nothing new + # The advisors of the computer commander (recreation_towers::advice): proposals that it takes or not. + State: the proposal in the hand of the Blue commander is none | the Red home tower for 5 points | the Red home tower for 1 point | the Red home tower for 5 points, good for 5 seconds | the Red home tower for 5 points, from the map of 3 seconds before + Shows: the Red home tower for 5 points: an advisor proposes that the first Blue drone takes the Red home tower, for a gain of 5 points + Shows: the Red home tower for 1 point: an advisor proposes the same, for a gain of 1 point + Shows: the Red home tower for 5 points, good for 5 seconds: an advisor proposes the same, for a gain of 5 points, good for 5 seconds + Shows: the Red home tower for 5 points, from the map of 3 seconds before: an advisor proposes the same, for a gain of 5 points, from the map as it was 3 seconds before + Event: the computer commander of Blue weighs the proposal + Say: wait for the next decision of the computer commander, which weighs the proposal in its hand + Event: the computer commander of Blue weighs the proposal 10 seconds later + Say: wait 10 seconds. Then wait for the next decision of the computer commander, which weighs the proposal in its hand + State: the advisor of the computer commander of Blue is the lookahead | none + Shows: the lookahead: the computer commander thinks ahead + Shows: none: the computer commander does not think ahead # The commander app (domains/teleoperation/command), in the Blue seat of a game on the server. State: the seat of the app is none | Blue | refused Shows: Blue: the top bar of the app says BLUE COMMANDER diff --git a/expectations/glossary.expect b/expectations/glossary.expect index f2b86cb..493463f 100644 --- a/expectations/glossary.expect +++ b/expectations/glossary.expect @@ -388,6 +388,19 @@ Term: Commander team, and gives a drone an order and its [[queue]]. It is a computer ([[captain]]) or a person at the commander app, in the team's [[seat]]. +Term: Advisor + In [[Towers]], what helps a [[commander]]: it looks at the map of the team, as the commander + does, and sends a [[proposal]] when it sees a better move. It does not give orders. It can be slow: its proposals then + reach the commander some seconds late. The first advisor is the lookahead, which plays the + game forward in a small model. A person who commands also has an advisor, which warns of + danger in words. + +Term: Proposal + In [[Towers]], what an [[advisor]] sends to a [[commander]]: one order for a drone, the + [[queue]] that goes with it, the points that the advisor expects to gain by it, and the time + after which it is stale. A computer commander takes a proposal that gains more than 2 points + and is not stale. + Term: Unit In [[Towers]], what flies one drone: one for each drone. It runs all the time, in contact or not, and knows only what its drone has on board: what its camera sees, its [[battery]], whether it is diff --git a/plans/actors.md b/plans/actors.md index ebc7aae..7c99036 100644 --- a/plans/actors.md +++ b/plans/actors.md @@ -148,6 +148,48 @@ server. reads. The lookahead moves out of `Commander` and is the first advisor. Then a Claude advisor, once every few seconds. The app gets "accept". Gate: a league contest, the commander with the advisor against the commander without, on paired seeds. + + **Results (advisors in the rules and the arena; the seat side is not built).** `recreation_towers` + has `Advise` (`advise(&mut self, view: &TeamView, hand: &Hand) -> Vec`), `Proposal + { drone, order, queue, gain, made, until, why }` and `Lookahead`, the old lookahead moved out of + `Commander` (same model, cadence and numbers). `Hand` is what the commander has in hand and the + map does not show: its orders after its own rules, the drones it may still change and the towers + each must stay away from; a seat has it, for the seat is the commander. A `Commander` holds + advisors, each with a delay in game time (`with_advisor(advisor, delay)`), and takes the best + proposal each decision that is not stale (`until`), gains more than `ADVICE_MARGIN` (2 points, a + constant: `CommanderTuning` is the league's candidate format and stays as it is) and still fits + (the drone is free and the tower is not one it gave up, nor, for a proposal made on an earlier map, + one the team holds now). `Commander::advise` puts proposals in hand from outside, for a person's + click or a process, and `Roster::with_advisor(team, advisor, delay)` gives a roster's team an + advisor in the arena. With `with_lookahead(false)` a commander has no advisor and does not think + ahead; `Commander::new` has the lookahead at delay 0 unless `SIM001_LOOKAHEAD=0`, as before. The + text `Advisor` is unchanged: its warnings are for a person and carry no order or gain, so it is not + an `Advise`. Not done: the seat side (proposals over the wire, the app's accept), a queue in the + lookahead's proposals (it proposes none), the Claude advisor. + + Gate, no drift: `denial`, `ruleset v2 200` and `judge --game captain --champion timid --challenger + default --seeds 24` are byte-identical before and after, with `SIM001_LOOKAHEAD` unset and with it + `0`. (`denial` and every policy of `ruleset` but the last line play with the lookahead off, so + only `judge` and the line "default against default" test it: Blue wins 50 % with it, 68 % without.) + + Gate, what advice is worth (`cargo run --release -p cognition_towers --example advice 400`: + commander + lookahead advisor at the delay, against a commander with no advisor, 400 seeds, both + sides, 800 games each; the lookahead's proposals are good for its 45 s horizon): + + | Delay | Margin | Won | Lost | + | --- | --- | --- | --- | + | 0 s | -0.47 ± 0.04 | 268 | 532 | + | 2 s | -0.46 ± 0.04 | 272 | 528 | + | 5 s | -0.44 ± 0.04 | 276 | 524 | + | 10 s | -0.28 ± 0.05 | 333 | 467 | + | 30 s | -0.81 ± 0.04 | 182 | 618 | + + In the arena by the default rules the lookahead is worth less than nothing: the commander that + thinks ahead loses to the one that does not, at every delay. Lateness up to 5 s costs nothing + measurable; at 10 s the harm is smaller and at 30 s it is worst. Why was not measured (the + proposals taken, and how many are stale or no longer fit, are not counted). So the + lookahead is a plumbing test for advisors, not a gain: it is the model and its enemy guesses that + need work, before advice from a slower and better advisor can be told from lateness. 5. **A person flies a unit.** A `Unit` seat. The server gives that drone's radio channels to the seat: the sim (or a TX15 on CRSF) joins a game as one drone, with the drone's camera and the mission on its HUD. The pilot sees only what the drone sees. Take-off is the pilot's. @@ -155,6 +197,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