Something went wrong. Try again.
Star Gambit Computer Player SDK
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152//! Hex geometry, ported from `shared/hexag`.//!//! The Dart engine models positions with `FractionalHex` (doubles) and a//! `num` rotation, but during real SGN play every translation is an//! integer-valued cube coordinate and every rotation is an integer number//! of sixths. The continuous (trigonometric) rotation path in//! `HexRotator.rotate` is only ever hit for fractional rotations, which the//! notation never produces. We therefore model translation with an integer//! [`Hex`] and rotation with an `i32`, which lets the whole engine run on//! exact integer arithmetic while staying bit-identical to the Dart state.
/// A hex on the grid in cube coordinates. Invariant: `x + y + z == 0`.#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]pub struct Hex { pub x: i32, pub y: i32, pub z: i32,}
impl Hex { pub const ORIGIN: Hex = Hex { x: 0, y: 0, z: 0 };
pub const fn new(x: i32, y: i32, z: i32) -> Hex { Hex { x, y, z } }
/// Ring number: 0 at the origin, growing outward. Equals /// `(|x| + |y| + |z|) / 2`. pub fn ring(self) -> i32 { (self.x.abs() + self.y.abs() + self.z.abs()) / 2 }}
impl std::ops::Add for Hex { type Output = Hex; fn add(self, o: Hex) -> Hex { Hex::new(self.x + o.x, self.y + o.y, self.z + o.z) }}
impl std::ops::Sub for Hex { type Output = Hex; fn sub(self, o: Hex) -> Hex { Hex::new(self.x - o.x, self.y - o.y, self.z - o.z) }}
/// The six faces of a hexagon, clockwise. Matches Dart `HexFace`.#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]pub enum HexFace { A, B, C, D, E, F,}
impl HexFace { pub fn index(self) -> usize { match self { HexFace::A => 0, HexFace::B => 1, HexFace::C => 2, HexFace::D => 3, HexFace::E => 4, HexFace::F => 5, } }
/// The unit direction vector for this face. pub fn vector(self) -> Hex { FACE_DIRECTIONS[self.index()] }}
/// Direction vectors, indexed A..F. Mirrors `HexVectors.faceDirections`.pub const A: Hex = Hex { x: 0, y: -1, z: 1 };pub const B: Hex = Hex { x: 1, y: -1, z: 0 };pub const C: Hex = Hex { x: 1, y: 0, z: -1 };pub const D: Hex = Hex { x: 0, y: 1, z: -1 };pub const E: Hex = Hex { x: -1, y: 1, z: 0 };pub const F: Hex = Hex { x: -1, y: 0, z: 1 };
pub const FACE_DIRECTIONS: [Hex; 6] = [A, B, C, D, E, F];
/// Discrete rotation about the origin. `rot` is in sixths of a turn,/// clockwise; only `rot mod 6` matters. Mirrors the `_hexRotators` table.pub fn discrete_rotate(h: Hex, rot: i32) -> Hex { match rot.rem_euclid(6) { 0 => Hex::new(h.x, h.y, h.z), 1 => Hex::new(-h.y, -h.z, -h.x), 2 => Hex::new(h.z, h.x, h.y), 3 => Hex::new(-h.x, -h.y, -h.z), 4 => Hex::new(h.y, h.z, h.x), 5 => Hex::new(-h.z, -h.x, -h.y), _ => unreachable!(), }}
/// A translate/rotate transform. `translate` is the location of local/// `Hex(0,0,0)` in the outer coordinate system; the local frame is also/// rotated clockwise by `rotate` sixths of a turn.////// `rotate` is kept as the raw accumulated value (not reduced mod 6) so that/// transform equality matches Dart's `HexTransform == ` (which compares the/// raw `rotate`).#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]pub struct HexTransform { pub translate: Hex, pub rotate: i32,}
impl HexTransform { pub const IDENTITY: HexTransform = HexTransform { translate: Hex::ORIGIN, rotate: 0, };
pub const fn new(translate: Hex, rotate: i32) -> HexTransform { HexTransform { translate, rotate } }
pub const fn from_translate(translate: Hex) -> HexTransform { HexTransform { translate, rotate: 0, } }
/// Converts a hex in local coordinates to the outer coordinate system. /// Exact (integer) because rotation is always discrete here. pub fn from_local(self, h: Hex) -> Hex { discrete_rotate(h, self.rotate) + self.translate }
/// Combines transforms: `self` is a position, `other` a move taken from /// it. NOT commutative. Mirrors `HexTransform.operator+`. pub fn combine(self, other: HexTransform) -> HexTransform { let om = other.rotate.rem_euclid(6); let rot = if om > 3 { self.rotate + om - 6 } else { self.rotate + other.rotate }; HexTransform { translate: self.from_local(other.translate), rotate: rot, } }}