rust/land_dump/src/main.rs
changeset 14133 b04dac00e8e2
parent 14130 376a0551b00a
child 14135 ab280be4b617
equal deleted inserted replaced
14132:0c5b9cfda9ab 14133:b04dac00e8e2
     1 extern crate integral_geometry;
       
     2 extern crate land2d;
       
     3 extern crate landgen;
       
     4 extern crate lfprng;
       
     5 extern crate png;
       
     6 extern crate structopt;
       
     7 
       
     8 use png::HasParameters;
     1 use png::HasParameters;
     9 use std::fs::File;
     2 use std::{
    10 use std::io::BufWriter;
     3     fs::File,
    11 use std::path::Path;
     4     io::{BufWriter, Read},
       
     5     path::{Path, PathBuf}
       
     6 };
    12 use structopt::StructOpt;
     7 use structopt::StructOpt;
    13 
     8 
    14 use integral_geometry::{Point, Rect, Size};
     9 use integral_geometry::{Point, Rect, Size};
    15 use landgen::outline_template::OutlineTemplate;
    10 use landgen::{
    16 use landgen::template_based::TemplatedLandGenerator;
    11     outline_template::OutlineTemplate,
    17 use landgen::LandGenerationParameters;
    12     template_based::TemplatedLandGenerator,
    18 use landgen::LandGenerator;
    13     LandGenerationParameters,
       
    14     LandGenerator
       
    15 };
       
    16 use mapgen::MapGenerator;
    19 use lfprng::LaggedFibonacciPRNG;
    17 use lfprng::LaggedFibonacciPRNG;
    20 
    18 
    21 #[derive(StructOpt, Debug)]
    19 #[derive(StructOpt, Debug)]
    22 #[structopt(name = "basic")]
    20 #[structopt(name = "basic")]
    23 struct Opt {
    21 struct Opt {
    27     dump_before_distort: bool,
    25     dump_before_distort: bool,
    28     #[structopt(short = "b", long = "dump-before-bezierize")]
    26     #[structopt(short = "b", long = "dump-before-bezierize")]
    29     dump_before_bezierize: bool,
    27     dump_before_bezierize: bool,
    30     #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
    28     #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
    31     distance_divisor: u32,
    29     distance_divisor: u32,
       
    30     #[structopt(short = "i", long = "templates-file")]
       
    31     templates_file: Option<String>,
       
    32     #[structopt(short = "t", long = "template-type")]
       
    33     template_type: Option<String>
    32 }
    34 }
    33 
    35 
    34 fn template() -> OutlineTemplate {
    36 fn template() -> OutlineTemplate {
    35     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    37     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    36     template.islands = vec![vec![
    38     template.islands = vec![vec![
    43 
    45 
    44     template
    46     template
    45 }
    47 }
    46 
    48 
    47 fn dump(
    49 fn dump(
       
    50     template: &OutlineTemplate,
    48     seed: &[u8],
    51     seed: &[u8],
    49     distance_divisor: u32,
    52     distance_divisor: u32,
    50     skip_distort: bool,
    53     skip_distort: bool,
    51     skip_bezier: bool,
    54     skip_bezier: bool,
    52     file_name: &Path,
    55     file_name: &Path,
    53 ) -> std::io::Result<()> {
    56 ) -> std::io::Result<()> {
    54     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    57     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    55     let landgen = TemplatedLandGenerator::new(template());
    58     let landgen = TemplatedLandGenerator::new(template.clone());
    56     let mut prng = LaggedFibonacciPRNG::new(seed);
    59     let mut prng = LaggedFibonacciPRNG::new(seed);
    57     let land = landgen.generate_land(&params, &mut prng);
    60     let land = landgen.generate_land(&params, &mut prng);
    58 
    61 
    59     let file = File::create(file_name)?;
    62     let file = File::create(file_name)?;
    60     let ref mut w = BufWriter::new(file);
    63     let ref mut w = BufWriter::new(file);
    72 
    75 
    73 fn main() {
    76 fn main() {
    74     let opt = Opt::from_args();
    77     let opt = Opt::from_args();
    75     println!("{:?}", opt);
    78     println!("{:?}", opt);
    76 
    79 
       
    80     let template =
       
    81         if let Some(path) = opt.templates_file {
       
    82             let mut result = String::new();
       
    83             File::open(path)
       
    84                 .expect("Unable to read templates file")
       
    85                 .read_to_string(&mut result);
       
    86 
       
    87             let mut generator = MapGenerator::new();
       
    88 
       
    89             let bom = b"\xEF\xBB\xBF";
       
    90             let source = if &result.as_bytes()[..bom.len()] == &bom[..] {
       
    91                 &result[bom.len()..]
       
    92             } else {
       
    93                 &result[..]
       
    94             };
       
    95 
       
    96             generator.import_yaml_templates(source);
       
    97 
       
    98             let template_type = &opt.template_type
       
    99                 .expect("No template type specified");
       
   100             generator.get_template(template_type)
       
   101                 .expect(&format!("Template type {} not found", template_type))
       
   102                 .clone()
       
   103         } else {
       
   104             template()
       
   105         };
       
   106 
    77     if opt.dump_before_distort {
   107     if opt.dump_before_distort {
    78         dump(
   108         dump(
       
   109             &template,
    79             opt.seed.as_str().as_bytes(),
   110             opt.seed.as_str().as_bytes(),
    80             opt.distance_divisor,
   111             opt.distance_divisor,
    81             true,
   112             true,
    82             true,
   113             true,
    83             Path::new("out.before_distort.png"),
   114             Path::new("out.before_distort.png"),
    84         )
   115         )
    85         .unwrap();
   116         .unwrap();
    86     }
   117     }
    87     if opt.dump_before_bezierize {
   118     if opt.dump_before_bezierize {
    88         dump(
   119         dump(
       
   120             &template,
    89             opt.seed.as_str().as_bytes(),
   121             opt.seed.as_str().as_bytes(),
    90             opt.distance_divisor,
   122             opt.distance_divisor,
    91             false,
   123             false,
    92             true,
   124             true,
    93             Path::new("out.before_bezier.png"),
   125             Path::new("out.before_bezier.png"),
    94         )
   126         )
    95         .unwrap();
   127         .unwrap();
    96     }
   128     }
    97     dump(
   129     dump(
       
   130         &template,
    98         opt.seed.as_str().as_bytes(),
   131         opt.seed.as_str().as_bytes(),
    99         opt.distance_divisor,
   132         opt.distance_divisor,
   100         false,
   133         false,
   101         true,
   134         true,
   102         Path::new("out.full.png"),
   135         Path::new("out.full.png"),