rust/land_dump/src/main.rs
author unC0Rr
Wed, 07 Nov 2018 15:55:00 +0100
changeset 14152 5acfdf49742d
parent 14137 3119d665d3c6
child 14164 1749961647b9
permissions -rw-r--r--
Fix some warnings here and there, remove unwelcomed code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     1
use png::HasParameters;
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
     2
use std::{
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
     3
    fs::File,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
     4
    io::{BufWriter, Read},
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
     5
    path::{Path, PathBuf}
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
     6
};
14121
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};
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    10
use landgen::{
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    11
    outline_template::OutlineTemplate,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    12
    template_based::TemplatedLandGenerator,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    13
    LandGenerationParameters,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    14
    LandGenerator
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    15
};
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    16
use mapgen::MapGenerator;
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    17
use lfprng::LaggedFibonacciPRNG;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    18
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    19
#[derive(StructOpt, Debug)]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    20
#[structopt(name = "basic")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    21
struct Opt {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    22
    #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    23
    seed: String,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    24
    #[structopt(short = "d", long = "dump-before-distort")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    25
    dump_before_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    26
    #[structopt(short = "b", long = "dump-before-bezierize")]
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    27
    dump_before_bezierize: bool,
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
    28
    #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
    29
    distance_divisor: u32,
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    30
    #[structopt(short = "i", long = "templates-file")]
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    31
    templates_file: Option<String>,
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    32
    #[structopt(short = "t", long = "template-type")]
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    33
    template_type: Option<String>
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    34
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    35
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    36
fn template() -> OutlineTemplate {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    37
    let mut template = OutlineTemplate::new(Size::new(4096, 2048));
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    38
    template.islands = vec![vec![
14137
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14130
diff changeset
    39
        Rect::from_size_coords(100, 2050, 1, 1),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14130
diff changeset
    40
        Rect::from_size_coords(100, 500, 400, 1200),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14130
diff changeset
    41
        Rect::from_size_coords(3600, 500, 400, 1200),
3119d665d3c6 collapse rectangle types back together with consistent usage of size
alfadur
parents: 14130
diff changeset
    42
        Rect::from_size_coords(3900, 2050, 1, 1),
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    43
    ]];
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    44
    template.fill_points = vec![Point::new(2047, 2047)];
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    45
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    46
    template
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
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    49
fn dump(
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    50
    template: &OutlineTemplate,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    51
    seed: &[u8],
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
    52
    distance_divisor: u32,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    53
    skip_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    54
    skip_bezier: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    55
    file_name: &Path,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    56
) -> std::io::Result<()> {
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
    57
    let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    58
    let landgen = TemplatedLandGenerator::new(template.clone());
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    59
    let mut prng = LaggedFibonacciPRNG::new(seed);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    60
    let land = landgen.generate_land(&params, &mut prng);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    61
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    62
    let file = File::create(file_name)?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    63
    let ref mut w = BufWriter::new(file);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    64
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    65
    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
    66
    encoder
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    67
        .set(png::ColorType::Grayscale)
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    68
        .set(png::BitDepth::Eight);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    69
    let mut writer = encoder.write_header()?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    70
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    71
    writer.write_image_data(land.raw_pixels()).unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    72
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    73
    Ok(())
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    74
}
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
fn main() {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    77
    let opt = Opt::from_args();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    78
    println!("{:?}", opt);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    79
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    80
    let template =
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    81
        if let Some(path) = opt.templates_file {
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    82
            let mut result = String::new();
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    83
            File::open(path)
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    84
                .expect("Unable to read templates file")
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    85
                .read_to_string(&mut result);
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    86
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    87
            let mut generator = MapGenerator::new();
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    88
14152
5acfdf49742d Fix some warnings here and there, remove unwelcomed code
unC0Rr
parents: 14137
diff changeset
    89
            let source =  &result[..];
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    90
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    91
            generator.import_yaml_templates(source);
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    92
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    93
            let template_type = &opt.template_type
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    94
                .expect("No template type specified");
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    95
            generator.get_template(template_type)
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    96
                .expect(&format!("Template type {} not found", template_type))
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    97
                .clone()
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    98
        } else {
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
    99
            template()
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
   100
        };
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
   101
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   102
    if opt.dump_before_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   103
        dump(
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
   104
            &template,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   105
            opt.seed.as_str().as_bytes(),
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
   106
            opt.distance_divisor,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   107
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   108
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   109
            Path::new("out.before_distort.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   110
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   111
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   112
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   113
    if opt.dump_before_bezierize {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   114
        dump(
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
   115
            &template,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   116
            opt.seed.as_str().as_bytes(),
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
   117
            opt.distance_divisor,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   118
            false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   119
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   120
            Path::new("out.before_bezier.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   121
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   122
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   123
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   124
    dump(
14128
b04dac00e8e2 add command arguments to use a template from file into land_dump
alfadur
parents: 14125
diff changeset
   125
        &template,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   126
        opt.seed.as_str().as_bytes(),
14125
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14121
diff changeset
   127
        opt.distance_divisor,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   128
        false,
14130
ab280be4b617 - Fix land_dump always skipping bezierize step
unc0rr
parents: 14128
diff changeset
   129
        false,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   130
        Path::new("out.full.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
}