author | alfadur |
Sun, 04 Nov 2018 07:16:34 +0300 | |
changeset 14128 | b04dac00e8e2 |
parent 14125 | 376a0551b00a |
child 14130 | ab280be4b617 |
permissions | -rw-r--r-- |
14121 | 1 |
use png::HasParameters; |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
2 |
use std::{ |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
3 |
fs::File, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
4 |
io::{BufWriter, Read}, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
5 |
path::{Path, PathBuf} |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
6 |
}; |
14121 | 7 |
use structopt::StructOpt; |
8 |
||
9 |
use integral_geometry::{Point, Rect, Size}; |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
10 |
use landgen::{ |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
11 |
outline_template::OutlineTemplate, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
12 |
template_based::TemplatedLandGenerator, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
13 |
LandGenerationParameters, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
14 |
LandGenerator |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
15 |
}; |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
16 |
use mapgen::MapGenerator; |
14121 | 17 |
use lfprng::LaggedFibonacciPRNG; |
18 |
||
19 |
#[derive(StructOpt, Debug)] |
|
20 |
#[structopt(name = "basic")] |
|
21 |
struct Opt { |
|
22 |
#[structopt(short = "s", long = "seed", default_value = "TEST_SEED")] |
|
23 |
seed: String, |
|
24 |
#[structopt(short = "d", long = "dump-before-distort")] |
|
25 |
dump_before_distort: bool, |
|
26 |
#[structopt(short = "b", long = "dump-before-bezierize")] |
|
27 |
dump_before_bezierize: bool, |
|
14125 | 28 |
#[structopt(short = "f", long = "distance-divisor", default_value = "100")] |
29 |
distance_divisor: u32, |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
30 |
#[structopt(short = "i", long = "templates-file")] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
31 |
templates_file: Option<String>, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
32 |
#[structopt(short = "t", long = "template-type")] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
33 |
template_type: Option<String> |
14121 | 34 |
} |
35 |
||
36 |
fn template() -> OutlineTemplate { |
|
37 |
let mut template = OutlineTemplate::new(Size::new(4096, 2048)); |
|
38 |
template.islands = vec![vec![ |
|
39 |
Rect::new(100, 2050, 1, 1), |
|
40 |
Rect::new(100, 500, 400, 1200), |
|
41 |
Rect::new(3600, 500, 400, 1200), |
|
42 |
Rect::new(3900, 2050, 1, 1), |
|
43 |
]]; |
|
44 |
template.fill_points = vec![Point::new(2047, 2047)]; |
|
45 |
||
46 |
template |
|
47 |
} |
|
48 |
||
49 |
fn dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
50 |
template: &OutlineTemplate, |
14121 | 51 |
seed: &[u8], |
14125 | 52 |
distance_divisor: u32, |
14121 | 53 |
skip_distort: bool, |
54 |
skip_bezier: bool, |
|
55 |
file_name: &Path, |
|
56 |
) -> std::io::Result<()> { |
|
14125 | 57 |
let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier); |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
58 |
let landgen = TemplatedLandGenerator::new(template.clone()); |
14121 | 59 |
let mut prng = LaggedFibonacciPRNG::new(seed); |
60 |
let land = landgen.generate_land(¶ms, &mut prng); |
|
61 |
||
62 |
let file = File::create(file_name)?; |
|
63 |
let ref mut w = BufWriter::new(file); |
|
64 |
||
65 |
let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1. |
|
66 |
encoder |
|
67 |
.set(png::ColorType::Grayscale) |
|
68 |
.set(png::BitDepth::Eight); |
|
69 |
let mut writer = encoder.write_header()?; |
|
70 |
||
71 |
writer.write_image_data(land.raw_pixels()).unwrap(); |
|
72 |
||
73 |
Ok(()) |
|
74 |
} |
|
75 |
||
76 |
fn main() { |
|
77 |
let opt = Opt::from_args(); |
|
78 |
println!("{:?}", opt); |
|
79 |
||
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
80 |
let template = |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
81 |
if let Some(path) = opt.templates_file { |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
82 |
let mut result = String::new(); |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
83 |
File::open(path) |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
84 |
.expect("Unable to read templates file") |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
85 |
.read_to_string(&mut result); |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
86 |
|
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
87 |
let mut generator = MapGenerator::new(); |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
88 |
|
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
89 |
let bom = b"\xEF\xBB\xBF"; |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
90 |
let source = if &result.as_bytes()[..bom.len()] == &bom[..] { |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
91 |
&result[bom.len()..] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
92 |
} else { |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
93 |
&result[..] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
94 |
}; |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
95 |
|
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
96 |
generator.import_yaml_templates(source); |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
97 |
|
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
98 |
let template_type = &opt.template_type |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
99 |
.expect("No template type specified"); |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
100 |
generator.get_template(template_type) |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
101 |
.expect(&format!("Template type {} not found", template_type)) |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
102 |
.clone() |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
103 |
} else { |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
104 |
template() |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
105 |
}; |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
106 |
|
14121 | 107 |
if opt.dump_before_distort { |
108 |
dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
109 |
&template, |
14121 | 110 |
opt.seed.as_str().as_bytes(), |
14125 | 111 |
opt.distance_divisor, |
14121 | 112 |
true, |
113 |
true, |
|
114 |
Path::new("out.before_distort.png"), |
|
115 |
) |
|
116 |
.unwrap(); |
|
117 |
} |
|
118 |
if opt.dump_before_bezierize { |
|
119 |
dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
120 |
&template, |
14121 | 121 |
opt.seed.as_str().as_bytes(), |
14125 | 122 |
opt.distance_divisor, |
14121 | 123 |
false, |
124 |
true, |
|
125 |
Path::new("out.before_bezier.png"), |
|
126 |
) |
|
127 |
.unwrap(); |
|
128 |
} |
|
129 |
dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
130 |
&template, |
14121 | 131 |
opt.seed.as_str().as_bytes(), |
14125 | 132 |
opt.distance_divisor, |
14121 | 133 |
false, |
134 |
true, |
|
135 |
Path::new("out.full.png"), |
|
136 |
) |
|
137 |
.unwrap(); |
|
138 |
} |