rust/land_dump/src/main.rs
author unC0Rr
Thu, 19 Dec 2024 12:40:48 +0100
branchtransitional_engine
changeset 16079 624b74443b53
parent 15850 44b49f255e31
permissions -rw-r--r--
Make FPNum use saturating calculations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     1
use png::HasParameters;
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
     2
use std::{
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
     3
    fs::File,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
     4
    io::{BufWriter, Read},
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
     5
    path::{Path, PathBuf},
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
     6
};
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     7
use structopt::StructOpt;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     8
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     9
use integral_geometry::{Point, Rect, Size};
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    10
use land2d::Land2D;
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    11
use landgen::{
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    12
    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    13
    LandGenerationParameters, LandGenerator,
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    14
};
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    15
use lfprng::LaggedFibonacciPRNG;
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    16
use mapgen::{
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    17
    theme::{slice_u32_to_u8, Theme},
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    18
    MapGenerator,
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    19
};
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    20
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    21
#[derive(StructOpt, Debug)]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    22
#[structopt(name = "basic")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    23
struct Opt {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    24
    #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    25
    seed: String,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    26
    #[structopt(short = "d", long = "dump-before-distort")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    27
    dump_before_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    28
    #[structopt(short = "b", long = "dump-before-bezierize")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    29
    dump_before_bezierize: bool,
14146
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
    30
    #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
    31
    distance_divisor: u32,
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    32
    #[structopt(short = "i", long = "templates-file")]
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    33
    templates_file: Option<String>,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    34
    #[structopt(short = "t", long = "template-type")]
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    35
    template_type: Option<String>,
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    36
    #[structopt(short = "z", long = "theme-dir")]
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    37
    theme_dir: Option<String>,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    38
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    39
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    40
fn template() -> OutlineTemplate {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    41
    let mut template = OutlineTemplate::new(Size::new(4096, 2048));
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    42
    template.islands = vec![vec![
14158
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14151
diff changeset
    43
        Rect::from_size_coords(100, 2050, 1, 1),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14151
diff changeset
    44
        Rect::from_size_coords(100, 500, 400, 1200),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14151
diff changeset
    45
        Rect::from_size_coords(3600, 500, 400, 1200),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14151
diff changeset
    46
        Rect::from_size_coords(3900, 2050, 1, 1),
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    47
    ]];
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    48
    template.fill_points = vec![Point::new(2047, 2047)];
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    49
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    50
    template
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    51
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    52
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    53
fn dump(
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    54
    template: &OutlineTemplate,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    55
    seed: &[u8],
14146
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
    56
    distance_divisor: u32,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    57
    skip_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    58
    skip_bezier: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    59
    file_name: &Path,
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    60
) -> std::io::Result<Land2D<u8>> {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    61
    let params =
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    62
        LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
    63
    let landgen = TemplatedLandGenerator::new(template.clone());
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    64
    let mut prng = LaggedFibonacciPRNG::new(seed);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    65
    let land = landgen.generate_land(&params, &mut prng);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    66
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    67
    let file = File::create(file_name)?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    68
    let ref mut w = BufWriter::new(file);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    69
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    70
    let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    71
    encoder
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    72
        .set(png::ColorType::Grayscale)
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    73
        .set(png::BitDepth::Eight);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    74
    let mut writer = encoder.write_header()?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    75
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    76
    writer.write_image_data(land.raw_pixels()).unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    77
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    78
    Ok(land)
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    79
}
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    80
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    81
fn texturize(theme_dir: &Path, land: &Land2D<u8>, output_filename: &Path) {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    82
    let theme = Theme::load(theme_dir).unwrap();
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    83
    let texture = MapGenerator::new().make_texture(land, &theme);
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    84
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    85
    let file = File::create(output_filename).unwrap();
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    86
    let ref mut w = BufWriter::new(file);
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    87
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    88
    let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    89
    encoder.set(png::ColorType::RGBA).set(png::BitDepth::Eight);
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    90
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    91
    let mut writer = encoder.write_header().unwrap();
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
    92
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    93
    writer
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    94
        .write_image_data(slice_u32_to_u8(texture.as_slice()))
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
    95
        .unwrap();
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    96
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    97
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    98
fn main() {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    99
    let opt = Opt::from_args();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   100
    println!("{:?}", opt);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   101
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   102
    let template = if let Some(path) = opt.templates_file {
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   103
        let mut result = String::new();
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   104
        File::open(path)
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   105
            .expect("Unable to read templates file")
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   106
            .read_to_string(&mut result);
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   107
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   108
        let mut generator = MapGenerator::new();
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   109
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   110
        let source = &result[..];
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   111
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   112
        generator.import_yaml_templates(source);
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   113
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   114
        let template_type = &opt.template_type.expect("No template type specified");
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   115
        generator
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   116
            .get_template(template_type)
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   117
            .expect(&format!("Template type {} not found", template_type))
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   118
            .clone()
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   119
    } else {
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   120
        template()
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   121
    };
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   122
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   123
    if opt.dump_before_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   124
        dump(
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   125
            &template,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   126
            opt.seed.as_str().as_bytes(),
14146
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
   127
            opt.distance_divisor,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   128
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   129
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   130
            Path::new("out.before_distort.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   131
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   132
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   133
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   134
    if opt.dump_before_bezierize {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   135
        dump(
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   136
            &template,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   137
            opt.seed.as_str().as_bytes(),
14146
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
   138
            opt.distance_divisor,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   139
            false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   140
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   141
            Path::new("out.before_bezier.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   142
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   143
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   144
    }
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
   145
    let land = dump(
14149
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14146
diff changeset
   146
        &template,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   147
        opt.seed.as_str().as_bytes(),
14146
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14142
diff changeset
   148
        opt.distance_divisor,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   149
        false,
14151
ab280be4b617 - Fix land_dump always skipping bezierize step
unc0rr
parents: 14149
diff changeset
   150
        false,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   151
        Path::new("out.full.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   152
    )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   153
    .unwrap();
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
   154
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
   155
    if let Some(dir) = opt.theme_dir {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14185
diff changeset
   156
        texturize(&Path::new(&dir), &land, &Path::new("out.texture.png"));
14185
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14173
diff changeset
   157
    }
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   158
}