Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions domains/recreation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down Expand Up @@ -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).

Expand Down
19 changes: 18 additions & 1 deletion domains/recreation/towers/src/seat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team> {
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),
Expand Down Expand Up @@ -97,6 +110,10 @@ pub enum ToSeat {
Umpire(Umpire),
/// What the team's towers and base report, to a seat that commands.
Sites(Sites),
/// The orders the team's drones have now (by drone number in the team: the order, and whether it
/// is flown low), once, right after the welcome of a seat that commands: a commander that takes
/// over a game goes on from them.
Orders(Vec<(Option<Order>, bool)>),
Truth(Truth),
/// The advisor of the team speaks, to a seat that commands it, when something matters.
Advice(Advice),
Expand Down
4 changes: 4 additions & 0 deletions domains/recreation/towers_ground/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
26 changes: 26 additions & 0 deletions domains/recreation/towers_ground/src/bin/towers_commander.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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));
}
73 changes: 73 additions & 0 deletions domains/recreation/towers_ground/src/commander.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! The computer commander: a seat like a person's. It takes a `Command` seat on the game server
//! (as `Join::Compute`, a seat that gives way to a person), flies the team's drones from the
//! station of the seat (`station`: its own ground, the map from telemetry) and, once a second of
//! game time, asks [`recreation_towers::Commander`] for orders on the map the station built. It
//! knows only what a seat gets.

use crate::seat::{Seat, Status};
use crate::station::{self, Tally};
use recreation_towers::seat::Join;
use recreation_towers::{Class, Commander, Team};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

/// How often the commander decides (s of the game).
pub const DECIDE_EVERY: f32 = 1.0;

/// What the commander does with its seat.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Brain {
/// Plays: decides once a second.
Play,
/// Sits in the seat and gives no orders: its drones fly home (a game to practise in).
Idle,
}

/// Takes the seat of `team` on the server at `addr` (`host:port`) and commands it until the game is
/// over, a person takes the seat, or `stop` is set. If the seat is held, it waits for it to be free.
pub fn run(addr: &str, team: Team, brain: Brain, tally: Option<Arc<Tally>>, stop: &AtomicBool) {
let host = addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string();
let seat = Seat::connect(addr, Join::Compute(team));
station::start(seat.clone(), host, tally);
let mut commander: Option<(Commander, f32)> = None;
while !stop.load(Ordering::SeqCst) {
std::thread::sleep(Duration::from_millis(50));
let (status, rules, view) = {
let s = seat.state();
(s.status.clone(), s.rules, s.view.clone())
};
match status {
// Relieved by a person, or refused for good.
Status::Refused(why) if !why.contains("has its commander") => break,
Status::Seated(_) => {}
_ => {
commander = None;
continue;
}
}
let (Some(rules), Some(view)) = (rules, view) else { continue };
if view.outcome.is_some() {
break;
}
if brain == Brain::Idle || view.drones.len() != rules.drones || view.time <= 0.0 {
continue;
}
// The seat starts from the orders the drones have: they are on the map.
let (c, next) = commander.get_or_insert_with(|| {
let mut c = Commander::new(rules);
for d in &view.drones {
c.assign(d.k, d.order);
}
(c, f32::NEG_INFINITY)
});
if view.time < *next {
continue;
}
*next = view.time + DECIDE_EVERY;
for (k, order) in c.decide(&view) {
seat.order(k, order, rules.class(k) == Class::Light);
}
}
seat.stop();
}
7 changes: 6 additions & 1 deletion domains/recreation/towers_ground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Loading
Loading