|
1 mod template; |
1 pub mod theme; |
2 pub mod theme; |
2 |
3 |
3 use self::theme::Theme; |
4 use self::theme::Theme; |
|
5 use crate::template::outline::TemplateCollectionDesc as OutlineTemplateCollectionDesc; |
|
6 use crate::template::wavefront_collapse::TemplateCollectionDesc as WfcTemplateCollectionDesc; |
4 use integral_geometry::{Point, Rect, Size}; |
7 use integral_geometry::{Point, Rect, Size}; |
5 use land2d::Land2D; |
8 use land2d::Land2D; |
6 use landgen::{outline_template_based::outline_template::OutlineTemplate, LandGenerationParameters}; |
9 use landgen::{ |
|
10 outline_template_based::{ |
|
11 outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
|
12 }, |
|
13 wavefront_collapse::generator::{ |
|
14 TemplateDescription as WfcTemplate, WavefrontCollapseLandGenerator, |
|
15 }, |
|
16 LandGenerationParameters, LandGenerator, |
|
17 }; |
|
18 use rand::{seq::SliceRandom, Rng}; |
7 use serde_derive::Deserialize; |
19 use serde_derive::Deserialize; |
8 use serde_yaml; |
20 use serde_yaml; |
9 use std::{borrow::Borrow, collections::hash_map::HashMap, mem::replace}; |
21 use std::{borrow::Borrow, collections::hash_map::HashMap, mem::replace}; |
10 use vec2d::Vec2D; |
22 use vec2d::Vec2D; |
11 use rand::{Rng, seq::SliceRandom}; |
|
12 |
|
13 #[derive(Deserialize)] |
|
14 struct PointDesc { |
|
15 x: u32, |
|
16 y: u32, |
|
17 } |
|
18 |
|
19 #[derive(Deserialize)] |
|
20 struct RectDesc { |
|
21 x: u32, |
|
22 y: u32, |
|
23 w: u32, |
|
24 h: u32, |
|
25 } |
|
26 |
|
27 #[derive(Deserialize)] |
|
28 struct TemplateDesc { |
|
29 width: usize, |
|
30 height: usize, |
|
31 can_flip: bool, |
|
32 can_invert: bool, |
|
33 can_mirror: bool, |
|
34 is_negative: bool, |
|
35 put_girders: bool, |
|
36 max_hedgehogs: u8, |
|
37 outline_points: Vec<Vec<RectDesc>>, |
|
38 fill_points: Vec<PointDesc>, |
|
39 } |
|
40 |
|
41 #[derive(Deserialize)] |
|
42 struct TemplateCollectionDesc { |
|
43 templates: Vec<TemplateDesc>, |
|
44 template_types: HashMap<String, Vec<usize>>, |
|
45 } |
|
46 |
|
47 impl From<&TemplateDesc> for OutlineTemplate { |
|
48 fn from(desc: &TemplateDesc) -> Self { |
|
49 OutlineTemplate { |
|
50 islands: desc |
|
51 .outline_points |
|
52 .iter() |
|
53 .map(|v| { |
|
54 v.iter() |
|
55 .map(|r| { |
|
56 Rect::from_size( |
|
57 Point::new(r.x as i32, r.y as i32), |
|
58 Size::new(r.w as usize, r.h as usize), |
|
59 ) |
|
60 }) |
|
61 .collect() |
|
62 }) |
|
63 .collect(), |
|
64 fill_points: desc |
|
65 .fill_points |
|
66 .iter() |
|
67 .map(|p| Point::new(p.x as i32, p.y as i32)) |
|
68 .collect(), |
|
69 size: Size::new(desc.width, desc.height), |
|
70 can_flip: desc.can_flip, |
|
71 can_invert: desc.can_invert, |
|
72 can_mirror: desc.can_mirror, |
|
73 is_negative: desc.is_negative, |
|
74 } |
|
75 } |
|
76 } |
|
77 |
23 |
78 #[derive(PartialEq, Eq, Hash, Clone, Debug)] |
24 #[derive(PartialEq, Eq, Hash, Clone, Debug)] |
79 struct TemplateType(String); |
25 struct TemplateType(String); |
80 |
26 |
81 impl Borrow<str> for TemplateType { |
27 impl Borrow<str> for TemplateType { |
83 self.0.as_str() |
29 self.0.as_str() |
84 } |
30 } |
85 } |
31 } |
86 |
32 |
87 #[derive(Debug)] |
33 #[derive(Debug)] |
88 pub struct MapGenerator { |
34 pub struct MapGenerator<T> { |
89 pub(crate) templates: HashMap<TemplateType, Vec<OutlineTemplate>>, |
35 pub(crate) templates: HashMap<TemplateType, Vec<T>>, |
90 } |
36 } |
91 |
37 |
92 impl MapGenerator { |
38 impl<T> MapGenerator<T> { |
93 pub fn new() -> Self { |
39 pub fn new() -> Self { |
94 Self { |
40 Self { |
95 templates: HashMap::new(), |
41 templates: HashMap::new(), |
96 } |
42 } |
97 } |
43 } |
98 |
44 |
99 pub fn import_yaml_templates(&mut self, text: &str) { |
45 pub fn get_template<R: Rng>(&self, template_type: &str, rng: &mut R) -> Option<&T> { |
100 let mut desc: TemplateCollectionDesc = serde_yaml::from_str(text).unwrap(); |
|
101 let templates = replace(&mut desc.templates, vec![]); |
|
102 self.templates = desc |
|
103 .template_types |
|
104 .into_iter() |
|
105 .map(|(size, indices)| { |
|
106 ( |
|
107 TemplateType(size), |
|
108 indices.iter().map(|i| (&templates[*i]).into()).collect(), |
|
109 ) |
|
110 }) |
|
111 .collect(); |
|
112 } |
|
113 |
|
114 pub fn get_template<R: Rng>(&self, template_type: &str, rng: &mut R) -> Option<&OutlineTemplate> { |
|
115 self.templates |
46 self.templates |
116 .get(template_type) |
47 .get(template_type) |
117 .and_then(|t| t.as_slice().choose(rng)) |
48 .and_then(|t| t.as_slice().choose(rng)) |
118 } |
49 } |
119 |
50 |
185 |x, y| border_sprite.get_pixel(x % border_sprite.width(), y), |
116 |x, y| border_sprite.get_pixel(x % border_sprite.width(), y), |
186 ); |
117 ); |
187 } |
118 } |
188 |
119 |
189 texture |
120 texture |
|
121 } |
|
122 } |
|
123 |
|
124 impl MapGenerator<OutlineTemplate> { |
|
125 pub fn import_yaml_templates(&mut self, text: &str) { |
|
126 let mut desc: OutlineTemplateCollectionDesc = serde_yaml::from_str(text).unwrap(); |
|
127 let templates = replace(&mut desc.templates, vec![]); |
|
128 self.templates = desc |
|
129 .template_types |
|
130 .into_iter() |
|
131 .map(|(size, indices)| { |
|
132 ( |
|
133 TemplateType(size), |
|
134 indices.iter().map(|i| (&templates[*i]).into()).collect(), |
|
135 ) |
|
136 }) |
|
137 .collect(); |
|
138 } |
|
139 |
|
140 pub fn build_generator(&self, template: OutlineTemplate) -> impl LandGenerator { |
|
141 TemplatedLandGenerator::new(template) |
|
142 } |
|
143 } |
|
144 |
|
145 impl MapGenerator<WfcTemplate> { |
|
146 pub fn import_yaml_templates(&mut self, text: &str) { |
|
147 let mut desc: WfcTemplateCollectionDesc = serde_yaml::from_str(text).unwrap(); |
|
148 let templates = replace(&mut desc.templates, vec![]); |
|
149 self.templates = desc |
|
150 .template_types |
|
151 .into_iter() |
|
152 .map(|(size, indices)| { |
|
153 ( |
|
154 TemplateType(size), |
|
155 indices.iter().map(|i| (&templates[*i]).into()).collect(), |
|
156 ) |
|
157 }) |
|
158 .collect(); |
|
159 } |
|
160 |
|
161 pub fn build_generator(&self, template: WfcTemplate) -> impl LandGenerator { |
|
162 WavefrontCollapseLandGenerator::new(template) |
190 } |
163 } |
191 } |
164 } |
192 |
165 |
193 #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
166 #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
194 struct Color(u32); |
167 struct Color(u32); |