rust/mapgen/src/lib.rs
author alfadur
Wed, 07 Nov 2018 03:35:56 +0300
changeset 14172 3c8a33ba06ba
parent 14158 3119d665d3c6
child 14177 74ca70cb753d
permissions -rw-r--r--
start loading theme textures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
     1
pub mod theme;
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
     2
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     3
use std::{
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     4
    collections::hash_map::HashMap,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     5
    borrow::Borrow,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     6
    mem::replace
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     7
};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     8
use serde::{Deserialize};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     9
use serde_derive::{Deserialize};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    10
use serde_yaml;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    11
use integral_geometry::{Point, Size, Rect};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    12
use landgen::{
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    13
    outline_template::OutlineTemplate
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    14
};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    15
use rand::{thread_rng, Rng};
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
    16
use land2d::Land2D;
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
    17
use theme::Theme;
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    18
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    19
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    20
struct PointDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    21
    x: u32,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    22
    y: u32
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    23
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    24
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    25
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    26
struct RectDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    27
    x: u32,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    28
    y: u32,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    29
    w: u32,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    30
    h: u32
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    31
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    32
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    33
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    34
struct TemplateDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    35
    width: usize,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    36
    height: usize,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    37
    can_flip: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    38
    can_invert: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    39
    can_mirror: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    40
    is_negative: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    41
    put_girders: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    42
    max_hedgehogs: u8,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    43
    outline_points: Vec<Vec<RectDesc>>,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    44
    fill_points: Vec<PointDesc>
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    45
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    46
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    47
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    48
struct TemplateCollectionDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    49
    templates: Vec<TemplateDesc>,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    50
    template_types: HashMap<String, Vec<usize>>
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    51
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    52
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    53
impl From<&TemplateDesc> for OutlineTemplate {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    54
    fn from(desc: &TemplateDesc) -> Self {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    55
        OutlineTemplate {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    56
            islands: desc.outline_points.iter()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    57
                .map(|v| v.iter()
14158
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14149
diff changeset
    58
                    .map(|r| Rect::from_size(
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14149
diff changeset
    59
                        Point::new(r.x as i32, r.y as i32),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14149
diff changeset
    60
                        Size::new(r.w as usize, r.h as usize)))
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    61
                    .collect())
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    62
                .collect(),
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    63
            fill_points: desc.fill_points.iter()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    64
                .map(|p| Point::new(p.x as i32, p.y as i32))
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    65
                .collect(),
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    66
            size: Size::new(desc.width, desc.height),
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    67
            can_flip: desc.can_flip,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    68
            can_invert: desc.can_invert,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    69
            can_mirror: desc.can_mirror,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    70
            is_negative: desc.is_negative
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    71
        }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    72
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    73
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    74
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    75
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    76
struct TemplateType(String);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    77
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    78
impl Borrow<str> for TemplateType {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    79
    fn borrow(&self) -> &str {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    80
        self.0.as_str()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    81
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    82
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    83
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    84
#[derive(Debug)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    85
pub struct MapGenerator {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    86
    pub(crate) templates: HashMap<TemplateType, Vec<OutlineTemplate>>
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    87
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    88
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    89
impl MapGenerator {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    90
    pub fn new() -> Self {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    91
        Self { templates: HashMap::new() }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    92
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    93
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    94
    pub fn import_yaml_templates(&mut self, text: &str) {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    95
        let mut desc: TemplateCollectionDesc = serde_yaml::from_str(text).unwrap();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    96
        let templates = replace(&mut desc.templates, vec![]);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    97
        self.templates = desc.template_types.into_iter()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    98
            .map(|(size, indices)|
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    99
                (TemplateType(size), indices.iter()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   100
                    .map(|i| (&templates[*i]).into())
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   101
                    .collect()))
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   102
            .collect();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   103
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   104
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   105
    pub fn get_template(&self, template_type: &str) -> Option<&OutlineTemplate> {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   106
        self.templates.get(template_type).and_then(|t| thread_rng().choose(t))
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   107
    }
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   108
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   109
    pub fn make_texture(&self, land: &Land2D<u32>, theme: &Theme) {
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   110
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   111
    }
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   112
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   113
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   114
#[cfg(test)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   115
mod tests {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   116
    use crate::{
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   117
        MapGenerator,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   118
        TemplateType
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   119
    };
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   120
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   121
    #[test]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   122
    fn simple_load() {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   123
        let text = r#"
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   124
# comment
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   125
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   126
templates:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   127
  -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   128
    width: 3072
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   129
    height: 1424
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   130
    can_flip: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   131
    can_invert: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   132
    can_mirror: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   133
    is_negative: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   134
    put_girders: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   135
    max_hedgehogs: 18
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   136
    outline_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   137
      -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   138
        - {x: 748, y: 1424, w: 1, h: 1}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   139
        - {x: 636, y: 1252, w: 208, h: 72}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   140
        - {x: 898, y: 1110, w: 308, h: 60}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   141
        - {x: 1128, y: 1252, w: 434, h: 40}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   142
        - {x: 1574, y: 1112, w: 332, h: 40}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   143
        - {x: 1802, y: 1238, w: 226, h: 36}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   144
        - {x: 1930, y: 1424, w: 1, h: 1}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   145
    fill_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   146
      - {x: 1023, y: 0}
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   147
      - {x: 1023, y: 0}
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   149
template_types:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   150
    test: [0]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   151
"#;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   152
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   153
        let mut generator = MapGenerator::new();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   154
        generator.import_yaml_templates(&text);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   155
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   156
        assert!(generator.templates.contains_key(&TemplateType("test".to_string())));
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   157
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   158
        let template = generator.get_template("test").unwrap();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   159
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   160
        assert_eq!(template.islands[0].len(), 7);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   161
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   162
}