//! The game's objects as a renderer wants them: one entry per sprite slot, //! in cabinet (portrait, 224x288) screen coordinates, with the game's own //! state byte for that slot. Read-only over `Shared`; the board is unchanged. //! //! Where the facts come from is in `docs/ram-map.md`: the sprite decode is //! `Video::draw_sprites` (MAME's `draw_sprites`) followed by the rotation in //! `Video::screen_rgba`; the state table at 0x8800 is measured with //! `galaga-probe` and cross-checked against the community disassembly. use super::bus::Shared; /// The object status table: 64 slots x 2 bytes at 0x8800 (RAM 1 offset 0). pub const STATUS_BASE: usize = 0x000; /// Values of the state byte (`0x8800 + 2 * slot`) seen in play; see /// `docs/ram-map.md` for what each one was measured to be. pub mod state { pub const RESCUED_FIGHTER: u8 = 0x00; pub const IN_FORMATION: u8 = 0x01; pub const HOME_ROTATING: u8 = 0x02; pub const ENTERING: u8 = 0x03; pub const EXPLODING: u8 = 0x04; pub const SCORE_POPUP: u8 = 0x05; pub const SHOT: u8 = 0x06; pub const SPAWNED: u8 = 0x07; pub const FIGHTER_EXPLODING: u8 = 0x08; pub const DIVING: u8 = 0x09; /// Any value with bit 7 set; a live fighter or rocket also reads 0x80. pub const FREE: u8 = 0x80; } /// Slot ranges (`docs/ram-map.md`, "Slot roles"). pub mod slot { pub const CAPTURED_FIGHTERS: std::ops::RangeInclusive = 0x00..=0x03; pub const BEES: std::ops::RangeInclusive = 0x04..=0x17; pub const BOSSES: std::ops::RangeInclusive = 0x18..=0x1b; pub const TRANSIENTS: std::ops::RangeInclusive = 0x1c..=0x1f; pub const BUTTERFLIES: std::ops::RangeInclusive = 0x20..=0x2f; pub const FIGHTER_2: u8 = 0x30; pub const FIGHTER: u8 = 0x31; pub const ROCKETS: std::ops::RangeInclusive = 0x32..=0x33; pub const BOMBS: std::ops::RangeInclusive = 0x34..=0x3b; } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Object { pub slot: u8, /// `0x8800 + 2 * slot`: the game's disposition of the object. pub state: u8, /// `0x8801 + 2 * slot`: an index or counter whose meaning depends on the /// state (motion-queue offset while flying, a countdown while exploding). pub aux: u8, /// Sprite code (0-127) and colour (0-63) as the hardware draws them. pub code: u8, pub color: u8, /// Top-left corner and size on the cabinet screen, in pixels. May lie /// partly or wholly off the 224x288 screen. pub x: i32, pub y: i32, pub w: i32, pub h: i32, /// The hardware flip bits, in the raster's (landscape) frame: raster X /// runs down the cabinet screen and raster Y runs right to left. pub flip_x: bool, pub flip_y: bool, } impl Object { /// Whether any of the sprite lands on the 224x288 screen. pub fn on_screen(&self) -> bool { self.x < 224 && self.x + self.w > 0 && self.y < 288 && self.y + self.h > 0 } pub fn center(&self) -> (i32, i32) { (self.x + self.w / 2, self.y + self.h / 2) } } /// Every slot's object this frame, from the live sprite registers (the /// upper 128 bytes of each work RAM), whatever its state. The flip bit /// (video latch bit 7, which the game sets only at the end of the power-on /// test) mirrors /// sprite pictures but not their positions in this pipeline, so the boxes /// hold either way. pub fn objects(shared: &Shared) -> [Object; 64] { std::array::from_fn(|n| { let o = 0x380 + 2 * n; let flags = shared.ram3[o]; let double_w = i32::from((flags >> 2) & 1); let double_h = i32::from((flags >> 3) & 1); // Raster position, as Video::draw_sprites computes it. let rx = i32::from(shared.ram2[o + 1]) + 256 * i32::from(shared.ram3[o + 1] & 3) - 40; let ry = ((256 - i32::from(shared.ram2[o]) + 1 - 16 * double_h) & 0xff) - 32; // Screen (x, y) = (223 - raster y, raster x). let w = 16 * (1 + double_h); let h = 16 * (1 + double_w); Object { slot: n as u8, state: shared.ram1[STATUS_BASE + 2 * n], aux: shared.ram1[STATUS_BASE + 2 * n + 1], code: shared.ram1[o] & 0x7f, color: shared.ram1[o + 1] & 0x3f, x: 224 - ry - w, y: rx, w, h, flip_x: flags & 1 != 0, flip_y: flags & 2 != 0, } }) }