Use more iterators

This commit is contained in:
2022-10-25 00:39:19 +02:00
parent 2984d147fa
commit 89387d82fd
+45 -63
View File
@@ -4,7 +4,6 @@ use glium::glutin::event_loop::{ControlFlow, EventLoop};
use glium::vertex::VertexBufferAny; use glium::vertex::VertexBufferAny;
use glium::{self, Display}; use glium::{self, Display};
use std::fs; use std::fs;
use std::str::Split;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub mod camera; pub mod camera;
@@ -77,6 +76,7 @@ fn get_altitude(height_columns: &[&str], columns_index: usize) -> f32 {
const SEABED_ALTITUDE: f32 = -100.0; const SEABED_ALTITUDE: f32 = -100.0;
// seabed altitude - biggest drop * tiles in a chunk * (chunks in a row - 1) // seabed altitude - biggest drop * tiles in a chunk * (chunks in a row - 1)
const ALTITUDE_BETWEEN_CHUNKS: f32 = SEABED_ALTITUDE - 16.17 * 250.0 * 30.0; const ALTITUDE_BETWEEN_CHUNKS: f32 = SEABED_ALTITUDE - 16.17 * 250.0 * 30.0;
const CHUNK_SIZE: u32 = 1_000;
/// Returns a vertex buffer that should be rendered as `TrianglesList`. /// Returns a vertex buffer that should be rendered as `TrianglesList`.
pub fn load_ground(display: &Display) -> VertexBufferAny { pub fn load_ground(display: &Display) -> VertexBufferAny {
@@ -106,24 +106,21 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
println!("{}", file_name); println!("{}", file_name);
let file_name_parts: Vec<&str> = file_name.split(' ').collect(); let file_name_parts: Vec<&str> = file_name.split(' ').collect();
const CHUNK_SIZE: u32 = 1_000; let chunk = [0, 1].map(|file_name_parts_index| {
let chunk = [ file_name_parts[file_name_parts_index]
file_name_parts[0].parse::<u32>().unwrap() * CHUNK_SIZE, .parse::<u32>()
file_name_parts[1].parse::<u32>().unwrap() * CHUNK_SIZE, .unwrap()
]; * CHUNK_SIZE
});
// have deleted a few height files to make it work // have deleted a few height files to make it work
let height_contents = fs::read_to_string(&format!( let [height_contents, biomes_contents] =
"Extensions/LemnosLife/Map/Altis/Ground/{}.height", [["Ground", "height"], ["Biomes", "biomes"]].map(|[folder, extension]| {
file_name fs::read_to_string(&format!(
)) "Extensions/LemnosLife/Map/Altis/{folder}/{file_name}.{extension}"
.expect("Unable to load ground file!"); ))
.expect(&format!("Unable to load {} file!", extension))
let biomes_contents = fs::read_to_string(&format!( });
"Extensions/LemnosLife/Map/Altis/Biomes/{}.biomes",
file_name
))
.expect("Unable to load biomes file!");
let height_lines: Vec<&str> = height_contents.split('\n').rev().collect(); let height_lines: Vec<&str> = height_contents.split('\n').rev().collect();
@@ -132,28 +129,22 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
let mut index: u32 = 0; let mut index: u32 = 0;
for lines_index in 0..height_lines.len() - 1 { for lines_index in 0..height_lines.len() - 1 {
let height_line = height_lines[lines_index]; let [height_columns, height_next_columns]: [Vec<&str>; 2] =
let height_next_line = height_lines[lines_index + 1]; [height_lines[lines_index], height_lines[lines_index + 1]]
let height_columns_split: Split<char> = height_line.split(' '); .map(|height_line| height_line.split(' '))
let height_next_columns_split: Split<char> = height_next_line.split(' '); .map(|height_columns_split| {
let (height_columns, height_next_columns): (Vec<&str>, Vec<&str>) = if lines_index % 2 == 0 {
if lines_index % 2 == 0 { height_columns_split.collect()
( } else {
height_columns_split.collect(), height_columns_split.rev().collect()
height_next_columns_split.collect(), }
) });
} else {
(
height_columns_split.rev().collect(),
height_next_columns_split.rev().collect(),
)
};
let biomes_line = biomes_lines[lines_index / 2]; let biomes_line = biomes_lines[lines_index / 2];
for columns_index in 0..height_columns.len() { for columns_index in 0..height_columns.len() {
let column = get_altitude(&height_columns, columns_index); let [column, column_below]: [f32; 2] = [&height_columns, &height_next_columns]
let column_below = get_altitude(&height_next_columns, columns_index); .map(|height_columns| get_altitude(&height_columns, columns_index));
let biome_index = if lines_index % 2 == 0 { let biome_index = if lines_index % 2 == 0 {
columns_index columns_index
@@ -163,37 +154,28 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
let biome = get_biome(biomes_line, biome_index); let biome = get_biome(biomes_line, biome_index);
if lines_index == 0 && columns_index == 0 { if lines_index == 0 && columns_index == 0 {
// This vertex defines the end of the wall below the ground. // These vertexes respectively define the end of the wall below the ground and make the cursor ready to be used.
vertex_data.push(Vertex { [ALTITUDE_BETWEEN_CHUNKS, column]
altitude: ALTITUDE_BETWEEN_CHUNKS, .into_iter()
index, .for_each(|altitude| {
biome, vertex_data.push(Vertex {
chunk, altitude,
}); index,
// This vertex defines the cursor ready to be used. biome,
vertex_data.push(Vertex { chunk,
altitude: column, })
index, });
biome,
chunk,
});
} }
vertex_data.push(Vertex { [column, column_below].into_iter().for_each(|altitude| {
altitude: column, vertex_data.push(Vertex {
index, altitude,
biome, index,
chunk, biome,
chunk,
});
index += 1
}); });
index += 1;
vertex_data.push(Vertex {
altitude: column_below,
index,
biome,
chunk,
});
index += 1;
} }
} }
// This vertex remove the wall across the chunks above the ground. // This vertex remove the wall across the chunks above the ground.