Skip to content

Commit

Permalink
Merge pull request #78 from scd31/memeory_improvements
Browse files Browse the repository at this point in the history
Memory improvements
  • Loading branch information
louis-e authored Oct 27, 2024
2 parents 0e53b18 + 4aea786 commit c71fced
Show file tree
Hide file tree
Showing 17 changed files with 768 additions and 738 deletions.
625 changes: 339 additions & 286 deletions src/block_definitions.rs

Large diffs are not rendered by default.

19 changes: 3 additions & 16 deletions src/data_processing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::args::Args;
use crate::block_definitions::{DIRT, GRASS_BLOCK};
use crate::element_processing::*;
use crate::osm_parser::ProcessedElement;
use crate::world_editor::WorldEditor;
Expand Down Expand Up @@ -170,22 +171,8 @@ pub fn generate_world(

for x in 0..=(scale_factor_x as i32) {
for z in 0..=(scale_factor_z as i32) {
editor.set_block(
&crate::block_definitions::GRASS_BLOCK,
x,
ground_level,
z,
None,
None,
);
editor.set_block(
&crate::block_definitions::DIRT,
x,
ground_level - 1,
z,
None,
None,
);
editor.set_block(GRASS_BLOCK, x, ground_level, z, None, None);
editor.set_block(DIRT, x, ground_level - 1, z, None, None);

block_counter += 1;
if block_counter % batch_size == 0 {
Expand Down
40 changes: 20 additions & 20 deletions src/element_processing/amenities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ pub fn generate_amenities(
"waste_disposal" | "waste_basket" => {
// Place a cauldron for waste disposal or waste basket
if let Some((x, z)) = first_node {
editor.set_block(&CAULDRON, x, ground_level + 1, z, None, None);
editor.set_block(CAULDRON, x, ground_level + 1, z, None, None);
}
return;
}
"vending_machine" | "atm" => {
if let Some((x, z)) = first_node {
editor.set_block(&IRON_BLOCK, x, ground_level + 1, z, None, None);
editor.set_block(&IRON_BLOCK, x, ground_level + 2, z, None, None);
editor.set_block(IRON_BLOCK, x, ground_level + 1, z, None, None);
editor.set_block(IRON_BLOCK, x, ground_level + 2, z, None, None);
}
return;
}
"bicycle_parking" => {
let ground_block: &once_cell::sync::Lazy<Block> = &OAK_PLANKS;
let roof_block: &once_cell::sync::Lazy<Block> = &STONE_BLOCK_SLAB;
let ground_block = OAK_PLANKS;
let roof_block = STONE_BLOCK_SLAB;

let polygon_coords: Vec<(i32, i32)> = element.nodes().map(|n| (n.x, n.z)).collect();
let floor_area: Vec<(i32, i32)> =
Expand All @@ -62,7 +62,7 @@ pub fn generate_amenities(

for y in 1..=4 {
editor.set_block(ground_block, x, ground_level, z, None, None);
editor.set_block(&OAK_FENCE, x, ground_level + y, z, None, None);
editor.set_block(OAK_FENCE, x, ground_level + y, z, None, None);
}
editor.set_block(roof_block, x, ground_level + 5, z, None, None);
}
Expand All @@ -76,16 +76,16 @@ pub fn generate_amenities(
"bench" => {
// Place a bench
if let Some((x, z)) = first_node {
editor.set_block(&SMOOTH_STONE, x, ground_level + 1, z, None, None);
editor.set_block(&OAK_LOG, x + 1, ground_level + 1, z, None, None);
editor.set_block(&OAK_LOG, x - 1, ground_level + 1, z, None, None);
editor.set_block(SMOOTH_STONE, x, ground_level + 1, z, None, None);
editor.set_block(OAK_LOG, x + 1, ground_level + 1, z, None, None);
editor.set_block(OAK_LOG, x - 1, ground_level + 1, z, None, None);
}
}
"vending" => {
// Place vending machine blocks
if let Some((x, z)) = first_node {
editor.set_block(&IRON_BLOCK, x, ground_level + 1, z, None, None);
editor.set_block(&IRON_BLOCK, x, ground_level + 2, z, None, None);
editor.set_block(IRON_BLOCK, x, ground_level + 1, z, None, None);
editor.set_block(IRON_BLOCK, x, ground_level + 2, z, None, None);
}
}
"parking" | "fountain" => {
Expand All @@ -94,10 +94,10 @@ pub fn generate_amenities(
let mut corner_addup: (i32, i32, i32) = (0, 0, 0);
let mut current_amenity: Vec<(i32, i32)> = vec![];

let block_type: &once_cell::sync::Lazy<Block> = match amenity_type.as_str() {
"fountain" => &WATER,
"parking" => &GRAY_CONCRETE,
_ => &GRAY_CONCRETE,
let block_type = match amenity_type.as_str() {
"fountain" => WATER,
"parking" => GRAY_CONCRETE,
_ => GRAY_CONCRETE,
};
for node in element.nodes() {
let x = node.x;
Expand All @@ -113,7 +113,7 @@ pub fn generate_amenities(
bx,
ground_level,
bz,
Some(&[&BLACK_CONCRETE]),
Some(&[BLACK_CONCRETE]),
None,
);

Expand All @@ -123,7 +123,7 @@ pub fn generate_amenities(
for dz in [-1, 0, 1].iter() {
if (*dx, *dz) != (0, 0) {
editor.set_block(
&LIGHT_GRAY_CONCRETE,
LIGHT_GRAY_CONCRETE,
bx + dx,
ground_level,
bz + dz,
Expand Down Expand Up @@ -156,18 +156,18 @@ pub fn generate_amenities(
x,
ground_level,
z,
Some(&[&BLACK_CONCRETE, &GRAY_CONCRETE]),
Some(&[BLACK_CONCRETE, GRAY_CONCRETE]),
None,
);

// Add parking spot markings
if amenity_type == "parking" && (x + z) % 8 == 0 && (x * z) % 32 != 0 {
editor.set_block(
&LIGHT_GRAY_CONCRETE,
LIGHT_GRAY_CONCRETE,
x,
ground_level,
z,
Some(&[&BLACK_CONCRETE, &GRAY_CONCRETE]),
Some(&[BLACK_CONCRETE, GRAY_CONCRETE]),
None,
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/element_processing/barriers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn generate_barriers(editor: &mut WorldEditor, element: &ProcessedElement, g
if barrier_type == "bollard" {
if let ProcessedElement::Node(node) = element {
editor.set_block(
&COBBLESTONE_WALL,
COBBLESTONE_WALL,
node.x,
ground_level + 1,
node.z,
Expand Down Expand Up @@ -43,14 +43,14 @@ pub fn generate_barriers(editor: &mut WorldEditor, element: &ProcessedElement, g
for (bx, _, bz) in bresenham_points {
// Build the barrier wall to the specified height
for y in (ground_level + 1)..=(ground_level + wall_height) {
editor.set_block(&COBBLESTONE_WALL, bx, y, bz, None, None);
editor.set_block(COBBLESTONE_WALL, bx, y, bz, None, None);
// Barrier wall
}

// Add an optional top to the barrier if the height is more than 1
if wall_height > 1 {
editor.set_block(
&STONE_BRICK_SLAB,
STONE_BRICK_SLAB,
bx,
ground_level + wall_height + 1,
bz,
Expand Down
4 changes: 2 additions & 2 deletions src/element_processing/bridges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pub fn generate_bridges(editor: &mut WorldEditor, element: &ProcessedWay, ground
};

// Set bridge blocks
editor.set_block(&LIGHT_GRAY_CONCRETE, bx, current_height, bz, None, None);
editor.set_block(LIGHT_GRAY_CONCRETE, bx, current_height, bz, None, None);
for (offset_x, offset_z) in &[(-1, -1), (1, -1), (1, 1), (-1, 1)] {
editor.set_block(
&LIGHT_GRAY_CONCRETE,
LIGHT_GRAY_CONCRETE,
bx + offset_x,
current_height,
bz + offset_z,
Expand Down
38 changes: 18 additions & 20 deletions src/element_processing/buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::colors::{color_text_to_rgb_tuple, rgb_distance, RGBTuple};
use crate::floodfill::flood_fill_area;
use crate::osm_parser::ProcessedWay;
use crate::world_editor::WorldEditor;
use once_cell::sync::Lazy;
use rand::Rng;
use std::collections::HashSet;
use std::time::Duration;
Expand All @@ -25,9 +24,8 @@ pub fn generate_buildings(
let variation_index_wall: usize = rng.gen_range(0..building_wall_variations().len());
let variation_index_floor: usize = rng.gen_range(0..building_floor_variations().len());

let corner_block: &&once_cell::sync::Lazy<Block> =
&building_corner_variations()[variation_index_corner];
let wall_block: &once_cell::sync::Lazy<Block> = element
let corner_block = building_corner_variations()[variation_index_corner];
let wall_block = element
.tags
.get("building:colour")
.map(|building_colour| {
Expand All @@ -37,7 +35,7 @@ pub fn generate_buildings(
.flatten()
.flatten()
.unwrap_or_else(|| building_wall_variations()[variation_index_wall]);
let floor_block: &once_cell::sync::Lazy<Block> = element
let floor_block = element
.tags
.get("roof:colour")
.map(|roof_colour| {
Expand All @@ -47,7 +45,7 @@ pub fn generate_buildings(
.flatten()
.flatten()
.unwrap_or_else(|| building_floor_variations()[variation_index_floor]);
let window_block: &once_cell::sync::Lazy<Block> = &WHITE_STAINED_GLASS;
let window_block = WHITE_STAINED_GLASS;

// Set to store processed flood fill points
let mut processed_points: HashSet<(i32, i32)> = HashSet::new();
Expand Down Expand Up @@ -88,8 +86,8 @@ pub fn generate_buildings(
building_height = 2;

if element.tags.contains_key("bicycle_parking") {
let ground_block: &once_cell::sync::Lazy<Block> = &OAK_PLANKS;
let roof_block: &once_cell::sync::Lazy<Block> = &STONE_BLOCK_SLAB;
let ground_block = OAK_PLANKS;
let roof_block = STONE_BLOCK_SLAB;

let polygon_coords: Vec<(i32, i32)> =
element.nodes.iter().map(|n| (n.x, n.z)).collect();
Expand All @@ -108,7 +106,7 @@ pub fn generate_buildings(

for y in 1..=4 {
editor.set_block(ground_block, x, ground_level, z, None, None);
editor.set_block(&OAK_FENCE, x, ground_level + y, z, None, None);
editor.set_block(OAK_FENCE, x, ground_level + y, z, None, None);
}
editor.set_block(roof_block, x, ground_level + 5, z, None, None);
}
Expand All @@ -133,13 +131,13 @@ pub fn generate_buildings(
let bresenham_points: Vec<(i32, i32, i32)> =
bresenham_line(prev.0, roof_height, prev.1, x, roof_height, z);
for (bx, _, bz) in bresenham_points {
editor.set_block(&STONE_BRICK_SLAB, bx, roof_height, bz, None, None);
editor.set_block(STONE_BRICK_SLAB, bx, roof_height, bz, None, None);
// Set roof block at edge
}
}

for y in (ground_level + 1)..=(roof_height - 1) {
editor.set_block(&COBBLESTONE_WALL, x, y, z, None, None);
editor.set_block(COBBLESTONE_WALL, x, y, z, None, None);
}

previous_node = Some((x, z));
Expand All @@ -152,7 +150,7 @@ pub fn generate_buildings(

// Fill the interior of the roof with STONE_BRICK_SLAB
for (x, z) in roof_area.iter() {
editor.set_block(&STONE_BRICK_SLAB, *x, roof_height, *z, None, None);
editor.set_block(STONE_BRICK_SLAB, *x, roof_height, *z, None, None);
// Set roof block
}

Expand Down Expand Up @@ -192,13 +190,13 @@ pub fn generate_buildings(
editor.set_block(window_block, bx, h, bz, None, None);
// Window block
} else {
editor.set_block(&wall_block, bx, h, bz, None, None);
editor.set_block(wall_block, bx, h, bz, None, None);
// Wall block
}
}
}
editor.set_block(
&COBBLESTONE,
COBBLESTONE,
bx,
ground_level + building_height + 1,
bz,
Expand Down Expand Up @@ -226,13 +224,13 @@ pub fn generate_buildings(
if building_height > 4 {
for h in (ground_level + 2 + 4..ground_level + building_height).step_by(4) {
if x % 6 == 0 && z % 6 == 0 {
editor.set_block(&GLOWSTONE, x, h, z, None, None); // Light fixtures
editor.set_block(GLOWSTONE, x, h, z, None, None); // Light fixtures
} else {
editor.set_block(floor_block, x, h, z, None, None);
}
}
} else if x % 6 == 0 && z % 6 == 0 {
editor.set_block(&GLOWSTONE, x, ground_level + building_height, z, None, None);
editor.set_block(GLOWSTONE, x, ground_level + building_height, z, None, None);
// Light fixtures
}

Expand All @@ -252,8 +250,8 @@ pub fn generate_buildings(

fn find_nearest_block_in_color_map(
rgb: &RGBTuple,
color_map: Vec<(RGBTuple, &'static Lazy<Block>)>,
) -> Option<&'static once_cell::sync::Lazy<Block>> {
color_map: Vec<(RGBTuple, Block)>,
) -> Option<Block> {
color_map
.into_iter()
.min_by_key(|(entry_rgb, _)| rgb_distance(entry_rgb, rgb))
Expand All @@ -275,8 +273,8 @@ fn generate_bridge(
}
}

let floor_block: &once_cell::sync::Lazy<Block> = &STONE;
let railing_block: &once_cell::sync::Lazy<Block> = &STONE_BRICKS;
let floor_block = STONE;
let railing_block = STONE_BRICKS;

// Process the nodes to create bridge pathways and railings
let mut previous_node: Option<(i32, i32)> = None;
Expand Down
6 changes: 3 additions & 3 deletions src/element_processing/doors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub fn generate_doors(editor: &mut WorldEditor, element: &ProcessedNode, ground_
let z = element.z;

// Set the ground block and the door blocks
editor.set_block(&GRAY_CONCRETE, x, ground_level, z, None, None);
editor.set_block(&DARK_OAK_DOOR_LOWER, x, ground_level + 1, z, None, None);
editor.set_block(&DARK_OAK_DOOR_UPPER, x, ground_level + 2, z, None, None);
editor.set_block(GRAY_CONCRETE, x, ground_level, z, None, None);
editor.set_block(DARK_OAK_DOOR_LOWER, x, ground_level + 1, z, None, None);
editor.set_block(DARK_OAK_DOOR_UPPER, x, ground_level + 2, z, None, None);
}
}
Loading

0 comments on commit c71fced

Please sign in to comment.