terrain_forge/lib.rs
1//! # TerrainForge
2//!
3//! A modular procedural generation engine for terrain, dungeons, and maps.
4//!
5//! ## Quick Start
6//!
7//! ```rust
8//! use terrain_forge::{Grid, Tile, algorithms};
9//!
10//! let mut grid = Grid::new(80, 60);
11//! let algo = algorithms::get("bsp").unwrap();
12//! algo.generate(&mut grid, 12345);
13//!
14//! println!("Generated {} floor tiles", grid.count(|t| t.is_floor()));
15//! ```
16//!
17//! ## Semantic Generation
18//!
19//! Generate maps with entity spawn markers and region metadata:
20//!
21//! ```rust
22//! use terrain_forge::{algorithms, generate_with_semantic};
23//!
24//! let result = generate_with_semantic("room_accretion", 80, 60, 12345);
25//! if let Some(semantic) = result.semantic {
26//! println!("Generated {} regions with {} markers",
27//! semantic.regions.len(), semantic.markers.len());
28//! }
29//! ```
30//!
31//! ## Algorithms
32//!
33//! 14 generation algorithms available via [`algorithms::get`]:
34//! - `bsp` - Binary Space Partitioning for structured rooms
35//! - `cellular` - Cellular automata for organic caves
36//! - `drunkard` - Random walk for winding corridors
37//! - `maze` - Perfect maze generation
38//! - `rooms` - Simple rectangular rooms
39//! - `voronoi` - Voronoi-based regions
40//! - `dla` - Diffusion-limited aggregation
41//! - `wfc` - Wave Function Collapse
42//! - `percolation` - Connected cluster generation
43//! - `diamond_square` - Heightmap terrain
44//! - `fractal` - Fractal noise terrain
45//! - `agent` - Multi-agent carving
46//! - `glass_seam` - Region connector
47//! - `room_accretion` - Brogue-style organic dungeons
48//!
49//! ## Composition
50//!
51//! Chain algorithms with [`compose::Pipeline`] or layer with [`compose::LayeredGenerator`]:
52//!
53//! ```rust
54//! use terrain_forge::{Grid, Tile, Algorithm, algorithms};
55//! use terrain_forge::compose::Pipeline;
56//!
57//! let mut grid = Grid::new(80, 60);
58//! let pipeline = Pipeline::new()
59//! .add(algorithms::get("rooms").unwrap())
60//! .add(algorithms::get("cellular").unwrap());
61//! pipeline.generate(&mut grid, 12345);
62//! ```
63//!
64//! ## Effects
65//!
66//! Post-process with [`effects`]: morphology, connectivity, filters, transforms.
67//!
68//! ## Noise
69//!
70//! [`noise`] module provides Perlin, Simplex, Value, Worley with FBM and modifiers.
71
72mod grid;
73mod rng;
74mod algorithm;
75mod semantic;
76
77#[cfg(test)]
78mod semantic_tests;
79
80pub mod algorithms;
81pub mod noise;
82pub mod effects;
83pub mod compose;
84pub mod constraints;
85
86pub use grid::{Grid, Cell, Tile};
87pub use rng::Rng;
88pub use algorithm::Algorithm;
89pub use semantic::{Region, Marker, Masks, ConnectivityGraph, SemanticLayers, GenerationResult, SemanticGenerator};
90
91/// Generate a map with semantic layers if the algorithm supports it
92pub fn generate_with_semantic(
93 algorithm_name: &str,
94 width: usize,
95 height: usize,
96 seed: u64,
97) -> GenerationResult {
98 let mut grid = Grid::new(width, height);
99 let mut rng = Rng::new(seed);
100
101 // Generate tiles
102 if let Some(algo) = algorithms::get(algorithm_name) {
103 algo.generate(&mut grid, seed);
104 }
105
106 // Try to generate semantic layers
107 let semantic = match algorithm_name {
108 "bsp" => {
109 let algo = algorithms::Bsp::default();
110 Some(algo.generate_semantic(&grid, &mut rng))
111 },
112 "room_accretion" | "accretion" => {
113 let algo = algorithms::RoomAccretion::default();
114 Some(algo.generate_semantic(&grid, &mut rng))
115 },
116 _ => None,
117 };
118
119 GenerationResult { tiles: grid, semantic }
120}