rust/mapgen/src/lib.rs
author fkaa
Fri, 22 Mar 2019 18:01:08 +0200
changeset 14723 29dbe9ce8b7d
parent 14196 76a52e8149e3
child 14731 946df0bb3b28
permissions -rw-r--r--
add basic map rendering with gl
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_derive::{Deserialize};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     9
use serde_yaml;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    10
use integral_geometry::{Point, Size, Rect};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    11
use landgen::{
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    12
    outline_template::OutlineTemplate
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    13
};
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    14
use rand::{thread_rng, Rng};
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
    15
use land2d::Land2D;
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
    16
use vec2d::Vec2D;
14177
74ca70cb753d fix mapgen
alfadur
parents: 14172
diff changeset
    17
use self::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
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   109
    pub fn make_texture(&self, land: &Land2D<u8>, theme: &Theme) -> Vec2D<u32> {
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   110
        let mut texture = Vec2D::new(land.size(), 0);
14191
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   111
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   112
        if let Some(land_sprite) = theme.land_texture() {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   113
            for (row_index, (land_row, tex_row)) in land.rows()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   114
                .zip(texture.rows_mut())
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   115
                .enumerate()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   116
            {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   117
                let sprite_row = land_sprite.get_row(row_index % land_sprite.height());
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   118
                let mut x_offset = 0;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   119
                while sprite_row.len() < land.width() - x_offset {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   120
                    let copy_range = x_offset..x_offset + sprite_row.len();
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   121
                    tex_row_copy(
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   122
                        &land_row[copy_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   123
                        &mut tex_row[copy_range],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   124
                        sprite_row
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   125
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   126
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   127
                    x_offset += land_sprite.width()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   128
                }
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   129
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   130
                if x_offset < land.width() {
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   131
                    let final_range = x_offset..land.width();
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   132
                    tex_row_copy(
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   133
                        &land_row[final_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   134
                        &mut tex_row[final_range],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   135
                        &sprite_row[..land.width() - x_offset]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   136
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   137
                }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   138
            }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   139
        }
14191
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   140
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   141
        if let Some(border_sprite) = theme.border_texture() {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   142
            assert!(border_sprite.height() <= 512);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   143
            let border_width = (border_sprite.height() / 2) as u8;
14196
76a52e8149e3 add some texture transforms
alfadur
parents: 14191
diff changeset
   144
            let border_sprite = border_sprite.to_tiled();
14191
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   145
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   146
            let mut offsets = vec![255u8; land.width()];
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   147
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   148
            land_border_pass(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   149
                land.rows().rev().zip(texture.rows_mut().rev()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   150
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   151
                border_width,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   152
                |x, y| border_sprite.get_pixel(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   153
                    x % border_sprite.width(),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   154
                    border_sprite.height() - 1 - y,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   155
                )
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   156
            );
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   157
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   158
            offsets.iter_mut().for_each(|v| *v = 255);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   159
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   160
            land_border_pass(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   161
                land.rows().zip(texture.rows_mut()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   162
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   163
                border_width,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   164
                |x, y| border_sprite.get_pixel(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   165
                    x % border_sprite.width(),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   166
                    y,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   167
                )
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   168
            );
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   169
        }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   170
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   171
        texture
14172
3c8a33ba06ba start loading theme textures
alfadur
parents: 14158
diff changeset
   172
    }
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   173
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   174
    // TODO: no way to pass both u8 & u32?
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   175
    pub fn make_texture32(&self, land: &Land2D<u32>, theme: &Theme) -> Vec2D<u32> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   176
        let mut texture = Vec2D::new(land.size(), 0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   177
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   178
        if let Some(land_sprite) = theme.land_texture() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   179
            for (row_index, (land_row, tex_row)) in land.rows()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   180
                .zip(texture.rows_mut())
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   181
                .enumerate()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   182
            {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   183
                let sprite_row = land_sprite.get_row(row_index % land_sprite.height());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   184
                let mut x_offset = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   185
                while sprite_row.len() < land.width() - x_offset {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   186
                    let copy_range = x_offset..x_offset + sprite_row.len();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   187
                    tex_row_copy32(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   188
                        &land_row[copy_range.clone()],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   189
                        &mut tex_row[copy_range],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   190
                        sprite_row
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   191
                    );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   192
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   193
                    x_offset += land_sprite.width()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   194
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   195
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   196
                if x_offset < land.width() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   197
                    let final_range = x_offset..land.width();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   198
                    tex_row_copy32(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   199
                        &land_row[final_range.clone()],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   200
                        &mut tex_row[final_range],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   201
                        &sprite_row[..land.width() - x_offset]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   202
                    );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   203
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   204
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   205
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   206
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   207
        if let Some(border_sprite) = theme.border_texture() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   208
            assert!(border_sprite.height() <= 512);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   209
            let border_width = (border_sprite.height() / 2) as u8;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   210
            let border_sprite = border_sprite.to_tiled();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   211
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   212
            let mut offsets = vec![255u8; land.width()];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   213
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   214
            land_border_pass32(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   215
                land.rows().rev().zip(texture.rows_mut().rev()),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   216
                &mut offsets,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   217
                border_width,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   218
                |x, y| border_sprite.get_pixel(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   219
                    x % border_sprite.width(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   220
                    border_sprite.height() - 1 - y,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   221
                )
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   222
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   223
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   224
            offsets.iter_mut().for_each(|v| *v = 255);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   225
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   226
            land_border_pass32(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   227
                land.rows().zip(texture.rows_mut()),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   228
                &mut offsets,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   229
                border_width,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   230
                |x, y| border_sprite.get_pixel(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   231
                    x % border_sprite.width(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   232
                    y,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   233
                )
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   234
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   235
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   236
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   237
        texture
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   238
    }
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   239
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   240
14191
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   241
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   242
struct Color(u32);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   243
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   244
impl Color {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   245
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   246
    fn red(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   247
        (self.0 >> 0 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   248
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   249
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   250
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   251
    fn green(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   252
        (self.0 >> 8 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   253
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   254
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   255
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   256
    fn blue(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   257
        (self.0 >> 16 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   258
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   259
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   260
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   261
    fn alpha(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   262
        (self.0 >> 24 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   263
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   264
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   265
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   266
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   267
fn lerp(from: u8, to: u8, coef: u8) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   268
    ((from as u16 * (256 - coef as u16) + to as u16 * coef as u16) / 256) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   269
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   270
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   271
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   272
fn blend(source: u32, target: u32) -> u32 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   273
    let source = Color(source);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   274
    let target = Color(target);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   275
    let alpha = lerp(target.alpha(), 255, source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   276
    let red = lerp(target.red(), source.red(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   277
    let green = lerp(target.green(), source.green(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   278
    let blue = lerp(target.blue(), source.blue(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   279
    (red as u32) << 0 | (green as u32) << 8 | (blue as u32) << 16 | (alpha as u32) << 24
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   280
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   281
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   282
fn land_border_pass<'a, T, F>(rows: T, offsets: &mut [u8], border_width: u8, pixel_getter: F)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   283
    where T: Iterator<Item = (&'a [u8], &'a mut [u32])>,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   284
          F: (Fn(usize, usize) -> u32)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   285
{
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   286
    for (land_row, tex_row) in rows {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   287
        for (x, ((land_v, tex_v), offset_v)) in land_row.iter()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   288
            .zip(tex_row.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   289
            .zip(offsets.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   290
            .enumerate()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   291
        {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   292
            *offset_v = if *land_v == 0 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   293
                if *offset_v < border_width {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   294
                    *tex_v = blend(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   295
                        pixel_getter(x, *offset_v as usize),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   296
                        *tex_v,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   297
                    )
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   298
                }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   299
                offset_v.saturating_add(1)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   300
            } else {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   301
                0
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   302
            }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   303
        }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   304
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   305
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14185
diff changeset
   306
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   307
fn land_border_pass32<'a, T, F>(rows: T, offsets: &mut [u8], border_width: u8, pixel_getter: F)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   308
    where T: Iterator<Item = (&'a [u32], &'a mut [u32])>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   309
          F: (Fn(usize, usize) -> u32)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   310
{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   311
    for (land_row, tex_row) in rows {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   312
        for (x, ((land_v, tex_v), offset_v)) in land_row.iter()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   313
            .zip(tex_row.iter_mut())
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   314
            .zip(offsets.iter_mut())
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   315
            .enumerate()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   316
        {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   317
            *offset_v = if *land_v == 0 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   318
                if *offset_v < border_width {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   319
                    *tex_v = blend(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   320
                        pixel_getter(x, *offset_v as usize),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   321
                        *tex_v,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   322
                    )
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   323
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   324
                offset_v.saturating_add(1)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   325
            } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   326
                0
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   327
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   328
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   329
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   330
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   331
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   332
fn tex_row_copy(land_row: &[u8], tex_row: &mut [u32], sprite_row: &[u32]) {
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   333
    for ((land_v, tex_v), sprite_v) in
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   334
        land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row)
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   335
    {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   336
        *tex_v = if *land_v == 0 {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   337
            *sprite_v
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   338
        } else {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   339
            0
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   340
        }
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14181
diff changeset
   341
    }
14181
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   342
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14177
diff changeset
   343
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   344
fn tex_row_copy32(land_row: &[u32], tex_row: &mut [u32], sprite_row: &[u32]) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   345
    for ((land_v, tex_v), sprite_v) in
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   346
        land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   347
    {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   348
        *tex_v = if *land_v == 0 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   349
            *sprite_v
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   350
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   351
            0
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   352
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   353
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   354
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14196
diff changeset
   355
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   356
#[cfg(test)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   357
mod tests {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   358
    use crate::{
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   359
        MapGenerator,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   360
        TemplateType
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   361
    };
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   362
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   363
    #[test]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   364
    fn simple_load() {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   365
        let text = r#"
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   366
# comment
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   367
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   368
templates:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   369
  -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   370
    width: 3072
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   371
    height: 1424
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   372
    can_flip: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   373
    can_invert: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   374
    can_mirror: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   375
    is_negative: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   376
    put_girders: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   377
    max_hedgehogs: 18
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   378
    outline_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   379
      -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   380
        - {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
   381
        - {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
   382
        - {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
   383
        - {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
   384
        - {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
   385
        - {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
   386
        - {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
   387
    fill_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   388
      - {x: 1023, y: 0}
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14148
diff changeset
   389
      - {x: 1023, y: 0}
14148
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   390
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   391
template_types:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   392
    test: [0]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   393
"#;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   394
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   395
        let mut generator = MapGenerator::new();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   396
        generator.import_yaml_templates(&text);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   397
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   398
        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
   399
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   400
        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
   401
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   402
        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
   403
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   404
}