Something went wrong. Try again.
Star Gambit Computer Player SDK
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155//! A tiny DSL for writing Star Gambit coms.//!//! Implement [`Com`] and hand it to [`run_as_com`]. The runner connects to a//! server as a registered com (authenticated with an API token), plays the//! games the server assigns it, and drives your com to the end of each game.//! Move/placement legality is provided by replaying candidate actions through//! the `stellar` engine, so a com only ever sees legal options.
mod cli;mod client;
pub use cli::{operate_com, DEFAULT_SERVER};pub use client::{run_as_com, ComError, Outcome};pub use stellar::{GameEngine, GameResult, SgnError};
/// The view a [`Com`] is given when it is asked to act.////// Wraps the authoritative game state ([`GameEngine`]) plus which player the/// com is (`0` or `1`).pub struct Game<'a> { engine: &'a GameEngine, me: i32,}
impl<'a> Game<'a> { /// The full game state. Use this for any custom inspection beyond /// [`Game::legal_actions`]. pub fn engine(&self) -> &GameEngine { self.engine }
/// Which player this com controls: `0` (moves first) or `1`. pub fn me(&self) -> i32 { self.me }
/// `true` while it is this com's turn and the game is unfinished. pub fn is_my_turn(&self) -> bool { self.engine.result.is_none() && self.engine.whos_turn == self.me }
/// Every legal SGN action in the current position, including `"END"` when /// the turn may be ended. Any of these may be returned from [`Com::act`] /// (one, several, or all of them); each is validated before it is sent. pub fn legal_actions(&self) -> Vec<String> { legal_actions(self.engine) }}
/// A Star Gambit com.////// [`Com::act`] is called when it becomes the com's turn, and again while the/// turn continues, until the com ends the turn.////// Return the action(s) to play, in order, each a single SGN action (one of/// [`Game::legal_actions`]). End the turn by including `"END"` as the final/// action. You may return the whole turn at once or one action per call.////// Each returned action is validated against the engine before it is sent;/// illegal actions are skipped and logged. A call that makes no progress/// (all-illegal or empty) is retried with the same state, but the same/// no-progress output three times in a row, or 20 consecutive no-progress/// calls, is treated as a crash and abandons the game.pub trait Com { fn act(&mut self, game: &Game) -> Vec<String>;}
/// All legal single-action SGN strings for `engine`'s current position.////// Each candidate is validated by replaying it through/// [`GameEngine::execute`], so this exactly matches what the engine accepts./// `"END"` is offered (rather than `"END:<time>"`) because the server stamps/// the timestamp itself.pub fn legal_actions(engine: &GameEngine) -> Vec<String> { let mut out = Vec::new(); if engine.result.is_some() { return out; }
// Placements: `<kind>@<placement>`. for kind_id in 0..engine.config.unit_kinds.len() { let placements = engine .config .placements .get(kind_id) .map(|p| p.len()) .unwrap_or(0); for placement_id in 0..placements { let candidate = format!("{kind_id}@{placement_id}"); if engine.execute(&candidate).is_ok() { out.push(candidate); } } }
// Moves (`<unit>+<move>`) and attacks (`<unit>*<attack>`). for unit_id in 0..engine.units.len() { let kind = &engine.units[unit_id].kind; for move_id in 0..kind.moves.len() { let candidate = format!("{unit_id}+{move_id}"); if engine.execute(&candidate).is_ok() { out.push(candidate); } } for attack_id in 0..kind.ray_attacks.len() { let candidate = format!("{unit_id}*{attack_id}"); if engine.execute(&candidate).is_ok() { out.push(candidate); } } }
// End turn. The engine needs a timestamp to validate it, but the server // expects a bare "END" (it adds the time), so we test one and emit the // other. if engine.execute("END:0").is_ok() { out.push("END".to_string()); }
out}
/// Apply one SGN token to `engine`, returning the resulting state.////// Translates the bare `"END"` coms emit into the timestamped `"END:<t>"` form/// the engine validates. The stamped time is a local placeholder/// (`engine.timestamp + 1`); it only feeds the clocks (server-authoritative)/// and never the position hash, so it is irrelevant off-server. Any other token/// is executed verbatim.pub fn apply(engine: &GameEngine, token: &str) -> Result<GameEngine, SgnError> { if token == "END" { engine.execute(&format!("END:{}", engine.timestamp + 1)) } else { engine.execute(token) }}
/// Apply a whole turn (a slice of tokens, normally ending in `"END"`) on top/// of `engine`, skipping any token the engine rejects, and return the resulting/// state. Stops early once the game ends or the turn passes to the opponent.pub fn apply_turn(engine: &GameEngine, turn: &[String]) -> GameEngine { let mut cur = engine.clone(); let me = engine.whos_turn; for token in turn { match apply(&cur, token) { Ok(next) => cur = next, Err(_) => continue, } if cur.result.is_some() || cur.whos_turn != me { break; } } cur}