rust/land_dump/src/main.rs
author unc0rr
Sat, 03 Nov 2018 18:29:58 +0100
changeset 14142 69db1d2e4cec
child 14146 376a0551b00a
permissions -rw-r--r--
land_dump app for testing templated landgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     1
extern crate integral_geometry;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     2
extern crate land2d;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     3
extern crate landgen;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     4
extern crate lfprng;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     5
extern crate png;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     6
extern crate structopt;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     7
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     8
use png::HasParameters;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
     9
use std::fs::File;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    10
use std::io::BufWriter;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    11
use std::path::Path;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    12
use structopt::StructOpt;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    13
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    14
use integral_geometry::{Point, Rect, Size};
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    15
use landgen::outline_template::OutlineTemplate;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    16
use landgen::template_based::TemplatedLandGenerator;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    17
use landgen::LandGenerationParameters;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    18
use landgen::LandGenerator;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    19
use lfprng::LaggedFibonacciPRNG;
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,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    30
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    31
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    32
fn template() -> OutlineTemplate {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    33
    let mut template = OutlineTemplate::new(Size::new(4096, 2048));
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    34
    template.islands = vec![vec![
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    35
        Rect::new(100, 2050, 1, 1),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    36
        Rect::new(100, 500, 400, 1200),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    37
        Rect::new(3600, 500, 400, 1200),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    38
        Rect::new(3900, 2050, 1, 1),
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
    template.fill_points = vec![Point::new(2047, 2047)];
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    41
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    42
    template
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
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    45
fn dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    46
    seed: &[u8],
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    47
    skip_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    48
    skip_bezier: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    49
    file_name: &Path,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    50
) -> std::io::Result<()> {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    51
    let params = LandGenerationParameters::new(0 as u8, 255, 100, skip_distort, skip_bezier);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    52
    let landgen = TemplatedLandGenerator::new(template());
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    53
    let mut prng = LaggedFibonacciPRNG::new(seed);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    54
    let land = landgen.generate_land(&params, &mut prng);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    55
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    56
    let file = File::create(file_name)?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    57
    let ref mut w = BufWriter::new(file);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    58
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    59
    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
    60
    encoder
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    61
        .set(png::ColorType::Grayscale)
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    62
        .set(png::BitDepth::Eight);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    63
    let mut writer = encoder.write_header()?;
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
    writer.write_image_data(land.raw_pixels()).unwrap();
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
    Ok(())
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    68
}
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
fn main() {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    71
    let opt = Opt::from_args();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    72
    println!("{:?}", opt);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    73
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    74
    if opt.dump_before_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    75
        dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    76
            opt.seed.as_str().as_bytes(),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    77
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    78
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    79
            Path::new("out.before_distort.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    80
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    81
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    82
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    83
    if opt.dump_before_bezierize {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    84
        dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    85
            opt.seed.as_str().as_bytes(),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    86
            false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    87
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    88
            Path::new("out.before_bezier.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    89
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    90
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    91
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    92
    dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    93
        opt.seed.as_str().as_bytes(),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    94
        false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    95
        true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    96
        Path::new("out.full.png"),
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
    .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    99
}