tile.rs

2.8 kB · rust · 102 lines

1use super::geometry::{cut, iso, pro};2use super::models::Cell6d;3use super::Projection;4use crate::three;5use mrlycore::errors::Result;6use mrlycore::state::choice;7use mrlycore::tile::Tile;89/// The tile configuration the hex pipeline shares with the 3d builder.10pub type Config = three::tile::Config;1112/// A 3d tile paired with the projection that flattens it.13#[derive(Clone, Debug)]14pub struct HexTile {15    /// The projection that flattens the tile.16    pub projection: Projection,17    /// The 3d tile underneath.18    pub tile: Tile,19}2021fn projection() -> Projection {22    choice(&[Projection::Iso, Projection::Pro, Projection::Cut])23}2425/// Draws a 3d tile from the config under a random projection.26pub fn create(config: &Config) -> Result<HexTile> {27    Ok(HexTile {28        projection: projection(),29        tile: three::tile::create(config)?,30    })31}3233/// Draws a random 3d tile up to the given size under a random projection.34pub fn random_tile(max_size: usize) -> Result<HexTile> {35    Ok(HexTile {36        projection: projection(),37        tile: three::tile::random_tile(max_size)?,38    })39}4041/// Builds the tile's cube and flattens it through its projection.42pub fn build(hex: &HexTile) -> Result<Cell6d> {43    let cell = three::tile::build(&hex.tile)?;44    match hex.projection {45        Projection::Iso => iso(&cell),46        Projection::Pro => pro(&cell),47        Projection::Cut => cut(&cell),48    }49}5051#[cfg(test)]52mod tests {53    use super::*;54    use mrlycore::state::{guard as rng_lock, seed};55    use mrlycore::tile::Group;56    fn config() -> Config {57        Config {58            min_size: 3,59            max_size: 9,60            anti: Some(false),61            ..Config::default()62        }63    }64    #[test]65    fn projects_every_group_in_every_projection() {66        let _guard = rng_lock();67        let config = config();68        for s in 0..40 {69            seed(s);70            let hex = create(&config).unwrap();71            let cell = build(&hex).unwrap();72            assert!(73                cell.width() > 0,74                "empty width seed {} {:?}",75                s,76                hex.tile.group77            );78            assert!(cell.height() > 0, "empty height seed {}", s);79        }80    }81    #[test]82    fn magic_projects() {83        let _guard = rng_lock();84        let config = Config {85            min_size: 3,86            max_size: 15,87            groups: vec![Group::Magic],88            anti: Some(false),89            ..Config::default()90        };91        let mut built = 0;92        for s in 0..30 {93            seed(s);94            if let Ok(hex) = create(&config) {95                let cell = build(&hex).unwrap();96                assert!(cell.width() > 0);97                built += 1;98            }99        }100        assert!(built > 0, "expected magic tiles to project");101    }102}