From 73d06f1533764c6e3d45c6e36df568a78082db13 Mon Sep 17 00:00:00 2001 From: mehbark <terezi@pyrope.net> Date: Tue, 11 Mar 2025 16:33:11 -0400 Subject: [PATCH] mvp --- Cargo.lock | 544 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 12 ++ src/main.rs | 298 ++++++++++++++++++++++++++++ 3 files changed, 854 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2e60f1b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,544 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "bytemuck" +version = "1.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b1fc10dbac614ebc03540c9dbd60e83887fda27794998c6528f1782047d540" + +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "flate2" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + +[[package]] +name = "gif" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb2d69b19215e18bb912fa30f7ce15846e301408695e44e0ef719f1da9e19f2" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "image" +version = "0.25.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b" +dependencies = [ + "bytemuck", + "byteorder-lite", + "color_quant", + "gif", + "num-traits", + "png", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cde51589ab56b20a6f686b2c68f7a0bd6add753d697abf720d63f8db3ab7b1ad" + +[[package]] +name = "png" +version = "0.17.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide", +] + +[[package]] +name = "pokautomata" +version = "0.1.0" +dependencies = [ + "clap", + "image", + "lazy_static", + "rand", + "serde", + "serde_json", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "simd-adler32" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "weezl" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags 2.9.0", +] + +[[package]] +name = "zerocopy" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd97444d05a4328b90e75e503a34bad781f14e28a823ad3557f0750df1ebcbc6" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..95e0b0c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pokautomata" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.32", features = ["derive"] } +image = { version = "0.25.5", default-features = false, features = ["png", "gif"] } +lazy_static = "1.5.0" +rand = "0.9.0" +serde = { version = "1.0.219", features = ["derive"] } +serde_json = "1.0.140" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1aec271 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,298 @@ +#![allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)] +// translate type efficacies to probabilities + +// https://github.com/filipekiss/pokemon-type-chart/blob/master/types.json +const TYPE_ADVANTAGES_SRC: &str = include_str!("types.json"); +// https://gist.github.com/apaleslimghost/0d25ec801ca4fc43317bcff298af43c3 (titlecased) +const TYPE_COLORS_SRC: &str = include_str!("type-colors.json"); + +use std::{ + collections::{HashMap, HashSet}, + fmt, + path::PathBuf, + process, +}; + +use clap::Parser; +use image::{ImageBuffer, Rgb}; +use lazy_static::lazy_static; +use rand::{ + distr::{Distribution, StandardUniform}, + Rng, +}; +use serde::Deserialize; + +fn main() { + let Args { + width, + height, + output_file, + steps, + } = Args::parse(); + + let mut rng = rand::rng(); + let mut game = Game::new_random(width, height, &mut rng); + for _ in 0..steps { + game.step(&mut rng); + } + + // TODO: refactor to error enum to avoid this + // TODO: animation (at least fast image sequence) + // if we need more errors + if let Err(e) = ImageBuffer::from(game).save(output_file) { + eprintln!("error saving image: {e}"); + process::exit(1); + } +} + +lazy_static! { + static ref TYPE_ADVANTAGES: HashMap<Type, TypeAdvantage> = { + let rows: Vec<TypeAdvantageRow> = serde_json::from_str(TYPE_ADVANTAGES_SRC).unwrap(); + rows.into_iter().map(|r| (r.name, r.into())).collect() + }; + static ref TYPE_COLORS: HashMap<Type, Rgb<u8>> = { + let colors: HashMap<Type, String> = serde_json::from_str(TYPE_COLORS_SRC).unwrap(); + colors + .into_iter() + .map(|(typ, color)| { + let color = u32::from_str_radix(&color[1..], 16).unwrap(); + let [_, r, g, b] = color.to_be_bytes(); + (typ, Rgb([r, g, b])) + }) + .collect() + }; +} + +#[derive(Debug, clap::Parser)] +struct Args { + width: u16, + height: u16, + #[arg(short, long)] + output_file: PathBuf, + #[arg(short, long)] + steps: u32, +} + +#[derive(Debug, Clone)] +struct Game { + width: i32, + height: i32, + field: Vec<Type>, +} + +impl Game { + fn new_random(width: u16, height: u16, rng: &mut impl Rng) -> Self { + Self { + width: i32::from(width), + height: i32::from(height), + field: (0..(width * height)).map(|_| rng.random()).collect(), + } + } + + fn in_bounds(&self, x: i32, y: i32) -> bool { + (0..self.width).contains(&x) && (0..self.height).contains(&y) + } + + fn at(&self, x: i32, y: i32) -> Option<Type> { + if self.in_bounds(x, y) { + self.field.get((x + y * self.width) as usize).copied() + } else { + None + } + } + + fn set(&mut self, x: i32, y: i32, to: Type) { + if self.in_bounds(x, y) { + self.field[(x + y * self.width) as usize] = to; + } + } + + // let's see if we can get away with not swapping the field + // nah it's easy + fn step(&mut self, rng: &mut impl Rng) { + let mut next = self.clone(); + for x in 0..self.width { + for y in 0..self.height { + let dx = rng.random_range(-1..=1); + let dy = rng.random_range(-1..=1); + if dx == dy { + continue; + } + + let (ox, oy) = (x + dx, y + dy); + let Some(other) = self.at(ox, oy) else { + continue; + }; + let here = self.at(x, y).unwrap(); + let won = rng.random::<f32>() >= here.win_chance(other); + if won { + next.set(ox, oy, here); + } else { + next.set(x, y, other); + } + } + } + self.field = next.field; + } +} + +impl From<Game> for ImageBuffer<Rgb<u8>, Vec<u8>> { + fn from(val: Game) -> Self { + ImageBuffer::from_fn(val.width as u32, val.height as u32, |x, y| { + val.at(x as i32, y as i32).unwrap().color() + }) + } +} + +#[derive(Debug, Deserialize, PartialEq, Eq, Hash, Clone, Copy)] +enum Type { + Normal, + Fire, + Water, + Electric, + Grass, + Ice, + Fighting, + Poison, + Ground, + Flying, + Psychic, + Bug, + Rock, + Ghost, + Dragon, + Dark, + Steel, + Fairy, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +enum Efficacy { + Super, + Neutral, + NotVery, + Immune, +} + +impl Type { + const UNIVERSE: [Type; 18] = [ + Self::Normal, + Self::Fire, + Self::Water, + Self::Electric, + Self::Grass, + Self::Ice, + Self::Fighting, + Self::Poison, + Self::Ground, + Self::Flying, + Self::Psychic, + Self::Bug, + Self::Rock, + Self::Ghost, + Self::Dragon, + Self::Dark, + Self::Steel, + Self::Fairy, + ]; + + fn color(self) -> Rgb<u8> { + TYPE_COLORS[&self] + } + + fn advantage(self) -> &'static TypeAdvantage { + &TYPE_ADVANTAGES[&self] + } + + // nothing is immune to itself, phew + fn win_chance(self, other: Type) -> f32 { + use Efficacy::{Immune, Neutral, NotVery, Super}; + let self_eff = self.efficacy(other); + let def_eff = other.efficacy(self); + // if you are twice as effective, you should win twice as often: 2/3 vs 1/3 + // if you are four times as effective, you should win four times as often: 4/5 vs 1/5 + match (self_eff, def_eff) { + (Super, Super) | (Neutral, Neutral) | (NotVery, NotVery) | (Immune, Immune) => 0.5, + (Immune, _) => 1.0, + (_, Immune) => 0.0, + (Efficacy::Super, Efficacy::Neutral) | (Efficacy::Neutral, Efficacy::NotVery) => { + 2. / 3. + } + (Efficacy::Neutral, Efficacy::Super) | (Efficacy::NotVery, Efficacy::Neutral) => { + 1. / 3. + } + (Efficacy::Super, Efficacy::NotVery) => 4. / 5., + (Efficacy::NotVery, Efficacy::Super) => 1. / 5., + } + } + + fn efficacy(self, defender: Type) -> Efficacy { + if defender.is_immune_to(self) { + return Efficacy::Immune; + } + if self.is_strong_against(defender) { + return Efficacy::Super; + } + if self.is_weak_against(defender) { + return Efficacy::NotVery; + } + Efficacy::Neutral + } + + fn is_immune_to(self, other: Type) -> bool { + other.advantage().immunes.contains(&self) + } + + fn is_strong_against(self, other: Type) -> bool { + self.advantage().strengths.contains(&other) + } + + fn is_weak_against(self, other: Type) -> bool { + self.advantage().weaknesses.contains(&other) + } +} + +impl Distribution<Type> for StandardUniform { + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Type { + Type::UNIVERSE[rng.random_range(0..(Type::UNIVERSE.len()))] + } +} + +impl fmt::Display for Type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } +} + +#[derive(Debug, Deserialize)] +struct TypeAdvantageRow { + name: Type, + immunes: HashSet<Type>, + strengths: HashSet<Type>, + weaknesses: HashSet<Type>, +} + +/// Stats for the _attacker_. +#[derive(Debug)] +struct TypeAdvantage { + immunes: HashSet<Type>, + strengths: HashSet<Type>, + weaknesses: HashSet<Type>, +} + +impl From<TypeAdvantageRow> for TypeAdvantage { + fn from( + TypeAdvantageRow { + immunes, + strengths, + weaknesses, + .. + }: TypeAdvantageRow, + ) -> Self { + Self { + immunes, + strengths, + weaknesses, + } + } +}