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