-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdata_processing.rs
More file actions
552 lines (509 loc) · 21.9 KB
/
data_processing.rs
File metadata and controls
552 lines (509 loc) · 21.9 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use crate::args::Args;
use crate::coordinate_system::cartesian::XZBBox;
use crate::coordinate_system::geographic::LLBBox;
use crate::element_processing::*;
use crate::floodfill_cache::FloodFillCache;
use crate::ground::Ground;
use crate::ground_generation;
use crate::map_renderer;
use crate::osm_parser::{OutlineSuppression, ProcessedElement};
use crate::progress::{emit_gui_progress_update, emit_map_preview_ready, emit_show_in_folder};
#[cfg(feature = "gui")]
use crate::telemetry::{send_log, LogLevel};
use crate::world_editor::{WorldEditor, WorldFormat};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
/// Generation options that can be passed separately from CLI Args
#[derive(Clone)]
pub struct GenerationOptions {
pub path: PathBuf,
pub format: WorldFormat,
pub level_name: Option<String>,
pub spawn_point: Option<(i32, i32)>,
pub luanti_game: Option<crate::luanti_block_map::LuantiGame>,
pub ground_level: i32,
}
/// Generate world with explicit format options (used by GUI for Bedrock support)
pub fn generate_world_with_options(
elements: Vec<ProcessedElement>,
xzbbox: XZBBox,
llbbox: LLBBox,
ground: Ground,
args: &Args,
options: GenerationOptions,
outline_suppression: OutlineSuppression,
) -> Result<PathBuf, String> {
let output_path = options.path.clone();
let world_format = options.format;
let generation_start = args.benchmark.then(std::time::Instant::now);
// Create editor with appropriate format
let mut editor: WorldEditor = if options.format == WorldFormat::LuantiWorld {
WorldEditor::new_luanti(
options.path,
&xzbbox,
llbbox,
options
.luanti_game
.unwrap_or(crate::luanti_block_map::LuantiGame::Mineclonia),
options.level_name.clone(),
options.spawn_point,
options.ground_level,
)
} else {
WorldEditor::new_with_format_and_name(
options.path,
&xzbbox,
llbbox,
options.format,
options.level_name.clone(),
options.spawn_point,
args.disable_height_limit,
)
};
editor.set_bake_lighting(args.bake_lighting);
let ground = Arc::new(ground);
// Per-cell water depth field from the LC_WATER mask; empty without land cover.
let big_water_field = crate::water_depth::compute_big_water_field(&ground, &xzbbox);
println!("{} Processing data...", "[4/7]".bold());
// Build highway connectivity map once before processing
let highway_connectivity = highways::build_highway_connectivity_map(&elements);
// Collect subway centerline points for post-ground-fill air carving (phase 2).
let mut subway_points: Vec<(i32, i32)> = Vec::new();
// Set ground reference in the editor to enable elevation-aware block placement
editor.set_ground(Arc::clone(&ground));
println!("{} Processing terrain...", "[5/7]".bold());
emit_gui_progress_update(25.0, "Processing terrain...");
// Pre-compute all flood fills in parallel for better CPU utilization
let mut flood_fill_cache = FloodFillCache::precompute(&elements, args.timeout.as_ref());
// Collect building footprints to prevent trees from spawning inside buildings
// Uses a memory-efficient bitmap (~1 bit per coordinate) instead of a HashSet (~24 bytes per coordinate)
let building_footprints = flood_fill_cache.collect_building_footprints(&elements, &xzbbox);
// Collect coordinates covered by tunnel=building_passage highways so that
// building generation can cut ground-level openings through walls and floors.
let building_passages =
highways::collect_building_passage_coords(&elements, &xzbbox, args.scale);
// Pre-build a bitmap of every (x, z) block coordinate covered by a rendered
// road or path surface. Uses the same Bresenham + block_range geometry as
// generate_highways_internal, so the bitmap is a 1:1 match of what gets placed.
// Amenity processors use this for O(1) nearest-road-block lookups.
let road_mask = highways::collect_road_surface_coords(&elements, &xzbbox, args.scale);
let bridge_outlines =
crate::element_processing::bridge_styles::BridgeOutlineIndex::build(&elements);
let bridge_structures =
bridges::BridgeStructureMap::build(&elements, &editor, &bridge_outlines);
let bridge_surface =
bridges::BridgeSurfaceMap::build(&elements, &bridge_structures, args.scale);
let rail_bridge_internal_endpoints =
railways::collect_rail_bridge_internal_endpoints(&elements);
// Process all elements (no longer need to partition boundaries)
let elements_count: usize = elements.len();
let process_pb: ProgressBar = ProgressBar::new(elements_count as u64);
process_pb.set_style(ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:45.white/black}] {pos}/{len} elements ({eta}) {msg}")
.unwrap()
.progress_chars("█▓░"));
let progress_increment_prcs: f64 = 45.0 / elements_count as f64;
let mut current_progress_prcs: f64 = 25.0;
let mut last_emitted_progress: f64 = current_progress_prcs;
let desired_updates: u64 = 500;
let pb_batch_size: u64 = (elements_count as u64 / desired_updates).max(1);
let mut element_counter: u64 = 0;
let models_3d_pipeline = args
.use_3d
.then(|| crate::models_3d::Models3dPipeline::prescan(&elements, args));
let empty_suppressed: HashSet<(&'static str, u64)> = HashSet::new();
let models_3d_suppressed: &HashSet<(&'static str, u64)> = models_3d_pipeline
.as_ref()
.map(|p| p.suppressed())
.unwrap_or(&empty_suppressed);
// Process all elements
for element in elements.into_iter() {
element_counter += 1;
let suppression_key = (element.kind(), element.id());
if models_3d_suppressed.contains(&suppression_key)
|| outline_suppression.contains(&suppression_key)
{
continue;
}
if element_counter.is_multiple_of(pb_batch_size) {
process_pb.inc(pb_batch_size);
}
current_progress_prcs += progress_increment_prcs;
if (current_progress_prcs - last_emitted_progress).abs() > 0.25 {
emit_gui_progress_update(current_progress_prcs, "");
last_emitted_progress = current_progress_prcs;
}
if args.debug {
process_pb.set_message(format!(
"(Element ID: {} / Type: {})",
element.id(),
element.kind()
));
} else {
// Clear on every non-debug iteration so any transient warning
// message set by downstream element processing (missing nodes,
// etc.) doesn't stick for the rest of the run. The cost is
// trivial — indicatif's set_message is just a mutex + string
// compare (~20 ns), ~40 ms over a full world.
process_pb.set_message("");
}
match &element {
ProcessedElement::Way(way) => {
if way.tags.contains_key("building") || way.tags.contains_key("building:part") {
buildings::generate_buildings(
&mut editor,
way,
args,
None,
None,
&flood_fill_cache,
&building_passages,
);
} else if way.tags.contains_key("highway") {
highways::generate_highways(
&mut editor,
&element,
args,
&highway_connectivity,
&flood_fill_cache,
&road_mask,
&bridge_structures,
&bridge_surface,
);
} else if way.tags.contains_key("landuse") {
landuse::generate_landuse(
&mut editor,
way,
args,
&flood_fill_cache,
&building_footprints,
);
} else if way.tags.contains_key("natural") {
natural::generate_natural(
&mut editor,
&element,
args,
&flood_fill_cache,
&building_footprints,
);
} else if way.tags.contains_key("amenity") {
amenities::generate_amenities(
&mut editor,
&element,
args,
&flood_fill_cache,
&road_mask,
);
} else if way.tags.contains_key("leisure") {
leisure::generate_leisure(
&mut editor,
way,
args,
&flood_fill_cache,
&building_footprints,
);
} else if way.tags.contains_key("barrier") {
barriers::generate_barriers(&mut editor, &element, &bridge_surface);
} else if let Some(val) = way.tags.get("waterway") {
if val == "dock" {
// docks count as water areas
water_areas::generate_water_area_from_way(
&mut editor,
way,
&xzbbox,
&big_water_field,
&road_mask,
);
} else {
waterways::generate_waterways(&mut editor, way);
}
} else if way.tags.contains_key("railway") {
railways::generate_railways(
&mut editor,
way,
&mut subway_points,
&rail_bridge_internal_endpoints,
&bridge_outlines,
);
} else if way.tags.contains_key("roller_coaster") {
railways::generate_roller_coaster(&mut editor, way);
} else if way.tags.contains_key("aeroway") || way.tags.contains_key("area:aeroway")
{
highways::generate_aeroway(&mut editor, way, args);
} else if way.tags.get("service") == Some(&"siding".to_string()) {
highways::generate_siding(&mut editor, way, &bridge_surface);
} else if way.tags.get("tomb") == Some(&"pyramid".to_string()) {
historic::generate_pyramid(&mut editor, way, args, &flood_fill_cache);
} else if way.tags.contains_key("man_made") {
man_made::generate_man_made(&mut editor, &element, args);
} else if way.tags.contains_key("power") {
power::generate_power(&mut editor, &element);
} else if way.tags.contains_key("place") {
landuse::generate_place(&mut editor, way, args, &flood_fill_cache);
}
// Release flood fill cache entry for this way
flood_fill_cache.remove_way(way.id);
}
ProcessedElement::Node(node) => {
if node.tags.contains_key("door") || node.tags.contains_key("entrance") {
doors::generate_doors(&mut editor, node);
} else if node.tags.contains_key("natural")
&& node.tags.get("natural") == Some(&"tree".to_string())
{
natural::generate_natural(
&mut editor,
&element,
args,
&flood_fill_cache,
&building_footprints,
);
} else if node.tags.contains_key("amenity") {
amenities::generate_amenities(
&mut editor,
&element,
args,
&flood_fill_cache,
&road_mask,
);
} else if node.tags.contains_key("barrier") {
barriers::generate_barrier_nodes(&mut editor, node, &bridge_surface);
} else if node.tags.contains_key("highway") {
highways::generate_highways(
&mut editor,
&element,
args,
&highway_connectivity,
&flood_fill_cache,
&road_mask,
&bridge_structures,
&bridge_surface,
);
} else if node.tags.contains_key("tourism") {
tourisms::generate_tourisms(&mut editor, node);
} else if node.tags.contains_key("man_made") {
man_made::generate_man_made_nodes(&mut editor, node, args);
} else if node.tags.contains_key("power") {
power::generate_power_nodes(&mut editor, node);
} else if node.tags.contains_key("historic") {
historic::generate_historic(&mut editor, node);
} else if node.tags.contains_key("emergency") {
emergency::generate_emergency(&mut editor, node);
} else if node.tags.contains_key("advertising") {
advertising::generate_advertising(&mut editor, node);
}
}
ProcessedElement::Relation(rel) => {
let is_building_relation = rel.tags.contains_key("building")
|| rel.tags.contains_key("building:part")
|| rel.tags.get("type").map(|t| t.as_str()) == Some("building");
if is_building_relation {
buildings::generate_building_from_relation(
&mut editor,
rel,
args,
&flood_fill_cache,
&xzbbox,
&building_passages,
);
} else if rel.tags.contains_key("water")
|| rel
.tags
.get("natural")
.map(|val| val == "water" || val == "bay")
.unwrap_or(false)
{
water_areas::generate_water_areas_from_relation(
&mut editor,
rel,
&xzbbox,
&big_water_field,
&road_mask,
);
} else if rel.tags.contains_key("natural") {
natural::generate_natural_from_relation(
&mut editor,
rel,
args,
&flood_fill_cache,
&building_footprints,
);
} else if rel.tags.contains_key("landuse") {
landuse::generate_landuse_from_relation(
&mut editor,
rel,
args,
&flood_fill_cache,
&building_footprints,
);
} else if rel.tags.get("leisure") == Some(&"park".to_string()) {
leisure::generate_leisure_from_relation(
&mut editor,
rel,
args,
&flood_fill_cache,
&building_footprints,
);
} else if rel.tags.contains_key("man_made") {
man_made::generate_man_made(&mut editor, &element, args);
}
// Release flood fill cache entries for all ways in this relation
let way_ids: Vec<u64> = rel.members.iter().map(|m| m.way.id).collect();
flood_fill_cache.remove_relation_ways(&way_ids);
}
}
// Element is dropped here, freeing its memory immediately
}
process_pb.inc(element_counter % pb_batch_size);
process_pb.finish();
// Keep road_mask alive for the LC_WATER carve below.
drop(highway_connectivity);
drop(flood_fill_cache);
// Generate ground layer (surface blocks, vegetation, shorelines, underground fill)
ground_generation::generate_ground_layer(
&mut editor,
ground.as_ref(),
args,
&xzbbox,
&building_footprints,
)?;
if args.fillground {
crate::ore_generation::generate_ores(&mut editor, &xzbbox);
}
// Carve depth into ESA water cells (water_areas.rs only covers OSM polygons).
crate::water_depth::carve_lc_water_pass(
&mut editor,
ground.as_ref(),
&xzbbox,
&big_water_field,
&road_mask,
);
drop(road_mask);
// Carve subway tunnel interiors now that underground is filled with stone.
// This must happen after ground generation so AIR blocks are not overwritten.
if !subway_points.is_empty() {
railways::carve_subway_interior(&mut editor, &subway_points);
}
// Run after ground generation so anchor Y reflects the final terrain.
if let Some(p) = models_3d_pipeline.as_ref() {
p.place(&mut editor, args);
}
// Save world
if let Err(e) = editor.save() {
return Err(e.to_string());
}
if let Some(start) = generation_start {
let gen_ms = start.elapsed().as_millis();
eprintln!("[BENCHMARK] generation_time_ms={gen_ms}");
}
emit_gui_progress_update(99.0, "Finalizing world...");
// Update player spawn Y coordinate based on terrain height after generation
#[cfg(feature = "gui")]
if world_format == WorldFormat::JavaAnvil {
use crate::gui::update_player_spawn_y_after_generation;
// Reconstruct bbox string to match the format that GUI originally provided.
// This ensures LLBBox::from_str() can parse it correctly.
let bbox_string = format!(
"{},{},{},{}",
args.bbox.min().lat(),
args.bbox.min().lng(),
args.bbox.max().lat(),
args.bbox.max().lng()
);
// Always update spawn Y since we now always set a spawn point (user-selected or default)
if let Some(ref world_path) = args.path {
if let Err(e) = update_player_spawn_y_after_generation(
world_path,
bbox_string,
args.scale,
ground.as_ref(),
) {
let warning_msg = format!("Failed to update spawn point Y coordinate: {}", e);
eprintln!("Warning: {}", warning_msg);
#[cfg(feature = "gui")]
send_log(LogLevel::Warning, &warning_msg);
}
}
}
// For Bedrock format, emit event to open the mcworld file
if world_format == WorldFormat::BedrockMcWorld {
if let Some(path_str) = output_path.to_str() {
emit_show_in_folder(path_str);
}
}
// For Java worlds saved to the Desktop (GUI falls back there when .minecraft/saves
// is missing), open the folder in the file explorer so the user can find the world.
if world_format == WorldFormat::JavaAnvil {
if let Some(desktop) = dirs::desktop_dir() {
if output_path.starts_with(&desktop) {
if let Some(path_str) = output_path.to_str() {
emit_show_in_folder(path_str);
}
}
}
}
Ok(output_path)
}
/// Information needed to generate a map preview after world generation is complete
#[derive(Clone)]
pub struct MapPreviewInfo {
pub world_path: PathBuf,
pub min_x: i32,
pub max_x: i32,
pub min_z: i32,
pub max_z: i32,
pub world_area: i64,
}
impl MapPreviewInfo {
/// Create MapPreviewInfo from world bounds
pub fn new(world_path: PathBuf, xzbbox: &XZBBox) -> Self {
let world_width = (xzbbox.max_x() - xzbbox.min_x()) as i64;
let world_height = (xzbbox.max_z() - xzbbox.min_z()) as i64;
Self {
world_path,
min_x: xzbbox.min_x(),
max_x: xzbbox.max_x(),
min_z: xzbbox.min_z(),
max_z: xzbbox.max_z(),
world_area: world_width * world_height,
}
}
}
/// Maximum area for which map preview generation is allowed (to avoid memory issues)
pub const MAX_MAP_PREVIEW_AREA: i64 = 6400 * 6900;
/// Start map preview generation in a background thread.
/// This should be called AFTER the world generation is complete, the session lock is released,
/// and the GUI has been notified of 100% completion.
///
/// For Java worlds only, and only if the world area is within limits.
pub fn start_map_preview_generation(info: MapPreviewInfo) {
if info.world_area > MAX_MAP_PREVIEW_AREA {
return;
}
std::thread::spawn(move || {
// Use catch_unwind to prevent any panic from affecting the application
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
map_renderer::render_world_map(
&info.world_path,
info.min_x,
info.max_x,
info.min_z,
info.max_z,
)
}));
match result {
Ok(Ok(_path)) => {
// Notify the GUI that the map preview is ready
emit_map_preview_ready();
}
Ok(Err(e)) => {
eprintln!("Warning: Failed to generate map preview: {}", e);
}
Err(_) => {
eprintln!("Warning: Map preview generation panicked unexpectedly");
}
}
});
}