rust/mapgen/src/template/wavefront_collapse.rs
author unC0Rr
Mon, 13 Feb 2023 12:31:30 +0100
branchtransitional_engine
changeset 15922 da6b67f13c12
child 15923 d46ad15c6dec
permissions -rw-r--r--
Refactor mapgen to allow for easy switching between generators

use integral_geometry::{Point, Rect, Size};
use land2d::Land2D;
use landgen::{wavefront_collapse::generator::*, LandGenerationParameters};
use serde_derive::Deserialize;
use serde_yaml;
use std::{borrow::Borrow, collections::hash_map::HashMap, mem::replace};

#[derive(Deserialize)]
#[serde(remote = "EdgeDescription")]
pub struct EdgeDesc {
    pub name: String,
    pub reversed: Option<bool>,
    pub symmetrical: Option<bool>,
}

#[derive(Deserialize)]
#[serde(remote = "EdgesDescription")]
pub struct EdgesDesc {
    #[serde(with = "EdgeDesc")]
    pub top: EdgeDescription,
    #[serde(with = "EdgeDesc")]
    pub right: EdgeDescription,
    #[serde(with = "EdgeDesc")]
    pub bottom: EdgeDescription,
    #[serde(with = "EdgeDesc")]
    pub left: EdgeDescription,
}

#[derive(Deserialize)]
#[serde(remote = "TileDescription")]
pub struct TileDesc {
    pub name: String,
    #[serde(with = "EdgesDesc")]
    pub edges: EdgesDescription,
    pub can_flip: bool,
    pub can_mirror: bool,
    pub can_rotate90: bool,
    pub can_rotate180: bool,
    pub can_rotate270: bool,
}

#[derive(Deserialize)]
pub struct TileDescriptionHelper(#[serde(with = "TileDesc")] TileDescription);

#[derive(Deserialize)]
pub struct TemplateDesc {
    pub width: usize,
    pub height: usize,
    pub can_flip: bool,
    pub is_negative: bool,
    pub put_girders: bool,
    pub max_hedgehogs: u8,
    pub tiles: Vec<TileDescriptionHelper>,
}

#[derive(Deserialize)]
pub struct TemplateCollectionDesc {
    pub templates: Vec<TemplateDesc>,
    pub template_types: HashMap<String, Vec<usize>>,
}

impl From<&TemplateDesc> for TemplateDescription {
    fn from(desc: &TemplateDesc) -> Self {
        Self {
            size: Size::new(desc.width, desc.height),
            tiles: desc
                .tiles
                .iter()
                .map(|TileDescriptionHelper(t)| t.clone())
                .collect(),
        }
    }
}