rust/land_dump/src/main.rs
author unc0rr
Sun, 04 Nov 2018 00:20:27 +0100
changeset 14130 376a0551b00a
parent 14126 69db1d2e4cec
child 14133 b04dac00e8e2
permissions -rw-r--r--
- Add distance-divider option to land_dump - Fix multiplication overflow errors (iy went widly out of bounds)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14126
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,
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    30
    #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    31
    distance_divisor: u32,
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    32
}
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    33
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    34
fn template() -> OutlineTemplate {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    35
    let mut template = OutlineTemplate::new(Size::new(4096, 2048));
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    36
    template.islands = vec![vec![
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    37
        Rect::new(100, 2050, 1, 1),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    38
        Rect::new(100, 500, 400, 1200),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    39
        Rect::new(3600, 500, 400, 1200),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    40
        Rect::new(3900, 2050, 1, 1),
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.fill_points = vec![Point::new(2047, 2047)];
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
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
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    47
fn dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    48
    seed: &[u8],
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    49
    distance_divisor: u32,
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    50
    skip_distort: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    51
    skip_bezier: bool,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    52
    file_name: &Path,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    53
) -> std::io::Result<()> {
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    54
    let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    55
    let landgen = TemplatedLandGenerator::new(template());
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    56
    let mut prng = LaggedFibonacciPRNG::new(seed);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    57
    let land = landgen.generate_land(&params, &mut prng);
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 file = File::create(file_name)?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    60
    let ref mut w = BufWriter::new(file);
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 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
    63
    encoder
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    64
        .set(png::ColorType::Grayscale)
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    65
        .set(png::BitDepth::Eight);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    66
    let mut writer = encoder.write_header()?;
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    67
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    68
    writer.write_image_data(land.raw_pixels()).unwrap();
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
    Ok(())
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    71
}
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
fn main() {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    74
    let opt = Opt::from_args();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    75
    println!("{:?}", opt);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    76
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    77
    if opt.dump_before_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    78
        dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    79
            opt.seed.as_str().as_bytes(),
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    80
            opt.distance_divisor,
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    81
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    82
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    83
            Path::new("out.before_distort.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    84
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    85
        .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    86
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    87
    if opt.dump_before_bezierize {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    88
        dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    89
            opt.seed.as_str().as_bytes(),
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    90
            opt.distance_divisor,
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    91
            false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    92
            true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    93
            Path::new("out.before_bezier.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    94
        )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    95
        .unwrap();
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
    dump(
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
    98
        opt.seed.as_str().as_bytes(),
14130
376a0551b00a - Add distance-divider option to land_dump
unc0rr
parents: 14126
diff changeset
    99
        opt.distance_divisor,
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   100
        false,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   101
        true,
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   102
        Path::new("out.full.png"),
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   103
    )
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   104
    .unwrap();
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents:
diff changeset
   105
}