author | S.D. |
Tue, 27 Sep 2022 14:59:03 +0300 | |
changeset 15900 | fc3cb23fd26f |
parent 15850 | 44b49f255e31 |
permissions | -rw-r--r-- |
14142 | 1 |
use png::HasParameters; |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
2 |
use std::{ |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
3 |
fs::File, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
4 |
io::{BufWriter, Read}, |
15850 | 5 |
path::{Path, PathBuf}, |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
6 |
}; |
14142 | 7 |
use structopt::StructOpt; |
8 |
||
9 |
use integral_geometry::{Point, Rect, Size}; |
|
15850 | 10 |
use land2d::Land2D; |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
11 |
use landgen::{ |
15850 | 12 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
13 |
LandGenerationParameters, LandGenerator, |
|
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
14 |
}; |
14142 | 15 |
use lfprng::LaggedFibonacciPRNG; |
15850 | 16 |
use mapgen::{ |
17 |
theme::{slice_u32_to_u8, Theme}, |
|
18 |
MapGenerator, |
|
19 |
}; |
|
14142 | 20 |
|
21 |
#[derive(StructOpt, Debug)] |
|
22 |
#[structopt(name = "basic")] |
|
23 |
struct Opt { |
|
24 |
#[structopt(short = "s", long = "seed", default_value = "TEST_SEED")] |
|
25 |
seed: String, |
|
26 |
#[structopt(short = "d", long = "dump-before-distort")] |
|
27 |
dump_before_distort: bool, |
|
28 |
#[structopt(short = "b", long = "dump-before-bezierize")] |
|
29 |
dump_before_bezierize: bool, |
|
14146 | 30 |
#[structopt(short = "f", long = "distance-divisor", default_value = "100")] |
31 |
distance_divisor: u32, |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
32 |
#[structopt(short = "i", long = "templates-file")] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
33 |
templates_file: Option<String>, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
34 |
#[structopt(short = "t", long = "template-type")] |
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
35 |
template_type: Option<String>, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
36 |
#[structopt(short = "z", long = "theme-dir")] |
15850 | 37 |
theme_dir: Option<String>, |
14142 | 38 |
} |
39 |
||
40 |
fn template() -> OutlineTemplate { |
|
41 |
let mut template = OutlineTemplate::new(Size::new(4096, 2048)); |
|
42 |
template.islands = vec![vec![ |
|
14158
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14151
diff
changeset
|
43 |
Rect::from_size_coords(100, 2050, 1, 1), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14151
diff
changeset
|
44 |
Rect::from_size_coords(100, 500, 400, 1200), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14151
diff
changeset
|
45 |
Rect::from_size_coords(3600, 500, 400, 1200), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14151
diff
changeset
|
46 |
Rect::from_size_coords(3900, 2050, 1, 1), |
14142 | 47 |
]]; |
48 |
template.fill_points = vec![Point::new(2047, 2047)]; |
|
49 |
||
50 |
template |
|
51 |
} |
|
52 |
||
53 |
fn dump( |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
54 |
template: &OutlineTemplate, |
14142 | 55 |
seed: &[u8], |
14146 | 56 |
distance_divisor: u32, |
14142 | 57 |
skip_distort: bool, |
58 |
skip_bezier: bool, |
|
59 |
file_name: &Path, |
|
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
60 |
) -> std::io::Result<Land2D<u8>> { |
15850 | 61 |
let params = |
62 |
LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier); |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
63 |
let landgen = TemplatedLandGenerator::new(template.clone()); |
14142 | 64 |
let mut prng = LaggedFibonacciPRNG::new(seed); |
65 |
let land = landgen.generate_land(¶ms, &mut prng); |
|
66 |
||
67 |
let file = File::create(file_name)?; |
|
68 |
let ref mut w = BufWriter::new(file); |
|
69 |
||
70 |
let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1. |
|
71 |
encoder |
|
72 |
.set(png::ColorType::Grayscale) |
|
73 |
.set(png::BitDepth::Eight); |
|
74 |
let mut writer = encoder.write_header()?; |
|
75 |
||
76 |
writer.write_image_data(land.raw_pixels()).unwrap(); |
|
77 |
||
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
78 |
Ok(land) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
79 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
80 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
81 |
fn texturize(theme_dir: &Path, land: &Land2D<u8>, output_filename: &Path) { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
82 |
let theme = Theme::load(theme_dir).unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
83 |
let texture = MapGenerator::new().make_texture(land, &theme); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
84 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
85 |
let file = File::create(output_filename).unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
86 |
let ref mut w = BufWriter::new(file); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
87 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
88 |
let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1. |
15850 | 89 |
encoder.set(png::ColorType::RGBA).set(png::BitDepth::Eight); |
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
90 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
91 |
let mut writer = encoder.write_header().unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
92 |
|
15850 | 93 |
writer |
94 |
.write_image_data(slice_u32_to_u8(texture.as_slice())) |
|
95 |
.unwrap(); |
|
14142 | 96 |
} |
97 |
||
98 |
fn main() { |
|
99 |
let opt = Opt::from_args(); |
|
100 |
println!("{:?}", opt); |
|
101 |
||
15850 | 102 |
let template = if let Some(path) = opt.templates_file { |
103 |
let mut result = String::new(); |
|
104 |
File::open(path) |
|
105 |
.expect("Unable to read templates file") |
|
106 |
.read_to_string(&mut result); |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
107 |
|
15850 | 108 |
let mut generator = MapGenerator::new(); |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
109 |
|
15850 | 110 |
let source = &result[..]; |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
111 |
|
15850 | 112 |
generator.import_yaml_templates(source); |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
113 |
|
15850 | 114 |
let template_type = &opt.template_type.expect("No template type specified"); |
115 |
generator |
|
116 |
.get_template(template_type) |
|
117 |
.expect(&format!("Template type {} not found", template_type)) |
|
118 |
.clone() |
|
119 |
} else { |
|
120 |
template() |
|
121 |
}; |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
122 |
|
14142 | 123 |
if opt.dump_before_distort { |
124 |
dump( |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
125 |
&template, |
14142 | 126 |
opt.seed.as_str().as_bytes(), |
14146 | 127 |
opt.distance_divisor, |
14142 | 128 |
true, |
129 |
true, |
|
130 |
Path::new("out.before_distort.png"), |
|
131 |
) |
|
132 |
.unwrap(); |
|
133 |
} |
|
134 |
if opt.dump_before_bezierize { |
|
135 |
dump( |
|
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
136 |
&template, |
14142 | 137 |
opt.seed.as_str().as_bytes(), |
14146 | 138 |
opt.distance_divisor, |
14142 | 139 |
false, |
140 |
true, |
|
141 |
Path::new("out.before_bezier.png"), |
|
142 |
) |
|
143 |
.unwrap(); |
|
144 |
} |
|
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
145 |
let land = dump( |
14149
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14146
diff
changeset
|
146 |
&template, |
14142 | 147 |
opt.seed.as_str().as_bytes(), |
14146 | 148 |
opt.distance_divisor, |
14142 | 149 |
false, |
14151 | 150 |
false, |
14142 | 151 |
Path::new("out.full.png"), |
152 |
) |
|
153 |
.unwrap(); |
|
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
154 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
155 |
if let Some(dir) = opt.theme_dir { |
15850 | 156 |
texturize(&Path::new(&dir), &land, &Path::new("out.texture.png")); |
14185
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14173
diff
changeset
|
157 |
} |
14142 | 158 |
} |