diff --git a/glitch/Cargo.toml b/glitch/Cargo.toml new file mode 100644 index 0000000..74b2c2e --- /dev/null +++ b/glitch/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "glitch" +edition.workspace = true +license.workspace = true +version.workspace = true + +[dependencies] +cmf-wp2-app.workspace = true +cmf-wp2-ui.workspace = true + +[[bin]] +name = "glitch" +bench = false diff --git a/glitch/LICENSE b/glitch/LICENSE new file mode 100644 index 0000000..1f27f13 --- /dev/null +++ b/glitch/LICENSE @@ -0,0 +1,19 @@ +MIT License + +Copyright (c) 2026 X0D4N + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This licence covers the app code (src/*.rs except src/sprites.rs, tools/, icon.svg, docs). The bitmaps in +src/sprites.rs come from Chromium and stay under the BSD-3-Clause licence in NOTICE-CHROMIUM. diff --git a/glitch/NOTICE-CHROMIUM b/glitch/NOTICE-CHROMIUM new file mode 100644 index 0000000..0655883 --- /dev/null +++ b/glitch/NOTICE-CHROMIUM @@ -0,0 +1,41 @@ +Third-party notice: Chromium offline dinosaur game +================================================== + +src/sprites.rs holds 1-bit bitmaps cut from the sprite sheet of the Chromium offline "T-Rex runner" game +(components/neterror/resources/images/default_100_percent/offline/100-offline-sprite.png), and the physics in +src/dino.rs is a port of the constants and rules in components/neterror/resources/dino_game/*.ts, rewritten in +integer Rust. Source: https://chromium.googlesource.com/chromium/src/+/main/components/neterror/resources/ + +Credit: the T-Rex, cacti, pterodactyl, cloud and ground art and the game's rules are (c) The Chromium Authors. + +The Chromium LICENSE follows verbatim (fetched from https://chromium.googlesource.com/chromium/src/+/main/LICENSE): + +---------------------------------------------------------------------------------------------------------------- + +// Copyright 2015 The Chromium Authors +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google LLC nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/glitch/icon.svg b/glitch/icon.svg new file mode 100644 index 0000000..fb9b54a --- /dev/null +++ b/glitch/icon.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/glitch/shots/2048.png b/glitch/shots/2048.png new file mode 100644 index 0000000..30b087e Binary files /dev/null and b/glitch/shots/2048.png differ diff --git a/glitch/shots/bottle.png b/glitch/shots/bottle.png new file mode 100644 index 0000000..ba05fdc Binary files /dev/null and b/glitch/shots/bottle.png differ diff --git a/glitch/shots/bottle_spin.png b/glitch/shots/bottle_spin.png new file mode 100644 index 0000000..3d7a6a4 Binary files /dev/null and b/glitch/shots/bottle_spin.png differ diff --git a/glitch/shots/bottle_stop.png b/glitch/shots/bottle_stop.png new file mode 100644 index 0000000..dd7c13b Binary files /dev/null and b/glitch/shots/bottle_stop.png differ diff --git a/glitch/shots/dice.png b/glitch/shots/dice.png new file mode 100644 index 0000000..1e32970 Binary files /dev/null and b/glitch/shots/dice.png differ diff --git a/glitch/shots/dice_rolled.png b/glitch/shots/dice_rolled.png new file mode 100644 index 0000000..9f48cd0 Binary files /dev/null and b/glitch/shots/dice_rolled.png differ diff --git a/glitch/shots/dino.png b/glitch/shots/dino.png new file mode 100644 index 0000000..4e86be6 Binary files /dev/null and b/glitch/shots/dino.png differ diff --git a/glitch/shots/dino_jump.png b/glitch/shots/dino_jump.png new file mode 100644 index 0000000..761b9f0 Binary files /dev/null and b/glitch/shots/dino_jump.png differ diff --git a/glitch/shots/g2048_play.png b/glitch/shots/g2048_play.png new file mode 100644 index 0000000..fb8816b Binary files /dev/null and b/glitch/shots/g2048_play.png differ diff --git a/glitch/shots/hub.png b/glitch/shots/hub.png new file mode 100644 index 0000000..d2d2e5c Binary files /dev/null and b/glitch/shots/hub.png differ diff --git a/glitch/shots/snake.png b/glitch/shots/snake.png new file mode 100644 index 0000000..faf1bcf Binary files /dev/null and b/glitch/shots/snake.png differ diff --git a/glitch/shots/snake_play.png b/glitch/shots/snake_play.png new file mode 100644 index 0000000..f15e274 Binary files /dev/null and b/glitch/shots/snake_play.png differ diff --git a/glitch/shots/tictactoe.png b/glitch/shots/tictactoe.png new file mode 100644 index 0000000..5bea8bb Binary files /dev/null and b/glitch/shots/tictactoe.png differ diff --git a/glitch/shots/tictactoe_win.png b/glitch/shots/tictactoe_win.png new file mode 100644 index 0000000..ebfbc7c Binary files /dev/null and b/glitch/shots/tictactoe_win.png differ diff --git a/glitch/shots/ttt_play.png b/glitch/shots/ttt_play.png new file mode 100644 index 0000000..27ef377 Binary files /dev/null and b/glitch/shots/ttt_play.png differ diff --git a/glitch/src/app.rs b/glitch/src/app.rs new file mode 100644 index 0000000..6e65be5 --- /dev/null +++ b/glitch/src/app.rs @@ -0,0 +1,242 @@ +// The hub: a list of games, one tap to start, back (crown press / right swipe) to return. Pure logic over a +// `Surface`, so the host tests drive it exactly as the device loop does. + +use crate::dino::Dino; +use crate::g2048::G2048; +use crate::bottle::Bottle; +use crate::dice::Dice; +use crate::gfx::*; +use crate::snake::Snake; +use crate::ttt::Ttt; +use cmf_wp2_ui::haptic::{DONE, DOUBLE, Pattern, TICK}; +use cmf_wp2_ui::text::chars; +use cmf_wp2_ui::Surface; + +pub const NAMES: [&str; 6] = ["DINO RUN", "SNAKE", "BOTTLE", "DICE", "2048", "TIC TAC TOE"]; +const MAGIC: u8 = 0x47; +const ROW0: i32 = 96; +#[cfg(test)] +pub const ROW0_FOR_TESTS: i32 = ROW0; +const ROWH: i32 = 52; + +pub enum Cur { + Dino(Dino), + Snake(Snake), + Bottle(Bottle), + Dice(Dice), + G2048(G2048), + Ttt(Ttt), +} + +macro_rules! each { + ($c:expr, $g:ident => $e:expr) => { + match $c { + Cur::Dino($g) => $e, + Cur::Snake($g) => $e, + Cur::Bottle($g) => $e, + Cur::Dice($g) => $e, + Cur::G2048($g) => $e, + Cur::Ttt($g) => $e, + } + }; +} + +#[derive(Default, Clone, Copy)] +pub struct Out { + pub haptic: Option, + pub save: bool, + pub exit: bool, + pub redraw: bool, +} + +pub struct App { + pub cur: Option, + pub idx: usize, + pub sel: usize, + pub best: [u32; 6], + pub seed: u32, + need_full: bool, +} + +fn out(ev: u8) -> Out { + Out { + haptic: if ev & EV_DONE != 0 { + Some(DONE) + } else if ev & EV_DOUBLE != 0 { + Some(DOUBLE) + } else if ev & EV_TICK != 0 { + Some(TICK) + } else { + None + }, + save: ev & EV_DONE != 0, + exit: false, + redraw: ev & EV_DIRTY != 0, + } +} + +impl App { + pub fn new(blob: &[u8]) -> App { + let mut best = [0u32; 6]; + if blob.len() >= 25 && blob[0] == MAGIC { + for i in 0..6 { + best[i] = u32::from_le_bytes([blob[1 + i * 4], blob[2 + i * 4], blob[3 + i * 4], blob[4 + i * 4]]); + } + } + App { cur: None, idx: 0, sel: 0, best, seed: 1, need_full: true } + } + + pub fn blob(&self) -> [u8; 25] { + let mut b = [0u8; 25]; + b[0] = MAGIC; + for i in 0..6 { + b[1 + i * 4..5 + i * 4].copy_from_slice(&self.best[i].to_le_bytes()); + } + b + } + + pub fn full(&mut self) { + self.need_full = true; + } + + pub fn in_game(&self) -> bool { + self.cur.is_some() + } + + /// ms until the next tick, 0 when nothing animates. + pub fn period(&self) -> u32 { + match &self.cur { + Some(c) => each!(c, g => if g.running() { g.period() } else { 0 }), + None => 0, + } + } + + fn launch(&mut self, i: usize) { + let (seed, best) = (self.seed, self.best[i]); + self.cur = Some(match i { + 0 => Cur::Dino(Dino::new(seed, best)), + 1 => Cur::Snake(Snake::new(seed, best)), + 2 => Cur::Bottle(Bottle::new(seed, best)), + 3 => Cur::Dice(Dice::new(seed, best)), + 4 => Cur::G2048(G2048::new(seed, best)), + _ => Cur::Ttt(Ttt::new(seed, best)), + }); + self.idx = i; + self.need_full = true; + } + + fn note(&mut self, mut o: Out) -> Out { + if let Some(c) = &self.cur { + let sc = each!(c, g => g.score()); + if sc > self.best[self.idx] { + self.best[self.idx] = sc; + if o.save || o.haptic.is_some() && matches!(c, Cur::Ttt(_)) { + o.save = true; + } + } + } + o + } + + pub fn touch(&mut self, x: i32, y: i32, down: bool) -> Out { + if let Some(c) = &mut self.cur { + let ev = each!(c, g => g.touch(x, y, down)); + return self.note(out(ev)); + } + if down && y >= ROW0 && y < ROW0 + ROWH * 6 { + let i = ((y - ROW0) / ROWH) as usize; + self.sel = i; + self.launch(i); + return Out { haptic: Some(TICK), redraw: true, ..Out::default() }; + } + Out::default() + } + + pub fn crown(&mut self, d: i32) -> Out { + if let Some(c) = &mut self.cur { + let ev = each!(c, g => g.crown(d)); + return self.note(out(ev)); + } + self.sel = (self.sel as i32 + d.signum() + 6) as usize % 6; + Out { haptic: Some(TICK), redraw: true, ..Out::default() } + } + + pub fn tick(&mut self) -> Out { + if let Some(c) = &mut self.cur { + let ev = each!(c, g => g.tick()); + return self.note(out(ev)); + } + Out::default() + } + + /// Back: a game returns to the hub, the hub leaves the app. + pub fn back(&mut self) -> Out { + if self.cur.is_some() { + self.cur = None; + self.need_full = true; + return Out { save: true, redraw: true, ..Out::default() }; + } + Out { save: true, exit: true, ..Out::default() } + } + + pub fn draw(&mut self, s: &mut S) { + let full = self.need_full; + self.need_full = false; + if let Some(c) = &mut self.cur { + each!(c, g => g.draw(s, full)); + return; + } + clear(s); + txt(s, C, 40, "GAMES", 4, 3, RED); + for (i, n) in NAMES.iter().enumerate() { + let y = ROW0 + ROWH * i as i32 + 14; + let on = i == self.sel; + txt(s, C, y, n, 3, 2, if on { WHITE } else { MID }); + if on { + dot(s, C - chars(n) as i32 * 12 - 22, y + 11, 10, RED); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scores_roundtrip_through_the_blob() { + let mut a = App::new(&[]); + a.best = [1, 2, 3, 4, 5, 6]; + let b = a.blob(); + assert_eq!(App::new(&b).best, [1, 2, 3, 4, 5, 6]); + assert_eq!(App::new(&[9, 9, 9]).best, [0; 6]); + } + + #[test] + fn hub_launches_every_game_and_back_returns() { + for i in 0..6 { + let mut a = App::new(&[]); + let o = a.touch(233, ROW0 + ROWH * i + 20, true); + assert!(o.redraw && a.in_game() && a.idx == i as usize); + let o = a.back(); + assert!(!o.exit && !a.in_game()); + } + let mut a = App::new(&[]); + assert!(a.back().exit); + assert!(!a.touch(233, 20, true).redraw); // title area does nothing + } + + #[test] + fn high_score_is_kept_and_saved_on_game_over() { + let mut a = App::new(&[]); + a.touch(233, ROW0 + 10, true); // dino + a.touch(233, 233, true); // start + for _ in 0..4000 { + let o = a.tick(); + if o.save { + break; + } + } + assert!(a.best[0] > 0, "no score recorded"); + } +} diff --git a/glitch/src/bottle.rs b/glitch/src/bottle.rs new file mode 100644 index 0000000..555ac07 --- /dev/null +++ b/glitch/src/bottle.rs @@ -0,0 +1,236 @@ +// Spin the Bottle, after the Nothing widget: a dark grey field says SPIN ME. Tap and a bottle made of small white squares +// spins and slows down (a haptic click per notch). When it stops the field turns red, says TRUTH or DARE, and the +// bottle's neck, marked with a small white triangle, points wherever it landed. Tap again to reset. + +use crate::gfx::*; +use cmf_wp2_ui::draw::disc; +use cmf_wp2_ui::motion::{cos, sin}; +use cmf_wp2_ui::Surface; + +const SQ: i32 = 11; // square side +const NOTCH: i32 = 65536 / 16; +/// The pointer triangle sits this far from the bottle's centre along the neck. +const TIP: i32 = 168; +/// The mosaic's lattice axis relative to the neck, 44 deg in turn units (65536 = 360). +const LAT: i32 = 8010; +/// The squares of the real bottle, as measured centres (px, neck up), read off a clean screenshot. +const CELLS: [(i16, i16); 48] = [(17, -132), (-2, -131), (-21, -131), (17, -107), (-20, -107), (17, -88), (-20, -88), (17, -69), (-20, -69), (18, -50), (-20, -50), (28, -41), (-29, -40), (37, -32), (-39, -31), (37, -13), (-38, -11), (28, -3), (9, -3), (-10, -3), (-28, -2), (38, 6), (-38, 7), (29, 16), (10, 16), (-9, 16), (-28, 17), (38, 25), (-37, 26), (29, 34), (10, 35), (-9, 35), (-28, 36), (39, 44), (-37, 45), (29, 53), (10, 54), (-9, 54), (-28, 54), (39, 63), (-37, 64), (39, 82), (-36, 83), (40, 101), (21, 101), (2, 101), (-17, 102), (-36, 102)]; + +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum St { + Idle, + Spin, + Done, +} + +pub struct Bottle { + rng: Rng, + pub st: St, + angle: i32, + vel: i32, + drawn: Option, + pub dare: bool, + redraw: bool, +} + +/// A square of side `d` turned by the lattice angle, filled row by row (integer only). +fn rot_sq(s: &mut S, cx: i32, cy: i32, d: i32, sn: i32, cs: i32, c: u16) { + let r = d * 3 / 4 + 1; + let lim = d << 13; // |2u| <= d in Q14 + for dy in -r..=r { + let (mut x0, mut x1) = (i32::MAX, i32::MIN); + for dx in -r..=r { + let u = dx * cs + dy * sn; + let v = -dx * sn + dy * cs; + if u.abs() <= lim && v.abs() <= lim { + x0 = x0.min(dx); + x1 = x1.max(dx); + } + } + if x0 <= x1 { + s.rect(cx + x0, cy + dy, x1 - x0 + 1, 1, c); + } + } +} + +impl Bottle { + /// `a` = 0 points the neck up. Squares and lattice turn together, like the real mosaic. + fn squares(&self, s: &mut S, a: i32, c: u16, tc: u16, d: i32) { + let phi = (a + LAT) as u16; + let (sn, cs) = (sin(phi), cos(phi)); // Q14 + let (sa, ca) = (sin(a as u16), cos(a as u16)); + for &(px, py) in CELLS.iter() { + let (px, py) = (px as i32, py as i32); + let x = C + ((px * ca - py * sa) >> 14); + let y = C + ((px * sa + py * ca) >> 14); + rot_sq(s, x, y, d, sn, cs, c); + } + // the pointer: a small triangle past the mouth, apex outward + let (na, nb) = (sin(a as u16), cos(a as u16)); // neck direction = (na, -nb) + let (px, py) = (C + ((TIP * na) >> 14), C - ((TIP * nb) >> 14)); + let col = tc; + let pts = [(8i32, 0i32), (-6, -7), (-6, 7)]; // (along, across) + let v: [(i32, i32); 3] = core::array::from_fn(|i| { + let (al, ac) = pts[i]; + (px + ((al * na + ac * nb) >> 14), py + ((-al * nb + ac * na) >> 14)) + }); + let (minx, maxx) = (v.iter().map(|p| p.0).min().unwrap_or(0) - 1, v.iter().map(|p| p.0).max().unwrap_or(0) + 1); + let (miny, maxy) = (v.iter().map(|p| p.1).min().unwrap_or(0) - 1, v.iter().map(|p| p.1).max().unwrap_or(0) + 1); + let edge = |a: (i32, i32), b: (i32, i32), x: i32, y: i32| (b.0 - a.0) * (y - a.1) - (b.1 - a.1) * (x - a.0); + for y in miny..=maxy { + let (mut x0, mut x1) = (i32::MAX, i32::MIN); + for x in minx..=maxx { + let (e0, e1, e2) = (edge(v[0], v[1], x, y), edge(v[1], v[2], x, y), edge(v[2], v[0], x, y)); + if (e0 >= 0 && e1 >= 0 && e2 >= 0) || (e0 <= 0 && e1 <= 0 && e2 <= 0) { + x0 = x0.min(x); + x1 = x1.max(x); + } + } + if x0 <= x1 { + s.rect(x0, y, x1 - x0 + 1, 1, col); + } + } + } +} + +impl Game for Bottle { + fn new(seed: u32, _best: u32) -> Self { + Bottle { rng: Rng::new(seed), st: St::Idle, angle: 0, vel: 0, drawn: None, dare: false, redraw: true } + } + + fn period(&self) -> u32 { + 33 + } + + fn running(&self) -> bool { + self.st == St::Spin + } + + fn tick(&mut self) -> u8 { + if self.st != St::Spin { + return 0; + } + let before = self.angle.div_euclid(NOTCH); + self.angle = (self.angle + self.vel) & 0xFFFF; + let after = self.angle.div_euclid(NOTCH); + self.vel = self.vel * 247 / 256; + let mut ev = EV_DIRTY; + if before != after && self.vel > 600 { + ev |= EV_TICK; + } + if self.vel < 60 { + self.st = St::Done; + self.redraw = true; + ev |= EV_DONE; + } + ev + } + + fn touch(&mut self, _x: i32, _y: i32, down: bool) -> u8 { + if !down { + return 0; + } + match self.st { + St::Idle => { + self.vel = 6000 + self.rng.below(4000) as i32; + self.angle = self.rng.below(65536) as i32; + self.dare = self.rng.below(2) == 1; + self.st = St::Spin; + self.redraw = true; + EV_TICK | EV_DIRTY + } + St::Done => { + self.st = St::Idle; + self.redraw = true; + EV_DIRTY + } + St::Spin => 0, + } + } + + fn crown(&mut self, d: i32) -> u8 { + if d != 0 && self.st != St::Spin { self.touch(0, 0, true) } else { 0 } + } + + fn score(&self) -> u32 { + 0 + } + + fn draw(&mut self, s: &mut S, full: bool) { + if full || self.redraw { + self.redraw = false; + self.drawn = None; + clear(s); + match self.st { + St::Idle => txt(s, C, C - 16, "SPIN ME", 4, 3, WHITE), + St::Done => { + disc(s, C, C, 232, REDC, BGC); + txt_adv(s, C, 89, if self.dare { "Dare" } else { "Truth" }, 5, 3, 31, WHITE); + } + St::Spin => {} + } + } + match self.st { + St::Spin => { + if let Some(a) = self.drawn { + self.squares(s, a, BG, BG, SQ + 3); + } + self.squares(s, self.angle, WHITE, RED, SQ); + self.drawn = Some(self.angle); + } + St::Done => { + if self.drawn.is_none() { + self.squares(s, self.angle, WHITE, WHITE, SQ); + self.drawn = Some(self.angle); + } + } + St::Idle => {} + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn spin_stops_after_turns_and_lands_anywhere() { + let mut ends = [0u8; 4]; + for seed in 1..40 { + let mut g = Bottle::new(seed * 7919, 0); + g.touch(0, 0, true); + let (mut turned, mut n, mut ticks) = (0i64, 0, 0); + let mut last = g.angle; + while g.running() && n < 2000 { + if g.tick() & EV_TICK != 0 { + ticks += 1; + } + turned += (g.angle - last).rem_euclid(65536) as i64; + last = g.angle; + n += 1; + } + assert_eq!(g.st, St::Done, "never stopped"); + assert!(turned > 65536 * 2 && ticks > 10); + ends[(g.angle / 16384) as usize] += 1; + assert_eq!(g.touch(0, 0, true) & EV_DIRTY, EV_DIRTY); // back to idle + assert_eq!(g.st, St::Idle); + } + assert!(ends.iter().all(|n| *n > 0), "lands only in some quadrants: {ends:?}"); + } + + #[test] + fn shape_has_a_bottle_profile() { + assert!((46..=50).contains(&CELLS.len()), "{}", CELLS.len()); + } +} + +#[cfg(test)] +impl Bottle { + pub fn set_ref(&mut self, angle: i32, st: St, dare: bool) { + self.angle = angle; + self.st = st; + self.dare = dare; + self.redraw = true; + self.drawn = None; + } +} diff --git a/glitch/src/dice.rs b/glitch/src/dice.rs new file mode 100644 index 0000000..b10a6d0 --- /dev/null +++ b/glitch/src/dice.rs @@ -0,0 +1,169 @@ +// Dice Roller, after the Nothing widget: two dark grey rounded dice, tilted, overlapping a little, white pips and a red +// pip on a 1. Tap to roll: they tumble, then land on random faces at random tilts. The total sits below in dots. + +use crate::gfx::*; +use cmf_wp2_ui::color::{Rgb, rgb565}; +use cmf_wp2_ui::draw::disc; +use cmf_wp2_ui::motion::{cos, sin}; +use cmf_wp2_ui::Surface; + +/// 3x3 pip masks, row-major bit 0 = top-left. +const PIPS: [u16; 7] = [0, 0b000_010_000, 0b100_000_001, 0b100_010_001, 0b101_000_101, 0b101_010_101, 0b101_101_101]; +const H: i32 = 63; // half side +const R: i32 = 23; // corner radius +const BODY: u16 = rgb565(45, 46, 53); +const BODYC: Rgb = Rgb(45, 46, 53); +const CY: i32 = C - 35; +const CX: [i32; 2] = [C - 67, C + 67]; +const DY: [i32; 2] = [-14, 15]; + +pub struct Dice { + rng: Rng, + pub face: [u8; 2], + tilt: [i32; 2], // angle units, 65536 = a turn + left: u8, + redraw: bool, +} + +/// A rounded square (an octagon with short chamfers), `h` half side, turned by `a`, filled row by row. +fn body(s: &mut S, cx: i32, cy: i32, h: i32, a: i32, c: u16) { + let (sn, cs) = (sin(a as u16), cos(a as u16)); + let r = h * 142 / 100 + 1; + for dy in -r..=r { + let (mut x0, mut x1) = (i32::MAX, i32::MIN); + for dx in -r..=r { + let u = (dx * cs + dy * sn) >> 14; + let v = (-dx * sn + dy * cs) >> 14; + let (au, av) = (u.abs(), v.abs()); + let (ex, ey) = ((au - (h - R)).max(0), (av - (h - R)).max(0)); + if au <= h && av <= h && ex * ex + ey * ey <= R * R { + x0 = x0.min(dx); + x1 = x1.max(dx); + } + } + if x0 <= x1 { + s.rect(cx + x0, cy + dy, x1 - x0 + 1, 1, c); + } + } +} + +fn die(s: &mut S, cx: i32, cy: i32, face: u8, a: i32) { + body(s, cx, cy, H, a, BODY); + let (sn, cs) = (sin(a as u16), cos(a as u16)); + let m = PIPS[face as usize]; + for i in 0..9 { + if m >> i & 1 != 0 { + let (u, v) = ((i % 3 - 1) * 34, (i / 3 - 1) * 34); + let (x, y) = (cx + ((u * cs - v * sn) >> 14), cy + ((u * sn + v * cs) >> 14)); + disc(s, x, y, 10, if face == 1 { REDC } else { WHITEC }, BODYC); + } + } +} + +impl Dice { + fn shuffle(&mut self) { + self.face = [1 + self.rng.below(6) as u8, 1 + self.rng.below(6) as u8]; + self.tilt = [self.rng.below(8000) as i32 - 4000, self.rng.below(8000) as i32 - 4000]; + } +} + +impl Game for Dice { + fn new(seed: u32, _best: u32) -> Self { + let mut d = Dice { rng: Rng::new(seed), face: [6, 1], tilt: [0, 0], left: 0, redraw: true }; + d.shuffle(); + d.tilt = [-2300, 2600]; + d + } + + fn period(&self) -> u32 { + 70 + } + + fn running(&self) -> bool { + self.left > 0 + } + + fn tick(&mut self) -> u8 { + if self.left == 0 { + return 0; + } + self.left -= 1; + self.shuffle(); + EV_DIRTY | if self.left == 0 { EV_DONE } else { EV_TICK } + } + + fn touch(&mut self, _x: i32, _y: i32, down: bool) -> u8 { + if down && self.left == 0 { + self.left = 10; + return EV_TICK; + } + 0 + } + + fn crown(&mut self, d: i32) -> u8 { + // turning the crown rolls too: the primary input on the watch + if d != 0 { self.touch(0, 0, true) } else { 0 } + } + + fn score(&self) -> u32 { + 0 + } + + fn draw(&mut self, s: &mut S, full: bool) { + if full || self.redraw { + clear(s); + self.redraw = false; + } else { + s.rect(C - 190, 90, 380, 270, BG); + } + for i in 0..2 { + die(s, CX[i], CY + DY[i], self.face[i], self.tilt[i]); + } + if self.left == 0 { + let mut buf = [0u8; 11]; + let t = cmf_wp2_ui::text::int((self.face[0] + self.face[1]) as i32, &mut buf); + txt(s, C, 286, t, 7, 5, WHITE); + } + txt_adv(s, C, 366, "TAP TO ROLL", 2, 1, 12, GREY); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn roll_ends_on_valid_faces_and_tilts() { + let mut g = Dice::new(2, 0); + g.touch(0, 0, true); + let mut done = false; + for _ in 0..20 { + if g.tick() & EV_DONE != 0 { + done = true; + } + assert!((1..=6).contains(&g.face[0]) && (1..=6).contains(&g.face[1])); + assert!(g.tilt[0].abs() <= 4000); + } + assert!(done && !g.running()); + } + + #[test] + fn every_face_shows_up() { + let mut g = Dice::new(9, 0); + let mut seen = [false; 7]; + for _ in 0..200 { + g.shuffle(); + seen[g.face[0] as usize] = true; + } + assert!(seen[1..].iter().all(|v| *v)); + } +} + +#[cfg(test)] +impl Dice { + pub fn set_ref(&mut self, face: [u8; 2], tilt: [i32; 2]) { + self.face = face; + self.tilt = tilt; + self.redraw = true; + } +} diff --git a/glitch/src/dino.rs b/glitch/src/dino.rs new file mode 100644 index 0000000..3ee037f --- /dev/null +++ b/glitch/src/dino.rs @@ -0,0 +1,533 @@ +// Dino Runner: the Chrome T-Rex game, physics ported from Chromium's dino_game (BSD-3-Clause, see NOTICE-CHROMIUM) to +// integers. World units are Chromium's sprite pixels, drawn at 2x. Physics steps at 60 Hz (two per 33 ms tick), with +// Q16 fixed point for speed, position and jump height. Speed starts lower than Chromium's because the round screen +// shows 224 world px of track instead of 600. + +use crate::gfx::*; +use crate::sprites::*; +use cmf_wp2_ui::Surface; + +const K: i32 = 2; +const FLOOR_Y: i32 = 330; // screen y of world y 140 (the dino's feet) +const X0: i32 = 12; // screen x of world x 0 +const VIEW_W: i32 = 224; +const DINO_X: i32 = 30; +const DINO_TOP: i32 = 93; +const Q: i32 = 65536; +const START_SPD: i32 = 229_376; // 3.5 world px per 60 Hz step +const MAX_SPD: i32 = 524_288; // 8.0 +const ACC: i32 = 70; // per step: max speed after about 70 s +const PTERO_SPD: i32 = 327_680; // pterodactyls from 5.0 (Chromium: 8.5 of 13) +const GRAV: i32 = 39_322; // 0.6 +const JUMP_V: i32 = 655_360; // 10 +const CLEAR_STEPS: i32 = 90; + +type B = (i32, i32, i32, i32); +const RUN_BOXES: [B; 6] = [(22, 0, 17, 16), (1, 18, 30, 9), (10, 35, 14, 8), (1, 24, 29, 5), (5, 30, 21, 4), (9, 34, 15, 4)]; +const DUCK_BOXES: [B; 1] = [(1, 18, 55, 25)]; +const SMALL_BOXES: [B; 3] = [(0, 7, 5, 27), (4, 0, 6, 34), (10, 4, 7, 14)]; +const LARGE_BOXES: [B; 3] = [(0, 12, 7, 38), (8, 0, 7, 49), (13, 10, 10, 38)]; +const PTERO_BOXES: [B; 5] = [(15, 15, 16, 5), (18, 21, 24, 6), (2, 14, 4, 3), (6, 10, 4, 7), (10, 8, 6, 9)]; + +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum State { + Ready, + Playing, + Over, +} + +#[derive(Clone, Copy)] +struct Obs { + on: bool, + kind: u8, // 0 small cactus, 1 large cactus, 2 pterodactyl + size: u8, + x: i32, // Q16 world x + y: i32, // world y of the top + w: i32, + gap: i32, + off: i32, // Q16 speed offset (pterodactyl) + frame: u8, +} + +const NO_OBS: Obs = Obs { on: false, kind: 0, size: 1, x: 0, y: 0, w: 0, gap: 0, off: 0, frame: 0 }; + +pub struct Dino { + pub state: State, + rng: Rng, + best: u32, + jh: i32, + vy: i32, + jumping: bool, + drop: bool, + duck_t: u8, + spd: i32, + dist: i32, + anim: u32, + clear: i32, + lock: u8, + obs: [Obs; 4], + clouds: [(i32, i32); 2], + last_kind: u8, + dups: u8, + milestone: u32, + redraw: bool, + prev: [B; 7], + shown: u32, + shown_hi: u32, +} + +fn boxes(o: &Obs) -> ([B; 5], usize) { + let (src, n): (&[B], usize) = match o.kind { + 0 => (&SMALL_BOXES, 3), + 1 => (&LARGE_BOXES, 3), + _ => (&PTERO_BOXES, 5), + }; + let mut b = [(0, 0, 0, 0); 5]; + b[..n].copy_from_slice(&src[..n]); + if o.kind < 2 && o.size > 1 { + b[1].2 = o.w - b[0].2 - b[2].2; + b[2].0 = o.w - b[2].2; + } + (b, n) +} + +fn overlap(a: B, b: B) -> bool { + a.0 < b.0 + b.2 && a.0 + a.2 > b.0 && a.1 < b.1 + b.3 && a.1 + a.3 > b.1 +} + +impl Dino { + fn reset(&mut self) { + self.jh = 0; + self.vy = 0; + self.jumping = false; + self.drop = false; + self.duck_t = 0; + self.spd = START_SPD; + self.dist = 0; + self.anim = 0; + self.clear = CLEAR_STEPS; + self.obs = [NO_OBS; 4]; + self.last_kind = 9; + self.dups = 0; + self.milestone = 0; + self.clouds = [(60 * Q, 30), (170 * Q, 55)]; + } + + pub fn ducking(&self) -> bool { + self.duck_t > 0 && !self.jumping + } + + fn dino_y(&self) -> i32 { + DINO_TOP - (self.jh >> 16) + } + + /// Does the dino touch an obstacle right now? + pub fn hit(&self) -> bool { + let dy = self.dino_y(); + let ducking = self.ducking(); + let db: &[B] = if ducking { &DUCK_BOXES } else { &RUN_BOXES }; + for o in self.obs.iter().filter(|o| o.on) { + let ox = o.x >> 16; + let (ob, n) = boxes(o); + for d in db { + for b in &ob[..n] { + if overlap((DINO_X + d.0, dy + d.1, d.2, d.3), (ox + b.0, o.y + b.1, b.2, b.3)) { + return true; + } + } + } + } + false + } + + fn jump(&mut self) -> u8 { + if self.jumping { + return 0; + } + self.jumping = true; + self.vy = JUMP_V; + self.duck_t = 0; + EV_TICK + } + + fn spawn(&mut self) { + if self.clear > 0 { + self.clear -= 1; + return; + } + let mut live = 0; + let mut last: Option = None; + for o in self.obs.iter().filter(|o| o.on) { + live += 1; + if last.map_or(true, |l| o.x > l.x) { + last = Some(*o); + } + } + let room = last.map_or(true, |l| (l.x >> 16) + l.w + l.gap < VIEW_W); + if !room || live >= 3 { + return; + } + let nk: u32 = if self.spd >= PTERO_SPD { 3 } else { 2 }; + let mut kind = self.rng.below(nk) as u8; + if kind == self.last_kind { + self.dups += 1; + if self.dups >= 2 { + kind = (kind + 1) % nk as u8; + self.dups = 1; + } + } else { + self.dups = 1; + } + self.last_kind = kind; + let (w1, min_gap, multi) = match kind { + 0 => (17, 120, 245_760), + 1 => (25, 120, 294_912), + _ => (46, 150, i32::MAX), + }; + let size = if self.spd >= multi { 1 + self.rng.below(3) as u8 } else { 1 }; + let w = w1 * size as i32; + let h = [35, 50, 40][kind as usize]; + let y = if kind == 2 { [100, 75, 50][self.rng.below(3) as usize] } else { 140 - h }; + let off = if kind == 2 { if self.rng.below(2) == 0 { 32_768 } else { -32_768 } } else { 0 }; + let m = ((w as i64 * self.spd as i64 + 32_768) >> 16) as i32 + min_gap * 6 / 10; + let gap = m + self.rng.below((m / 2 + 1) as u32) as i32; + if let Some(slot) = self.obs.iter_mut().find(|o| !o.on) { + *slot = Obs { on: true, kind, size, x: (VIEW_W + 8) * Q, y, w, gap, off, frame: 0 }; + } + } + + /// One 60 Hz physics step. + fn step(&mut self) -> u8 { + let mut ev = 0; + self.anim += 1; + if self.jumping { + self.jh += self.vy; + self.vy -= if self.drop { GRAV * 3 } else { GRAV }; + if self.jh <= 0 { + self.jh = 0; + self.vy = 0; + self.jumping = false; + self.drop = false; + } + } + self.spd = (self.spd + ACC).min(MAX_SPD); + self.dist = self.dist.saturating_add(self.spd); + let anim = self.anim; + let spd = self.spd; + for o in self.obs.iter_mut().filter(|o| o.on) { + o.x -= spd + o.off; + if o.kind == 2 { + o.frame = ((anim / 10) % 2) as u8; + } + if (o.x >> 16) + o.w < 0 { + o.on = false; + } + } + for c in self.clouds.iter_mut() { + c.0 -= spd / 6; + if (c.0 >> 16) + 46 < 0 { + c.0 = (VIEW_W + 10) * Q; + c.1 = 20 + (self.rng.0 >> 9) as i32 % 50; + } + } + self.spawn(); + let m = self.score() / 100; + if m > self.milestone { + self.milestone = m; + ev |= EV_TICK; + } + if self.hit() { + self.state = State::Over; + self.lock = 22; + self.redraw = true; + ev |= EV_DONE; + } + ev + } + + fn dino_sprite(&self) -> &'static Sprite { + match self.state { + State::Over => &TREX_CRASH, + State::Ready => &TREX_STAND, + State::Playing => { + if self.jumping { + &TREX_STAND + } else if self.ducking() { + if (self.anim / 8) % 2 == 0 { &TREX_DUCK1 } else { &TREX_DUCK2 } + } else if (self.anim / 5) % 2 == 0 { + &TREX_RUN1 + } else { + &TREX_RUN2 + } + } + } + } +} + +fn obs_sprite(o: &Obs) -> &'static Sprite { + match (o.kind, o.size) { + (0, 1) => &CACTUS_S1, + (0, 2) => &CACTUS_S2, + (0, _) => &CACTUS_S3, + (1, 1) => &CACTUS_L1, + (1, 2) => &CACTUS_L2, + (1, _) => &CACTUS_L3, + _ => { + if o.frame == 0 { &PTERO1 } else { &PTERO2 } + } + } +} + +impl Game for Dino { + fn new(seed: u32, best: u32) -> Self { + let mut d = Dino { + state: State::Ready, + rng: Rng::new(seed), + best, + jh: 0, + vy: 0, + jumping: false, + drop: false, + duck_t: 0, + spd: START_SPD, + dist: 0, + anim: 0, + clear: 0, + lock: 0, + obs: [NO_OBS; 4], + clouds: [(0, 0); 2], + last_kind: 9, + dups: 0, + milestone: 0, + redraw: true, + prev: [(0, 0, 0, 0); 7], + shown: u32::MAX, + shown_hi: u32::MAX, + }; + d.reset(); + d + } + + fn period(&self) -> u32 { + 33 + } + + fn running(&self) -> bool { + self.state == State::Playing || self.lock > 0 + } + + fn tick(&mut self) -> u8 { + if self.state != State::Playing { + self.lock = self.lock.saturating_sub(1); + return 0; + } + let mut ev = 0; + for _ in 0..2 { + ev |= self.step(); + if self.state != State::Playing { + break; + } + } + self.duck_t = self.duck_t.saturating_sub(1); + ev | EV_DIRTY + } + + fn touch(&mut self, _x: i32, _y: i32, down: bool) -> u8 { + if !down { + return 0; + } + match self.state { + State::Ready => { + self.reset(); + self.state = State::Playing; + self.redraw = true; + self.jump() | EV_DIRTY + } + State::Over => { + if self.lock > 0 { + return 0; // a tap meant for the run that just ended + } + self.reset(); + self.state = State::Playing; + self.redraw = true; + self.jump() | EV_DIRTY + } + State::Playing => self.jump(), + } + } + + fn crown(&mut self, _d: i32) -> u8 { + if self.state == State::Playing { + if self.jumping { + self.drop = true; + } else { + self.duck_t = 14; + } + } + 0 + } + + fn score(&self) -> u32 { + (self.dist >> 16) as u32 / 25 + } + + fn draw(&mut self, s: &mut S, full: bool) { + if full || self.redraw { + clear(s); + self.redraw = false; + self.prev = [(0, 0, 0, 0); 7]; + self.shown = u32::MAX; + self.shown_hi = u32::MAX; + match self.state { + State::Ready => { + txt(s, C, 150, "DINO RUN", 4, 3, WHITE); + txt(s, C, 196, "TAP TO JUMP", 3, 2, GREY); + txt(s, C, 236, "CROWN TO DUCK", 2, 2, MID); + } + State::Over => { + txt(s, C, 170, "GAME OVER", 4, 3, RED); + txt(s, C, 214, "TAP TO RETRY", 2, 2, GREY); + } + State::Playing => {} + } + } + for r in self.prev.iter() { + s.rect(r.0, r.1, r.2, r.3, BG); + } + let gy = FLOOR_Y + (127 - 140) * K; + s.rect(0, gy + 7 * K, SIZE, 5 * K, BG); + let go = ((self.dist >> 16) % 240) * K; + bmp(s, &GROUND, X0 - go, gy, K, MID); + bmp(s, &GROUND, X0 - go + 240 * K, gy, K, MID); + for (i, c) in self.clouds.iter().enumerate().filter(|_| self.state == State::Playing) { + let (x, y) = (X0 + (c.0 >> 16) * K, FLOOR_Y + (c.1 - 140) * K); + bmp(s, &CLOUD, x, y, K, DIM); + self.prev[5 + i] = (x, y, CLOUD.w * K, CLOUD.h * K); + } + for i in 0..4 { + self.prev[1 + i] = (0, 0, 0, 0); + let o = self.obs[i]; + if o.on { + let sp = obs_sprite(&o); + let (x, y) = (X0 + (o.x >> 16) * K, FLOOR_Y + (o.y - 140) * K); + bmp(s, sp, x, y, K, if o.kind == 2 { RED } else { WHITE }); + self.prev[1 + i] = (x, y, sp.w * K, sp.h * K); + } + } + let sp = self.dino_sprite(); + let (x, y) = (X0 + DINO_X * K, FLOOR_Y + (self.dino_y() - 140) * K); + bmp(s, sp, x, y, K, WHITE); + self.prev[0] = (x, y, sp.w * K, sp.h * K); + let sc = self.score(); + if sc != self.shown { + self.shown = sc; + num_opaque(s, C, 352, "", sc, 5, 4, 3, WHITE); + } + let hi = self.best.max(sc); + if hi != self.shown_hi { + self.shown_hi = hi; + num_opaque(s, C, 392, "HI ", hi, 5, 2, 2, GREY); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn jump_peaks_near_chromium_height() { + let mut d = Dino::new(1, 0); + d.state = State::Playing; + d.clear = 1_000_000; + d.jump(); + let mut top = 0; + let mut steps = 0; + while d.jumping { + d.step(); + top = top.max(d.jh >> 16); + steps += 1; + } + assert!((78..=90).contains(&top), "apex {top}"); + assert!((30..=38).contains(&steps), "air steps {steps}"); + } + + #[test] + fn standing_still_hits_a_cactus_and_a_jump_clears_it() { + let mut d = Dino::new(1, 0); + d.state = State::Playing; + d.clear = 1_000_000; + d.obs[0] = Obs { on: true, kind: 1, size: 1, x: (DINO_X + 80) * Q, y: 90, w: 25, gap: 0, off: 0, frame: 0 }; + let mut hit = false; + for _ in 0..200 { + d.step(); + if d.state == State::Over { + hit = true; + break; + } + } + assert!(hit); + let mut d = Dino::new(1, 0); + d.state = State::Playing; + d.clear = 1_000_000; + d.obs[0] = Obs { on: true, kind: 1, size: 1, x: (DINO_X + 100) * Q, y: 90, w: 25, gap: 0, off: 0, frame: 0 }; + let mut t = 0; + while d.obs[0].on && t < 400 { + // jump when the cactus is about 60 world px ahead + if !d.jumping && ((d.obs[0].x >> 16) - DINO_X) < 62 { + d.jump(); + } + d.step(); + t += 1; + } + assert_eq!(d.state, State::Playing, "jump should clear a large cactus"); + } + + #[test] + fn low_ptero_hits_ducker_mid_ptero_is_ducked_under() { + let mk = |y| { + let mut d = Dino::new(1, 0); + d.state = State::Playing; + d.clear = 1_000_000; + d.obs[0] = Obs { on: true, kind: 2, size: 1, x: (DINO_X - 5) * Q, y, w: 46, gap: 0, off: 0, frame: 0 }; + d + }; + let mut d = mk(75); + assert!(d.hit(), "standing dino hits mid ptero"); + d.duck_t = 10; + assert!(!d.hit(), "ducking passes under mid ptero"); + let mut d = mk(100); + d.duck_t = 10; + assert!(d.hit(), "low ptero must be jumped"); + let d = mk(50); + assert!(!d.hit(), "high ptero passes over a running dino"); + } + + #[test] + fn speed_ramps_and_score_counts() { + let mut d = Dino::new(7, 0); + d.state = State::Playing; + d.clear = 1_000_000; + let s0 = d.spd; + for _ in 0..3000 { + d.step(); + } + assert!(d.spd > s0 && d.spd <= MAX_SPD); + assert!(d.score() > 100); + } + + #[test] + fn obstacles_spawn_with_gaps() { + let mut d = Dino::new(3, 0); + d.state = State::Playing; + d.clear = 0; + let mut seen = 0; + for _ in 0..2000 { + d.jh = 0; // god mode: only watch spawning + d.step(); + d.state = State::Playing; + seen = seen.max(d.obs.iter().filter(|o| o.on).count()); + } + assert!((1..=3).contains(&seen)); + } +} diff --git a/glitch/src/g2048.rs b/glitch/src/g2048.rs new file mode 100644 index 0000000..36c6b2f --- /dev/null +++ b/glitch/src/g2048.rs @@ -0,0 +1,300 @@ +// 2048 on a round screen: a 4x4 board inscribed in the circle. Swipe (or turn the crown for up/down) to slide. +// Tile numbers are dot-matrix. Grey tiles for low numbers, red from 256. + +use crate::gfx::*; +use cmf_wp2_ui::color::Rgb; +use cmf_wp2_ui::draw::rrect; +use cmf_wp2_ui::text::int; +use cmf_wp2_ui::Surface; + +const T: i32 = 66; +const GAP: i32 = 10; +const X0: i32 = C - (4 * T + 3 * GAP) / 2; +const Y0: i32 = 100; + +const GREYB: u16 = cmf_wp2_ui::color::rgb565(120, 120, 126); + +pub struct G2048 { + pub b: [u8; 16], // exponents, 0 = empty + pub score: u32, + best: u32, + rng: Rng, + pub over: bool, + tx: i32, + ty: i32, + redraw: bool, + lock: bool, +} + +fn idx(dir: usize, i: usize, j: usize) -> usize { + match dir { + 0 => i * 4 + j, + 1 => i * 4 + 3 - j, + 2 => j * 4 + i, + _ => (3 - j) * 4 + i, + } +} + +impl G2048 { + fn spawn(&mut self) { + let mut n = 0; + for v in self.b.iter() { + if *v == 0 { + n += 1; + } + } + if n == 0 { + return; + } + let mut k = self.rng.below(n); + let e = if self.rng.below(10) == 0 { 2 } else { 1 }; + for v in self.b.iter_mut() { + if *v == 0 { + if k == 0 { + *v = e; + return; + } + k -= 1; + } + } + } + + /// 0 left, 1 right, 2 up, 3 down. Returns whether anything moved. + pub fn slide(&mut self, dir: usize) -> bool { + let mut moved = false; + for i in 0..4 { + let mut out = [0u8; 4]; + let mut merged = [false; 4]; + let mut n = 0; + for j in 0..4 { + let v = self.b[idx(dir, i, j)]; + if v == 0 { + continue; + } + if n > 0 && out[n - 1] == v && !merged[n - 1] { + out[n - 1] += 1; + merged[n - 1] = true; + self.score += 1 << out[n - 1]; + } else { + out[n] = v; + n += 1; + } + } + for j in 0..4 { + let k = idx(dir, i, j); + if self.b[k] != out[j] { + moved = true; + } + self.b[k] = out[j]; + } + } + moved + } + + pub fn stuck(&self) -> bool { + for i in 0..16 { + if self.b[i] == 0 { + return false; + } + if i % 4 < 3 && self.b[i] == self.b[i + 1] { + return false; + } + if i < 12 && self.b[i] == self.b[i + 4] { + return false; + } + } + true + } + + fn mv(&mut self, dir: usize) -> u8 { + if self.over || !self.slide(dir) { + return 0; + } + self.spawn(); + self.redraw = true; + if self.stuck() { + self.over = true; + self.lock = true; + return EV_DONE | EV_DIRTY; + } + EV_TICK | EV_DIRTY + } + + fn fresh(&mut self) { + self.b = [0; 16]; + self.score = 0; + self.over = false; + self.spawn(); + self.spawn(); + self.redraw = true; + } +} + +fn tile_colour(e: u8) -> (Rgb, Rgb) { + let white = Rgb(241, 240, 246); + match e { + 0 => (Rgb(47, 48, 53), white), + 1 => (Rgb(81, 82, 87), white), + 2 => (Rgb(93, 94, 99), white), + 3 => (Rgb(108, 107, 113), white), + 4 => (Rgb(116, 117, 122), white), + 5 => (Rgb(124, 125, 130), white), + 6 => (Rgb(132, 133, 138), white), + 7 => (Rgb(142, 142, 148), white), + 8 | 9 => (Rgb(226, 32, 38), white), + _ => (Rgb(255, 72, 64), white), + } +} + +impl Game for G2048 { + fn new(seed: u32, best: u32) -> Self { + let mut g = G2048 { b: [0; 16], score: 0, best, rng: Rng::new(seed), over: false, tx: 0, ty: 0, redraw: true, lock: false }; + g.fresh(); + g + } + + fn period(&self) -> u32 { + 100 + } + + fn running(&self) -> bool { + false + } + + fn tick(&mut self) -> u8 { + 0 + } + + fn touch(&mut self, x: i32, y: i32, down: bool) -> u8 { + if down { + self.tx = x; + self.ty = y; + return 0; + } + let (dx, dy) = (x - self.tx, y - self.ty); + if self.over { + self.fresh(); + return EV_DIRTY; + } + if dx.abs().max(dy.abs()) < 30 { + return 0; + } + let dir = if dx.abs() > dy.abs() { + if dx < 0 { 0 } else { 1 } + } else if dy < 0 { + 2 + } else { + 3 + }; + self.mv(dir) + } + + fn crown(&mut self, d: i32) -> u8 { + self.mv(if d < 0 { 2 } else { 3 }) + } + + fn score(&self) -> u32 { + self.score + } + + fn draw(&mut self, s: &mut S, full: bool) { + if full { + clear(s); + } + self.redraw = false; + for i in 0..16 { + let (x, y) = (X0 + (i as i32 % 4) * (T + GAP), Y0 + (i as i32 / 4) * (T + GAP)); + let e = self.b[i]; + let (bg, fg) = tile_colour(e); + rrect(s, x, y, T, T, 15, bg, BGC); + if e > 0 { + let mut buf = [0u8; 11]; + let t = int(1 << e, &mut buf); + let n = t.len() as i32; + let c = cmf_wp2_ui::color::rgb565(fg.0, fg.1, fg.2); + // one digit height for every tile, as in the widget; 5 columns of 3 px per digit + let adv = if n >= 4 { 14 } else { 15 }; + txt_adv(s, x + T / 2, y + T / 2 - 11, t, 3, 2, adv, c); + } + } + // score top-left, BEST top-right, in dots (opaque: replaces the old number without a clear) + let mut b1 = [0u8; 11]; + let mut b2 = [0u8; 11]; + let sc = int(self.score as i32, &mut b1); + let bt = int(self.best.max(self.score) as i32, &mut b2); + s.rect(C - 150, 56, 300, 30, BG); + txt_at(s, C - 140, 62, sc, 3, 2, WHITE); + let w = (bt.len() as i32 + 5) * 16; + txt_at(s, C + 140 - w, 66, "BEST", 2, 1, GREYB); + txt_at(s, C + 140 - bt.len() as i32 * 16, 66, bt, 2, 1, GREYB); + if self.over { + s.rect(C - 120, C - 34, 240, 68, BG); + txt(s, C, C - 24, "GAME OVER", 3, 2, RED); + txt(s, C, C + 8, "TAP TO RETRY", 2, 2, GREY); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn with(b: [u8; 16]) -> G2048 { + let mut g = G2048::new(1, 0); + g.b = b; + g.score = 0; + g + } + + #[test] + fn merges_once_per_move_and_scores() { + let mut g = with([1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0]); + assert!(g.slide(0)); + assert_eq!(&g.b[..4], &[2, 2, 0, 0]); + assert_eq!(&g.b[8..12], &[3, 0, 0, 0]); + assert_eq!(g.score, 4 + 4 + 8); + } + + #[test] + fn every_direction_and_no_op() { + let mut g = with([1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + assert!(g.slide(1)); + assert_eq!(&g.b[..4], &[0, 0, 0, 2]); + assert!(!g.slide(1)); + let mut g = with([1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + assert!(g.slide(2)); + assert_eq!(g.b[0], 2); + let mut g = with([1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]); + assert!(g.slide(3)); + assert_eq!(g.b[12], 2); + } + + #[test] + fn detects_stuck() { + let g = with([1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1]); + assert!(g.stuck()); + let g = with([1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1]); + assert!(!g.stuck()); + } + + #[test] + fn swipe_moves_and_spawns() { + let mut g = with([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + g.touch(200, 200, true); + let ev = g.touch(320, 205, false); + assert!(ev & EV_DIRTY != 0); + assert_eq!(g.b[3], 1); + assert_eq!(g.b.iter().filter(|v| **v > 0).count(), 2); + } +} + +#[cfg(test)] +impl G2048 { + /// Test hook: the board of the reference screenshot. + pub fn set_ref(&mut self, b: [u8; 16], score: u32, best: u32) { + self.b = b; + self.score = score; + self.best = best; + self.redraw = true; + } +} diff --git a/glitch/src/gfx.rs b/glitch/src/gfx.rs new file mode 100644 index 0000000..e15a692 --- /dev/null +++ b/glitch/src/gfx.rs @@ -0,0 +1,143 @@ +// The one shared drawing core every game uses: colours, 1-bit sprites, round dots, dot-matrix numbers, a tiny RNG +// and the `Game` trait the hub drives. No game draws with anything else. + +use crate::sprites::Sprite; +use cmf_wp2_ui::color::{Rgb, rgb565}; +use cmf_wp2_ui::text::{dots, dots_centre, dots_opaque}; +use cmf_wp2_ui::Surface; + +pub const SIZE: i32 = 466; +pub const C: i32 = SIZE / 2; + +pub const BG: u16 = rgb565(27, 27, 29); +pub const WHITE: u16 = rgb565(240, 239, 245); +/// The Nothing accent. +pub const RED: u16 = rgb565(225, 32, 37); +pub const GREY: u16 = rgb565(150, 150, 158); +pub const MID: u16 = rgb565(96, 96, 104); +pub const LINE: u16 = rgb565(71, 71, 73); +pub const DIM: u16 = rgb565(45, 46, 53); +pub const BGC: Rgb = Rgb(27, 27, 29); +pub const REDC: Rgb = Rgb(225, 32, 37); +pub const WHITEC: Rgb = Rgb(240, 239, 245); + +/// Event bits a game returns. +pub const EV_TICK: u8 = 1; // light haptic click +pub const EV_DONE: u8 = 2; // long buzz: run over +pub const EV_DOUBLE: u8 = 4; // double click: good news +pub const EV_DIRTY: u8 = 8; // draw again + +pub trait Game { + fn new(seed: u32, best: u32) -> Self; + /// ms between ticks while `running`. + fn period(&self) -> u32; + fn running(&self) -> bool; + fn tick(&mut self) -> u8; + fn touch(&mut self, x: i32, y: i32, down: bool) -> u8; + fn crown(&mut self, d: i32) -> u8; + fn draw(&mut self, s: &mut S, full: bool); + fn score(&self) -> u32; +} + +/// xorshift32. +pub struct Rng(pub u32); + +impl Rng { + pub fn new(seed: u32) -> Rng { + Rng(if seed == 0 { 0x9E37_79B9 } else { seed }) + } + pub fn next(&mut self) -> u32 { + let mut x = self.0; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + self.0 = x; + x + } + /// 0..n (n > 0). + pub fn below(&mut self, n: u32) -> u32 { + (self.next() >> 8) % n.max(1) + } +} + +pub fn clear(s: &mut S) { + s.rect(0, 0, SIZE, SIZE, BG); +} + +/// A dot: a square with its corners nicked, centred on (cx, cy). +pub fn dot(s: &mut S, cx: i32, cy: i32, d: i32, c: u16) { + let (x, y) = (cx - d / 2, cy - d / 2); + if d < 4 { + s.rect(x, y, d, d, c); + } else { + s.rect(x + 1, y, d - 2, d, c); + s.rect(x, y + 1, d, d - 2, c); + } +} + +/// A 1-bit sprite at `k` screen px per sprite px, runs merged into as few rects as possible. Clipping is the surface's. +pub fn bmp(s: &mut S, sp: &Sprite, x: i32, y: i32, k: i32, c: u16) { + let nb = ((sp.w + 7) / 8) as usize; + let h = sp.h as usize; + let mut r = 0; + while r < h { + let row = &sp.rows[r * nb..(r + 1) * nb]; + let mut n = 1; + while r + n < h && &sp.rows[(r + n) * nb..(r + n + 1) * nb] == row { + n += 1; + } + let bit = |i: i32| row[(i >> 3) as usize] & (0x80 >> (i & 7)) != 0; + let mut cx = 0; + while cx < sp.w { + if bit(cx) { + let st = cx; + while cx < sp.w && bit(cx) { + cx += 1; + } + s.rect(x + st * k, y + r as i32 * k, (cx - st) * k, n as i32 * k, c); + } else { + cx += 1; + } + } + r += n; + } +} + +/// Dot-matrix text centred on `cx`. +pub fn txt(s: &mut S, cx: i32, y: i32, t: &str, pitch: i32, d: i32, c: u16) { + dots_centre(s, cx, y, t, pitch, d, c); +} + +pub fn txt_at(s: &mut S, x: i32, y: i32, t: &str, pitch: i32, d: i32, c: u16) { + dots(s, x, y, t, pitch, d, c); +} + +/// `prefix` then `n` zero-padded to `w` digits, in `buf`. +pub fn label<'a>(buf: &'a mut [u8; 24], prefix: &str, n: u32, w: usize) -> &'a str { + let p = prefix.as_bytes(); + let pl = p.len().min(8); + buf[..pl].copy_from_slice(&p[..pl]); + let mut n = n; + for i in (0..w).rev() { + buf[pl + i] = b'0' + (n % 10) as u8; + n /= 10; + } + core::str::from_utf8(&buf[..pl + w]).unwrap_or("") +} + +/// A number that replaces the one drawn before it without a clear. +pub fn num_opaque(s: &mut S, cx: i32, y: i32, prefix: &str, n: u32, w: usize, pitch: i32, d: i32, c: u16) { + let mut buf = [0u8; 24]; + let t = label(&mut buf, prefix, n.min(10u32.pow(w as u32) - 1), w); + let width = t.len() as i32 * 4 * pitch; + dots_opaque(s, cx - width, y, t, pitch, d, c, BG); +} + +/// Dot-matrix text centred on `cx` with a tighter per-character advance of `adv` px (the SDK cell is 8 columns wide). +pub fn txt_adv(s: &mut S, cx: i32, y: i32, t: &str, pitch: i32, d: i32, adv: i32, c: u16) { + let n = t.chars().count() as i32; + let x0 = cx - n * adv / 2; + for (i, (b, ch)) in t.char_indices().enumerate() { + dots(s, x0 + i as i32 * adv, y, &t[b..b + ch.len_utf8()], pitch, d, c); + } +} diff --git a/glitch/src/main.rs b/glitch/src/main.rs new file mode 100644 index 0000000..7f0eae7 --- /dev/null +++ b/glitch/src/main.rs @@ -0,0 +1,146 @@ +#![cfg_attr(any(target_os = "none", wp2_sim), no_main, cfg_attr(target_os = "none", no_std))] + +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod app; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod bottle; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod dice; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod dino; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod g2048; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod gfx; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod snake; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod sprites; +#[cfg_attr(not(any(target_os = "none", wp2_sim)), allow(dead_code))] +mod ttt; +#[cfg(test)] +mod shots; + +#[cfg(not(any(target_os = "none", wp2_sim)))] +fn main() {} + +#[cfg(any(target_os = "none", wp2_sim))] +mod device { + use crate::app::{App, Out}; + use cmf_wp2_app::{Api, BUTTON, CROWN, PAUSE, RESUME, TOUCH_DOWN, TOUCH_UP}; + use cmf_wp2_ui::haptic::Motor; + + cmf_wp2_app::entry!(main); + cmf_wp2_app::app_icon!(); + + fn due(now: u32, at: u32) -> bool { + (now.wrapping_sub(at) as i32) >= 0 + } + + // true on RESUME, false on BUTTON + fn paused(api: &Api) -> bool { + loop { + match api.event(u32::MAX).map(|e| e.kind) { + Some(RESUME) => return true, + Some(BUTTON) => return false, + _ => {} + } + } + } + + fn seed(api: &Api) -> u32 { + let t = api.time().map_or(0, |t| (t.minute as u32 * 60 + t.second as u32) * 1000 + t.millis as u32); + api.now().wrapping_mul(2_654_435_761) ^ t + } + + fn apply(api: &Api, app: &mut App, motor: &mut Motor, mut screen: &Api, o: Out) -> bool { + if let Some(p) = o.haptic { + motor.play(p); + } + if o.save { + api.save(&app.blob()); + } + if o.exit { + return true; + } + if o.redraw { + app.draw(&mut screen); + api.show(); + } + api.keep_screen_on(app.in_game()); + false + } + + fn main(api: &'static Api) -> i32 { + let screen: &Api = api; + let mut motor = Motor::new(api); // dropped on return: the motor goes off + api.own_back(true); // BUTTON is "back": game -> hub, hub -> leave (ABI 1.3; older loaders just leave) + let mut blob = [0u8; 64]; + let n = api.load(&mut blob).max(0) as usize; + let mut app = App::new(&blob[..n.min(64)]); + app.seed = seed(api); + { + let mut s = screen; + app.draw(&mut s); + } + api.show(); + let mut next = api.now().wrapping_add(33); + loop { + motor.update(); + let now = api.now(); + let p = app.period(); + if p == 0 { + next = now.wrapping_add(33); + } + let left = if p == 0 { u32::MAX } else { (next.wrapping_sub(now) as i32).max(0) as u32 }; + let wait = motor.wait().map_or(left, |m| m.min(left)); + if let Some(e) = api.event(wait) { + app.seed = app.seed.wrapping_mul(1_664_525).wrapping_add(api.now() ^ (e.x as u32) << 8); + let o = match e.kind { + BUTTON => { + if api.abi_minor >= 3 { + app.back() + } else { + motor.off(); + api.save(&app.blob()); + return 0; + } + } + PAUSE => { + motor.off(); + api.keep_screen_on(false); + api.save(&app.blob()); + if !paused(api) { + return 0; + } + app.full(); + next = api.now().wrapping_add(33); + Out { redraw: true, ..Out::default() } + } + TOUCH_DOWN => app.touch(e.x as i32, e.y as i32, true), + TOUCH_UP => app.touch(e.x as i32, e.y as i32, false), + CROWN => app.crown(e.delta as i32), + _ => continue, + }; + if apply(api, &mut app, &mut motor, screen, o) { + api.keep_screen_on(false); + return 0; + } + continue; + } + let now = api.now(); + if p == 0 || !due(now, next) { + continue; + } + let mut o = app.tick(); + o.redraw = true; + next = next.wrapping_add(p); + if due(now, next) { + next = now.wrapping_add(p); + } + if apply(api, &mut app, &mut motor, screen, o) { + return 0; + } + } + } +} diff --git a/glitch/src/shots.rs b/glitch/src/shots.rs new file mode 100644 index 0000000..6b4faca --- /dev/null +++ b/glitch/src/shots.rs @@ -0,0 +1,147 @@ +// Host-only: render each screen with the SDK's `Fb` (same clipping and font as the loader) and write PPMs when +// SHOTS_DIR is set. Also checks that nothing is drawn outside the round panel's circle. + +use crate::app::{App, ROW0_FOR_TESTS}; +use crate::gfx::*; +use cmf_wp2_ui::Fb; + +fn snap(a: &mut App, px: &mut Vec, name: &str) { + { + let mut fb = Fb::new(px); + a.draw(&mut fb); + } + // a few lit pixels outside the circle are expected for the bezel corners; flag real overflow + let mut outside = 0; + for y in 0..SIZE { + for x in 0..SIZE { + let (dx, dy) = (x - C, y - C); + if dx * dx + dy * dy > 233 * 233 && px[(y * SIZE + x) as usize] != BG { + outside += 1; + } + } + } + assert!(outside < 400, "{name}: {outside} lit px outside the circle"); + if let Ok(dir) = std::env::var("SHOTS_DIR") { + let mut out = format!("P6\n{SIZE} {SIZE}\n255\n").into_bytes(); + for p in px.iter() { + out.push(((p >> 11) as u8 & 31) << 3); + out.push(((p >> 5) as u8 & 63) << 2); + out.push((*p as u8 & 31) << 3); + } + std::fs::write(format!("{dir}/{name}.ppm"), out).unwrap(); + } +} + +#[test] +fn screenshots() { + let mut a = App::new(&[]); + a.seed = 12345; + let mut px = vec![0u16; (SIZE * SIZE) as usize]; + snap(&mut a, &mut px, "hub"); + for i in 0..6 { + let mut a = App::new(&[]); + a.seed = 777 + i as u32; + let mut px = vec![0u16; (SIZE * SIZE) as usize]; + a.touch(233, ROW0_FOR_TESTS + 52 * i + 20, true); + snap(&mut a, &mut px, &format!("g{i}_ready")); + match i { + 0 => { + a.touch(233, 233, true); + for _ in 0..30 { + a.tick(); + } + snap(&mut a, &mut px, "g0_play"); + for _ in 0..3000 { + if a.tick().save { + break; + } + } + snap(&mut a, &mut px, "g0_over"); + } + 1 => { + a.touch(233, 233, true); + for _ in 0..4 { + a.tick(); + } + a.touch(400, 233, true); + for _ in 0..3 { + a.tick(); + } + snap(&mut a, &mut px, "g1_play"); + } + 2 => { + a.touch(233, 233, true); + for _ in 0..12 { + a.tick(); + } + snap(&mut a, &mut px, "g2_spin"); + } + 3 => { + a.touch(233, 233, true); + for _ in 0..12 { + a.tick(); + } + snap(&mut a, &mut px, "g3_rolled"); + } + 4 => { + for (x0, y0, x1, y1) in [(300, 233, 150, 233), (233, 300, 233, 150), (150, 233, 300, 233)] { + a.touch(x0, y0, true); + a.touch(x1, y1, false); + } + snap(&mut a, &mut px, "g4_play"); + } + _ => { + a.touch(233 - 84, 233 - 90, true); + a.touch(233, 233 + 84 - 6, true); + snap(&mut a, &mut px, "g5_play"); + } + } + } +} + +fn refshot(g: &mut G, name: &str) { + let mut px = vec![0u16; (SIZE * SIZE) as usize]; + { + let mut fb = Fb::new(&mut px); + g.draw(&mut fb, true); + } + if let Ok(dir) = std::env::var("SHOTS_DIR") { + let mut out = format!("P6\n{SIZE} {SIZE}\n255\n").into_bytes(); + for p in px.iter() { + out.push(((p >> 11) as u8 & 31) << 3); + out.push(((p >> 5) as u8 & 63) << 2); + out.push((*p as u8 & 31) << 3); + } + std::fs::write(format!("{dir}/{name}.ppm"), out).unwrap(); + } +} + +/// The exact states of the user's reference screenshots, for the pixel diff (tools/diff_refs.py). +#[test] +fn reference_states() { + use crate::{bottle, dice, g2048, snake, ttt}; + let mut sn = snake::Snake::new(1, 0); + sn.set_ref(&[(5, 7), (6, 7), (7, 7), (8, 7), (9, 7), (10, 7), (11, 7), (11, 6), (11, 5), (11, 4), (12, 4)], (3, 3)); + refshot(&mut sn, "ref_snake"); + let mut g = g2048::G2048::new(1, 0); + g.set_ref([1, 2, 4, 3, 3, 4, 1, 4, 0, 3, 4, 2, 0, 0, 1, 2], 224, 224); + refshot(&mut g, "ref_2048"); + let mut b = bottle::Bottle::new(1, 0); + b.set_ref(6554, bottle::St::Spin, false); + refshot(&mut b, "ref_bottle_spin"); + b.set_ref(1274, bottle::St::Done, false); + refshot(&mut b, "ref_truth"); + b.set_ref(44050, bottle::St::Done, false); + refshot(&mut b, "ref_truth2"); + b.set_ref(23440, bottle::St::Done, true); + refshot(&mut b, "ref_dare"); + let mut d = dice::Dice::new(1, 0); + d.set_ref([5, 4], [0, 1640]); + refshot(&mut d, "ref_dice54"); + d.set_ref([5, 1], [-500, 1640]); + refshot(&mut d, "ref_dice51"); + let mut t = ttt::Ttt::new(1, 0); + refshot(&mut t, "ref_ttt_empty"); + t.set_ref([1, 0, 2, 1, 2, 2, 1, 0, 1]); + refshot(&mut t, "ref_ttt_win"); +} diff --git a/glitch/src/snake.rs b/glitch/src/snake.rs new file mode 100644 index 0000000..725492e --- /dev/null +++ b/glitch/src/snake.rs @@ -0,0 +1,473 @@ +// Snake on the whole round face: a 17x17 grid of dots, the cells inside the circle are playable. Tap the left half to +// turn left, the right half to turn right (or turn the crown). Leaving the circle wraps to the opposite side of the same row or column; only biting yourself ends the run. + +use crate::gfx::*; +use cmf_wp2_ui::draw::disc; +use cmf_wp2_ui::text::int; +use cmf_wp2_ui::Surface; + +const N: i32 = 17; +const CELL: i32 = 28; +const ORG: i32 = C - N * CELL / 2; // negative: the grid is a little wider than the screen +const DX: [i32; 4] = [1, 0, -1, 0]; +const DY: [i32; 4] = [0, 1, 0, -1]; + +#[derive(Clone, Copy, PartialEq, Debug)] +pub enum St { + Ready, + Playing, + Over, +} + +pub struct Snake { + pub st: St, + rng: Rng, + best: u32, + body: [u16; 256], // ring buffer of cell indices, tail at `tail` + tail: usize, + len: usize, + dir: usize, + turn: i8, + grid: [u8; (N * N) as usize], // 0 empty, 1 body, 2 food, 3 head + food: u16, + dirty: [u16; 4], + nd: usize, + lock: u8, + redraw: bool, + shown: u32, +} + +pub fn valid(x: i32, y: i32) -> bool { + if x < 0 || y < 0 || x >= N || y >= N { + return false; + } + // the whole round face: every cell whose centre is inside the circle, so edge cells are partly clipped + let (dx, dy) = (2 * x + 1 - N, 2 * y + 1 - N); + dx * dx + dy * dy <= N * N +} + +fn cell(x: i32, y: i32) -> u16 { + (y * N + x) as u16 +} + +impl Snake { + fn reset(&mut self) { + self.grid = [0; (N * N) as usize]; + self.len = 3; + self.tail = 0; + self.dir = 0; + self.turn = 0; + for i in 0..3 { + let c = cell(6 + i, 8); + self.body[i as usize] = c; + self.grid[c as usize] = 1; + } + self.grid[cell(8, 8) as usize] = 3; + self.place_food(); + self.redraw = true; + self.shown = u32::MAX; + } + + fn head(&self) -> u16 { + self.body[(self.tail + self.len - 1) & 255] + } + + fn place_food(&mut self) { + for _ in 0..400 { + let (x, y) = (self.rng.below(N as u32) as i32, self.rng.below(N as u32) as i32); + if valid(x, y) && self.grid[cell(x, y) as usize] == 0 { + self.food = cell(x, y); + self.grid[self.food as usize] = 2; + self.mark(self.food); + return; + } + } + // crowded board: first free cell + for y in 0..N { + for x in 0..N { + if valid(x, y) && self.grid[cell(x, y) as usize] == 0 { + self.food = cell(x, y); + self.grid[self.food as usize] = 2; + self.mark(self.food); + return; + } + } + } + } + + fn mark(&mut self, c: u16) { + if self.nd < 4 { + self.dirty[self.nd] = c; + self.nd += 1; + } + } + + fn die(&mut self) -> u8 { + self.st = St::Over; + self.lock = 20; + self.redraw = true; + EV_DONE | EV_DIRTY + } + + fn turn_by(&mut self, t: i8) { + self.turn = t; + } +} + +/// Wrap-around on the round board: when the next cell is outside the circle, re-enter at the far end of the same +/// row or column (walk back against the heading from the head while still inside). +fn wrap(hx: i32, hy: i32, dir: usize) -> (i32, i32) { + let (mut x, mut y) = (hx, hy); + while valid(x - DX[dir], y - DY[dir]) { + x -= DX[dir]; + y -= DY[dir]; + } + (x, y) +} + +/// Round white dots for the snake, a round red dot for the food, like the widget. Empty cells are blank: there is +/// no wall. +fn draw_cell(s: &mut S, c: u16, kind: u8) { + let (x, y) = ((c as i32 % N), (c as i32 / N)); + let (cx, cy) = (ORG + x * CELL + CELL / 2, ORG + y * CELL + CELL / 2); + s.rect(cx - CELL / 2, cy - CELL / 2, CELL, CELL, BG); + match kind { + 0 => {} + 1 | 3 => disc(s, cx, cy, 12, WHITEC, BGC), + _ => disc(s, cx, cy, 12, REDC, BGC), + } +} + +impl Game for Snake { + fn new(seed: u32, best: u32) -> Self { + let mut g = Snake { + st: St::Ready, + rng: Rng::new(seed), + best, + body: [0; 256], + tail: 0, + len: 3, + dir: 0, + turn: 0, + grid: [0; (N * N) as usize], + food: 0, + dirty: [0; 4], + nd: 0, + lock: 0, + redraw: true, + shown: u32::MAX, + }; + g.reset(); + g + } + + fn period(&self) -> u32 { + let sc = self.score() as i32; + (170 - sc * 4).max(80) as u32 + } + + fn running(&self) -> bool { + self.st == St::Playing || self.lock > 0 + } + + fn tick(&mut self) -> u8 { + if self.st != St::Playing { + self.lock = self.lock.saturating_sub(1); + return 0; + } + if self.turn != 0 { + self.dir = ((self.dir as i32 + self.turn as i32 + 4) % 4) as usize; + self.turn = 0; + } + let h = self.head() as i32; + let (mut nx, mut ny) = (h % N + DX[self.dir], h / N + DY[self.dir]); + if !valid(nx, ny) { + (nx, ny) = wrap(h % N, h / N, self.dir); + } + let nc = cell(nx, ny); + let eat = self.grid[nc as usize] == 2; + let tail_c = self.body[self.tail & 255]; + if self.grid[nc as usize] == 1 && !(nc == tail_c && !eat) { + return self.die(); + } + let old_head = self.head(); + self.nd = 0; + if !eat { + self.grid[tail_c as usize] = 0; + self.mark(tail_c); + self.tail = (self.tail + 1) & 255; + self.len -= 1; + } + self.grid[old_head as usize] = 1; + self.mark(old_head); + self.body[(self.tail + self.len) & 255] = nc; + self.len += 1; + self.grid[nc as usize] = 3; + self.mark(nc); + let mut ev = EV_DIRTY; + if eat { + ev |= EV_TICK; + self.place_food(); + self.redraw = true; // the score changed: redraw whole field so the score text sits on top cleanly + } + ev + } + + fn touch(&mut self, x: i32, _y: i32, down: bool) -> u8 { + if !down { + return 0; + } + match self.st { + St::Ready => { + self.st = St::Playing; + self.redraw = true; + EV_DIRTY + } + St::Over => { + if self.lock > 0 { + return 0; + } + self.reset(); + self.st = St::Ready; + EV_DIRTY + } + St::Playing => { + self.turn_by(if x < C { -1 } else { 1 }); + 0 + } + } + } + + fn crown(&mut self, d: i32) -> u8 { + if self.st == St::Playing { + self.turn_by(if d < 0 { -1 } else { 1 }); + } + 0 + } + + fn score(&self) -> u32 { + self.len as u32 - 3 + } + + fn draw(&mut self, s: &mut S, full: bool) { + if full || self.redraw { + clear(s); + self.redraw = false; + self.shown = u32::MAX; + self.nd = 0; + for y in 0..N { + for x in 0..N { + if valid(x, y) { + let c = cell(x, y); + draw_cell(s, c, self.grid[c as usize]); + } + } + } + match self.st { + St::Ready => { + txt(s, C, C + 60, "TAP TO START", 2, 2, GREY); + txt(s, C, C + 84, "TAP SIDES TO TURN", 2, 1, MID); + } + St::Over => { + s.rect(C - 150, C - 44, 300, 92, BG); + txt(s, C, C - 28, "GAME OVER", 3, 2, RED); + txt(s, C, C + 10, "TAP TO RETRY", 2, 2, GREY); + } + St::Playing => {} + } + } else { + for i in 0..self.nd { + let c = self.dirty[i]; + draw_cell(s, c, self.grid[c as usize]); + } + self.nd = 0; + } + let sc = self.score(); + if sc != self.shown { + self.shown = sc; + let mut buf = [0u8; 11]; + s.rect(C - 70, 34, 140, 16, BG); + s.text(C - 56, 34, "Score:", WHITE); + s.text(C + 48, 34, int(sc as i32, &mut buf), WHITE); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn board_is_round() { + let n = (0..N).flat_map(|y| (0..N).map(move |x| (x, y))).filter(|&(x, y)| valid(x, y)).count(); + assert!((200..=250).contains(&n), "{n}"); + assert!(!valid(0, 0) && valid(8, 8) && valid(8, 0) && !valid(-1, 5)); + } + + #[test] + fn eats_then_wraps_instead_of_dying() { + let mut g = Snake::new(5, 0); + g.st = St::Playing; + // put food right ahead of the head + g.grid[g.food as usize] = 0; + g.food = cell(9, 8); + g.grid[g.food as usize] = 2; + assert_eq!(g.tick() & EV_TICK, EV_TICK); + assert_eq!(g.score(), 1); + let mut n = 0; + while g.st == St::Playing && n < 100 { + g.tick(); + n += 1; + } + assert_eq!(g.st, St::Playing, "must not die at the edge"); + assert!(n >= 100 || g.score() >= 1); + } + + #[test] + fn wrap_maps_to_opposite_side_same_line() { + for y in 0..N { + for x in 0..N { + if !valid(x, y) { + continue; + } + for d in 0..4 { + if valid(x + DX[d], y + DY[d]) { + continue; + } + let (wx, wy) = wrap(x, y, d); + assert!(valid(wx, wy)); + // same row (horizontal) or column (vertical) + if d % 2 == 0 { assert_eq!(wy, y) } else { assert_eq!(wx, x) } + // the far end: one more step backwards leaves the circle + assert!(!valid(wx - DX[d], wy - DY[d])); + } + } + } + } + + fn straight(g: &mut Snake, x: i32, y: i32, dir: usize) { + g.st = St::Playing; + g.grid = [0; (N * N) as usize]; + g.tail = 0; + g.dir = dir; + for i in 0..3 { + let (px, py) = (x - DX[dir] * (2 - i), y - DY[dir] * (2 - i)); + g.body[i as usize] = cell(px, py); + g.grid[cell(px, py) as usize] = if i == 2 { 3 } else { 1 }; + } + g.food = cell(5, 5); + g.grid[g.food as usize] = 2; + } + + #[test] + fn wraps_all_four_directions() { + // right edge -> left + let mut g = Snake::new(1, 0); + let (ex, _) = wrap(8, 8, 0); // leftmost of row 7 + let rx = 16 - ex; // rightmost of row 7 (symmetric) + straight(&mut g, rx, 8, 0); + g.tick(); + assert_eq!(g.st, St::Playing); + assert_eq!(g.head() as i32, cell(ex, 8) as i32); + // left -> right + let mut g = Snake::new(1, 0); + straight(&mut g, ex, 8, 2); + g.tick(); + assert_eq!(g.st, St::Playing); + assert_eq!(g.head() as i32, cell(rx, 8) as i32); + // bottom -> top, top -> bottom (column 7) + let mut g = Snake::new(1, 0); + straight(&mut g, 8, 16, 1); + g.tick(); + assert_eq!(g.st, St::Playing); + assert_eq!(g.head() as i32, cell(8, 0) as i32); + let mut g = Snake::new(1, 0); + straight(&mut g, 8, 0, 3); + g.tick(); + assert_eq!(g.st, St::Playing); + assert_eq!(g.head() as i32, cell(8, 16) as i32); + } + + #[test] + fn wrap_into_own_body_still_kills() { + let mut g = Snake::new(1, 0); + straight(&mut g, 16 - wrap(8, 8, 0).0, 8, 0); + let c = cell(wrap(8, 8, 0).0, 8); + g.grid[c as usize] = 1; // body segment at the entry cell + g.tick(); + assert_eq!(g.st, St::Over); + } + + #[test] + fn turns_and_self_bite() { + let mut g = Snake::new(5, 0); + g.st = St::Playing; + g.turn_by(1); + g.tick(); + assert_eq!(g.dir, 1); + // grow long then curl into itself + let mut g = Snake::new(9, 0); + g.st = St::Playing; + // fabricate a 6-long snake curling + g.grid = [0; (N * N) as usize]; + let path = [(4, 7), (5, 7), (6, 7), (7, 7), (7, 8), (6, 8)]; + g.tail = 0; + g.len = 6; + for (i, &(x, y)) in path.iter().enumerate() { + g.body[i] = cell(x, y); + g.grid[cell(x, y) as usize] = 1; + } + g.dir = 2; // heading left from (7,9) -> (6,9) + g.turn_by(1); // turn to up: (7,8) is body + let ev = g.tick(); + assert_eq!(g.st, St::Over); + assert!(ev & EV_DONE != 0); + } + + #[test] + fn each_food_adds_exactly_one_segment_also_after_wrap() { + let mut g = Snake::new(2, 0); + let ex = wrap(8, 8, 0).0; + straight(&mut g, 16 - ex, 8, 0); + // food on the wrap-around entry cell + g.grid[g.food as usize] = 0; + g.food = cell(ex, 8); + g.grid[g.food as usize] = 2; + let (l0, s0) = (g.len, g.score()); + g.tick(); + assert_eq!(g.st, St::Playing); + assert_eq!((g.len, g.score()), (l0 + 1, s0 + 1)); + let n = g.grid.iter().filter(|v| **v == 1 || **v == 3).count(); + assert_eq!(n, g.len, "grid and length agree"); + // plain move without food keeps the length + g.tick(); + assert_eq!(g.len, l0 + 1); + // eat again mid-board: +1 again + let h = g.head() as i32; + let next = cell(h % N + 1, h / N); + g.grid[g.food as usize] = 0; + g.food = next; + g.grid[next as usize] = 2; + g.tick(); + assert_eq!(g.len, l0 + 2); + } +} + +#[cfg(test)] +impl Snake { + /// Test hook: a given body (tail first) and food, as in the reference screenshot. + pub fn set_ref(&mut self, path: &[(i32, i32)], food: (i32, i32)) { + self.grid = [0; (N * N) as usize]; + self.tail = 0; + self.len = path.len(); + for (i, &(x, y)) in path.iter().enumerate() { + self.body[i] = cell(x, y); + self.grid[cell(x, y) as usize] = if i + 1 == path.len() { 3 } else { 1 }; + } + self.food = cell(food.0, food.1); + self.grid[self.food as usize] = 2; + self.st = St::Playing; + self.redraw = true; + self.shown = u32::MAX; + } +} diff --git a/glitch/src/sprites.rs b/glitch/src/sprites.rs new file mode 100644 index 0000000..68e0a5d --- /dev/null +++ b/glitch/src/sprites.rs @@ -0,0 +1,245 @@ +// Sprite bitmaps cut from the Chromium T-Rex runner sprite sheet (100-offline-sprite.png), 1 bit per pixel, +// MSB first, rows padded to whole bytes. Copyright The Chromium Authors, BSD-3-Clause: see NOTICE-CHROMIUM. +// Generated by tools/extract_sprites.py. Do not edit by hand. + +pub struct Sprite { + pub w: i32, + pub h: i32, + pub rows: &'static [u8], +} + +pub static TREX_STAND: Sprite = Sprite { w: 44, h: 47, rows: &[ + 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0x0F, 0xFF, 0xFF,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0xFC, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, + 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x3F, 0xCF, 0xC0, 0x00, 0x00, + 0x00, 0x3F, 0xCF, 0xC0, 0x00, 0x00, 0x00, 0x3F, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x3F, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x3F, 0xCF, 0xF0, 0x00, 0x00, + 0x00, 0x3F, 0xCF, 0xF0, 0x00, 0x00, 0x00, 0x3F, 0xCF, 0xF0, 0x00, 0x00, 0x00, 0x3F, 0xCF, 0xF0, 0x00, 0x00, +] }; + +pub static TREX_RUN1: Sprite = Sprite { w: 44, h: 47, rows: &[ + 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, + 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, + 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x3C, 0x00, 0x3F, 0xFF, 0xFF, 0x00, + 0x3C, 0x00, 0x3F, 0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F, 0xFF, 0xFF, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, + 0x0F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, + 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xC0, 0x00, 0x00, + 0x00, 0x3F, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x0F, 0xCF, 0xC0, 0x00, 0x00, 0x00, 0x0F, 0xCF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, + 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, +] }; + +pub static TREX_CRASH: Sprite = Sprite { w: 44, h: 47, rows: &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, + 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x00, + 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x00, 0x30, 0x00, 0x0F, 0xFC, 0x00, 0x00, 0x30, 0x00, 0x0F, 0xFC, 0x00, 0x00, 0x30, 0x00, 0x7F, 0xFC, 0x00, 0x00, + 0x30, 0x00, 0x7F, 0xFC, 0x00, 0x00, 0x3C, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x3C, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x3F, 0x0F, 0xFF, 0xFC, 0xC0, 0x00, + 0x3F, 0x0F, 0xFF, 0xFC, 0xC0, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, + 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xF0, 0x00, 0x00, + 0x03, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, 0x00, + 0x00, 0x3F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xCF, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xCF, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x03, 0x00, 0x00, 0x00, + 0x00, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x03, 0xC0, 0x00, 0x00, + 0x00, 0x0F, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +] }; + +pub static TREX_DUCK1: Sprite = Sprite { w: 59, h: 47, rows: &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFC, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0x80, 0xFF, 0xC3, 0xFF, 0xFF, 0xE7, 0xFF, 0xFF, 0x80, 0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0x00, + 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, +] }; + +pub static TREX_DUCK2: Sprite = Sprite { w: 59, h: 47, rows: &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFC, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 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, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xE0, 0x07, 0xF8, 0x00, 0x00, 0x00, + 0x00, 0x1F, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x1F, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, +] }; + +pub static CACTUS_S1: Sprite = Sprite { w: 17, h: 35, rows: &[ + 0x01, 0xC0, 0x00, 0x03, 0xE0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF2, 0x00, 0x07, 0xF7, 0x00, 0x07, 0xFF, 0x80, 0x07, 0xFF, 0x80, + 0x27, 0xFF, 0x80, 0x77, 0xFF, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x80, + 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0x00, 0xFF, 0xFE, 0x00, 0xFF, 0xFC, 0x00, 0xFF, 0xF0, 0x00, 0x7F, 0xF0, 0x00, 0x3F, 0xF0, 0x00, 0x1F, 0xF0, 0x00, + 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, + 0x07, 0xF0, 0x00, 0x07, 0xF0, 0x00, 0x03, 0xE0, 0x00, +] }; + +pub static CACTUS_S2: Sprite = Sprite { w: 34, h: 35, rows: &[ + 0x01, 0xC0, 0x00, 0xE0, 0x00, 0x03, 0xE0, 0x01, 0xF0, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x00, 0x07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, 0x00, 0x07, 0xF0, 0x03, + 0xF8, 0x01, 0xFC, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, + 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, 0x00, 0x07, 0xF0, 0x03, 0xF8, 0x01, 0xFC, 0x00, 0x03, 0xE0, + 0x01, 0xF0, 0x00, 0xF8, 0x00, +] }; + +pub static CACTUS_L1: Sprite = Sprite { w: 25, h: 50, rows: &[ + 0x00, 0x3E, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, + 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x8E, 0x00, 0x00, 0xFF, 0x9F, 0x00, + 0x38, 0xFF, 0xBF, 0x80, 0x7C, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, + 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, + 0xFE, 0xFF, 0xBF, 0x80, 0xFE, 0xFF, 0xBF, 0x80, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFE, 0x00, 0x7F, 0xFF, 0xFC, 0x00, + 0x3F, 0xFF, 0xF8, 0x00, 0x1F, 0xFF, 0xF0, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, + 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, + 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, 0x90, 0x00, 0x05, 0xFF, 0xB8, 0x00, 0x0F, 0xFF, 0x90, 0x00, 0x05, 0xFF, 0x80, 0x00, + 0x00, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x80, 0x00, +] }; + +pub static CACTUS_L2: Sprite = Sprite { w: 50, h: 50, rows: &[ + 0x00, 0x3E, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, + 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x1C, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x3E, 0x7F, 0xC0, + 0x00, 0x00, 0xFF, 0x80, 0x7F, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x7F, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x7F, 0x7F, 0xC0, 0x00, 0x00, 0xFF, + 0x8C, 0x7F, 0x7F, 0xC7, 0x00, 0x00, 0xFF, 0x9F, 0x7F, 0x7F, 0xCF, 0x80, 0x38, 0xFF, 0xBF, 0x7F, 0x7F, 0xDF, 0xC0, 0x7C, 0xFF, 0xBF, 0x7F, 0x7F, + 0xDF,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0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, + 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x7F, 0xC0, 0x00, 0x00, 0xFF, 0x90, 0x00, + 0x7F, 0xC8, 0x00, 0x05, 0xFF, 0xB8, 0x02, 0xFF, 0xDC, 0x00, 0x0F, 0xFF, 0x90, 0x07, 0xFF, 0xC8, 0x00, 0x05, 0xFF, 0x80, 0x02, 0xFF, 0xC0, 0x00, + 0x00, 0x01, 0xC0, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x40, 0x00, +] }; + +pub static CACTUS_L3: Sprite = Sprite { w: 75, h: 50, rows: &[ + 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x80, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xC0, 0x00, 0x00, 0xFF, 0x80, 0x00, + 0xE0, 0x00, 0x00, 0x3F, 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x01, 0xF0, 0x00, 0x00, 0x3F, 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x03, 0xF8, 0x00, 0x00, 0x3F, + 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x03, 0xF8, 0x00, 0x00, 0x3F, 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x03, 0xF8, 0x00, 0x0E, 0x3F, 0xE0, 0x00, 0x00, 0xFF, + 0x80, 0x03, 0xF8, 0x80, 0x1F, 0x3F, 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x03, 0xF9, 0xC0, 0x3F, 0xBF, 0xE0, 0x00, 0x00, 0xFF, 0x80, 0x03, 0xFB, 0xE0, + 0x3F, 0xBF, 0xE0, 0x00, 0x00, 0xFF, 0x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, 0xF8, 0x1F, + 0x00, 0x3F, 0xEE, 0x00, 0x05, 0xFF, 0x90, 0x83, 0xF8, 0x1F, 0x01, 0x7F, 0xE4, 0x00, 0x0F, 0xFF, 0x81, 0xC3, 0xF8, 0x9F, 0x03, 0xFF, 0xE0, 0x00, + 0x05, 0xFF, 0xC0, 0x81, 0xF1, 0xCE, 0x01, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00, 0x20, 0x00, +] }; + +pub static PTERO1: Sprite = Sprite { w: 46, h: 40, rows: &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00, + 0x0F, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, + 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, + 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, +] }; + +pub static PTERO2: Sprite = Sprite { w: 46, h: 40, rows: &[ + 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0x00, 0x00, 0x00, + 0x00, 0x03, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, 0x00, + 0x00, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xF0, 0x00, 0x00, + 0x0F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, + 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, + 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xC0, + 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +] }; + +pub static CLOUD: Sprite = Sprite { w: 46, h: 14, rows: &[ + 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x40, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x40, 0x01, 0x3F, 0x00, 0x00, 0x03, 0xC0, 0x02, 0x01, 0x00, 0x00, 0x06, 0x00, 0x00, 0x01, 0xE0, + 0x03, 0xFC, 0x00, 0x00, 0x00, 0x20, 0x06, 0x00, 0x00, 0x00, 0x00, 0x38, 0x04, 0x00, 0x00, 0x00, 0x00, 0x08, 0x6C, 0x40, 0x00, 0x00, 0x00, 0x04, + 0xC0, 0x3F, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +] }; + +pub static GROUND: Sprite = Sprite { w: 240, h: 12, rows: &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, +] }; diff --git a/glitch/src/ttt.rs b/glitch/src/ttt.rs new file mode 100644 index 0000000..66cd82e --- /dev/null +++ b/glitch/src/ttt.rs @@ -0,0 +1,221 @@ +// Tic Tac Toe for two people on one watch: white X and red O take turns, X first. Tap a cell to place the mark. + +use crate::gfx::*; +use cmf_wp2_ui::color::{rgb565, Rgb}; +use cmf_wp2_ui::draw::{line, ring}; + +const WHITEC_565: u16 = WHITE; +use cmf_wp2_ui::Surface; + +const CS: i32 = 112; +const X0: i32 = C - CS * 3 / 2; +const Y0: i32 = C - CS * 3 / 2 - 4; +const LINES: [[usize; 3]; 8] = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]; + +pub struct Ttt { + pub b: [u8; 9], // 0 empty, 1 X, 2 O + pub turn: u8, // whose mark is next: 1 X, 2 O + pub res: u8, // 0 playing, 1 X wins, 2 O wins, 3 draw + wl: usize, + wins: u32, + best: u32, + redraw: bool, +} + +pub fn winner(b: &[u8; 9]) -> Option { + LINES.iter().position(|l| b[l[0]] != 0 && b[l[0]] == b[l[1]] && b[l[1]] == b[l[2]]) +} + +impl Ttt { + fn empties(&self) -> u32 { + self.b.iter().filter(|v| **v == 0).count() as u32 + } + + fn judge(&mut self) -> u8 { + if let Some(l) = winner(&self.b) { + self.wl = l; + self.res = self.b[LINES[l][0]]; + self.wins += 1; + return EV_DOUBLE; + } + if self.empties() == 0 { + self.res = 3; + return EV_DONE; + } + 0 + } +} + +fn centre(i: usize) -> (i32, i32) { + (X0 + (i as i32 % 3) * CS + CS / 2, Y0 + (i as i32 / 3) * CS + CS / 2) +} + +impl Game for Ttt { + fn new(seed: u32, best: u32) -> Self { + let _ = seed; + Ttt { b: [0; 9], turn: 1, res: 0, wl: 0, wins: 0, best, redraw: true } + } + + fn period(&self) -> u32 { + 100 + } + + fn running(&self) -> bool { + false + } + + fn tick(&mut self) -> u8 { + 0 + } + + fn touch(&mut self, x: i32, y: i32, down: bool) -> u8 { + if !down { + return 0; + } + if self.res != 0 { + self.b = [0; 9]; + self.turn = 1; + self.res = 0; + return EV_DIRTY; + } + let (cx, cy) = (x - X0, y - Y0); + if cx < 0 || cy < 0 || cx >= CS * 3 || cy >= CS * 3 { + return 0; + } + let i = (cy / CS * 3 + cx / CS) as usize; + if self.b[i] != 0 { + return 0; + } + self.b[i] = self.turn; + self.turn = 3 - self.turn; + let mut ev = self.judge(); + if ev == 0 { + ev = EV_TICK; + } + ev | EV_DIRTY + } + + fn crown(&mut self, _d: i32) -> u8 { + 0 + } + + fn score(&self) -> u32 { + self.wins + } + + fn draw(&mut self, s: &mut S, _full: bool) { + clear(s); + for k in 1..3 { + s.rect(X0 + k * CS - 3, Y0 + 6, 6, CS * 3 - 12, LINE); + s.rect(X0 + 6, Y0 + k * CS - 3, CS * 3 - 12, 6, LINE); + } + let over = self.res == 1 || self.res == 2; + let win = if over { LINES[self.wl] } else { [9; 3] }; + for i in 0..9 { + let (cx, cy) = centre(i); + let part = win.contains(&i); + // after a win everything outside the winning line goes dim, as in the widget + let (xc, oc) = if over && !part { (rgb565(86, 85, 90), Rgb(81, 29, 33)) } else { (WHITEC_565, REDC) }; + match self.b[i] { + 1 => { + let (a, b) = (34, 34); + line(s, cx - a, cy - b, cx + a, cy + b, 18, Rgb::from565(xc), BGC); + line(s, cx - a, cy + b, cx + a, cy - b, 18, Rgb::from565(xc), BGC); + } + 2 => ring(s, cx, cy, 21, 41, oc, BGC), + _ => {} + } + } + if over { + let (a, b) = (centre(win[0]), centre(win[2])); + line(s, a.0, a.1, b.0, b.1, 13, REDC, BGC); + } + let msg = match self.res { + 1 => "X WINS", + 2 => "O WINS", + 3 => "DRAW", + _ => "", + }; + let _ = msg; // the widget shows no text: the red line and the dimmed marks say it + let _ = self.best; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn winner_lines() { + let mut b = [0u8; 9]; + b[2] = 1; + b[4] = 1; + b[6] = 1; + assert_eq!(winner(&b), Some(7)); + } + + #[test] + fn two_players_alternate() { + let mut g = Ttt::new(3, 0); + let t = |g: &mut Ttt, i: usize| g.touch(centre(i).0, centre(i).1, true); + assert!(t(&mut g, 0) & EV_DIRTY != 0); + assert_eq!(g.b.iter().filter(|v| **v != 0).count(), 1, "no bot reply"); + assert_eq!(g.b[0], 1); + assert_eq!(t(&mut g, 0), 0); // occupied, turn not consumed + t(&mut g, 4); + assert_eq!(g.b[4], 2); + t(&mut g, 1); + assert_eq!(g.b[1], 1); + t(&mut g, 8); + t(&mut g, 2); // X completes 0,1,2 + assert_eq!(g.res, 1); + assert_eq!(t(&mut g, 5), 0 | EV_DIRTY); // tap restarts, does not place + assert_eq!(g.b, [0; 9]); + assert_eq!(g.turn, 1); + } + + #[test] + fn o_can_win_and_draw_is_detected() { + let mut g = Ttt::new(1, 0); + let t = |g: &mut Ttt, i: usize| g.touch(centre(i).0, centre(i).1, true); + for i in [0, 3, 1, 4, 8, 5] { + t(&mut g, i); + } + assert_eq!(g.res, 2); + t(&mut g, 0); + // X O X / X O O / O X X (no line) + for i in [0, 1, 2, 4, 3, 5, 7, 6, 8] { + t(&mut g, i); + } + assert_eq!(g.res, 3); + } + + #[test] + fn win_screen_renders() { + use cmf_wp2_ui::Fb; + let mut g = Ttt::new(1, 0); + g.b = [1, 2, 0, 2, 1, 0, 2, 0, 1]; + g.judge(); + assert_eq!(g.res, 1); + let mut px = vec![0u16; (SIZE * SIZE) as usize]; + g.draw(&mut Fb::new(&mut px), true); + if let Ok(dir) = std::env::var("SHOTS_DIR") { + let mut out = format!("P6\n{SIZE} {SIZE}\n255\n").into_bytes(); + for p in &px { + out.push(((p >> 11) as u8 & 31) << 3); + out.push(((p >> 5) as u8 & 63) << 2); + out.push((*p as u8 & 31) << 3); + } + std::fs::write(format!("{dir}/ttt_win.ppm"), out).unwrap(); + } + } +} + +#[cfg(test)] +impl Ttt { + pub fn set_ref(&mut self, b: [u8; 9]) { + self.b = b; + self.judge(); + self.redraw = true; + } +} diff --git a/glitch/store.md b/glitch/store.md new file mode 100644 index 0000000..47b4af0 --- /dev/null +++ b/glitch/store.md @@ -0,0 +1,16 @@ +Six small games in one app, drawn in a monochrome dot-matrix style with red accents and laid out for the round screen. Open it, tap a game, play, press the crown (or swipe right) to come back to the list. + +- **Dino Runner**: the Chrome offline T-Rex. Tap to jump, turn the crown to duck (or to drop faster in the air). Cacti and pterodactyls at three heights, speed climbing the longer you last, a milestone click every 100 points. +- **Snake**: tap the left or right half of the screen to turn (or turn the crown). The board is the whole round face and has no walls: leave one edge and you come back on the opposite side. Each food adds one segment, and biting yourself is the only way to lose. +- **Spin the Bottle**: tap (or turn the crown) to spin a bottle of white squares. It clicks past each notch, slows down, and the screen turns red with TRUTH or DARE, the neck pointing where it landed. +- **Dice**: tap or turn the crown to roll two tilted dice. They tumble and land on random faces, with the total underneath. +- **2048**: swipe to slide, or turn the crown for up and down. +- **Tic Tac Toe**: two players share the watch, taking turns as the white X and the red O (X goes first). Tap a cell to place your mark, a win gets a red line, tap to play again. + +Per-game high scores are kept on the watch. Feedback is a short haptic click, with no sound. + +The T-Rex and its obstacles are the sprites from Chromium's offline game, used under the Chromium BSD-3-Clause licence (see NOTICE-CHROMIUM in the app folder). Everything else is original. + +## Requirements + +The [patched firmware](/firmware) and the [App Loader](/firmware). Open it from the **Apps** row at the end of the watch's app list (list style, not the waterfall). diff --git a/glitch/store.toml b/glitch/store.toml new file mode 100644 index 0000000..b6d1728 --- /dev/null +++ b/glitch/store.toml @@ -0,0 +1,6 @@ +# The app's listing in the FMC store (README.md, "store.toml"). The page's body is store.md. +name = "Glitch" +summary = "Six round-screen games in one app: Dino Runner, Snake, Spin the Bottle, Dice, 2048, Tic Tac Toe" +version = "0.2.0" +author = "X0D4N" +screenshots = ["shots/hub.png", "shots/dino.png", "shots/snake.png", "shots/bottle.png", "shots/dice.png", "shots/2048.png", "shots/tictactoe.png", "shots/tictactoe_win.png"]