terrain-forge 0.5.0

A modular procedural generation engine for terrain, dungeons, and maps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# Usage Guide

## Installation

```toml
[dependencies]
terrain-forge = "0.5"
```

## Basic Generation

```rust
use terrain_forge::{Grid, Tile, algorithms};

fn main() {
    let mut grid = Grid::new(80, 60);
    algorithms::get("bsp").unwrap().generate(&mut grid, 12345);
    println!("Generated {} floor tiles", grid.count(|t| t.is_floor()));
}
```

## Enhanced Wave Function Collapse

```rust
use terrain_forge::{algorithms::*, Grid, Rng};

fn main() {
    let mut grid = Grid::new(40, 30);
    let mut rng = Rng::new(12345);
    
    // Create example pattern for learning
    let mut example = Grid::new(10, 10);
    algorithms::get("bsp").unwrap().generate(&mut example, 42);
    
    // Enhanced WFC with pattern learning
    let mut wfc = EnhancedWfc::new(WfcConfig::default());
    let patterns = wfc.learn_patterns(&example, 3); // 3x3 patterns
    
    // Generate with learned patterns
    wfc.generate_with_patterns(&mut grid, &patterns, &mut rng);
    println!("Generated with {} learned patterns", patterns.len());
}
```

## Delaunay Triangulation

```rust
use terrain_forge::{analysis::*, spatial::*, Grid, Rng};

fn main() {
    let mut grid = Grid::new(60, 40);
    algorithms::get("rooms").unwrap().generate(&mut grid, 12345);
    
    // Extract room centers
    let room_centers = find_room_centers(&grid);
    
    // Create Delaunay triangulation
    let triangulation = delaunay_triangulation(&room_centers);
    
    // Generate minimum spanning tree for connections
    let mst = minimum_spanning_tree(&triangulation);
    
    // Connect rooms with corridors
    for edge in mst {
        connect_points(&mut grid, edge.from, edge.to);
    }
    
    println!("Connected {} rooms with {} corridors", 
             room_centers.len(), mst.len());
}
```

## Advanced Prefab System

```rust
use terrain_forge::{algorithms::*, Grid, Rng};
use serde_json;

fn main() {
    let mut grid = Grid::new(80, 60);
    let mut rng = Rng::new(12345);
    
    // Load prefabs from JSON
    let prefab_json = r#"
    {
        "name": "treasure_room",
        "weight": 10,
        "pattern": [
            "###",
            "#T#",
            "###"
        ],
        "metadata": {
            "T": {"type": "treasure", "value": 100}
        }
    }"#;
    
    let prefab_data: PrefabData = serde_json::from_str(prefab_json).unwrap();
    let mut library = PrefabLibrary::new();
    library.add_prefab(prefab_data);
    
    // Generate with prefabs
    let mut prefab_gen = PrefabSystem::new(PrefabConfig {
        library,
        placement_attempts: 50,
        min_spacing: 5,
        ..Default::default()
    });
    
    prefab_gen.generate(&mut grid, 12345);
    println!("Placed prefabs with transformations");
}
```

## Spatial Analysis

```rust
use terrain_forge::{spatial::*, Grid, Rng};

fn main() {
    let mut grid = Grid::new(50, 40);
    algorithms::get("cellular").unwrap().generate(&mut grid, 12345);
    
    // Distance transforms with multiple metrics
    let euclidean = DistanceTransform::euclidean().compute(&grid);
    let manhattan = DistanceTransform::manhattan().compute(&grid);
    let chebyshev = DistanceTransform::chebyshev().compute(&grid);
    
    // Advanced pathfinding with Dijkstra maps
    let goals = vec![(10, 10), (40, 30)];
    let dijkstra_map = DijkstraMap::new(&goals).compute(&grid);
    
    // Generate flow fields for AI movement
    let flow_field = FlowField::from_dijkstra(&dijkstra_map);
    
    // Morphological operations
    let dilated = morphology::dilate(&grid, &StructuringElement::cross());
    let eroded = morphology::erode(&grid, &StructuringElement::square(3));
    
    println!("Computed spatial analysis with multiple metrics");
}
```

## Semantic Generation

```rust
use terrain_forge::{SemanticExtractor, Rng};
use terrain_forge::algorithms::Bsp;

fn main() {
    let mut grid = Grid::new(80, 60);
    let mut rng = Rng::new(12345);
    let algo = Bsp::default();
    
    // Generate terrain
    algo.generate(&mut grid, 12345);
    
    // Extract semantic information
    let semantic = SemanticExtractor::for_rooms().extract(&grid, &mut rng);
    
    // Use markers for entity spawning
    for (x, y, marker) in &semantic.markers {
        match marker.tag.as_str() {
            "PlayerStart" => println!("Spawn player at ({}, {})", x, y),
            "Exit" => println!("Place exit at ({}, {})", x, y),
            "Treasure" => println!("Place treasure at ({}, {})", x, y),
            _ => {}
        }
    }
    
    println!("Generated {} regions with {} markers", 
             semantic.regions.len(), semantic.markers.len());
}
```

## Marker Connectivity + Path Carving

```rust
use terrain_forge::effects::{connect_markers, clear_rect, MarkerConnectMethod};
use terrain_forge::semantic::MarkerType;
use terrain_forge::{Grid, Rng, SemanticExtractor};

fn main() {
    let mut grid = Grid::new(80, 60);
    terrain_forge::algorithms::get("bsp").unwrap().generate(&mut grid, 42);

    let mut rng = Rng::new(42);
    let semantic = SemanticExtractor::for_rooms().extract(&grid, &mut rng);

    // Clear a buffer around PlayerStart + Exit, then connect them.
    clear_rect(&mut grid, (10, 10), 5, 5);
    clear_rect(&mut grid, (70, 50), 5, 5);
    connect_markers(
        &mut grid,
        &semantic,
        &MarkerType::Custom("PlayerStart".to_string()),
        &MarkerType::Custom("Exit".to_string()),
        MarkerConnectMethod::Line,
        1,
    );
}
```

## Glass Seam Required Terminals

```rust
use terrain_forge::algorithms::{GlassSeam, GlassSeamConfig};
use terrain_forge::Grid;

fn main() {
    let mut grid = Grid::new(80, 60);
    terrain_forge::algorithms::get("drunkard").unwrap().generate(&mut grid, 7);

    let config = GlassSeamConfig {
        coverage_threshold: 0.85,
        required_points: vec![(5, 5), (70, 50)],
        carve_radius: 1,
        use_mst_terminals: true,
    };
    GlassSeam::new(config).generate(&mut grid, 7);
}
```

## Registry API

```rust
use terrain_forge::{Grid, algorithms};

let mut grid = Grid::new(80, 60);

// Get by name
let algo = algorithms::get("cellular").unwrap();
algo.generate(&mut grid, 42);

// List all
for name in algorithms::list() {
    println!("{}", name);
}
```

## Direct Instantiation

```rust
use terrain_forge::{Grid, Algorithm};
use terrain_forge::algorithms::{Bsp, BspConfig};

let config = BspConfig {
    min_room_size: 6,
    max_room_size: 15,
    min_depth: 3,
    max_depth: 8,
    room_ratio: 0.45,
};

let mut grid = Grid::new(80, 60);
let bsp = Bsp::new(config);
bsp.generate(&mut grid, 12345);
```

## Semantic Analysis Types

Generate game-agnostic metadata for entity spawning and region analysis:

```rust
use terrain_forge::{SemanticExtractor, Rng};
use terrain_forge::algorithms::{Bsp, CellularAutomata, Maze};

// Different algorithms provide different semantic insights
let mut grid = Grid::new(80, 60);
let mut rng = Rng::new(12345);

// Cave system analysis
let cellular = CellularAutomata::default();
cellular.generate(&mut grid, 12345);
let semantic = SemanticExtractor::for_caves().extract(&grid, &mut rng);

// Maze structure analysis  
let maze = Maze::default();
maze.generate(&mut grid, 12345);
let semantic = SemanticExtractor::for_mazes().extract(&grid, &mut rng);

// Entity spawning works the same across all algorithms
for (x, y, marker) in &semantic.markers {
    match marker.tag.as_str() {
        "PlayerStart" => spawn_player(x, y),
        "Exit" => place_exit(x, y),
        "Treasure" => place_loot(x, y),
        "Enemy" => spawn_enemy(x, y),
        _ => {}
    }
}
```

## Requirements System

Generate maps that meet specific gameplay requirements:

```rust
use terrain_forge::{
    requirements::{Requirements, ConnectivityRequirement, MarkerRequirement},
    generate_with_requirements, Grid, Rng
};

let mut grid = Grid::new(80, 60);
let mut rng = Rng::new(12345);

let requirements = Requirements::builder()
    .connectivity(ConnectivityRequirement {
        min_largest_region_ratio: 0.8,
        max_disconnected_regions: 2,
    })
    .marker(MarkerRequirement {
        marker_type: "PlayerStart".to_string(),
        min_count: 1,
        max_count: 1,
        min_distance_from_edge: 5,
    })
    .marker(MarkerRequirement {
        marker_type: "Exit".to_string(),
        min_count: 1,
        max_count: 3,
        min_distance_between: 15,
    })
    .build();

match generate_with_requirements(&mut grid, "bsp", requirements, &mut rng) {
    Ok(semantic) => {
        println!("Generated valid map with {} markers", semantic.markers.len());
        for (x, y, marker) in &semantic.markers {
            println!("  {} at ({}, {})", marker.tag, x, y);
        }
    }
    Err(e) => println!("Failed to meet requirements: {}", e),
}
```

## Demo Framework

The demo framework provides extensive examples and testing capabilities:

```bash
# Generate basic algorithms
cargo run --bin demo -- gen bsp -s 12345 -o output.png
cargo run --bin demo -- gen cellular --width 100 --height 80

# Semantic generation with visualization
cargo run --bin demo -- gen room_accretion --semantic --text -o semantic.txt
cargo run --bin demo -- gen bsp --semantic --png -o semantic.png

# Enhanced algorithms
cargo run --bin demo -- gen enhanced_wfc --pattern-size 3 -s 42
cargo run --bin demo -- gen prefab --library examples/prefabs.json

# Spatial analysis
cargo run --bin demo -- spatial distance_transform --metric euclidean
cargo run --bin demo -- spatial dijkstra --goals "10,10;40,30"

# NEW: Semantic configuration files
cargo run -- run configs/semantic_bsp.json
cargo run -- run configs/semantic_large_rooms.json
cargo run -- run configs/semantic_organic.json

# Compare algorithms
cargo run -- compare bsp cellular maze -s 12345

# List available algorithms
cargo run -- list
```

## Algorithm Parameters

Each algorithm supports extensive configuration:

### BSP (Binary Space Partitioning)
```rust
BspConfig {
    min_room_size: 6,      // Minimum room dimensions
    max_room_size: 15,     // Maximum room dimensions  
    min_depth: 3,          // Minimum tree depth
    max_depth: 8,          // Maximum tree depth
    room_ratio: 0.45,      // Ratio of leaf nodes that become rooms
}
```

### Cellular Automata
```rust
CellularConfig {
    initial_density: 0.45,  // Initial wall probability
    iterations: 5,          // Smoothing iterations
    birth_limit: 4,         // Neighbors needed for wall birth
    death_limit: 3,         // Neighbors needed for wall survival
}
```

### Wave Function Collapse
```rust
WfcConfig {
    pattern_size: 3,           // Pattern dimensions (3x3)
    enable_backtracking: true, // Allow constraint backtracking
    max_attempts: 1000,        // Maximum generation attempts
}
```

### Glass Seam Bridging
```rust
GlassSeamParams {
    ct: 0.75,                   // Connectivity threshold
    min_area_ratio: 0.05,       // Minimum area ratio filter
    use_pgd: true,              // Enable Perimeter Gradient Descent
    n_skew: 2,                  // PGD diagonal search width
    max_pgd_iterations: 20,     // PGD iteration limit
    use_delaunay: true,         // Enable Delaunay triangulation filter
    angular_sectors: 6,         // Directional sectors per vertex
    occlusion_factor: 1.2,      // Indirect path tolerance
    max_edge_distance: 100.0,   // Skip distant area pairs
}
```

## Performance Considerations

- **Grid Size**: Larger grids increase generation time exponentially for some algorithms
- **Algorithm Choice**: BSP and Rooms are fastest, WFC and Agent-based are slower
- **Semantic Analysis**: Adds ~10-20% overhead but provides valuable metadata
- **Caching**: Reuse `SemanticExtractor` instances for better performance
- **Parallel Generation**: Use multiple threads for batch generation

## Integration Examples

### Bevy Game Engine
```rust
use bevy::prelude::*;
use terrain_forge::{Grid, algorithms};

fn setup_terrain(mut commands: Commands) {
    let mut grid = Grid::new(100, 100);
    algorithms::get("bsp").unwrap().generate(&mut grid, 12345);
    
    // Convert to Bevy entities
    for y in 0..grid.height() {
        for x in 0..grid.width() {
            if grid[(x, y)].is_floor() {
                commands.spawn(SpriteBundle {
                    transform: Transform::from_xyz(x as f32, y as f32, 0.0),
                    // ... sprite configuration
                    ..default()
                });
            }
        }
    }
}
```

### Custom Game Loop
```rust
use terrain_forge::{Grid, SemanticExtractor, Rng};

struct GameMap {
    grid: Grid<terrain_forge::Tile>,
    entities: Vec<Entity>,
}

impl GameMap {
    fn generate(width: usize, height: usize, seed: u64) -> Self {
        let mut grid = Grid::new(width, height);
        let mut rng = Rng::new(seed);
        
        // Generate terrain
        terrain_forge::algorithms::get("room_accretion")
            .unwrap()
            .generate(&mut grid, seed);
        
        // Extract semantic information
        let semantic = SemanticExtractor::for_rooms()
            .extract(&grid, &mut rng);
        
        // Create entities from markers
        let entities = semantic.markers.iter()
            .map(|(x, y, marker)| Entity::new(*x, *y, &marker.tag))
            .collect();
        
        Self { grid, entities }
    }
}
```