author | Wuzzy <Wuzzy@disroot.org> |
Wed, 28 Jun 2023 18:57:00 +0200 | |
changeset 15967 | f362e82cddcf |
parent 15828 | 44b49f255e31 |
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}, |
15828 | 5 |
path::{Path, PathBuf}, |
14128
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}; |
|
15828 | 10 |
use land2d::Land2D; |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
11 |
use landgen::{ |
15828 | 12 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
13 |
LandGenerationParameters, LandGenerator, |
|
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
14 |
}; |
14121 | 15 |
use lfprng::LaggedFibonacciPRNG; |
15828 | 16 |
use mapgen::{ |
17 |
theme::{slice_u32_to_u8, Theme}, |
|
18 |
MapGenerator, |
|
19 |
}; |
|
14121 | 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, |
|
14125 | 30 |
#[structopt(short = "f", long = "distance-divisor", default_value = "100")] |
31 |
distance_divisor: u32, |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
32 |
#[structopt(short = "i", long = "templates-file")] |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
33 |
templates_file: Option<String>, |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
34 |
#[structopt(short = "t", long = "template-type")] |
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
35 |
template_type: Option<String>, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
36 |
#[structopt(short = "z", long = "theme-dir")] |
15828 | 37 |
theme_dir: Option<String>, |
14121 | 38 |
} |
39 |
||
40 |
fn template() -> OutlineTemplate { |
|
41 |
let mut template = OutlineTemplate::new(Size::new(4096, 2048)); |
|
42 |
template.islands = vec![vec![ |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14130
diff
changeset
|
43 |
Rect::from_size_coords(100, 2050, 1, 1), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14130
diff
changeset
|
44 |
Rect::from_size_coords(100, 500, 400, 1200), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14130
diff
changeset
|
45 |
Rect::from_size_coords(3600, 500, 400, 1200), |
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14130
diff
changeset
|
46 |
Rect::from_size_coords(3900, 2050, 1, 1), |
14121 | 47 |
]]; |
48 |
template.fill_points = vec![Point::new(2047, 2047)]; |
|
49 |
||
50 |
template |
|
51 |
} |
|
52 |
||
53 |
fn dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
54 |
template: &OutlineTemplate, |
14121 | 55 |
seed: &[u8], |
14125 | 56 |
distance_divisor: u32, |
14121 | 57 |
skip_distort: bool, |
58 |
skip_bezier: bool, |
|
59 |
file_name: &Path, |
|
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
60 |
) -> std::io::Result<Land2D<u8>> { |
15828 | 61 |
let params = |
62 |
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
|
63 |
let landgen = TemplatedLandGenerator::new(template.clone()); |
14121 | 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 |
||
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
78 |
Ok(land) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
79 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
80 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
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:
14152
diff
changeset
|
82 |
let theme = Theme::load(theme_dir).unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
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:
14152
diff
changeset
|
84 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
85 |
let file = File::create(output_filename).unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
86 |
let ref mut w = BufWriter::new(file); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
87 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
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. |
15828 | 89 |
encoder.set(png::ColorType::RGBA).set(png::BitDepth::Eight); |
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
90 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
91 |
let mut writer = encoder.write_header().unwrap(); |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
92 |
|
15828 | 93 |
writer |
94 |
.write_image_data(slice_u32_to_u8(texture.as_slice())) |
|
95 |
.unwrap(); |
|
14121 | 96 |
} |
97 |
||
98 |
fn main() { |
|
99 |
let opt = Opt::from_args(); |
|
100 |
println!("{:?}", opt); |
|
101 |
||
15828 | 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); |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
107 |
|
15828 | 108 |
let mut generator = MapGenerator::new(); |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
109 |
|
15828 | 110 |
let source = &result[..]; |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
111 |
|
15828 | 112 |
generator.import_yaml_templates(source); |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
113 |
|
15828 | 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 |
}; |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
122 |
|
14121 | 123 |
if opt.dump_before_distort { |
124 |
dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
125 |
&template, |
14121 | 126 |
opt.seed.as_str().as_bytes(), |
14125 | 127 |
opt.distance_divisor, |
14121 | 128 |
true, |
129 |
true, |
|
130 |
Path::new("out.before_distort.png"), |
|
131 |
) |
|
132 |
.unwrap(); |
|
133 |
} |
|
134 |
if opt.dump_before_bezierize { |
|
135 |
dump( |
|
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
136 |
&template, |
14121 | 137 |
opt.seed.as_str().as_bytes(), |
14125 | 138 |
opt.distance_divisor, |
14121 | 139 |
false, |
140 |
true, |
|
141 |
Path::new("out.before_bezier.png"), |
|
142 |
) |
|
143 |
.unwrap(); |
|
144 |
} |
|
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
145 |
let land = dump( |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14125
diff
changeset
|
146 |
&template, |
14121 | 147 |
opt.seed.as_str().as_bytes(), |
14125 | 148 |
opt.distance_divisor, |
14121 | 149 |
false, |
14130 | 150 |
false, |
14121 | 151 |
Path::new("out.full.png"), |
152 |
) |
|
153 |
.unwrap(); |
|
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
154 |
|
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
155 |
if let Some(dir) = opt.theme_dir { |
15828 | 156 |
texturize(&Path::new(&dir), &land, &Path::new("out.texture.png")); |
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14152
diff
changeset
|
157 |
} |
14121 | 158 |
} |