rust/land_dump/src/main.rs
changeset 15828 44b49f255e31
parent 14164 1749961647b9
equal deleted inserted replaced
15827:64b0a5cead86 15828:44b49f255e31
     1 use png::HasParameters;
     1 use png::HasParameters;
     2 use std::{
     2 use std::{
     3     fs::File,
     3     fs::File,
     4     io::{BufWriter, Read},
     4     io::{BufWriter, Read},
     5     path::{Path, PathBuf}
     5     path::{Path, PathBuf},
     6 };
     6 };
     7 use structopt::StructOpt;
     7 use structopt::StructOpt;
     8 
     8 
     9 use integral_geometry::{Point, Rect, Size};
     9 use integral_geometry::{Point, Rect, Size};
       
    10 use land2d::Land2D;
    10 use landgen::{
    11 use landgen::{
    11     outline_template::OutlineTemplate,
    12     outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
    12     template_based::TemplatedLandGenerator,
    13     LandGenerationParameters, LandGenerator,
    13     LandGenerationParameters,
       
    14     LandGenerator
       
    15 };
       
    16 use mapgen::{
       
    17     MapGenerator,
       
    18     theme::{Theme, slice_u32_to_u8}
       
    19 };
    14 };
    20 use lfprng::LaggedFibonacciPRNG;
    15 use lfprng::LaggedFibonacciPRNG;
    21 use land2d::Land2D;
    16 use mapgen::{
       
    17     theme::{slice_u32_to_u8, Theme},
       
    18     MapGenerator,
       
    19 };
    22 
    20 
    23 #[derive(StructOpt, Debug)]
    21 #[derive(StructOpt, Debug)]
    24 #[structopt(name = "basic")]
    22 #[structopt(name = "basic")]
    25 struct Opt {
    23 struct Opt {
    26     #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
    24     #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
    34     #[structopt(short = "i", long = "templates-file")]
    32     #[structopt(short = "i", long = "templates-file")]
    35     templates_file: Option<String>,
    33     templates_file: Option<String>,
    36     #[structopt(short = "t", long = "template-type")]
    34     #[structopt(short = "t", long = "template-type")]
    37     template_type: Option<String>,
    35     template_type: Option<String>,
    38     #[structopt(short = "z", long = "theme-dir")]
    36     #[structopt(short = "z", long = "theme-dir")]
    39     theme_dir: Option<String>
    37     theme_dir: Option<String>,
    40 }
    38 }
    41 
    39 
    42 fn template() -> OutlineTemplate {
    40 fn template() -> OutlineTemplate {
    43     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    41     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    44     template.islands = vec![vec![
    42     template.islands = vec![vec![
    58     distance_divisor: u32,
    56     distance_divisor: u32,
    59     skip_distort: bool,
    57     skip_distort: bool,
    60     skip_bezier: bool,
    58     skip_bezier: bool,
    61     file_name: &Path,
    59     file_name: &Path,
    62 ) -> std::io::Result<Land2D<u8>> {
    60 ) -> std::io::Result<Land2D<u8>> {
    63     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    61     let params =
       
    62         LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    64     let landgen = TemplatedLandGenerator::new(template.clone());
    63     let landgen = TemplatedLandGenerator::new(template.clone());
    65     let mut prng = LaggedFibonacciPRNG::new(seed);
    64     let mut prng = LaggedFibonacciPRNG::new(seed);
    66     let land = landgen.generate_land(&params, &mut prng);
    65     let land = landgen.generate_land(&params, &mut prng);
    67 
    66 
    68     let file = File::create(file_name)?;
    67     let file = File::create(file_name)?;
    85 
    84 
    86     let file = File::create(output_filename).unwrap();
    85     let file = File::create(output_filename).unwrap();
    87     let ref mut w = BufWriter::new(file);
    86     let ref mut w = BufWriter::new(file);
    88 
    87 
    89     let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
    88     let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
    90     encoder
    89     encoder.set(png::ColorType::RGBA).set(png::BitDepth::Eight);
    91         .set(png::ColorType::RGBA)
       
    92         .set(png::BitDepth::Eight);
       
    93 
    90 
    94     let mut writer = encoder.write_header().unwrap();
    91     let mut writer = encoder.write_header().unwrap();
    95 
    92 
    96     writer.write_image_data(slice_u32_to_u8(texture.as_slice())).unwrap();
    93     writer
       
    94         .write_image_data(slice_u32_to_u8(texture.as_slice()))
       
    95         .unwrap();
    97 }
    96 }
    98 
    97 
    99 fn main() {
    98 fn main() {
   100     let opt = Opt::from_args();
    99     let opt = Opt::from_args();
   101     println!("{:?}", opt);
   100     println!("{:?}", opt);
   102 
   101 
   103     let template =
   102     let template = if let Some(path) = opt.templates_file {
   104         if let Some(path) = opt.templates_file {
   103         let mut result = String::new();
   105             let mut result = String::new();
   104         File::open(path)
   106             File::open(path)
   105             .expect("Unable to read templates file")
   107                 .expect("Unable to read templates file")
   106             .read_to_string(&mut result);
   108                 .read_to_string(&mut result);
       
   109 
   107 
   110             let mut generator = MapGenerator::new();
   108         let mut generator = MapGenerator::new();
   111 
   109 
   112             let source =  &result[..];
   110         let source = &result[..];
   113 
   111 
   114             generator.import_yaml_templates(source);
   112         generator.import_yaml_templates(source);
   115 
   113 
   116             let template_type = &opt.template_type
   114         let template_type = &opt.template_type.expect("No template type specified");
   117                 .expect("No template type specified");
   115         generator
   118             generator.get_template(template_type)
   116             .get_template(template_type)
   119                 .expect(&format!("Template type {} not found", template_type))
   117             .expect(&format!("Template type {} not found", template_type))
   120                 .clone()
   118             .clone()
   121         } else {
   119     } else {
   122             template()
   120         template()
   123         };
   121     };
   124 
   122 
   125     if opt.dump_before_distort {
   123     if opt.dump_before_distort {
   126         dump(
   124         dump(
   127             &template,
   125             &template,
   128             opt.seed.as_str().as_bytes(),
   126             opt.seed.as_str().as_bytes(),
   153         Path::new("out.full.png"),
   151         Path::new("out.full.png"),
   154     )
   152     )
   155     .unwrap();
   153     .unwrap();
   156 
   154 
   157     if let Some(dir) = opt.theme_dir {
   155     if let Some(dir) = opt.theme_dir {
   158         texturize(
   156         texturize(&Path::new(&dir), &land, &Path::new("out.texture.png"));
   159             &Path::new(&dir),
       
   160             &land,
       
   161             &Path::new("out.texture.png")
       
   162         );
       
   163     }
   157     }
   164 }
   158 }