Skip to content

Commit

Permalink
Add explicit type annotations for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-e committed Nov 2, 2024
1 parent e578e5d commit e1c0848
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 185 deletions.
10 changes: 5 additions & 5 deletions src/block_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,31 @@ impl Block {
pub fn properties(&self) -> Option<Value> {
match self.id {
105 => Some(Value::Compound({
let mut map = HashMap::new();
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("age".to_string(), Value::String("7".to_string()));
map
})),

106 => Some(Value::Compound({
let mut map = HashMap::new();
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("half".to_string(), Value::String("lower".to_string()));
map
})),

107 => Some(Value::Compound({
let mut map = HashMap::new();
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("half".to_string(), Value::String("upper".to_string()));
map
})),

108 => Some(Value::Compound({
let mut map = HashMap::new();
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("age".to_string(), Value::String("7".to_string()));
map
})),

109 => Some(Value::Compound({
let mut map = HashMap::new();
let mut map: HashMap<String, Value> = HashMap::new();
map.insert("age".to_string(), Value::String("7".to_string()));
map
})),
Expand Down
26 changes: 13 additions & 13 deletions src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ pub fn color_text_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
fn full_hex_color_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
if text.len() != 7
|| !text.starts_with("#")
|| !text.chars().skip(1).all(|c| c.is_ascii_hexdigit())
|| !text.chars().skip(1).all(|c: char| c.is_ascii_hexdigit())
{
return None;
}
let r = u8::from_str_radix(&text[1..3], 16).unwrap();
let g = u8::from_str_radix(&text[3..5], 16).unwrap();
let b = u8::from_str_radix(&text[5..7], 16).unwrap();
let r: u8 = u8::from_str_radix(&text[1..3], 16).unwrap();
let g: u8 = u8::from_str_radix(&text[3..5], 16).unwrap();
let b: u8 = u8::from_str_radix(&text[5..7], 16).unwrap();
Some((r, g, b))
}

fn short_hex_color_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
if text.len() != 4
|| !text.starts_with("#")
|| text.chars().skip(1).all(|c| c.is_ascii_hexdigit())
|| text.chars().skip(1).all(|c: char| c.is_ascii_hexdigit())
{
return None;
}
let r = u8::from_str_radix(&text[1..2], 16).unwrap();
let r = r | r << 4;
let g = u8::from_str_radix(&text[2..3], 16).unwrap();
let g = g | g << 4;
let b = u8::from_str_radix(&text[3..4], 16).unwrap();
let b = b | b << 4;
let r: u8 = u8::from_str_radix(&text[1..2], 16).unwrap();
let r: u8 = r | r << 4;
let g: u8 = u8::from_str_radix(&text[2..3], 16).unwrap();
let g: u8 = g | g << 4;
let b: u8 = u8::from_str_radix(&text[3..4], 16).unwrap();
let b: u8 = b | b << 4;
Some((r, g, b))
}

Expand Down Expand Up @@ -78,11 +78,11 @@ fn color_name_to_rgb_tuple(text: &str) -> Option<RGBTuple> {

pub fn rgb_distance(from: &RGBTuple, to: &RGBTuple) -> u32 {
// i32 because .pow(2) returns the same data type as self and 255^2 wouldn't fit
let difference = (
let difference: (i32, i32, i32) = (
from.0 as i32 - to.0 as i32,
from.1 as i32 - to.1 as i32,
from.2 as i32 - to.2 as i32,
);
let distance = difference.0.pow(2) + difference.1.pow(2) + difference.2.pow(2);
let distance: i32 = difference.0.pow(2) + difference.1.pow(2) + difference.2.pow(2);
distance as u32
}
4 changes: 2 additions & 2 deletions src/data_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ pub fn generate_world(
for x in 0..=(scale_factor_x as i32) {
for z in 0..=(scale_factor_z as i32) {
// Use the smaller of [current block y, ground level y]
let max_y = (MIN_Y..MAX_Y)
.find(|y| editor.block_at(x, *y, z))
let max_y: i32 = (MIN_Y..MAX_Y)
.find(|y: &i32| editor.block_at(x, *y, z))
.unwrap_or(MAX_Y)
.min(GROUND_LEVEL);

Expand Down
16 changes: 8 additions & 8 deletions src/element_processing/amenities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn generate_amenities(
}

if let Some(amenity_type) = element.tags().get("amenity") {
let first_node = element.nodes().map(|n| (n.x, n.z)).next();
let first_node: Option<(i32, i32)> = element.nodes().map(|n: &crate::osm_parser::ProcessedNode| (n.x, n.z)).next();
match amenity_type.as_str() {
"waste_disposal" | "waste_basket" => {
// Place a cauldron for waste disposal or waste basket
Expand All @@ -41,10 +41,10 @@ pub fn generate_amenities(
}
}
"bicycle_parking" => {
let ground_block = OAK_PLANKS;
let roof_block = STONE_BLOCK_SLAB;
let ground_block: Block = OAK_PLANKS;
let roof_block: Block = STONE_BLOCK_SLAB;

let polygon_coords: Vec<(i32, i32)> = element.nodes().map(|n| (n.x, n.z)).collect();
let polygon_coords: Vec<(i32, i32)> = element.nodes().map(|n: &crate::osm_parser::ProcessedNode| (n.x, n.z)).collect();
let floor_area: Vec<(i32, i32)> =
flood_fill_area(&polygon_coords, floodfill_timeout);

Expand All @@ -55,8 +55,8 @@ pub fn generate_amenities(

// Place fences and roof slabs at each corner node directly
for node in element.nodes() {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

for y in 1..=4 {
editor.set_block(ground_block, x, ground_level, z, None, None);
Expand Down Expand Up @@ -98,8 +98,8 @@ pub fn generate_amenities(
_ => GRAY_CONCRETE,
};
for node in element.nodes() {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

if let Some(prev) = previous_node {
// Create borders for fountain or parking area
Expand Down
12 changes: 6 additions & 6 deletions src/element_processing/barriers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub fn generate_barriers(editor: &mut WorldEditor, element: &ProcessedElement, g

// Process nodes to create the barrier wall
for i in 1..way.nodes.len() {
let prev = &way.nodes[i - 1];
let x1 = prev.x;
let z1 = prev.z;
let prev: &crate::osm_parser::ProcessedNode = &way.nodes[i - 1];
let x1: i32 = prev.x;
let z1: i32 = prev.z;

let cur = &way.nodes[i];
let x2 = cur.x;
let z2 = cur.z;
let cur: &crate::osm_parser::ProcessedNode = &way.nodes[i];
let x2: i32 = cur.x;
let z2: i32 = cur.z;

// Generate the line of coordinates between the two nodes
let bresenham_points: Vec<(i32, i32, i32)> =
Expand Down
26 changes: 13 additions & 13 deletions src/element_processing/bridges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ pub fn generate_bridges(editor: &mut WorldEditor, element: &ProcessedWay, ground
let total_steps: usize = element
.nodes
.windows(2)
.map(|nodes| {
let x1 = nodes[0].x;
let z1 = nodes[0].z;
let x2 = nodes[1].x;
let z2 = nodes[1].z;
.map(|nodes: &[crate::osm_parser::ProcessedNode]| {
let x1: i32 = nodes[0].x;
let z1: i32 = nodes[0].z;
let x2: i32 = nodes[1].x;
let z2: i32 = nodes[1].z;

bresenham_line(x1, ground_level, z1, x2, ground_level, z2).len()
})
.sum();

let half_steps = total_steps / 2; // Calculate midpoint for descending after rising
let mut current_step = 0;
let half_steps: usize = total_steps / 2; // Calculate midpoint for descending after rising
let mut current_step: usize = 0;

for i in 1..element.nodes.len() {
let prev = &element.nodes[i - 1];
let x1 = prev.x;
let z1 = prev.z;
let prev: &crate::osm_parser::ProcessedNode = &element.nodes[i - 1];
let x1: i32 = prev.x;
let z1: i32 = prev.z;

let cur = &element.nodes[i];
let x2 = cur.x;
let z2 = cur.z;
let cur: &crate::osm_parser::ProcessedNode = &element.nodes[i];
let x2: i32 = cur.x;
let z2: i32 = cur.z;

// Generate the line of coordinates between the two nodes
let bresenham_points: Vec<(i32, i32, i32)> =
Expand Down
52 changes: 26 additions & 26 deletions src/element_processing/buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ 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 = building_corner_variations()[variation_index_corner];
let wall_block = element
let corner_block: Block = building_corner_variations()[variation_index_corner];
let wall_block: Block = element
.tags
.get("building:colour")
.and_then(|building_colour| {
.and_then(|building_colour: &String| {
color_text_to_rgb_tuple(building_colour)
.map(|rgb| find_nearest_block_in_color_map(&rgb, building_wall_color_map()))
.map(|rgb: (u8, u8, u8)| find_nearest_block_in_color_map(&rgb, building_wall_color_map()))
})
.flatten()
.unwrap_or_else(|| building_wall_variations()[variation_index_wall]);
let floor_block = element
let floor_block: Block = element
.tags
.get("roof:colour")
.and_then(|roof_colour| {
.and_then(|roof_colour: &String| {
color_text_to_rgb_tuple(roof_colour)
.map(|rgb| find_nearest_block_in_color_map(&rgb, building_floor_color_map()))
.map(|rgb: (u8, u8, u8)| find_nearest_block_in_color_map(&rgb, building_floor_color_map()))
})
.flatten()
.unwrap_or_else(|| building_floor_variations()[variation_index_floor]);
let window_block = WHITE_STAINED_GLASS;
let window_block: Block = WHITE_STAINED_GLASS;

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

if element.tags.contains_key("bicycle_parking") {
let ground_block = OAK_PLANKS;
let roof_block = STONE_BLOCK_SLAB;
let ground_block: Block = OAK_PLANKS;
let roof_block: Block = STONE_BLOCK_SLAB;

let polygon_coords: Vec<(i32, i32)> =
element.nodes.iter().map(|n| (n.x, n.z)).collect();
element.nodes.iter().map(|n: &crate::osm_parser::ProcessedNode| (n.x, n.z)).collect();
let floor_area: Vec<(i32, i32)> =
flood_fill_area(&polygon_coords, floodfill_timeout);

Expand All @@ -99,8 +99,8 @@ pub fn generate_buildings(

// Place fences and roof slabs at each corner node directly
for node in &element.nodes {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

for y in 1..=4 {
editor.set_block(ground_block, x, ground_level, z, None, None);
Expand All @@ -118,12 +118,12 @@ pub fn generate_buildings(
return;
}
} else if building_type == "roof" {
let roof_height = ground_level + 5;
let roof_height: i32 = ground_level + 5;

// Iterate through the nodes to create the roof edges using Bresenham's line algorithm
for node in &element.nodes {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

if let Some(prev) = previous_node {
let bresenham_points: Vec<(i32, i32, i32)> =
Expand All @@ -143,7 +143,7 @@ pub fn generate_buildings(

// Use flood-fill to fill the interior of the roof
let polygon_coords: Vec<(i32, i32)> =
element.nodes.iter().map(|node| (node.x, node.z)).collect();
element.nodes.iter().map(|node: &crate::osm_parser::ProcessedNode| (node.x, node.z)).collect();
let roof_area: Vec<(i32, i32)> = flood_fill_area(&polygon_coords, floodfill_timeout); // Use flood-fill to determine the area

// Fill the interior of the roof with STONE_BRICK_SLAB
Expand Down Expand Up @@ -171,8 +171,8 @@ pub fn generate_buildings(

// Process nodes to create walls and corners
for node in &element.nodes {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

if let Some(prev) = previous_node {
// Calculate walls and corners using Bresenham line
Expand Down Expand Up @@ -211,7 +211,7 @@ pub fn generate_buildings(

// Flood-fill interior with floor variation
if corner_addup != (0, 0, 0) {
let polygon_coords: Vec<(i32, i32)> = element.nodes.iter().map(|n| (n.x, n.z)).collect();
let polygon_coords: Vec<(i32, i32)> = element.nodes.iter().map(|n: &crate::osm_parser::ProcessedNode| (n.x, n.z)).collect();
let floor_area: Vec<(i32, i32)> = flood_fill_area(&polygon_coords, floodfill_timeout);

for (x, z) in floor_area {
Expand Down Expand Up @@ -264,21 +264,21 @@ fn generate_bridge(
floodfill_timeout: Option<&Duration>,
) {
// Calculate the bridge level
let mut bridge_level = base_level;
let mut bridge_level: i32 = base_level;
if let Some(level_str) = element.tags.get("level") {
if let Ok(level) = level_str.parse::<i32>() {
bridge_level += (level * 3) + 1; // Adjust height by levels
}
}

let floor_block = STONE;
let railing_block = STONE_BRICKS;
let floor_block: Block = STONE;
let railing_block: Block = STONE_BRICKS;

// Process the nodes to create bridge pathways and railings
let mut previous_node: Option<(i32, i32)> = None;
for node in &element.nodes {
let x = node.x;
let z = node.z;
let x: i32 = node.x;
let z: i32 = node.z;

// Create bridge path using Bresenham's line
if let Some(prev) = previous_node {
Expand All @@ -293,7 +293,7 @@ fn generate_bridge(
}

// Flood fill the area between the bridge path nodes
let polygon_coords: Vec<(i32, i32)> = element.nodes.iter().map(|n| (n.x, n.z)).collect();
let polygon_coords: Vec<(i32, i32)> = element.nodes.iter().map(|n: &crate::osm_parser::ProcessedNode| (n.x, n.z)).collect();
let bridge_area: Vec<(i32, i32)> = flood_fill_area(&polygon_coords, floodfill_timeout);
for (x, z) in bridge_area {
editor.set_block(floor_block, x, bridge_level, z, None, None);
Expand Down
4 changes: 2 additions & 2 deletions src/element_processing/doors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn generate_doors(editor: &mut WorldEditor, element: &ProcessedNode, ground_
}
}

let x = element.x;
let z = element.z;
let x: i32 = element.x;
let z: i32 = element.z;

// Set the ground block and the door blocks
editor.set_block(GRAY_CONCRETE, x, ground_level, z, None, None);
Expand Down
Loading

0 comments on commit e1c0848

Please sign in to comment.