-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathlanduse.rs
More file actions
483 lines (466 loc) · 21.6 KB
/
Copy pathlanduse.rs
File metadata and controls
483 lines (466 loc) · 21.6 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
use crate::args::Args;
use crate::block_definitions::*;
use crate::bresenham::bresenham_line;
use crate::deterministic_rng::element_rng;
use crate::element_processing::tree::{Tree, TreeType};
use crate::floodfill_cache::{BuildingFootprintBitmap, FloodFillCache};
use crate::osm_parser::{ProcessedMemberRole, ProcessedRelation, ProcessedWay};
use crate::world_editor::WorldEditor;
use rand::prelude::IndexedRandom;
use rand::Rng;
pub fn generate_landuse(
editor: &mut WorldEditor,
element: &ProcessedWay,
args: &Args,
flood_fill_cache: &FloodFillCache,
building_footprints: &BuildingFootprintBitmap,
) {
// Determine block type based on landuse tag
let binding: String = "".to_string();
let landuse_tag: &String = element.tags.get("landuse").unwrap_or(&binding);
// Use deterministic RNG seeded by element ID for consistent results across region boundaries
let mut rng = element_rng(element.id);
let block_type = match landuse_tag.as_str() {
"greenfield" | "meadow" | "grass" | "orchard" | "forest" => GRASS_BLOCK,
"farmland" => FARMLAND,
"cemetery" => PODZOL,
"construction" => COARSE_DIRT,
"traffic_island" => STONE_BLOCK_SLAB,
// residential and commercial are too broad, they cover entire zones including
// gardens, parks, and green spaces. ESA WorldCover handles built-up classification
// at 10m satellite resolution, which is far more precise.
"residential" | "commercial" => return,
"education" => POLISHED_ANDESITE,
"religious" => POLISHED_ANDESITE,
"industrial" => STONE, // Randomized per-block below
"military" => GRAY_CONCRETE, // Randomized per-block below
"railway" => GRAVEL,
"vineyard" => COARSE_DIRT,
"brownfield" => COARSE_DIRT,
"landfill" => {
// Gravel if man_made = spoil_heap or heap, coarse dirt else
let manmade_tag = element.tags.get("man_made").unwrap_or(&binding);
if manmade_tag == "spoil_heap" || manmade_tag == "heap" {
GRAVEL
} else {
COARSE_DIRT
}
}
"quarry" => STONE, // Randomized per-block below
_ => GRASS_BLOCK,
};
// Get the area of the landuse element using cache
let floor_area = flood_fill_cache.get_or_compute(element, args.timeout.as_ref());
// Cherry/FloweringOak only via the random Tree::create pool (rare).
let trees_ok_to_generate: Vec<TreeType> = {
let mut trees: Vec<TreeType> = vec![];
if let Some(leaf_type) = element.tags.get("leaf_type") {
match leaf_type.as_str() {
"broadleaved" => {
trees.push(TreeType::Oak);
trees.push(TreeType::Birch);
trees.push(TreeType::TallOak);
trees.push(TreeType::Bush);
trees.push(TreeType::AzaleaBush);
}
"needleleaved" => {
trees.push(TreeType::Spruce);
trees.push(TreeType::Pine);
}
_ => {
trees.push(TreeType::Oak);
trees.push(TreeType::Spruce);
trees.push(TreeType::Birch);
trees.push(TreeType::TallOak);
trees.push(TreeType::Pine);
trees.push(TreeType::Bush);
trees.push(TreeType::AzaleaBush);
trees.push(TreeType::Willow);
}
}
} else {
trees.push(TreeType::Oak);
trees.push(TreeType::Spruce);
trees.push(TreeType::Birch);
trees.push(TreeType::TallOak);
trees.push(TreeType::Pine);
trees.push(TreeType::Bush);
trees.push(TreeType::AzaleaBush);
}
trees
};
for &(x, z) in floor_area.iter() {
// Apply per-block randomness for certain landuse types
let actual_block = if landuse_tag == "industrial" {
// Industrial: primarily stone, with some stone bricks and smooth stone
let random_value = rng.random_range(0..100);
if random_value < 70 {
STONE
} else if random_value < 90 {
STONE_BRICKS
} else {
SMOOTH_STONE
}
} else if landuse_tag == "military" {
// Military: primarily gray concrete, with some stone bricks and cobblestone
let random_value = rng.random_range(0..100);
if random_value < 89 {
GRAY_CONCRETE
} else if random_value < 99 {
STONE_BRICKS
} else {
COBBLESTONE
}
} else if landuse_tag == "quarry" {
// Quarry: mix of stone, gravel, cobblestone, andesite
let random_value = rng.random_range(0..100);
if random_value < 40 {
STONE
} else if random_value < 60 {
GRAVEL
} else if random_value < 80 {
COBBLESTONE
} else {
ANDESITE
}
} else {
block_type
};
// Don't overwrite roads or water with landuse ground blocks
let is_protected = editor.check_for_block(
x,
0,
z,
Some(&[
BLACK_CONCRETE,
GRAY_CONCRETE_POWDER,
CYAN_TERRACOTTA,
GRAY_CONCRETE,
LIGHT_GRAY_CONCRETE,
WHITE_CONCRETE,
DIRT_PATH,
SMOOTH_STONE,
WATER,
]),
);
if landuse_tag == "traffic_island" {
editor.set_block(actual_block, x, 1, z, None, None);
} else if landuse_tag == "construction" || landuse_tag == "railway" {
editor.set_block(actual_block, x, 0, z, None, Some(&[SPONGE]));
} else if !is_protected {
editor.set_block(actual_block, x, 0, z, None, None);
}
// Add specific features for different landuse types
match landuse_tag.as_str() {
"cemetery" if (x % 3 == 0) && (z % 3 == 0) => {
let random_choice: i32 = rng.random_range(0..100);
if random_choice < 15 {
// Place graves
if editor.check_for_block(x, 0, z, Some(&[PODZOL])) {
if rng.random_bool(0.5) {
editor.set_block(COBBLESTONE, x - 1, 1, z, None, None);
editor.set_block(STONE_BRICK_SLAB, x - 1, 2, z, None, None);
editor.set_block(STONE_BRICK_SLAB, x, 1, z, None, None);
editor.set_block(STONE_BRICK_SLAB, x + 1, 1, z, None, None);
} else {
editor.set_block(COBBLESTONE, x, 1, z - 1, None, None);
editor.set_block(STONE_BRICK_SLAB, x, 2, z - 1, None, None);
editor.set_block(STONE_BRICK_SLAB, x, 1, z, None, None);
editor.set_block(STONE_BRICK_SLAB, x, 1, z + 1, None, None);
}
}
} else if random_choice < 30 {
if editor.check_for_block(x, 0, z, Some(&[PODZOL])) {
editor.set_block(RED_FLOWER, x, 1, z, None, None);
}
} else if random_choice < 33 {
Tree::create(editor, (x, 1, z), Some(building_footprints));
} else if !is_protected && random_choice < 35 {
editor.set_block(OAK_LEAVES, x, 1, z, None, None);
} else if !is_protected && random_choice < 37 {
editor.set_block(FERN, x, 1, z, None, None);
} else if !is_protected && random_choice < 41 {
editor.set_block(LARGE_FERN_LOWER, x, 1, z, None, None);
editor.set_block(LARGE_FERN_UPPER, x, 2, z, None, None);
}
}
"forest" if editor.check_for_block(x, 0, z, Some(&[GRASS_BLOCK])) => {
// Density-modulated spawn: thickets in some patches, clearings in others.
let density = crate::ground_generation::value_noise_01(x, z, 32);
let tree_threshold = ((60.0 - density * 45.0) as i32).max(5);
if rng.random_range(0..tree_threshold) == 0 {
let tree_type = *trees_ok_to_generate
.choose(&mut rng)
.unwrap_or(&TreeType::Oak);
Tree::create_of_type(editor, (x, 1, z), tree_type, Some(building_footprints));
} else {
let random_choice: i32 = rng.random_range(0..30);
if random_choice == 2 {
let flower_block: Block = match rng.random_range(1..=6) {
1 => OAK_LEAVES,
2 => RED_FLOWER,
3 => BLUE_FLOWER,
4 => YELLOW_FLOWER,
5 => FERN,
_ => WHITE_FLOWER,
};
editor.set_block(flower_block, x, 1, z, None, None);
} else if random_choice <= 12 {
if rng.random_range(0..100) < 12 {
editor.set_block(FERN, x, 1, z, None, None);
} else {
editor.set_block(GRASS, x, 1, z, None, None);
}
}
}
}
"farmland" if !editor.check_for_block(x, 0, z, Some(&[WATER])) => {
// Check if the current block is not water or another undesired block
if x % 9 == 0 && z % 9 == 0 {
// Place water in dot pattern
editor.set_block(WATER, x, 0, z, Some(&[FARMLAND]), None);
} else if rng.random_range(0..76) == 0 {
let special_choice: i32 = rng.random_range(1..=10);
if special_choice <= 4 {
editor.set_block(HAY_BALE, x, 1, z, None, Some(&[SPONGE]));
} else {
editor.set_block(OAK_LEAVES, x, 1, z, None, Some(&[SPONGE]));
}
} else {
// Set crops only if the block below is farmland
if editor.check_for_block(x, 0, z, Some(&[FARMLAND])) {
let crop_choice = [WHEAT, CARROTS, POTATOES][rng.random_range(0..3)];
editor.set_block(crop_choice, x, 1, z, None, None);
}
}
}
"construction" => {
let random_choice: i32 = rng.random_range(0..1501);
if random_choice < 15 {
editor.set_block(SCAFFOLDING, x, 1, z, None, None);
if random_choice < 2 {
editor.set_block(SCAFFOLDING, x, 2, z, None, None);
editor.set_block(SCAFFOLDING, x, 3, z, None, None);
} else if random_choice < 4 {
editor.set_block(SCAFFOLDING, x, 2, z, None, None);
editor.set_block(SCAFFOLDING, x, 3, z, None, None);
editor.set_block(SCAFFOLDING, x, 4, z, None, None);
editor.set_block(SCAFFOLDING, x, 1, z + 1, None, None);
} else {
editor.set_block(SCAFFOLDING, x, 2, z, None, None);
editor.set_block(SCAFFOLDING, x, 3, z, None, None);
editor.set_block(SCAFFOLDING, x, 4, z, None, None);
editor.set_block(SCAFFOLDING, x, 5, z, None, None);
editor.set_block(SCAFFOLDING, x - 1, 1, z, None, None);
editor.set_block(SCAFFOLDING, x + 1, 1, z - 1, None, None);
}
} else if random_choice < 55 {
let construction_items: [Block; 13] = [
OAK_LOG,
COBBLESTONE,
GRAVEL,
GLOWSTONE,
STONE,
COBBLESTONE_WALL,
BLACK_CONCRETE,
SAND,
OAK_PLANKS,
DIRT,
BRICK,
CRAFTING_TABLE,
FURNACE,
];
editor.set_block(
construction_items[rng.random_range(0..construction_items.len())],
x,
1,
z,
None,
None,
);
} else if random_choice < 65 {
if random_choice < 60 {
editor.set_block(DIRT, x, 1, z, None, None);
editor.set_block(DIRT, x, 2, z, None, None);
editor.set_block(DIRT, x + 1, 1, z, None, None);
editor.set_block(DIRT, x, 1, z + 1, None, None);
} else {
editor.set_block(DIRT, x, 1, z, None, None);
editor.set_block(DIRT, x, 2, z, None, None);
editor.set_block(DIRT, x - 1, 1, z, None, None);
editor.set_block(DIRT, x, 1, z - 1, None, None);
}
} else if random_choice < 100 {
editor.set_block(GRAVEL, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 115 {
editor.set_block(SAND, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 125 {
editor.set_block(DIORITE, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 145 {
editor.set_block(BRICK, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 155 {
editor.set_block(GRANITE, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 180 {
editor.set_block(ANDESITE, x, 0, z, None, Some(&[SPONGE]));
} else if random_choice < 565 {
editor.set_block(COBBLESTONE, x, 0, z, None, Some(&[SPONGE]));
}
}
"grass" if editor.check_for_block(x, 0, z, Some(&[GRASS_BLOCK])) => {
match rng.random_range(0..200) {
0 => editor.set_block(OAK_LEAVES, x, 1, z, None, None),
1..=8 => editor.set_block(FERN, x, 1, z, None, None),
9..=170 => editor.set_block(GRASS, x, 1, z, None, None),
_ => {}
}
}
"greenfield" if editor.check_for_block(x, 0, z, Some(&[GRASS_BLOCK])) => {
match rng.random_range(0..200) {
0 => editor.set_block(OAK_LEAVES, x, 1, z, None, None),
1..=2 => editor.set_block(FERN, x, 1, z, None, None),
3..=16 => editor.set_block(GRASS, x, 1, z, None, None),
_ => {}
}
}
"meadow" if editor.check_for_block(x, 0, z, Some(&[GRASS_BLOCK])) => {
let random_choice: i32 = rng.random_range(0..1001);
if random_choice < 5 {
Tree::create(editor, (x, 1, z), Some(building_footprints));
} else if random_choice < 6 {
editor.set_block(RED_FLOWER, x, 1, z, None, None);
} else if random_choice < 9 {
editor.set_block(OAK_LEAVES, x, 1, z, None, None);
} else if random_choice < 40 {
editor.set_block(FERN, x, 1, z, None, None);
} else if random_choice < 65 {
editor.set_block(LARGE_FERN_LOWER, x, 1, z, None, None);
editor.set_block(LARGE_FERN_UPPER, x, 2, z, None, None);
} else if random_choice < 825 {
editor.set_block(GRASS, x, 1, z, None, None);
}
}
"orchard" => {
if x % 18 == 0 && z % 10 == 0 {
Tree::create(editor, (x, 1, z), Some(building_footprints));
} else if editor.check_for_block(x, 0, z, Some(&[GRASS_BLOCK])) {
match rng.random_range(0..100) {
0 => editor.set_block(OAK_LEAVES, x, 1, z, None, None),
1..=2 => editor.set_block(FERN, x, 1, z, None, None),
3..=20 => editor.set_block(GRASS, x, 1, z, None, None),
_ => {}
}
}
}
"vineyard" | "brownfield" | "landfill"
if editor.check_for_block(x, 0, z, Some(&[COARSE_DIRT])) =>
{
// Sparse weeds/regrowth on coarse-dirt surfaces: vineyard rows
// grow some grass between vines, and brownfield/landfill are
// abandoned land that nature is slowly reclaiming. Kept rare so
// the ground still reads as dry/disturbed rather than meadow.
// (Skipped for landfill spoil heaps — those are GRAVEL, not
// COARSE_DIRT, and the guard above filters them out.)
match rng.random_range(0..150) {
0..=3 => editor.set_block(OAK_LEAVES, x, 1, z, None, None),
4 => editor.set_block(DEAD_BUSH, x, 1, z, None, None),
5..=15 => editor.set_block(GRASS, x, 1, z, None, None),
_ => {}
}
}
"quarry" => {
// Add stone layer under it
editor.set_block(STONE, x, -1, z, Some(&[STONE]), None);
editor.set_block(STONE, x, -2, z, Some(&[STONE]), None);
// Generate ore blocks
if let Some(resource) = element.tags.get("resource") {
let ore_block = match resource.as_str() {
"iron_ore" => IRON_ORE,
"coal" => COAL_ORE,
"copper" => COPPER_ORE,
"gold" => GOLD_ORE,
"clay" | "kaolinite" => CLAY,
_ => STONE,
};
let random_choice: i32 =
rng.random_range(0..100 + editor.get_absolute_y(x, 0, z)); // The deeper it is the more resources are there
if random_choice < 5 {
editor.set_block(ore_block, x, 0, z, Some(&[STONE]), None);
}
}
}
_ => {}
}
}
// Generate a stone brick wall fence around cemeteries
if landuse_tag == "cemetery" {
generate_cemetery_fence(editor, element);
}
}
/// Draws a stone-brick wall fence (with slab cap) along the outline of a
/// cemetery way.
fn generate_cemetery_fence(editor: &mut WorldEditor, element: &ProcessedWay) {
for i in 1..element.nodes.len() {
let prev = &element.nodes[i - 1];
let cur = &element.nodes[i];
let points = bresenham_line(prev.x, 0, prev.z, cur.x, 0, cur.z);
for (bx, _, bz) in points {
editor.set_block(STONE_BRICK_WALL, bx, 1, bz, None, None);
editor.set_block(STONE_BRICK_SLAB, bx, 2, bz, None, None);
}
}
}
pub fn generate_landuse_from_relation(
editor: &mut WorldEditor,
rel: &ProcessedRelation,
args: &Args,
flood_fill_cache: &FloodFillCache,
building_footprints: &BuildingFootprintBitmap,
) {
if rel.tags.contains_key("landuse") {
// Process each outer member way individually using cached flood fill.
// We intentionally do not combine all outer nodes into one mega-way,
// because that creates a nonsensical polygon spanning the whole relation
// extent, misses the flood fill cache, and can cause multi-GB allocations.
for member in &rel.members {
if member.role == ProcessedMemberRole::Outer {
// Use relation tags so the member inherits the relation's landuse=* type
let way_with_rel_tags = ProcessedWay {
id: member.way.id,
nodes: member.way.nodes.clone(),
tags: rel.tags.clone(),
};
generate_landuse(
editor,
&way_with_rel_tags,
args,
flood_fill_cache,
building_footprints,
);
}
}
}
}
/// Generates ground blocks for place=* areas (squares, neighbourhoods, etc.)
pub fn generate_place(
editor: &mut WorldEditor,
element: &ProcessedWay,
args: &Args,
flood_fill_cache: &FloodFillCache,
) {
let binding = String::new();
let place_tag = element.tags.get("place").unwrap_or(&binding);
// Determine block type based on place tag
let block_type = match place_tag.as_str() {
"square" => STONE_BRICKS,
// neighbourhood/city_block/quarter/suburb are too broad, ESA WorldCover
// land cover data handles built-up classification at 10m resolution instead
"neighbourhood" | "city_block" | "quarter" | "suburb" => return,
_ => return,
};
// Get the area using flood fill cache
let floor_area = flood_fill_cache.get_or_compute(element, args.timeout.as_ref());
// Place ground blocks
for &(x, z) in floor_area.iter() {
editor.set_block(block_type, x, 0, z, None, None);
}
}