rust/mapgen/src/template/outline.rs
author unC0Rr
Tue, 28 Jan 2025 15:49:45 +0100
changeset 16073 5d302b12d837
parent 16064 07cb6dbc8444
permissions -rw-r--r--
- Update landgen to use the latest rand crate - Change Size width and height from usize to u32 for portability - Implement backtracking in wfc generator

use integral_geometry::{Point, Rect, Size};

use landgen::outline_template_based::outline_template::OutlineTemplate;
use serde_derive::Deserialize;

use std::collections::hash_map::HashMap;

#[derive(Deserialize, Clone)]
pub struct PointDesc {
    x: u32,
    y: u32,
}

#[derive(Deserialize, Clone)]
pub struct RectDesc {
    x: u32,
    y: u32,
    w: u32,
    h: u32,
}

#[derive(Deserialize, Clone)]
pub struct TemplateDesc {
    width: u32,
    height: u32,
    can_flip: bool,
    can_invert: bool,
    can_mirror: bool,
    is_negative: bool,
    put_girders: bool,
    max_hedgehogs: u8,
    outline_points: Vec<Vec<RectDesc>>,
    walls: Option<Vec<Vec<RectDesc>>>,
    fill_points: Vec<PointDesc>,
}

#[derive(Deserialize)]
pub struct TemplateTypeDesc {
    pub indices: Vec<usize>,
    pub force_invert: Option<bool>,
}

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

impl From<TemplateDesc> for OutlineTemplate {
    fn from(desc: TemplateDesc) -> Self {
        OutlineTemplate {
            islands: desc
                .outline_points
                .iter()
                .map(|v| {
                    v.iter()
                        .map(|r| {
                            Rect::from_size(Point::new(r.x as i32, r.y as i32), Size::new(r.w, r.h))
                        })
                        .collect()
                })
                .collect(),
            walls: desc
                .walls
                .unwrap_or_default()
                .iter()
                .map(|v| {
                    v.iter()
                        .map(|r| {
                            Rect::from_size(Point::new(r.x as i32, r.y as i32), Size::new(r.w, r.h))
                        })
                        .collect()
                })
                .collect(),
            fill_points: desc
                .fill_points
                .iter()
                .map(|p| Point::new(p.x as i32, p.y as i32))
                .collect(),
            size: Size::new(desc.width, desc.height),
            can_flip: desc.can_flip,
            can_invert: desc.can_invert,
            can_mirror: desc.can_mirror,
            is_negative: desc.is_negative,
        }
    }
}