rust/mapgen/src/lib.rs
author unC0Rr
Wed, 04 Jan 2023 10:40:40 +0100
branchtransitional_engine
changeset 15903 230dc46487ea
parent 15828 44b49f255e31
child 15914 c571d4b8879c
permissions -rw-r--r--
Update mapgen to take into account actual values for 'zero' and 'basic' colors
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;
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
     6
use landgen::{outline_template::OutlineTemplate, LandGenerationParameters};
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     7
use serde_derive::Deserialize;
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
     8
use serde_yaml;
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
     9
use std::{borrow::Borrow, collections::hash_map::HashMap, mem::replace};
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    10
use vec2d::Vec2D;
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
    11
use rand::{Rng, seq::SliceRandom};
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
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   114
    pub fn get_template<R: Rng>(&self, template_type: &str, rng: &mut R) -> 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)
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   117
            .and_then(|t| t.as_slice().choose(rng))
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
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   120
    pub fn make_texture<LandT>(
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   121
        &self,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   122
        land: &Land2D<LandT>,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   123
        parameters: &LandGenerationParameters<LandT>,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   124
        theme: &Theme,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   125
    ) -> Vec2D<u32>
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   126
    where
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   127
        LandT: Copy + Default + PartialEq,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   128
    {
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14710
diff changeset
   129
        let mut texture = Vec2D::new(land.size().size(), 0);
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   130
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   131
        if let Some(land_sprite) = theme.land_texture() {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   132
            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
   133
            {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   134
                let sprite_row = land_sprite.get_row(row_index % land_sprite.height());
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   135
                let mut x_offset = 0;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   136
                while sprite_row.len() < land.width() - x_offset {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   137
                    let copy_range = x_offset..x_offset + sprite_row.len();
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   138
                    tex_row_copy(
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   139
                        parameters.basic(),
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   140
                        &land_row[copy_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   141
                        &mut tex_row[copy_range],
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   142
                        sprite_row,
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   143
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   144
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   145
                    x_offset += land_sprite.width()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   146
                }
14151
3c8a33ba06ba start loading theme textures
alfadur
parents: 14137
diff changeset
   147
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   148
                if x_offset < land.width() {
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   149
                    let final_range = x_offset..land.width();
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   150
                    tex_row_copy(
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   151
                        parameters.basic(),
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   152
                        &land_row[final_range.clone()],
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   153
                        &mut tex_row[final_range],
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   154
                        &sprite_row[..land.width() - x_offset],
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   155
                    );
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   156
                }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   157
            }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   158
        }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   159
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   160
        if let Some(border_sprite) = theme.border_texture() {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   161
            assert!(border_sprite.height() <= 512);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   162
            let border_width = (border_sprite.height() / 2) as u8;
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   163
            let border_sprite = border_sprite.to_tiled();
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   164
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   165
            let mut offsets = vec![255u8; land.width()];
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   166
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   167
            land_border_pass(
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   168
                parameters.basic(),
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   169
                land.rows().rev().zip(texture.rows_mut().rev()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   170
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   171
                border_width,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   172
                |x, y| {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   173
                    border_sprite
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   174
                        .get_pixel(x % border_sprite.width(), border_sprite.height() - 1 - y)
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   175
                },
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   176
            );
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   177
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   178
            offsets.iter_mut().for_each(|v| *v = 255);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   179
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   180
            land_border_pass(
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   181
                parameters.basic(),
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   182
                land.rows().zip(texture.rows_mut()),
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   183
                &mut offsets,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   184
                border_width,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   185
                |x, y| border_sprite.get_pixel(x % border_sprite.width(), y),
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   186
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   187
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   188
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   189
        texture
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   190
    }
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   191
}
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   192
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   193
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   194
struct Color(u32);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   195
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   196
impl Color {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   197
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   198
    fn red(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   199
        (self.0 >> 0 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   200
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   201
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   202
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   203
    fn green(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   204
        (self.0 >> 8 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   205
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   206
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   207
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   208
    fn blue(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   209
        (self.0 >> 16 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   210
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   211
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   212
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   213
    fn alpha(self) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   214
        (self.0 >> 24 & 0xFF) as u8
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   215
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   216
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   217
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   218
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   219
fn lerp(from: u8, to: u8, coef: u8) -> u8 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   220
    ((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
   221
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   222
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   223
#[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   224
fn blend(source: u32, target: u32) -> u32 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   225
    let source = Color(source);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   226
    let target = Color(target);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   227
    let alpha = lerp(target.alpha(), 255, source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   228
    let red = lerp(target.red(), source.red(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   229
    let green = lerp(target.green(), source.green(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   230
    let blue = lerp(target.blue(), source.blue(), source.alpha());
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   231
    (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
   232
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   233
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   234
fn land_border_pass<'a, LandT, T, F>(
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   235
    basic_value: LandT,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   236
    rows: T,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   237
    offsets: &mut [u8],
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   238
    border_width: u8,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   239
    pixel_getter: F,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   240
) where
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   241
    LandT: Default + PartialEq + 'a,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   242
    T: Iterator<Item = (&'a [LandT], &'a mut [u32])>,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   243
    F: (Fn(usize, usize) -> u32),
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   244
{
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   245
    for (land_row, tex_row) in rows {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   246
        for (x, ((land_v, tex_v), offset_v)) in land_row
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   247
            .iter()
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   248
            .zip(tex_row.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   249
            .zip(offsets.iter_mut())
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   250
            .enumerate()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   251
        {
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   252
            *offset_v = if *land_v == basic_value {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   253
                if *offset_v < border_width {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   254
                    *tex_v = blend(pixel_getter(x, *offset_v as usize), *tex_v)
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   255
                }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   256
                offset_v.saturating_add(1)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   257
            } else {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   258
                0
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   259
            }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   260
        }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   261
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   262
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   263
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   264
fn tex_row_copy<LandT>(
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   265
    basic_value: LandT,
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   266
    land_row: &[LandT],
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   267
    tex_row: &mut [u32],
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   268
    sprite_row: &[u32],
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   269
) where
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   270
    LandT: Default + PartialEq,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   271
{
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   272
    for ((land_v, tex_v), sprite_v) in land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row) {
15903
230dc46487ea Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents: 15828
diff changeset
   273
        *tex_v = if *land_v == basic_value { *sprite_v } else { 0 }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   274
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   275
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14175
diff changeset
   276
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   277
#[cfg(test)]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   278
mod tests {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   279
    use crate::{MapGenerator, TemplateType};
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   280
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   281
    #[test]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   282
    fn simple_load() {
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   283
        let text = r#"
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   284
# comment
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   285
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   286
templates:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   287
  -
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   288
    width: 3072
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   289
    height: 1424
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   290
    can_flip: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   291
    can_invert: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   292
    can_mirror: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   293
    is_negative: false
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   294
    put_girders: true
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   295
    max_hedgehogs: 18
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   296
    outline_points:
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
        - {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
   299
        - {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
   300
        - {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
   301
        - {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
   302
        - {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
   303
        - {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
   304
        - {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
   305
    fill_points:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   306
      - {x: 1023, y: 0}
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14127
diff changeset
   307
      - {x: 1023, y: 0}
14127
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   308
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   309
template_types:
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   310
    test: [0]
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   311
"#;
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   312
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   313
        let mut generator = MapGenerator::new();
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   314
        generator.import_yaml_templates(&text);
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   315
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   316
        assert!(generator
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   317
            .templates
946df0bb3b28 collapse mapgen back
alfadur
parents: 14702
diff changeset
   318
            .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
   319
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   320
        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
   321
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   322
        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
   323
    }
0c5b9cfda9ab add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff changeset
   324
}