rust/mapgen/src/lib.rs
author Wuzzy <Wuzzy2@mail.ru>
Thu, 18 Jul 2019 03:25:15 +0200
changeset 15251 9be05ae1b726
parent 14710 946df0bb3b28
child 15828 44b49f255e31
permissions -rw-r--r--
Use minimum size for keybinder category list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14151
3c8a33ba06ba start loading theme textures
alfadur
parents: 14137
diff changeset
     1
pub mod theme;
3c8a33ba06ba start loading theme textures
alfadur
parents: 14137
diff changeset
     2
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     3
use self::theme::Theme;
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     4
use integral_geometry::{Point, Rect, Size};
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     5
use land2d::Land2D;
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     6
use landgen::outline_template::OutlineTemplate;
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     7
use rand::{thread_rng, Rng};
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     8
use serde_derive::Deserialize;
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     9
use serde_yaml;
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    10
use std::{borrow::Borrow, collections::hash_map::HashMap, mem::replace};
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    11
use vec2d::Vec2D;
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    12
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    13
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    14
struct PointDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    15
    x: u32,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    16
    y: u32,
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    17
}
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 RectDesc {
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
    w: u32,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    24
    h: u32,
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    25
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    26
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    27
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    28
struct TemplateDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    29
    width: usize,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    30
    height: usize,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    31
    can_flip: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    32
    can_invert: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    33
    can_mirror: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    34
    is_negative: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    35
    put_girders: bool,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    36
    max_hedgehogs: u8,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    37
    outline_points: Vec<Vec<RectDesc>>,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    38
    fill_points: Vec<PointDesc>,
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    39
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    40
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    41
#[derive(Deserialize)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    42
struct TemplateCollectionDesc {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    43
    templates: Vec<TemplateDesc>,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    44
    template_types: HashMap<String, Vec<usize>>,
14127
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
impl From<&TemplateDesc> for OutlineTemplate {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    48
    fn from(desc: &TemplateDesc) -> Self {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    49
        OutlineTemplate {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    50
            islands: desc
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    51
                .outline_points
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    52
                .iter()
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    53
                .map(|v| {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    54
                    v.iter()
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    55
                        .map(|r| {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    56
                            Rect::from_size(
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    57
                                Point::new(r.x as i32, r.y as i32),
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    58
                                Size::new(r.w as usize, r.h as usize),
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    59
                            )
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    60
                        })
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    61
                        .collect()
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    62
                })
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    63
                .collect(),
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    64
            fill_points: desc
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    65
                .fill_points
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    66
                .iter()
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    67
                .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
    68
                .collect(),
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    69
            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
    70
            can_flip: desc.can_flip,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    71
            can_invert: desc.can_invert,
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    72
            can_mirror: desc.can_mirror,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    73
            is_negative: desc.is_negative,
14127
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
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    76
}
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
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    79
struct TemplateType(String);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    80
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    81
impl Borrow<str> for TemplateType {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    82
    fn borrow(&self) -> &str {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    83
        self.0.as_str()
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    84
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    85
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    86
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    87
#[derive(Debug)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    88
pub struct MapGenerator {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    89
    pub(crate) templates: HashMap<TemplateType, Vec<OutlineTemplate>>,
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    90
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    91
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    92
impl MapGenerator {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    93
    pub fn new() -> Self {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    94
        Self {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    95
            templates: HashMap::new(),
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
    96
        }
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    97
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    98
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
    99
    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
   100
        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
   101
        let templates = replace(&mut desc.templates, vec![]);
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   102
        self.templates = desc
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   103
            .template_types
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   104
            .into_iter()
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   105
            .map(|(size, indices)| {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   106
                (
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   107
                    TemplateType(size),
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   108
                    indices.iter().map(|i| (&templates[*i]).into()).collect(),
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   109
                )
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   110
            })
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   111
            .collect();
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
    pub fn get_template(&self, template_type: &str) -> Option<&OutlineTemplate> {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   115
        self.templates
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   116
            .get(template_type)
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   117
            .and_then(|t| thread_rng().choose(t))
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   118
    }
14151
3c8a33ba06ba start loading theme textures
alfadur
parents: 14137
diff changeset
   119
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   120
    pub fn make_texture<LandT>(&self, land: &Land2D<LandT>, theme: &Theme) -> Vec2D<u32>
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   121
    where
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   122
        LandT: Copy + Default + PartialEq,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   123
    {
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   124
        let mut texture = Vec2D::new(land.size(), 0);
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   125
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   126
        if let Some(land_sprite) = theme.land_texture() {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   127
            for (row_index, (land_row, tex_row)) in land.rows().zip(texture.rows_mut()).enumerate()
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   128
            {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   129
                let sprite_row = land_sprite.get_row(row_index % land_sprite.height());
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   130
                let mut x_offset = 0;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   131
                while sprite_row.len() < land.width() - x_offset {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   132
                    let copy_range = x_offset..x_offset + sprite_row.len();
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   133
                    tex_row_copy(
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   134
                        &land_row[copy_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   135
                        &mut tex_row[copy_range],
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   136
                        sprite_row,
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   137
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   138
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   139
                    x_offset += land_sprite.width()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   140
                }
14151
3c8a33ba06ba start loading theme textures
alfadur
parents: 14137
diff changeset
   141
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   142
                if x_offset < land.width() {
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   143
                    let final_range = x_offset..land.width();
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   144
                    tex_row_copy(
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   145
                        &land_row[final_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   146
                        &mut tex_row[final_range],
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   147
                        &sprite_row[..land.width() - x_offset],
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   148
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   149
                }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   150
            }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   151
        }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   152
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   153
        if let Some(border_sprite) = theme.border_texture() {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   154
            assert!(border_sprite.height() <= 512);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   155
            let border_width = (border_sprite.height() / 2) as u8;
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   156
            let border_sprite = border_sprite.to_tiled();
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   157
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   158
            let mut offsets = vec![255u8; land.width()];
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   159
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   160
            land_border_pass(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   161
                land.rows().rev().zip(texture.rows_mut().rev()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   162
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   163
                border_width,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   164
                |x, y| {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   165
                    border_sprite
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   166
                        .get_pixel(x % border_sprite.width(), border_sprite.height() - 1 - y)
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   167
                },
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   168
            );
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   169
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   170
            offsets.iter_mut().for_each(|v| *v = 255);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   171
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   172
            land_border_pass(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   173
                land.rows().zip(texture.rows_mut()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   174
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   175
                border_width,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   176
                |x, y| border_sprite.get_pixel(x % border_sprite.width(), y),
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   177
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   178
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   179
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   180
        texture
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   181
    }
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   182
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   183
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   184
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   185
struct Color(u32);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   186
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   187
impl Color {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   188
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   189
    fn red(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   190
        (self.0 >> 0 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   191
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   192
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   193
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   194
    fn green(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   195
        (self.0 >> 8 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   196
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   197
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   198
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   199
    fn blue(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   200
        (self.0 >> 16 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   201
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   202
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   203
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   204
    fn alpha(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   205
        (self.0 >> 24 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   206
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   207
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   208
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   209
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   210
fn lerp(from: u8, to: u8, coef: u8) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   211
    ((from as u16 * (256 - coef as u16) + to as u16 * coef as u16) / 256) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   212
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   213
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   214
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   215
fn blend(source: u32, target: u32) -> u32 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   216
    let source = Color(source);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   217
    let target = Color(target);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   218
    let alpha = lerp(target.alpha(), 255, source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   219
    let red = lerp(target.red(), source.red(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   220
    let green = lerp(target.green(), source.green(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   221
    let blue = lerp(target.blue(), source.blue(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   222
    (red as u32) << 0 | (green as u32) << 8 | (blue as u32) << 16 | (alpha as u32) << 24
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   223
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   224
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   225
fn land_border_pass<'a, LandT, T, F>(rows: T, offsets: &mut [u8], border_width: u8, pixel_getter: F)
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   226
where
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   227
    LandT: Default + PartialEq + 'a,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   228
    T: Iterator<Item = (&'a [LandT], &'a mut [u32])>,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   229
    F: (Fn(usize, usize) -> u32),
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   230
{
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   231
    for (land_row, tex_row) in rows {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   232
        for (x, ((land_v, tex_v), offset_v)) in land_row
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   233
            .iter()
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   234
            .zip(tex_row.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   235
            .zip(offsets.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   236
            .enumerate()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   237
        {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   238
            *offset_v = if *land_v == LandT::default() {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   239
                if *offset_v < border_width {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   240
                    *tex_v = blend(pixel_getter(x, *offset_v as usize), *tex_v)
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   241
                }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   242
                offset_v.saturating_add(1)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   243
            } else {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   244
                0
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   245
            }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   246
        }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   247
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   248
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   249
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   250
fn tex_row_copy<LandT>(land_row: &[LandT], tex_row: &mut [u32], sprite_row: &[u32])
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   251
where
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   252
    LandT: Default + PartialEq,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   253
{
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   254
    for ((land_v, tex_v), sprite_v) in land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row) {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   255
        *tex_v = if *land_v == LandT::default() {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   256
            *sprite_v
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   257
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   258
            0
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   259
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   260
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   261
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   262
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   263
#[cfg(test)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   264
mod tests {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   265
    use crate::{MapGenerator, TemplateType};
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   266
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   267
    #[test]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   268
    fn simple_load() {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   269
        let text = r#"
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   270
# comment
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   271
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   272
templates:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   273
  -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   274
    width: 3072
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   275
    height: 1424
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   276
    can_flip: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   277
    can_invert: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   278
    can_mirror: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   279
    is_negative: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   280
    put_girders: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   281
    max_hedgehogs: 18
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   282
    outline_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   283
      -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   284
        - {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
   285
        - {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
   286
        - {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
   287
        - {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
   288
        - {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
   289
        - {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
   290
        - {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
   291
    fill_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   292
      - {x: 1023, y: 0}
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   293
      - {x: 1023, y: 0}
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   294
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   295
template_types:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   296
    test: [0]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   297
"#;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   298
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   299
        let mut generator = MapGenerator::new();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   300
        generator.import_yaml_templates(&text);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   301
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   302
        assert!(generator
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   303
            .templates
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   304
            .contains_key(&TemplateType("test".to_string())));
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   305
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   306
        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
   307
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   308
        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
   309
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   310
}