Skip to main content

Prefab

Struct Prefab 

Source
pub struct Prefab {
    pub name: String,
    pub width: usize,
    pub height: usize,
    pub cells: Vec<PrefabCell>,
    pub symbols: Vec<char>,
    pub legend: Option<HashMap<char, PrefabLegendEntry>>,
    pub weight: f32,
    pub tags: Vec<String>,
}

Fields§

§name: String§width: usize§height: usize§cells: Vec<PrefabCell>§symbols: Vec<char>§legend: Option<HashMap<char, PrefabLegendEntry>>§weight: f32§tags: Vec<String>

Implementations§

Source§

impl Prefab

Source

pub fn new(pattern: &[&str]) -> Self

Source

pub fn from_data(data: PrefabData) -> Self

Examples found in repository?
examples/phase4_workflow.rs (line 306)
281fn create_specialized_library() -> PrefabLibrary {
282    let mut library = PrefabLibrary::new();
283
284    // Boss room (very rare, large)
285    let boss_room = PrefabData {
286        name: "boss_chamber".to_string(),
287        width: 9,
288        height: 7,
289        pattern: vec![
290            "#########".to_string(),
291            "#.......#".to_string(),
292            "#..###..#".to_string(),
293            "#..#B#..#".to_string(),
294            "#..###..#".to_string(),
295            "#.......#".to_string(),
296            "#########".to_string(),
297        ],
298        weight: 0.2,
299        tags: vec![
300            "boss".to_string(),
301            "special".to_string(),
302            "large".to_string(),
303        ],
304        legend: None,
305    };
306    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(boss_room));
307
308    // Treasure room (rare)
309    let treasure_room = PrefabData {
310        name: "treasure_vault".to_string(),
311        width: 5,
312        height: 5,
313        pattern: vec![
314            "#####".to_string(),
315            "#...#".to_string(),
316            "#.T.#".to_string(),
317            "#...#".to_string(),
318            "#####".to_string(),
319        ],
320        weight: 0.8,
321        tags: vec![
322            "treasure".to_string(),
323            "special".to_string(),
324            "small".to_string(),
325        ],
326        legend: None,
327    };
328    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(treasure_room));
329
330    // Secret passage (uncommon)
331    let secret_passage = PrefabData {
332        name: "secret_passage".to_string(),
333        width: 3,
334        height: 7,
335        pattern: vec![
336            "###".to_string(),
337            "#.#".to_string(),
338            "#.#".to_string(),
339            "#.#".to_string(),
340            "#.#".to_string(),
341            "#.#".to_string(),
342            "###".to_string(),
343        ],
344        weight: 1.2,
345        tags: vec!["secret".to_string(), "corridor".to_string()],
346        legend: None,
347    };
348    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(secret_passage));
349
350    library
351}
More examples
Hide additional examples
examples/advanced_prefabs.rs (line 212)
193fn create_sample_library() -> PrefabLibrary {
194    let mut library = PrefabLibrary::new();
195
196    // Small room (high weight)
197    let small_room = PrefabData {
198        name: "small_room".to_string(),
199        width: 5,
200        height: 5,
201        pattern: vec![
202            "#####".to_string(),
203            "#...#".to_string(),
204            "#...#".to_string(),
205            "#...#".to_string(),
206            "#####".to_string(),
207        ],
208        weight: 3.0,
209        tags: vec!["room".to_string(), "small".to_string()],
210        legend: None,
211    };
212    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(small_room));
213
214    // Large room (medium weight)
215    let large_room = PrefabData {
216        name: "large_room".to_string(),
217        width: 7,
218        height: 6,
219        pattern: vec![
220            "#######".to_string(),
221            "#.....#".to_string(),
222            "#.....#".to_string(),
223            "#.....#".to_string(),
224            "#.....#".to_string(),
225            "#######".to_string(),
226        ],
227        weight: 1.5,
228        tags: vec!["room".to_string(), "large".to_string()],
229        legend: None,
230    };
231    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(large_room));
232
233    // Corridor (low weight)
234    let corridor = PrefabData {
235        name: "corridor".to_string(),
236        width: 7,
237        height: 3,
238        pattern: vec![
239            "#######".to_string(),
240            ".......".to_string(),
241            "#######".to_string(),
242        ],
243        weight: 0.8,
244        tags: vec!["corridor".to_string(), "connection".to_string()],
245        legend: None,
246    };
247    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(corridor));
248
249    // L-shaped room (rare)
250    let mut legend = std::collections::HashMap::new();
251    legend.insert(
252        "M".to_string(),
253        terrain_forge::algorithms::PrefabLegendEntry {
254            tile: Some("floor".to_string()),
255            marker: Some("loot_slot".to_string()),
256            mask: None,
257        },
258    );
259    let l_room = PrefabData {
260        name: "l_shaped_room".to_string(),
261        width: 6,
262        height: 6,
263        pattern: vec![
264            "######".to_string(),
265            "#....#".to_string(),
266            "#....#".to_string(),
267            "#..M##".to_string(),
268            "#..###".to_string(),
269            "######".to_string(),
270        ],
271        weight: 0.5,
272        tags: vec![
273            "room".to_string(),
274            "special".to_string(),
275            "l_shaped".to_string(),
276        ],
277        legend: Some(legend),
278    };
279    library.add_prefab(terrain_forge::algorithms::Prefab::from_data(l_room));
280
281    library
282}
Source

pub fn rect(w: usize, h: usize) -> Self

Source

pub fn rotated(&self) -> Self

Rotate prefab 90 degrees clockwise

Examples found in repository?
examples/advanced_prefabs.rs (line 69)
10fn main() {
11    println!("=== Advanced Prefab System Demo ===\n");
12
13    // Step 1: Create prefab library programmatically
14    println!("1. Creating Prefab Library:");
15    let library = create_sample_library();
16
17    println!(
18        "   Created library with {} prefabs:",
19        library.get_prefabs().len()
20    );
21    for prefab in library.get_prefabs() {
22        println!(
23            "     - {} ({}x{}, weight: {:.1}, tags: {:?})",
24            prefab.name, prefab.width, prefab.height, prefab.weight, prefab.tags
25        );
26    }
27
28    // Step 2: Demonstrate weighted selection
29    println!("\n2. Weighted Selection Test:");
30    let mut rng = Rng::new(12345);
31    let mut selection_counts = std::collections::HashMap::new();
32
33    for _ in 0..100 {
34        if let Some(prefab) = library.select_weighted(&mut rng, None) {
35            *selection_counts.entry(prefab.name.clone()).or_insert(0) += 1;
36        }
37    }
38
39    println!("   Selection frequency (100 trials):");
40    for (name, count) in &selection_counts {
41        println!("     {}: {} times", name, count);
42    }
43
44    // Step 3: Tag-based selection
45    println!("\n3. Tag-based Selection:");
46    let room_prefabs = library.get_by_tag("room");
47    let corridor_prefabs = library.get_by_tag("corridor");
48
49    println!("   Room prefabs: {}", room_prefabs.len());
50    for prefab in &room_prefabs {
51        println!("     - {}", prefab.name);
52    }
53
54    println!("   Corridor prefabs: {}", corridor_prefabs.len());
55    for prefab in &corridor_prefabs {
56        println!("     - {}", prefab.name);
57    }
58
59    // Step 4: Transformation examples
60    println!("\n4. Prefab Transformations:");
61    if let Some(base_prefab) = library.get_prefabs().first() {
62        println!(
63            "   Base prefab '{}' ({}x{}):",
64            base_prefab.name, base_prefab.width, base_prefab.height
65        );
66        print_prefab_pattern(base_prefab);
67
68        // Rotation
69        let rotated = base_prefab.rotated();
70        println!(
71            "   After 90° rotation ({}x{}):",
72            rotated.width, rotated.height
73        );
74        print_prefab_pattern(&rotated);
75
76        // Horizontal mirror
77        let mirrored = base_prefab.mirrored_horizontal();
78        println!(
79            "   After horizontal mirror ({}x{}):",
80            mirrored.width, mirrored.height
81        );
82        print_prefab_pattern(&mirrored);
83
84        // Combined transformation
85        let transform = PrefabTransform {
86            rotation: 1,
87            mirror_h: true,
88            mirror_v: false,
89        };
90        let transformed = transform.apply(base_prefab);
91        println!(
92            "   After rotation + mirror ({}x{}):",
93            transformed.width, transformed.height
94        );
95        print_prefab_pattern(&transformed);
96    }
97
98    // Step 5: Generation with advanced prefabs
99    println!("\n5. Generation with Advanced Prefabs:");
100    let config = PrefabConfig {
101        max_prefabs: 5,
102        min_spacing: 3,
103        allow_rotation: true,
104        allow_mirroring: true,
105        weighted_selection: true,
106        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
107        tags: None,
108    };
109
110    let placer = PrefabPlacer::new(config, library.clone());
111    let mut grid = Grid::new(30, 25);
112    placer.generate(&mut grid, 54321);
113
114    let floor_count = grid.count(|t| t.is_floor());
115    println!(
116        "   Generated {}x{} grid with {} floor tiles",
117        grid.width(),
118        grid.height(),
119        floor_count
120    );
121
122    print_grid(&grid);
123
124    // Step 6: JSON serialization example
125    println!("\n6. JSON Serialization:");
126    match library.save_to_json("prefab_library.json") {
127        Ok(()) => {
128            println!("   ✅ Saved library to prefab_library.json");
129
130            // Try to load it back
131            match PrefabLibrary::load_from_json("prefab_library.json") {
132                Ok(loaded_library) => {
133                    println!("   ✅ Successfully loaded library back");
134                    println!("   Loaded {} prefabs", loaded_library.get_prefabs().len());
135                }
136                Err(e) => println!("   ❌ Failed to load: {}", e),
137            }
138        }
139        Err(e) => println!("   ❌ Failed to save: {}", e),
140    }
141
142    // Step 7: Performance comparison
143    println!("\n7. Performance Comparison:");
144
145    // Simple generation
146    let simple_config = PrefabConfig {
147        max_prefabs: 10,
148        min_spacing: 2,
149        allow_rotation: false,
150        allow_mirroring: false,
151        weighted_selection: false,
152        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
153        tags: None,
154    };
155
156    let start = std::time::Instant::now();
157    let simple_placer = PrefabPlacer::new(simple_config, library.clone());
158    let mut simple_grid = Grid::new(40, 30);
159    simple_placer.generate(&mut simple_grid, 98765);
160    let simple_time = start.elapsed();
161
162    // Advanced generation
163    let advanced_config = PrefabConfig {
164        max_prefabs: 10,
165        min_spacing: 2,
166        allow_rotation: true,
167        allow_mirroring: true,
168        weighted_selection: true,
169        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
170        tags: None,
171    };
172
173    let start = std::time::Instant::now();
174    let advanced_placer = PrefabPlacer::new(advanced_config, library);
175    let mut advanced_grid = Grid::new(40, 30);
176    advanced_placer.generate(&mut advanced_grid, 98765);
177    let advanced_time = start.elapsed();
178
179    println!("   Simple generation: {:?}", simple_time);
180    println!("   Advanced generation: {:?}", advanced_time);
181    println!(
182        "   Overhead: {:.1}x",
183        advanced_time.as_nanos() as f32 / simple_time.as_nanos() as f32
184    );
185
186    println!("\n✅ Advanced prefab system demo complete!");
187    println!("   - JSON serialization for persistent libraries");
188    println!("   - Weighted selection for balanced generation");
189    println!("   - Transformations for variety and reuse");
190    println!("   - Tag-based organization for targeted selection");
191}
Source

pub fn mirrored_horizontal(&self) -> Self

Mirror prefab horizontally

Examples found in repository?
examples/advanced_prefabs.rs (line 77)
10fn main() {
11    println!("=== Advanced Prefab System Demo ===\n");
12
13    // Step 1: Create prefab library programmatically
14    println!("1. Creating Prefab Library:");
15    let library = create_sample_library();
16
17    println!(
18        "   Created library with {} prefabs:",
19        library.get_prefabs().len()
20    );
21    for prefab in library.get_prefabs() {
22        println!(
23            "     - {} ({}x{}, weight: {:.1}, tags: {:?})",
24            prefab.name, prefab.width, prefab.height, prefab.weight, prefab.tags
25        );
26    }
27
28    // Step 2: Demonstrate weighted selection
29    println!("\n2. Weighted Selection Test:");
30    let mut rng = Rng::new(12345);
31    let mut selection_counts = std::collections::HashMap::new();
32
33    for _ in 0..100 {
34        if let Some(prefab) = library.select_weighted(&mut rng, None) {
35            *selection_counts.entry(prefab.name.clone()).or_insert(0) += 1;
36        }
37    }
38
39    println!("   Selection frequency (100 trials):");
40    for (name, count) in &selection_counts {
41        println!("     {}: {} times", name, count);
42    }
43
44    // Step 3: Tag-based selection
45    println!("\n3. Tag-based Selection:");
46    let room_prefabs = library.get_by_tag("room");
47    let corridor_prefabs = library.get_by_tag("corridor");
48
49    println!("   Room prefabs: {}", room_prefabs.len());
50    for prefab in &room_prefabs {
51        println!("     - {}", prefab.name);
52    }
53
54    println!("   Corridor prefabs: {}", corridor_prefabs.len());
55    for prefab in &corridor_prefabs {
56        println!("     - {}", prefab.name);
57    }
58
59    // Step 4: Transformation examples
60    println!("\n4. Prefab Transformations:");
61    if let Some(base_prefab) = library.get_prefabs().first() {
62        println!(
63            "   Base prefab '{}' ({}x{}):",
64            base_prefab.name, base_prefab.width, base_prefab.height
65        );
66        print_prefab_pattern(base_prefab);
67
68        // Rotation
69        let rotated = base_prefab.rotated();
70        println!(
71            "   After 90° rotation ({}x{}):",
72            rotated.width, rotated.height
73        );
74        print_prefab_pattern(&rotated);
75
76        // Horizontal mirror
77        let mirrored = base_prefab.mirrored_horizontal();
78        println!(
79            "   After horizontal mirror ({}x{}):",
80            mirrored.width, mirrored.height
81        );
82        print_prefab_pattern(&mirrored);
83
84        // Combined transformation
85        let transform = PrefabTransform {
86            rotation: 1,
87            mirror_h: true,
88            mirror_v: false,
89        };
90        let transformed = transform.apply(base_prefab);
91        println!(
92            "   After rotation + mirror ({}x{}):",
93            transformed.width, transformed.height
94        );
95        print_prefab_pattern(&transformed);
96    }
97
98    // Step 5: Generation with advanced prefabs
99    println!("\n5. Generation with Advanced Prefabs:");
100    let config = PrefabConfig {
101        max_prefabs: 5,
102        min_spacing: 3,
103        allow_rotation: true,
104        allow_mirroring: true,
105        weighted_selection: true,
106        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
107        tags: None,
108    };
109
110    let placer = PrefabPlacer::new(config, library.clone());
111    let mut grid = Grid::new(30, 25);
112    placer.generate(&mut grid, 54321);
113
114    let floor_count = grid.count(|t| t.is_floor());
115    println!(
116        "   Generated {}x{} grid with {} floor tiles",
117        grid.width(),
118        grid.height(),
119        floor_count
120    );
121
122    print_grid(&grid);
123
124    // Step 6: JSON serialization example
125    println!("\n6. JSON Serialization:");
126    match library.save_to_json("prefab_library.json") {
127        Ok(()) => {
128            println!("   ✅ Saved library to prefab_library.json");
129
130            // Try to load it back
131            match PrefabLibrary::load_from_json("prefab_library.json") {
132                Ok(loaded_library) => {
133                    println!("   ✅ Successfully loaded library back");
134                    println!("   Loaded {} prefabs", loaded_library.get_prefabs().len());
135                }
136                Err(e) => println!("   ❌ Failed to load: {}", e),
137            }
138        }
139        Err(e) => println!("   ❌ Failed to save: {}", e),
140    }
141
142    // Step 7: Performance comparison
143    println!("\n7. Performance Comparison:");
144
145    // Simple generation
146    let simple_config = PrefabConfig {
147        max_prefabs: 10,
148        min_spacing: 2,
149        allow_rotation: false,
150        allow_mirroring: false,
151        weighted_selection: false,
152        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
153        tags: None,
154    };
155
156    let start = std::time::Instant::now();
157    let simple_placer = PrefabPlacer::new(simple_config, library.clone());
158    let mut simple_grid = Grid::new(40, 30);
159    simple_placer.generate(&mut simple_grid, 98765);
160    let simple_time = start.elapsed();
161
162    // Advanced generation
163    let advanced_config = PrefabConfig {
164        max_prefabs: 10,
165        min_spacing: 2,
166        allow_rotation: true,
167        allow_mirroring: true,
168        weighted_selection: true,
169        placement_mode: terrain_forge::algorithms::PrefabPlacementMode::Overwrite,
170        tags: None,
171    };
172
173    let start = std::time::Instant::now();
174    let advanced_placer = PrefabPlacer::new(advanced_config, library);
175    let mut advanced_grid = Grid::new(40, 30);
176    advanced_placer.generate(&mut advanced_grid, 98765);
177    let advanced_time = start.elapsed();
178
179    println!("   Simple generation: {:?}", simple_time);
180    println!("   Advanced generation: {:?}", advanced_time);
181    println!(
182        "   Overhead: {:.1}x",
183        advanced_time.as_nanos() as f32 / simple_time.as_nanos() as f32
184    );
185
186    println!("\n✅ Advanced prefab system demo complete!");
187    println!("   - JSON serialization for persistent libraries");
188    println!("   - Weighted selection for balanced generation");
189    println!("   - Transformations for variety and reuse");
190    println!("   - Tag-based organization for targeted selection");
191}
Source

pub fn mirrored_vertical(&self) -> Self

Mirror prefab vertically

Source

pub fn get(&self, x: usize, y: usize) -> bool

Examples found in repository?
examples/advanced_prefabs.rs (line 288)
284fn print_prefab_pattern(prefab: &terrain_forge::algorithms::Prefab) {
285    for y in 0..prefab.height {
286        print!("     ");
287        for x in 0..prefab.width {
288            print!("{}", if prefab.get(x, y) { "." } else { "#" });
289        }
290        println!();
291    }
292}
Source

pub fn cell_tile(&self, x: usize, y: usize) -> Option<Tile>

Source

pub fn cell_marker(&self, x: usize, y: usize) -> Option<&str>

Source

pub fn cell_mask(&self, x: usize, y: usize) -> Option<&str>

Source

pub fn has_tag(&self, tag: &str) -> bool

Trait Implementations§

Source§

impl Clone for Prefab

Source§

fn clone(&self) -> Prefab

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Prefab

§

impl RefUnwindSafe for Prefab

§

impl Send for Prefab

§

impl Sync for Prefab

§

impl Unpin for Prefab

§

impl UnwindSafe for Prefab

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V