add command arguments to use a template from file into land_dump
authoralfadur
Sun, 04 Nov 2018 07:16:34 +0300
changeset 14133 b04dac00e8e2
parent 14132 0c5b9cfda9ab
child 14134 703242ade2c1
add command arguments to use a template from file into land_dump
rust/land_dump/Cargo.toml
rust/land_dump/src/main.rs
rust/landgen/src/outline_template.rs
rust/mapgen/src/lib.rs
--- a/rust/land_dump/Cargo.toml	Sun Nov 04 04:43:30 2018 +0300
+++ b/rust/land_dump/Cargo.toml	Sun Nov 04 07:16:34 2018 +0300
@@ -7,6 +7,7 @@
 [dependencies]
 land2d = { path = "../land2d" }
 landgen = { path = "../landgen" }
+mapgen = { path = "../mapgen" }
 lfprng = { path = "../lfprng" }
 integral-geometry = { path = "../integral-geometry" }
 png = "0.13"
--- a/rust/land_dump/src/main.rs	Sun Nov 04 04:43:30 2018 +0300
+++ b/rust/land_dump/src/main.rs	Sun Nov 04 07:16:34 2018 +0300
@@ -1,21 +1,19 @@
-extern crate integral_geometry;
-extern crate land2d;
-extern crate landgen;
-extern crate lfprng;
-extern crate png;
-extern crate structopt;
-
 use png::HasParameters;
-use std::fs::File;
-use std::io::BufWriter;
-use std::path::Path;
+use std::{
+    fs::File,
+    io::{BufWriter, Read},
+    path::{Path, PathBuf}
+};
 use structopt::StructOpt;
 
 use integral_geometry::{Point, Rect, Size};
-use landgen::outline_template::OutlineTemplate;
-use landgen::template_based::TemplatedLandGenerator;
-use landgen::LandGenerationParameters;
-use landgen::LandGenerator;
+use landgen::{
+    outline_template::OutlineTemplate,
+    template_based::TemplatedLandGenerator,
+    LandGenerationParameters,
+    LandGenerator
+};
+use mapgen::MapGenerator;
 use lfprng::LaggedFibonacciPRNG;
 
 #[derive(StructOpt, Debug)]
@@ -29,6 +27,10 @@
     dump_before_bezierize: bool,
     #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
     distance_divisor: u32,
+    #[structopt(short = "i", long = "templates-file")]
+    templates_file: Option<String>,
+    #[structopt(short = "t", long = "template-type")]
+    template_type: Option<String>
 }
 
 fn template() -> OutlineTemplate {
@@ -45,6 +47,7 @@
 }
 
 fn dump(
+    template: &OutlineTemplate,
     seed: &[u8],
     distance_divisor: u32,
     skip_distort: bool,
@@ -52,7 +55,7 @@
     file_name: &Path,
 ) -> std::io::Result<()> {
     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
-    let landgen = TemplatedLandGenerator::new(template());
+    let landgen = TemplatedLandGenerator::new(template.clone());
     let mut prng = LaggedFibonacciPRNG::new(seed);
     let land = landgen.generate_land(&params, &mut prng);
 
@@ -74,8 +77,36 @@
     let opt = Opt::from_args();
     println!("{:?}", opt);
 
+    let template =
+        if let Some(path) = opt.templates_file {
+            let mut result = String::new();
+            File::open(path)
+                .expect("Unable to read templates file")
+                .read_to_string(&mut result);
+
+            let mut generator = MapGenerator::new();
+
+            let bom = b"\xEF\xBB\xBF";
+            let source = if &result.as_bytes()[..bom.len()] == &bom[..] {
+                &result[bom.len()..]
+            } else {
+                &result[..]
+            };
+
+            generator.import_yaml_templates(source);
+
+            let template_type = &opt.template_type
+                .expect("No template type specified");
+            generator.get_template(template_type)
+                .expect(&format!("Template type {} not found", template_type))
+                .clone()
+        } else {
+            template()
+        };
+
     if opt.dump_before_distort {
         dump(
+            &template,
             opt.seed.as_str().as_bytes(),
             opt.distance_divisor,
             true,
@@ -86,6 +117,7 @@
     }
     if opt.dump_before_bezierize {
         dump(
+            &template,
             opt.seed.as_str().as_bytes(),
             opt.distance_divisor,
             false,
@@ -95,6 +127,7 @@
         .unwrap();
     }
     dump(
+        &template,
         opt.seed.as_str().as_bytes(),
         opt.distance_divisor,
         false,
--- a/rust/landgen/src/outline_template.rs	Sun Nov 04 04:43:30 2018 +0300
+++ b/rust/landgen/src/outline_template.rs	Sun Nov 04 07:16:34 2018 +0300
@@ -1,6 +1,6 @@
 use integral_geometry::{Point, Rect, Size};
 
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub struct OutlineTemplate {
     pub islands: Vec<Vec<Rect>>,
     pub fill_points: Vec<Point>,
--- a/rust/mapgen/src/lib.rs	Sun Nov 04 04:43:30 2018 +0300
+++ b/rust/mapgen/src/lib.rs	Sun Nov 04 07:16:34 2018 +0300
@@ -111,6 +111,8 @@
     #[test]
     fn simple_load() {
         let text = r#"
+# comment
+
 templates:
   -
     width: 3072
@@ -132,10 +134,10 @@
         - {x: 1930, y: 1424, w: 1, h: 1}
     fill_points:
       - {x: 1023, y: 0}
+      - {x: 1023, y: 0}
 
 template_types:
     test: [0]
-
 "#;
 
         let mut generator = MapGenerator::new();