diff --git a/Cargo.toml b/Cargo.toml index 100d0ae..6cd7845 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ noise = "0.7.0" rand = "0.8.4" parking_lot = "0.11.2" rayon = "1.5.1" -rand_xoshiro = "0.6.0" \ No newline at end of file +rand_xoshiro = "0.6.0" diff --git a/src/camera.rs b/src/camera.rs index 1cdfbbf..a977543 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,11 +1,11 @@ -use glium::glutin; use crate::world; +use glium::glutin; extern crate glm; pub struct CameraState { - pub camera_pos: [f32;3], - pub camera_front: [f32;3], - pub camera_up: [f32;3], + pub camera_pos: [f32; 3], + pub camera_front: [f32; 3], + pub camera_up: [f32; 3], pub aspect_ratio: f32, pub window_width: u32, @@ -15,7 +15,7 @@ pub struct CameraState { pub pitch: f32, pub fov: f32, - pub first_mouse: (bool,bool), + pub first_mouse: (bool, bool), pub last_x: f32, pub last_y: f32, @@ -48,9 +48,14 @@ pub struct CameraState { } impl CameraState { - - pub fn new(world: &mut world::World, player_height: f32, camera_pos: [f32;3], window_width: u32, window_height: u32) -> CameraState { - let mut player = CameraState{ + pub fn new( + world: &mut world::World, + player_height: f32, + camera_pos: [f32; 3], + window_width: u32, + window_height: u32, + ) -> CameraState { + let mut player = CameraState { camera_pos: camera_pos, camera_front: [0.0, 0.0, -1.0], camera_up: [0.0, 1.0, 0.0], @@ -62,27 +67,27 @@ impl CameraState { pitch: 0.0, fov: 100.0, - first_mouse: (false,false), + first_mouse: (false, false), last_x: window_width as f32 / 2.0, last_y: window_height as f32 / 2.0, - + delta_time: 0.0, last_frame: 0.0, player_height: player_height, mesh: false, flying: false, - + mouse_button_clicked: false, keyboard_w: false, keyboard_a: false, keyboard_s: false, keyboard_d: false, - + keyboard_space: false, keyboard_space_frames: 0, touched_ground: false, - + keyboard_ctrl: true, selected_block: 4, @@ -91,16 +96,19 @@ impl CameraState { liquid_speed_modifyer: 1.0, in_liquid: false, margin_for_player: 0.25, - }; - player.camera_pos = world::World::get_spawn_location(&world, &player.camera_pos, 0 as usize); + player.camera_pos = + world::World::get_spawn_location(&world, &player.camera_pos, 0 as usize); return player; } - pub fn process_input(&mut self, event: &glutin::event::WindowEvent<'_>, world: &mut world::World) { - + pub fn process_input( + &mut self, + event: &glutin::event::WindowEvent<'_>, + world: &mut world::World, + ) { match *event { glutin::event::WindowEvent::KeyboardInput { input, .. } => { let pressed = input.state == glutin::event::ElementState::Pressed; @@ -111,75 +119,72 @@ impl CameraState { match key { glutin::event::VirtualKeyCode::Space => { if pressed { - if !self.flying{ - if self.in_liquid{ + if !self.flying { + if self.in_liquid { self.keyboard_space = true; self.touched_ground = false; self.keyboard_ctrl = false; } - - if self.touched_ground && !self.in_liquid{ + + if self.touched_ground && !self.in_liquid { self.keyboard_space = true; self.keyboard_ctrl = false; self.touched_ground = false; self.acceleration_result = 1.5 } - }else{ + } else { self.keyboard_space = true; } - }else if self.keyboard_space { - if self.in_liquid && !self.flying{ + } else if self.keyboard_space { + if self.in_liquid && !self.flying { // Space up self.keyboard_space = false; self.keyboard_ctrl = true; - }else if self.flying{ + } else if self.flying { self.keyboard_space = false; self.keyboard_ctrl = false; } } - - }, + } glutin::event::VirtualKeyCode::LControl => { if pressed { - if self.flying{ + if self.flying { self.keyboard_ctrl = true; } - }else if self.keyboard_ctrl { - if self.flying{ + } else if self.keyboard_ctrl { + if self.flying { self.keyboard_ctrl = false; } } - - }, + } glutin::event::VirtualKeyCode::A => { if pressed { self.keyboard_a = true; - }else if self.keyboard_a { + } else if self.keyboard_a { self.keyboard_a = false; } - }, + } glutin::event::VirtualKeyCode::D => { if pressed { self.keyboard_d = true; - }else if self.keyboard_d { + } else if self.keyboard_d { self.keyboard_d = false; } - }, + } glutin::event::VirtualKeyCode::W => { if pressed { self.keyboard_w = true; - }else if self.keyboard_w { + } else if self.keyboard_w { self.keyboard_w = false; } - }, + } glutin::event::VirtualKeyCode::S => { if pressed { self.keyboard_s = true; - }else if self.keyboard_s { + } else if self.keyboard_s { self.keyboard_s = false; } - - }, + } glutin::event::VirtualKeyCode::Q => { println!("IMPLEMENT POLYGON MODE Q"); // unsafe { @@ -191,56 +196,56 @@ impl CameraState { // self.mesh = false; // } // } - }, + } glutin::event::VirtualKeyCode::E => { - if pressed{ + if pressed { if !self.flying { println!("Flying turned on"); self.flying = true; - } else{ + } else { println!("Flying turned off"); self.flying = false; self.keyboard_ctrl = true; } - } - }, + } + } glutin::event::VirtualKeyCode::F => { world::World::destroy_block(world, &self.camera_front, &self.camera_pos); - }, + } glutin::event::VirtualKeyCode::Key1 => { self.selected_block = 0; - }, + } glutin::event::VirtualKeyCode::Key2 => { self.selected_block = 1; - }, + } glutin::event::VirtualKeyCode::Key3 => { self.selected_block = 2; - }, + } glutin::event::VirtualKeyCode::Key4 => { self.selected_block = 3; - }, + } glutin::event::VirtualKeyCode::Key5 => { self.selected_block = 4; - }, + } glutin::event::VirtualKeyCode::Key6 => { self.selected_block = 5; - }, + } glutin::event::VirtualKeyCode::Key7 => { self.selected_block = 6; - }, + } _ => (), }; - }, + } glutin::event::WindowEvent::CursorMoved { position, .. } => { let x = position.x; let y = position.y; - if self.first_mouse.0 && !self.first_mouse.1{ + if self.first_mouse.0 && !self.first_mouse.1 { self.last_x = x as f32; self.last_y = y as f32; - self.first_mouse = (true,true); - }else if !self.first_mouse.0 && !self.first_mouse.1{ - self.first_mouse = (true,false); + self.first_mouse = (true, true); + } else if !self.first_mouse.0 && !self.first_mouse.1 { + self.first_mouse = (true, false); } let mut xoffset = x as f32 - self.last_x; @@ -267,29 +272,36 @@ impl CameraState { front[2] = self.yaw.to_radians().sin() * self.pitch.to_radians().cos(); self.camera_front = normalize(front); - }, - glutin::event::WindowEvent::MouseInput { state, button, ..} =>{ - - match state{ - glutin::event::ElementState::Pressed => { - if !self.mouse_button_clicked { - match button{ - glutin::event::MouseButton::Left => { - world::World::destroy_block(world, &self.camera_front, &self.camera_pos); - }, - glutin::event::MouseButton::Right => { - world::World::place_block(world, &self.camera_front, &self.camera_pos, self.selected_block, self.player_height); - }, - _ => return, + } + glutin::event::WindowEvent::MouseInput { state, button, .. } => match state { + glutin::event::ElementState::Pressed => { + if !self.mouse_button_clicked { + match button { + glutin::event::MouseButton::Left => { + world::World::destroy_block( + world, + &self.camera_front, + &self.camera_pos, + ); } - self.mouse_button_clicked = true; - } - }, - glutin::event::ElementState::Released => { - if self.mouse_button_clicked { - self.mouse_button_clicked = false; + glutin::event::MouseButton::Right => { + world::World::place_block( + world, + &self.camera_front, + &self.camera_pos, + self.selected_block, + self.player_height, + ); + } + _ => return, } - }, + self.mouse_button_clicked = true; + } + } + glutin::event::ElementState::Released => { + if self.mouse_button_clicked { + self.mouse_button_clicked = false; + } } }, _ => return, @@ -297,22 +309,34 @@ impl CameraState { } pub fn update(&mut self, world: &mut world::World) { - if self.in_liquid{ + if self.in_liquid { self.liquid_speed_modifyer = 0.45; - }else{ + } else { self.liquid_speed_modifyer = 1.0; } - if !self.flying{ + if !self.flying { if self.keyboard_w { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; - let desired_position = add(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = add( + self.camera_pos, + [ + camera_speed * self.camera_front[0], + 0.0, + camera_speed * self.camera_front[2], + ], + ); + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -323,17 +347,29 @@ impl CameraState { if self.keyboard_a { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; - - let normalized = normalize(cross(self.camera_front, self.camera_up)); - let desired_position = minus(self.camera_pos, [normalized[0] * camera_speed, normalized[1] * camera_speed, normalized[2] * camera_speed]); + let normalized = normalize(cross(self.camera_front, self.camera_up)); - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = minus( + self.camera_pos, + [ + normalized[0] * camera_speed, + normalized[1] * camera_speed, + normalized[2] * camera_speed, + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -344,14 +380,26 @@ impl CameraState { if self.keyboard_s { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; - let desired_position = minus(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = minus( + self.camera_pos, + [ + camera_speed * self.camera_front[0], + 0.0, + camera_speed * self.camera_front[2], + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -363,14 +411,26 @@ impl CameraState { if self.keyboard_d { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let normalized = normalize(cross(self.camera_front, self.camera_up)); - let desired_position = add(self.camera_pos, [normalized[0] * camera_speed, normalized[1] * camera_speed, normalized[2] * camera_speed]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = add( + self.camera_pos, + [ + normalized[0] * camera_speed, + normalized[1] * camera_speed, + normalized[2] * camera_speed, + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -380,53 +440,64 @@ impl CameraState { } if self.keyboard_space { - - if !self.in_liquid{ + if !self.in_liquid { self.keyboard_ctrl = false; if self.keyboard_space_frames < 10 { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; - let desired_position = add(self.camera_pos, [0.0, camera_speed * self.acceleration_result, 0.0]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = add( + self.camera_pos, + [0.0, camera_speed * self.acceleration_result, 0.0], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; self.keyboard_space_frames += 1; self.acceleration_result += self.acceleration * (-1.0); - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } self.in_liquid = false; } - }else{ + } else { self.keyboard_space = false; self.keyboard_ctrl = true; self.keyboard_space_frames = 0; self.acceleration_result = 0.2; } - }else{ + } else { self.keyboard_space = false; self.keyboard_ctrl = true; self.keyboard_space_frames = 0; self.acceleration_result = 0.2 } - }else if self.in_liquid{ + } else if self.in_liquid { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let desired_position = add(self.camera_pos, [0.0, camera_speed, 0.0]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { - - if move_location == 3{ + if move_location == 3 { self.in_liquid = true; - }else { + } else { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; self.camera_pos = desired_position; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -440,41 +511,60 @@ impl CameraState { } if self.keyboard_ctrl { - let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let desired_position; - if self.in_liquid{ + if self.in_liquid { desired_position = minus(self.camera_pos, [0.0, camera_speed, 0.0]); - }else{ - desired_position = minus(self.camera_pos, [0.0, camera_speed * self.acceleration_result, 0.0]); + } else { + desired_position = minus( + self.camera_pos, + [0.0, camera_speed * self.acceleration_result, 0.0], + ); } - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if self.acceleration_result < 5.0 && !self.in_liquid{ + if self.acceleration_result < 5.0 && !self.in_liquid { self.acceleration_result += self.acceleration } - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { self.in_liquid = false; } - }else{ + } else { self.touched_ground = true; self.acceleration_result = 0.0; } } - }else{ + } else { if self.keyboard_w { let camera_speed = 14.0 * self.delta_time * self.liquid_speed_modifyer; - let desired_position = add(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = add( + self.camera_pos, + [ + camera_speed * self.camera_front[0], + 0.0, + camera_speed * self.camera_front[2], + ], + ); + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -486,14 +576,26 @@ impl CameraState { if self.keyboard_a { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let normalized = normalize(cross(self.camera_front, self.camera_up)); - let desired_position = minus(self.camera_pos, [normalized[0] * camera_speed, normalized[1] * camera_speed, normalized[2] * camera_speed]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = minus( + self.camera_pos, + [ + normalized[0] * camera_speed, + normalized[1] * camera_speed, + normalized[2] * camera_speed, + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -504,14 +606,26 @@ impl CameraState { if self.keyboard_s { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; - let desired_position = minus(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = minus( + self.camera_pos, + [ + camera_speed * self.camera_front[0], + 0.0, + camera_speed * self.camera_front[2], + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -523,14 +637,26 @@ impl CameraState { if self.keyboard_d { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let normalized = normalize(cross(self.camera_front, self.camera_up)); - let desired_position = add(self.camera_pos, [normalized[0] * camera_speed, normalized[1] * camera_speed, normalized[2] * camera_speed]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let desired_position = add( + self.camera_pos, + [ + normalized[0] * camera_speed, + normalized[1] * camera_speed, + normalized[2] * camera_speed, + ], + ); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { if self.in_liquid { self.touched_ground = false; } @@ -542,28 +668,37 @@ impl CameraState { if self.keyboard_space { let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let desired_position = add(self.camera_pos, [0.0, camera_speed, 0.0]); - - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { self.in_liquid = false; } } } if self.keyboard_ctrl { - let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; let desired_position = minus(self.camera_pos, [0.0, camera_speed, 0.0]); - let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); + let move_location = world::World::move_to_direction( + &world, + &desired_position, + self.player_height, + self.margin_for_player, + ); if move_location == 0 || move_location == 1 { self.camera_pos = desired_position; - if move_location == 1{ + if move_location == 1 { self.in_liquid = true; - }else{ + } else { self.in_liquid = false; } } @@ -571,36 +706,81 @@ impl CameraState { } } - pub fn get_view(&self) -> [[f32;4]; 4]{ + pub fn get_view(&self) -> [[f32; 4]; 4] { let center = add(self.camera_pos, self.camera_front); let view_temp = glm::ext::look_at( - glm::vec3(self.camera_pos[0], self.camera_pos[1], self.camera_pos[2]), - glm::vec3(center[0], center[1], center[2]), - glm::vec3(self.camera_up[0], self.camera_up[1], self.camera_up[2]) + glm::vec3(self.camera_pos[0], self.camera_pos[1], self.camera_pos[2]), + glm::vec3(center[0], center[1], center[2]), + glm::vec3(self.camera_up[0], self.camera_up[1], self.camera_up[2]), ); [ - [view_temp.c0.x, view_temp.c0.y, view_temp.c0.z, view_temp.c0.w], - [view_temp.c1.x, view_temp.c1.y, view_temp.c1.z, view_temp.c1.w], - [view_temp.c2.x, view_temp.c2.y, view_temp.c2.z, view_temp.c2.w], - [view_temp.c3.x, view_temp.c3.y, view_temp.c3.z, view_temp.c3.w] + [ + view_temp.c0.x, + view_temp.c0.y, + view_temp.c0.z, + view_temp.c0.w, + ], + [ + view_temp.c1.x, + view_temp.c1.y, + view_temp.c1.z, + view_temp.c1.w, + ], + [ + view_temp.c2.x, + view_temp.c2.y, + view_temp.c2.z, + view_temp.c2.w, + ], + [ + view_temp.c3.x, + view_temp.c3.y, + view_temp.c3.z, + view_temp.c3.w, + ], ] } - pub fn get_projection(&self) -> [[f32;4]; 4]{ - let projection_temp = glm::ext::perspective(glm::radians(self.fov), (self.window_width as f32)/(self.window_height as f32), 0.1, 5000.0); - + pub fn get_projection(&self) -> [[f32; 4]; 4] { + let projection_temp = glm::ext::perspective( + glm::radians(self.fov), + (self.window_width as f32) / (self.window_height as f32), + 0.1, + 5000.0, + ); + [ - [projection_temp.c0.x, projection_temp.c0.y, projection_temp.c0.z, projection_temp.c0.w], - [projection_temp.c1.x, projection_temp.c1.y, projection_temp.c1.z, projection_temp.c1.w], - [projection_temp.c2.x, projection_temp.c2.y, projection_temp.c2.z, projection_temp.c2.w], - [projection_temp.c3.x, projection_temp.c3.y, projection_temp.c3.z, projection_temp.c3.w] + [ + projection_temp.c0.x, + projection_temp.c0.y, + projection_temp.c0.z, + projection_temp.c0.w, + ], + [ + projection_temp.c1.x, + projection_temp.c1.y, + projection_temp.c1.z, + projection_temp.c1.w, + ], + [ + projection_temp.c2.x, + projection_temp.c2.y, + projection_temp.c2.z, + projection_temp.c2.w, + ], + [ + projection_temp.c3.x, + projection_temp.c3.y, + projection_temp.c3.z, + projection_temp.c3.w, + ], ] } } -fn cross(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3]{ - let mut result: [f32; 3] = [0.0,0.0,0.0]; +fn cross(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; let i = arr1[1] * arr2[2] - arr1[2] * arr2[1]; let j = arr1[2] * arr2[0] - arr1[0] * arr2[2]; @@ -615,7 +795,7 @@ fn cross(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3]{ fn add(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { //Add two vectors - let mut result: [f32; 3] = [0.0,0.0,0.0]; + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; result[0] = arr1[0] + arr2[0]; result[1] = arr1[1] + arr2[1]; @@ -626,7 +806,7 @@ fn add(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { fn minus(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { //Minus two vectors - let mut result: [f32; 3] = [0.0,0.0,0.0]; + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; result[0] = arr1[0] - arr2[0]; result[1] = arr1[1] - arr2[1]; @@ -635,16 +815,16 @@ fn minus(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { result } -fn normalize(arr1: [f32; 3]) -> [f32; 3]{ - // Make unit vector +fn normalize(arr1: [f32; 3]) -> [f32; 3] { + // Make unit vector - let mut result: [f32; 3] = [0.0,0.0,0.0]; + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; let magnitude = (f32::powi(arr1[0], 2) + f32::powi(arr1[1], 2) + f32::powi(arr1[2], 2)).sqrt(); - result[0] = arr1[0]/magnitude; - result[1] = arr1[1]/magnitude; - result[2] = arr1[2]/magnitude; + result[0] = arr1[0] / magnitude; + result[1] = arr1[1] / magnitude; + result[2] = arr1[2] / magnitude; result } @@ -670,5 +850,5 @@ fn normalize(arr1: [f32; 3]) -> [f32; 3]{ // } // fn dot(arr1: [f32; 3], arr2: [f32; 3]) -> f32{ -// arr1[0]*arr2[0] + arr1[1]*arr2[1] + arr1[2]*arr2[2] -// } \ No newline at end of file +// arr1[0]*arr2[0] + arr1[1]*arr2[1] + arr1[2]*arr2[2] +// } diff --git a/src/main.rs b/src/main.rs index f0b27c6..f977635 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,27 +4,26 @@ extern crate glium; mod camera; use glium::{glutin, Surface}; extern crate stopwatch; -pub mod world; pub mod player; -pub mod skybox; +pub mod skybox; +pub mod world; use glutin::dpi::{LogicalPosition, Position}; extern crate rand_xoshiro; -use std::time::{SystemTime, UNIX_EPOCH}; -use rand::{SeedableRng}; use rand::Rng; +use rand::SeedableRng; +use std::time::{SystemTime, UNIX_EPOCH}; //$Env:RUST_BACKTRACE=1 fn main() { //Settings const WINDOW_WIDTH: u32 = 1280; // For windowed only const WINDOW_HEIGHT: u32 = 720; // For windowed only - - const SQUARE_CHUNK_WIDTH: usize = 16; //Values can be: 4,6,10,16,22,28 - const CHUNKS_LAYERS_FROM_PLAYER: usize = 53; //Odd numbers ONLYYY + + const SQUARE_CHUNK_WIDTH: usize = 16; //Values can be: 4,6,10,16,22,28 + const CHUNKS_LAYERS_FROM_PLAYER: usize = 53; //Odd numbers ONLYYY const PLAYER_HEIGHT: f32 = 1.5; - let start = SystemTime::now(); let since_the_epoch = start .duration_since(UNIX_EPOCH) @@ -32,26 +31,30 @@ fn main() { let mut rng = rand_xoshiro::SplitMix64::seed_from_u64((since_the_epoch.as_millis()) as u64); // const WORLD_GEN_SEED: u32 = 60; //Any number - let WORLD_GEN_SEED: u32 = rng.gen_range(1..999999999); + let world_gen_seed: u32 = rng.gen_range(1..999999999); - const MID_HEIGHT: u8 = 24; //The terrain variation part - const SKY_HEIGHT: u8 = 4; //Works as a buffer for the mid heigt needs to be at least 20 percent of mid size - const UNDERGROUND_HEIGHT: u8 = 0; + const MID_HEIGHT: u8 = 24; //The terrain variation part + const SKY_HEIGHT: u8 = 4; //Works as a buffer for the mid heigt needs to be at least 20 percent of mid size + const UNDERGROUND_HEIGHT: u8 = 0; const TIME_BETWEEN_FRAMES: u64 = 20; let event_loop = glutin::event_loop::EventLoop::new(); let wb = glutin::window::WindowBuilder::new() - .with_title(format!("Minecraft RS")) - .with_inner_size(glutin::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT)); + .with_title(format!("Minecraft RS")) + .with_inner_size(glutin::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT)); let cb = glutin::ContextBuilder::new(); let display = glium::Display::new(wb, cb, &event_loop).unwrap(); - - let monitor_handle = display.gl_window().window().available_monitors().next().unwrap(); + let monitor_handle = display + .gl_window() + .window() + .available_monitors() + .next() + .unwrap(); let fs = glium::glutin::window::Fullscreen::Borderless(Some(monitor_handle)); display.gl_window().window().set_fullscreen(Some(fs)); - display.gl_window().window().set_cursor_grab(true); + display.gl_window().window().set_cursor_grab(true).unwrap(); display.gl_window().window().set_cursor_visible(false); let vertex_shader_block = r#" @@ -93,22 +96,29 @@ fn main() { } "#; - let program_block = glium::Program::from_source(&display, vertex_shader_block, fragment_shader_block, None).unwrap(); - + let program_block = + glium::Program::from_source(&display, vertex_shader_block, fragment_shader_block, None) + .unwrap(); let camera_pos = [0.0, 0.0, 0.0]; - + let mut world = world::World::new( &display, - camera_pos, - &SQUARE_CHUNK_WIDTH, - &CHUNKS_LAYERS_FROM_PLAYER, - &WORLD_GEN_SEED, + camera_pos, + &SQUARE_CHUNK_WIDTH, + &CHUNKS_LAYERS_FROM_PLAYER, + &world_gen_seed, &MID_HEIGHT, &UNDERGROUND_HEIGHT, &SKY_HEIGHT, ); - let mut camera = camera::CameraState::new(&mut world, PLAYER_HEIGHT, camera_pos, WINDOW_WIDTH, WINDOW_HEIGHT); + let mut camera = camera::CameraState::new( + &mut world, + PLAYER_HEIGHT, + camera_pos, + WINDOW_WIDTH, + WINDOW_HEIGHT, + ); let skybox: skybox::Skybox = skybox::Skybox::new(&display); let mut time_increment: f32 = 0.0; @@ -116,7 +126,7 @@ fn main() { [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0f32] + [0.0, 0.0, 0.0, 1.0f32], ]; let mut stopwatch = stopwatch::Stopwatch::new(); @@ -127,7 +137,7 @@ fn main() { stopwatch.reset(); stopwatch.start(); match event { - glutin::event::Event::WindowEvent {event, .. } =>{ + glutin::event::Event::WindowEvent { event, .. } => { camera.process_input(&event, &mut world); match event { @@ -140,10 +150,10 @@ fn main() { glutin::event::VirtualKeyCode::Escape => { *control_flow = glutin::event_loop::ControlFlow::Exit; return; - }, + } _ => (), }; - }, + } glutin::event::WindowEvent::CloseRequested => { *control_flow = glutin::event_loop::ControlFlow::Exit; return; @@ -154,47 +164,81 @@ fn main() { } _ => (), } - }, + } glutin::event::Event::NewEvents(cause) => match cause { glutin::event::StartCause::ResumeTimeReached { .. } => (), glutin::event::StartCause::Init => (), _ => return, }, glutin::event::Event::MainEventsCleared => { - let position = Position::Logical(LogicalPosition::new(camera.window_width as f64 / 2.0, camera.window_height as f64 / 2.0)); - display.gl_window().window().set_cursor_position(position); - draw_frame(&display, &mut camera, &skybox, &mut world, &program_block, TIME_BETWEEN_FRAMES, &stopwatch, &mut time_increment, model); - }, + let position = Position::Logical(LogicalPosition::new( + camera.window_width as f64 / 2.0, + camera.window_height as f64 / 2.0, + )); + display + .gl_window() + .window() + .set_cursor_position(position) + .unwrap(); + draw_frame( + &display, + &mut camera, + &skybox, + &mut world, + &program_block, + TIME_BETWEEN_FRAMES, + &stopwatch, + &mut time_increment, + model, + ); + } _ => return, } }); } -fn draw_frame(display: &glium::Display, camera: &mut camera::CameraState, skybox: &skybox::Skybox, world: &mut world::World, program_block: &glium::Program, frame_time: u64, stopwatch: &stopwatch::Stopwatch, time_increment: &mut f32, model: [[f32;4]; 4], ){ +fn draw_frame( + display: &glium::Display, + camera: &mut camera::CameraState, + skybox: &skybox::Skybox, + world: &mut world::World, + program_block: &glium::Program, + frame_time: u64, + stopwatch: &stopwatch::Stopwatch, + time_increment: &mut f32, + model: [[f32; 4]; 4], +) { camera.delta_time = time_increment.clone() - camera.last_frame; camera.last_frame = time_increment.clone(); - + camera.update(world); let view = camera.get_view(); let projection = camera.get_projection(); let mut target = display.draw(); target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0); - - world.draw(&camera.camera_pos, view, projection, &mut target, &display, &program_block, model); + + world.draw( + &camera.camera_pos, + view, + projection, + &mut target, + &display, + &program_block, + model, + ); skybox.draw(&mut target, &display, view, projection); target.finish().unwrap(); *time_increment += 0.02; - + world.render_loop(); - loop{ + loop { if (stopwatch.elapsed_ms() as u64) < frame_time { world.render_loop(); - }else{ - + } else { break; } } -} \ No newline at end of file +} diff --git a/src/player.rs b/src/player.rs index 2db26db..ba7bd53 100644 --- a/src/player.rs +++ b/src/player.rs @@ -63,17 +63,17 @@ // player_height: player_height, // mesh: false, // flying: false, - + // mouse_button_clicked: false, // keyboard_w: false, // keyboard_a: false, // keyboard_s: false, // keyboard_d: false, - + // keyboard_space: false, // keyboard_space_frames: 0, // touched_ground: false, - + // keyboard_ctrl: true, // selected_block: 4, @@ -92,10 +92,10 @@ // pub fn handle_events(&mut self, world: &mut world::World, event_pump: &mut sdl2::EventPump) -> bool{ // let mut close_game = false; - + // for event in event_pump.poll_iter() { // match event { -// sdl2::event::Event::Quit { .. } => close_game = true, +// sdl2::event::Event::Quit { .. } => close_game = true, // sdl2::event::Event::KeyDown { timestamp: _, window_id: _, keycode: _, scancode, keymod: _, repeat: _ } => { // //Change to polygon mesh mode // if scancode.unwrap() == sdl2::keyboard::Scancode::Escape { @@ -127,7 +127,7 @@ // self.touched_ground = false; // self.keyboard_ctrl = false; // } - + // if self.touched_ground && !self.in_liquid{ // self.keyboard_space = true; // self.keyboard_ctrl = false; @@ -202,7 +202,7 @@ // self.keyboard_space = false; // self.keyboard_ctrl = false; // } - + // } // if scancode.unwrap() == sdl2::keyboard::Scancode::LCtrl { // if self.flying{ @@ -247,10 +247,10 @@ // sdl2::event::Event::MouseWheel { timestamp: _, window_id: _, which: _, x: _, y, direction: _ } => { // if self.fov >= 1.0 && self.fov <= 90.0 { // self.fov -= y as f32; -// } +// } // if self.fov < 1.0 { // self.fov = 1.0; -// } +// } // if self.fov > 90.0 { // self.fov = 90.0; // } @@ -270,8 +270,8 @@ // }, // _ => {} // } -// } - +// } + // if self.in_liquid{ // self.liquid_speed_modifyer = 0.45; // }else{ @@ -299,7 +299,7 @@ // if self.keyboard_a { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = multiply(minus(self.camera_pos, normalize(cross(self.camera_front, self.camera_up))), camera_speed); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -317,7 +317,7 @@ // if self.keyboard_s { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = minus(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -335,7 +335,7 @@ // if self.keyboard_d { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = multiply(add(self.camera_pos, normalize(cross(self.camera_front, self.camera_up))), camera_speed); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -351,13 +351,13 @@ // } // if self.keyboard_space { - + // if !self.in_liquid{ // self.keyboard_ctrl = false; // if self.keyboard_space_frames < 10 { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = add(self.camera_pos, [0.0, camera_speed * self.acceleration_result, 0.0]); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -386,10 +386,10 @@ // }else if self.in_liquid{ // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = add(self.camera_pos, [0.0, camera_speed, 0.0]); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { - + // if move_location == 3{ // self.in_liquid = true; // }else { @@ -411,7 +411,7 @@ // } // if self.keyboard_ctrl { - + // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position; // if self.in_liquid{ @@ -457,7 +457,7 @@ // if self.keyboard_a { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = multiply(minus(self.camera_pos, normalize(cross(self.camera_front, self.camera_up))), camera_speed); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -475,7 +475,7 @@ // if self.keyboard_s { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = minus(self.camera_pos, [camera_speed * self.camera_front[0], 0.0, camera_speed * self.camera_front[2]]); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -493,7 +493,7 @@ // if self.keyboard_d { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = multiply(add(self.camera_pos, normalize(cross(self.camera_front, self.camera_up))), camera_speed); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -511,7 +511,7 @@ // if self.keyboard_space { // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = add(self.camera_pos, [0.0, camera_speed, 0.0]); - + // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); // if move_location == 0 || move_location == 1 { // self.camera_pos = desired_position; @@ -524,7 +524,7 @@ // } // if self.keyboard_ctrl { - + // let camera_speed = 7.0 * self.delta_time * self.liquid_speed_modifyer; // let desired_position = minus(self.camera_pos, [0.0, camera_speed, 0.0]); // let move_location = world::World::move_to_direction(&world, &desired_position, self.player_height, self.margin_for_player); @@ -580,7 +580,7 @@ // } // fn normalize(arr1: [f32; 3]) -> [f32; 3]{ -// // Make unit vector +// // Make unit vector // let mut result: [f32; 3] = [0.0,0.0,0.0]; @@ -601,4 +601,4 @@ // result[2] = arr1[2]/value; // result -// } \ No newline at end of file +// } diff --git a/src/skybox.rs b/src/skybox.rs index a63315f..456e2f1 100644 --- a/src/skybox.rs +++ b/src/skybox.rs @@ -1,8 +1,6 @@ - - extern crate glium; -use std::io::Cursor; use glium::Surface; +use std::io::Cursor; pub struct Skybox { program: glium::Program, skybox_vao: glium::VertexBuffer, @@ -14,7 +12,7 @@ pub struct Skybox { tex_posz: glium::Texture2d, tex_negz: glium::Texture2d, cubemap: glium::texture::Cubemap, - dest_rect1: glium::BlitTarget + dest_rect1: glium::BlitTarget, } #[derive(Copy, Clone)] struct Vertex { @@ -23,35 +21,64 @@ struct Vertex { implement_vertex!(Vertex, position); -impl Skybox{ - pub fn new(display: &glium::Display) -> Skybox{ - - let image = image::load(Cursor::new(&include_bytes!("../resources/sides.png")),image::ImageFormat::Png).unwrap().to_rgba8(); +impl Skybox { + pub fn new(display: &glium::Display) -> Skybox { + let image = image::load( + Cursor::new(&include_bytes!("../resources/sides.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_posx = glium::Texture2d::new(display, image).unwrap(); - let image = image::load(Cursor::new(&include_bytes!("../resources/sides.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/sides.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_negx = glium::Texture2d::new(display, image).unwrap(); - let image = image::load(Cursor::new(&include_bytes!("../resources/posy.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/posy.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_posy = glium::Texture2d::new(display, image).unwrap(); - let image = image::load(Cursor::new(&include_bytes!("../resources/negy.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/negy.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_negy = glium::Texture2d::new(display, image).unwrap(); - let image = image::load(Cursor::new(&include_bytes!("../resources/sides.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/sides.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_posz = glium::Texture2d::new(display, image).unwrap(); - let image = image::load(Cursor::new(&include_bytes!("../resources/sides.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/sides.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let tex_negz = glium::Texture2d::new(display, image).unwrap(); @@ -60,64 +87,111 @@ impl Skybox{ // skybox let skybox_vertex_buffer = { - - let side2: f32 = 50.0 / 2.0; - glium::VertexBuffer::new(display, + glium::VertexBuffer::new( + display, &[ // Front - Vertex { position: [-side2, -side2, side2] }, - Vertex { position: [ side2, -side2, side2] }, - Vertex { position: [ side2, side2, side2] }, - Vertex { position: [-side2, side2, side2] }, - // Right - Vertex { position: [ side2, -side2, side2] }, - Vertex { position: [ side2, -side2, -side2] }, - Vertex { position: [ side2, side2, -side2] }, - Vertex { position: [ side2, side2, side2] }, - // Back - Vertex { position: [-side2, -side2, -side2] }, - Vertex { position: [-side2, side2, -side2] }, - Vertex { position: [ side2, side2, -side2] }, - Vertex { position: [ side2, -side2, -side2] }, - // Left - Vertex { position: [-side2, -side2, side2] }, - Vertex { position: [-side2, side2, side2] }, - Vertex { position: [-side2, side2, -side2] }, - Vertex { position: [-side2, -side2, -side2] }, - // Bottom - Vertex { position: [-side2, -side2, side2] }, - Vertex { position: [-side2, -side2, -side2] }, - Vertex { position: [ side2, -side2, -side2] }, - Vertex { position: [ side2, -side2, side2] }, - // Top - Vertex { position: [-side2, side2, side2] }, - Vertex { position: [ side2, side2, side2] }, - Vertex { position: [ side2, side2, -side2] }, - Vertex { position: [-side2, side2, -side2] }, - ] - ).unwrap() - }; - - let skybox_index_buffer = glium::IndexBuffer::new(display, - glium::index::PrimitiveType::TrianglesList, - &[ - // Front - 0u16, 2, 1, 0, 3, 2, + Vertex { + position: [-side2, -side2, side2], + }, + Vertex { + position: [side2, -side2, side2], + }, + Vertex { + position: [side2, side2, side2], + }, + Vertex { + position: [-side2, side2, side2], + }, // Right - 4, 6, 5, 4, 7, 6, + Vertex { + position: [side2, -side2, side2], + }, + Vertex { + position: [side2, -side2, -side2], + }, + Vertex { + position: [side2, side2, -side2], + }, + Vertex { + position: [side2, side2, side2], + }, // Back - 8, 10, 9, 8, 11, 10, + Vertex { + position: [-side2, -side2, -side2], + }, + Vertex { + position: [-side2, side2, -side2], + }, + Vertex { + position: [side2, side2, -side2], + }, + Vertex { + position: [side2, -side2, -side2], + }, // Left - 12, 14, 13, 12, 15, 14, + Vertex { + position: [-side2, -side2, side2], + }, + Vertex { + position: [-side2, side2, side2], + }, + Vertex { + position: [-side2, side2, -side2], + }, + Vertex { + position: [-side2, -side2, -side2], + }, // Bottom - 16, 18, 17, 16, 19, 18, + Vertex { + position: [-side2, -side2, side2], + }, + Vertex { + position: [-side2, -side2, -side2], + }, + Vertex { + position: [side2, -side2, -side2], + }, + Vertex { + position: [side2, -side2, side2], + }, // Top - 20, 22, 21, 20, 23, 22, - ]).unwrap(); + Vertex { + position: [-side2, side2, side2], + }, + Vertex { + position: [side2, side2, side2], + }, + Vertex { + position: [side2, side2, -side2], + }, + Vertex { + position: [-side2, side2, -side2], + }, + ], + ) + .unwrap() + }; + + let skybox_index_buffer = glium::IndexBuffer::new( + display, + glium::index::PrimitiveType::TrianglesList, + &[ + // Front + 0u16, 2, 1, 0, 3, 2, // Right + 4, 6, 5, 4, 7, 6, // Back + 8, 10, 9, 8, 11, 10, // Left + 12, 14, 13, 12, 15, 14, // Bottom + 16, 18, 17, 16, 19, 18, // Top + 20, 22, 21, 20, 23, 22, + ], + ) + .unwrap(); - let skybox_program = glium::Program::from_source(display, + let skybox_program = glium::Program::from_source( + display, " #version 140 in vec3 position; @@ -142,8 +216,9 @@ impl Skybox{ color = texture(cubetex, ReflectDir); } ", - None).unwrap(); - + None, + ) + .unwrap(); let dest_rect1 = glium::BlitTarget { left: 0, @@ -152,7 +227,7 @@ impl Skybox{ height: 512, }; - return Skybox{ + return Skybox { program: skybox_program, skybox_vao: skybox_vertex_buffer, skybox_indices: skybox_index_buffer, @@ -163,63 +238,123 @@ impl Skybox{ tex_posz: tex_posz, tex_negz: tex_negz, cubemap: cubemap, - dest_rect1: dest_rect1 + dest_rect1: dest_rect1, }; } - pub fn draw(&self, target: &mut glium::Frame, display: &glium::Display, view: [[f32; 4]; 4], projection: [[f32; 4]; 4]){ - + pub fn draw( + &self, + target: &mut glium::Frame, + display: &glium::Display, + view: [[f32; 4]; 4], + projection: [[f32; 4]; 4], + ) { { - let framebuffer1 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::PositiveX)).unwrap(); - let framebuffer2 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::NegativeX)).unwrap(); - let framebuffer3 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::PositiveY)).unwrap(); - let framebuffer4 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::NegativeY)).unwrap(); - let framebuffer5 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::PositiveZ)).unwrap(); - let framebuffer6 = glium::framebuffer::SimpleFrameBuffer::new(display, - self.cubemap.main_level().image(glium::texture::CubeLayer::NegativeZ)).unwrap(); - - self.tex_posx.as_surface().blit_whole_color_to(&framebuffer1, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); - self.tex_negx.as_surface().blit_whole_color_to(&framebuffer2, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); - self.tex_posy.as_surface().blit_whole_color_to(&framebuffer3, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); - self.tex_negy.as_surface().blit_whole_color_to(&framebuffer4, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); - self.tex_posz.as_surface().blit_whole_color_to(&framebuffer5, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); - self.tex_negz.as_surface().blit_whole_color_to(&framebuffer6, &self.dest_rect1, - glium::uniforms::MagnifySamplerFilter::Linear); + let framebuffer1 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::PositiveX), + ) + .unwrap(); + let framebuffer2 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::NegativeX), + ) + .unwrap(); + let framebuffer3 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::PositiveY), + ) + .unwrap(); + let framebuffer4 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::NegativeY), + ) + .unwrap(); + let framebuffer5 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::PositiveZ), + ) + .unwrap(); + let framebuffer6 = glium::framebuffer::SimpleFrameBuffer::new( + display, + self.cubemap + .main_level() + .image(glium::texture::CubeLayer::NegativeZ), + ) + .unwrap(); + + self.tex_posx.as_surface().blit_whole_color_to( + &framebuffer1, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); + self.tex_negx.as_surface().blit_whole_color_to( + &framebuffer2, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); + self.tex_posy.as_surface().blit_whole_color_to( + &framebuffer3, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); + self.tex_negy.as_surface().blit_whole_color_to( + &framebuffer4, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); + self.tex_posz.as_surface().blit_whole_color_to( + &framebuffer5, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); + self.tex_negz.as_surface().blit_whole_color_to( + &framebuffer6, + &self.dest_rect1, + glium::uniforms::MagnifySamplerFilter::Linear, + ); } let params = glium::DrawParameters { depth: glium::Depth { test: glium::draw_parameters::DepthTest::IfLess, write: true, - .. Default::default() + ..Default::default() }, - .. Default::default() + ..Default::default() }; - let view: [[f32;4];4] = [ + let view: [[f32; 4]; 4] = [ [view[0][0], view[0][1], view[0][2], 0.0], [view[1][0], view[1][1], view[1][2], 0.0], [view[2][0], view[2][1], view[2][2], 0.0], - [0.0, 0.0, 0.0, 1.0] + [0.0, 0.0, 0.0, 1.0], ]; let skybox_uniforms = uniform! { - view: view, - projection: projection, - cubetex: self.cubemap.sampled().magnify_filter(glium::uniforms::MagnifySamplerFilter::Linear), - }; + view: view, + projection: projection, + cubetex: self.cubemap.sampled().magnify_filter(glium::uniforms::MagnifySamplerFilter::Linear), + }; - target.draw(&self.skybox_vao, &self.skybox_indices, &self.program, - &skybox_uniforms, ¶ms).unwrap(); + target + .draw( + &self.skybox_vao, + &self.skybox_indices, + &self.program, + &skybox_uniforms, + ¶ms, + ) + .unwrap(); } -} \ No newline at end of file +} diff --git a/src/world.rs b/src/world.rs index 91df57c..d06b880 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,12 +1,12 @@ +extern crate glium; extern crate image; extern crate noise; extern crate stopwatch; -extern crate glium; use std::io::Cursor; // use crate::render_gl; -pub mod chunk; pub mod block_model; +pub mod chunk; use self::{block_model::BlockModel, chunk::block}; use block::Block; use chunk::Chunk; @@ -24,93 +24,138 @@ pub struct World { pub unbuilt_models: Vec<(usize, usize, bool, bool, bool)>, pub set_blocks: HashMap, pub change_block: Vec<(usize, usize, usize, usize, usize, u8)>, - pub build_mesh: Vec<(usize, usize)> + pub build_mesh: Vec<(usize, usize)>, } // Make a thread method that iterates over the regenerate and get visibility functions -impl World{ - - pub fn new(display: &glium::Display, camera_position: [f32;3], square_chunk_width: &usize, chunks_layers_from_player: &usize, world_gen_seed: &u32, mid_height: &u8, underground_height: &u8, sky_height: &u8) -> World{ - let mut world = World{ +impl World { + pub fn new( + display: &glium::Display, + camera_position: [f32; 3], + square_chunk_width: &usize, + chunks_layers_from_player: &usize, + world_gen_seed: &u32, + mid_height: &u8, + underground_height: &u8, + sky_height: &u8, + ) -> World { + let mut world = World { chunk_width: square_chunk_width.clone(), loaded_textures: glium::texture::SrgbTexture2d::empty(display, 1, 1).unwrap(), chunk_grid: vec![], block_model: block_model::BlockModel::init(), - view_distance: ((chunks_layers_from_player - 1) / 2 * square_chunk_width + square_chunk_width) as f32, + view_distance: ((chunks_layers_from_player - 1) / 2 * square_chunk_width + + square_chunk_width) as f32, square_chunk_width: square_chunk_width.clone(), unbuilt_models: vec![], set_blocks: HashMap::new(), change_block: vec![], - build_mesh: vec![] + build_mesh: vec![], }; //LOAD TEXTURES setup_texture(&mut world, display); - - //LOAD CHUNKS AROUND PLAYER - generate_chunks(display, &mut world.chunk_grid, &mut world.change_block, &camera_position, &square_chunk_width, &chunks_layers_from_player, world_gen_seed, mid_height, &mut world.set_blocks, underground_height, sky_height); - //CHECK CHUNK VISIBILITY + //LOAD CHUNKS AROUND PLAYER + generate_chunks( + display, + &mut world.chunk_grid, + &mut world.change_block, + &camera_position, + &square_chunk_width, + &chunks_layers_from_player, + world_gen_seed, + mid_height, + &mut world.set_blocks, + underground_height, + sky_height, + ); + + //CHECK CHUNK VISIBILITY check_visibility(&mut world); - //BUILD CHUNK MESH + //BUILD CHUNK MESH build_mesh(&mut world, display); - return world; } - // Make the chunk grid as a variables with ARC<> and then copy the thing when you use it i guess - pub fn render_loop(&mut self){ + // Make the chunk grid as a variables with ARC<> and then copy the thing when you use it i guess + pub fn render_loop(&mut self) { if self.unbuilt_models.len() != 0 { - - if self.unbuilt_models[0].3 && !self.unbuilt_models[0].4{ - self.chunk_grid[self.unbuilt_models[0].0][self.unbuilt_models[0].1].regenerate(&mut self.change_block, self.unbuilt_models[0].0, self.unbuilt_models[0].1, &mut self.set_blocks); + if self.unbuilt_models[0].3 && !self.unbuilt_models[0].4 { + self.chunk_grid[self.unbuilt_models[0].0][self.unbuilt_models[0].1].regenerate( + &mut self.change_block, + self.unbuilt_models[0].0, + self.unbuilt_models[0].1, + &mut self.set_blocks, + ); self.unbuilt_models[0].4 = true; } - for i in 0..self.unbuilt_models.len(){ - - if self.unbuilt_models[i].3 && !self.unbuilt_models[i].4{ - self.chunk_grid[self.unbuilt_models[i].0][self.unbuilt_models[i].1].regenerate(&mut self.change_block, self.unbuilt_models[i].0, self.unbuilt_models[i].1, &mut self.set_blocks); + for i in 0..self.unbuilt_models.len() { + if self.unbuilt_models[i].3 && !self.unbuilt_models[i].4 { + self.chunk_grid[self.unbuilt_models[i].0][self.unbuilt_models[i].1].regenerate( + &mut self.change_block, + self.unbuilt_models[i].0, + self.unbuilt_models[i].1, + &mut self.set_blocks, + ); self.unbuilt_models[i].4 = true; break; } } - if self.unbuilt_models[0].2{ + if self.unbuilt_models[0].2 { check_chunk_visibility(self, self.unbuilt_models[0].0, self.unbuilt_models[0].1); } - self.build_mesh.push((self.unbuilt_models[0].0, self.unbuilt_models[0].1)); + self.build_mesh + .push((self.unbuilt_models[0].0, self.unbuilt_models[0].1)); self.unbuilt_models.remove(0); } // Set any blocks. Mostly leaves - if self.change_block.len() != 0 && self.unbuilt_models.len() == 0{ - - for i in 0..self.change_block.len(){ - self.chunk_grid[self.change_block[i].0][self.change_block[i].1].blocks[self.change_block[i].2][self.change_block[i].3][self.change_block[i].4].id = self.change_block[i].5; - check_blocks_around_block(self, self.change_block[i].0, self.change_block[i].1, self.change_block[i].2, self.change_block[i].3, self.change_block[i].4); + if self.change_block.len() != 0 && self.unbuilt_models.len() == 0 { + for i in 0..self.change_block.len() { + self.chunk_grid[self.change_block[i].0][self.change_block[i].1].blocks + [self.change_block[i].2][self.change_block[i].3][self.change_block[i].4] + .id = self.change_block[i].5; + check_blocks_around_block( + self, + self.change_block[i].0, + self.change_block[i].1, + self.change_block[i].2, + self.change_block[i].3, + self.change_block[i].4, + ); } - + self.change_block.clear(); - for k in 0..self.unbuilt_models.len(){ - self.build_mesh.push(( - self.unbuilt_models[k].0, self.unbuilt_models[k].1)); + for k in 0..self.unbuilt_models.len() { + self.build_mesh + .push((self.unbuilt_models[k].0, self.unbuilt_models[k].1)); } self.unbuilt_models.clear(); } } - pub fn draw(&mut self, camera_pos: &[f32;3], view: [[f32; 4]; 4], projection: [[f32; 4]; 4], target: &mut glium::Frame, display: &glium::Display, program: &glium::Program, model: [[f32; 4]; 4]){ - for i in 0..self.build_mesh.len(){ + pub fn draw( + &mut self, + camera_pos: &[f32; 3], + view: [[f32; 4]; 4], + projection: [[f32; 4]; 4], + target: &mut glium::Frame, + display: &glium::Display, + program: &glium::Program, + model: [[f32; 4]; 4], + ) { + for i in 0..self.build_mesh.len() { build_mesh_single(self, self.build_mesh[i].0, self.build_mesh[i].1, display); } self.build_mesh.clear(); - let behavior = glium::uniforms::SamplerBehavior { minify_filter: glium::uniforms::MinifySamplerFilter::Nearest, magnify_filter: glium::uniforms::MagnifySamplerFilter::Nearest, @@ -121,10 +166,10 @@ impl World{ depth: glium::Depth { test: glium::draw_parameters::DepthTest::IfLess, write: true, - .. Default::default() + ..Default::default() }, backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, - .. Default::default() + ..Default::default() }; let params_transparent = glium::DrawParameters { @@ -132,18 +177,17 @@ impl World{ depth: glium::Depth { test: glium::draw_parameters::DepthTest::IfLess, write: true, - .. Default::default() + ..Default::default() }, - .. Default::default() + ..Default::default() }; let mut change_direction: usize = 0; - let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList); - for i in 0..self.chunk_grid.len(){ - for k in 0..self.chunk_grid[i].len(){ + for i in 0..self.chunk_grid.len() { + for k in 0..self.chunk_grid[i].len() { let chunk_model = &self.chunk_grid[i][k].vertex_non_transparent; let uniforms = uniform! { @@ -153,18 +197,27 @@ impl World{ projection: projection }; - target.draw(chunk_model, &indices, program, &uniforms, ¶ms).unwrap(); - - if change_direction == 0 && self.unbuilt_models.len() == 0 && self.change_block.len() == 0 && !distance(self.view_distance, &camera_pos, &self.chunk_grid[i][k].position){ + target + .draw(chunk_model, &indices, program, &uniforms, ¶ms) + .unwrap(); + + if change_direction == 0 + && self.unbuilt_models.len() == 0 + && self.change_block.len() == 0 + && !distance( + self.view_distance, + &camera_pos, + &self.chunk_grid[i][k].position, + ) + { change_direction = get_direction(&camera_pos, &self.chunk_grid[i][k].position); } } } - //Seperate render call for partialy transparent objects - for i in 0..self.chunk_grid.len(){ - for k in 0..self.chunk_grid[i].len(){ + for i in 0..self.chunk_grid.len() { + for k in 0..self.chunk_grid[i].len() { let chunk_model = &self.chunk_grid[i][k].vertex_transparent; let uniforms = uniform! { @@ -174,118 +227,150 @@ impl World{ projection: projection }; - target.draw(chunk_model, &indices, program, &uniforms, ¶ms_transparent).unwrap(); + target + .draw( + chunk_model, + &indices, + program, + &uniforms, + ¶ms_transparent, + ) + .unwrap(); } } let length: usize = self.chunk_grid.len(); if change_direction != 0 { - for i in 0..length{ - if change_direction == 2 || change_direction == 4{ + for i in 0..length { + if change_direction == 2 || change_direction == 4 { match change_direction { 2 => { // -X - if i != self.chunk_grid.len()-1{ - if i == 0{ - self.chunk_grid.swap(length-1, 0); - - for k in 0..length{ - let mut new_position = self.chunk_grid[length-1][k].position.clone(); - let mut grid = self.chunk_grid[length-1][k].get_grid().clone(); + if i != self.chunk_grid.len() - 1 { + if i == 0 { + self.chunk_grid.swap(length - 1, 0); + + for k in 0..length { + let mut new_position = + self.chunk_grid[length - 1][k].position.clone(); + let mut grid = + self.chunk_grid[length - 1][k].get_grid().clone(); new_position[2] += self.chunk_width as f32; grid.0 -= 1; - + self.chunk_grid[0][k].grid_x = grid.0; self.chunk_grid[0][k].grid_z = grid.1; self.chunk_grid[0][k].position = new_position; - - self.unbuilt_models.push((0,k,true, true, false)); - self.unbuilt_models.push((1,k,true, false, false)); - self.unbuilt_models.push((length-1,k,true, false, false)); + + self.unbuilt_models.push((0, k, true, true, false)); + self.unbuilt_models.push((1, k, true, false, false)); + self.unbuilt_models.push(( + length - 1, + k, + true, + false, + false, + )); } - }else{ + } else { self.chunk_grid.swap(length - i, length - i - 1); } } - }, - 4 => { //+X - if i != self.chunk_grid.len()-1{ - if i == 0{ - - self.chunk_grid.swap(0, length-1); - - for k in 0..length{ - let mut new_position = self.chunk_grid[0][k].position.clone(); + } + 4 => { + //+X + if i != self.chunk_grid.len() - 1 { + if i == 0 { + self.chunk_grid.swap(0, length - 1); + + for k in 0..length { + let mut new_position = + self.chunk_grid[0][k].position.clone(); let mut grid = self.chunk_grid[0][k].get_grid().clone(); new_position[2] -= self.chunk_width as f32; grid.0 += 1; - self.chunk_grid[length-1][k].grid_x = grid.0; - self.chunk_grid[length-1][k].grid_z = grid.1; - self.chunk_grid[length-1][k].position = new_position; - - self.unbuilt_models.push((length-1,k,true, true, false)); - self.unbuilt_models.push((length-2,k,true, false, false)); - self.unbuilt_models.push((0,k,true, false, false)); + self.chunk_grid[length - 1][k].grid_x = grid.0; + self.chunk_grid[length - 1][k].grid_z = grid.1; + self.chunk_grid[length - 1][k].position = new_position; + + self.unbuilt_models.push(( + length - 1, + k, + true, + true, + false, + )); + self.unbuilt_models.push(( + length - 2, + k, + true, + false, + false, + )); + self.unbuilt_models.push((0, k, true, false, false)); } - - }else{ - self.chunk_grid.swap(i, i-1); + } else { + self.chunk_grid.swap(i, i - 1); } } - }, - _ => println!("KKA") + } + _ => println!("KKA"), } - }else{ - for k in 0..length{ + } else { + for k in 0..length { match change_direction { - 1 => { //-Z - if k == 0{ - self.chunk_grid[i].swap(length-1, 0); - - let mut new_position = self.chunk_grid[i][length-1].position.clone(); - let mut grid = self.chunk_grid[i][length-1].get_grid().clone(); + 1 => { + //-Z + if k == 0 { + self.chunk_grid[i].swap(length - 1, 0); + + let mut new_position = + self.chunk_grid[i][length - 1].position.clone(); + let mut grid = + self.chunk_grid[i][length - 1].get_grid().clone(); new_position[0] += self.chunk_width as f32; grid.1 -= 1; self.chunk_grid[i][0].grid_x = grid.0; self.chunk_grid[i][0].grid_z = grid.1; self.chunk_grid[i][0].position = new_position; - - self.unbuilt_models.push((i,0,true, true, false)); - self.unbuilt_models.push((i,1,true, false, false)); - self.unbuilt_models.push((i,length-1,true, false, false)); - }else{ - if k != length-1{ + self.unbuilt_models.push((i, 0, true, true, false)); + self.unbuilt_models.push((i, 1, true, false, false)); + self.unbuilt_models + .push((i, length - 1, true, false, false)); + } else { + if k != length - 1 { self.chunk_grid[i].swap(length - k, length - k - 1); } } - }, - 3 => { // +Z - if k == 0{ - self.chunk_grid[i].swap(0, length-1); + } + 3 => { + // +Z + if k == 0 { + self.chunk_grid[i].swap(0, length - 1); let mut new_position = self.chunk_grid[i][0].position.clone(); let mut grid = self.chunk_grid[i][0].get_grid().clone(); new_position[0] -= self.chunk_width as f32; - grid.1 += 1; - - self.chunk_grid[i][length-1].grid_x = grid.0; - self.chunk_grid[i][length-1].grid_z = grid.1; - self.chunk_grid[i][length-1].position = new_position; - - self.unbuilt_models.push((i,length-1,true, true, false)); - self.unbuilt_models.push((i,length-2,true, false, false)); - self.unbuilt_models.push((i,0,true, false, false)); - - }else{ - if k != length-1{ - self.chunk_grid[i].swap(k, k-1); + grid.1 += 1; + + self.chunk_grid[i][length - 1].grid_x = grid.0; + self.chunk_grid[i][length - 1].grid_z = grid.1; + self.chunk_grid[i][length - 1].position = new_position; + + self.unbuilt_models.push((i, length - 1, true, true, false)); + self.unbuilt_models + .push((i, length - 2, true, false, false)); + self.unbuilt_models.push((i, 0, true, false, false)); + } else { + if k != length - 1 { + self.chunk_grid[i].swap(k, k - 1); } } - }, - _ => println!("KKA") + } + _ => println!("KKA"), } } } @@ -293,53 +378,113 @@ impl World{ } } - pub fn destroy_block(&mut self, camera_front: &[f32;3], camera_pos: &[f32;3]){ - let (position, mut end, direction) = (camera_pos.clone(), camera_pos.clone(), camera_front.clone()); + pub fn destroy_block(&mut self, camera_front: &[f32; 3], camera_pos: &[f32; 3]) { + let (position, mut end, direction) = + (camera_pos.clone(), camera_pos.clone(), camera_front.clone()); while distance_3d(&position.clone(), &end.clone()) < 6.0 { let block_index = get_block(&self, &end); - if block_index.0 != 9999 && block_index.1 != 9999 && block_index.2 != 9999 && block_index.3 != 9999 && block_index.4 != 9999 { - + if block_index.0 != 9999 + && block_index.1 != 9999 + && block_index.2 != 9999 + && block_index.3 != 9999 + && block_index.4 != 9999 + { //Setting block in saved blocks let grid = self.chunk_grid[block_index.0][block_index.1].get_grid(); - check_and_set_block(&mut self.set_blocks, grid.0, grid.1, block_index.2, block_index.3, block_index.4, 240); - - let mut block = &mut self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3][block_index.4]; - - block.id = 240; //240 is air + check_and_set_block( + &mut self.set_blocks, + grid.0, + grid.1, + block_index.2, + block_index.3, + block_index.4, + 240, + ); + + let block = &mut self.chunk_grid[block_index.0][block_index.1].blocks + [block_index.2][block_index.3][block_index.4]; + + block.id = 240; //240 is air block.visible = false; - check_blocks_around_block(self, block_index.0, block_index.1, block_index.2, block_index.3, block_index.4); + check_blocks_around_block( + self, + block_index.0, + block_index.1, + block_index.2, + block_index.3, + block_index.4, + ); break; - } ray_step(&mut end, &direction, 0.1) } } - pub fn place_block(&mut self, camera_front: &[f32;3], camera_pos: &[f32;3], selected_block: u8, player_height: f32 ){ - let (position, mut end, direction) = (camera_pos.clone(), camera_pos.clone(), camera_front.clone()); - let mut last_air_block_index: (usize, usize, usize, usize, usize) = (0,0,0,0,0); + pub fn place_block( + &mut self, + camera_front: &[f32; 3], + camera_pos: &[f32; 3], + selected_block: u8, + player_height: f32, + ) { + let (position, mut end, direction) = + (camera_pos.clone(), camera_pos.clone(), camera_front.clone()); + let mut last_air_block_index: (usize, usize, usize, usize, usize) = (0, 0, 0, 0, 0); while distance_3d(&position.clone(), &end.clone()) < 6.0 { let block_index = get_block_or_air(&self, &end); - - if block_index.0 != 9999 && block_index.1 != 9999 && block_index.2 != 9999 && block_index.3 != 9999 && block_index.4 != 9999 { - let block = &self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3][block_index.4]; + if block_index.0 != 9999 + && block_index.1 != 9999 + && block_index.2 != 9999 + && block_index.3 != 9999 + && block_index.4 != 9999 + { + let block = &self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2] + [block_index.3][block_index.4]; if Block::is_air(&block) || Block::is_water(&block) { last_air_block_index = block_index.clone(); - }else{ - if is_player_in_block_location(self, camera_pos, player_height, last_air_block_index.0, last_air_block_index.1, last_air_block_index.2, last_air_block_index.3, last_air_block_index.4){ + } else { + if is_player_in_block_location( + self, + camera_pos, + player_height, + last_air_block_index.0, + last_air_block_index.1, + last_air_block_index.2, + last_air_block_index.3, + last_air_block_index.4, + ) { break; } //Setting block in saved blocks - let grid = self.chunk_grid[last_air_block_index.0][last_air_block_index.1].get_grid(); - check_and_set_block(&mut self.set_blocks, grid.0, grid.1, block_index.2, block_index.3, block_index.4, selected_block); - - let mut block = &mut self.chunk_grid[last_air_block_index.0][last_air_block_index.1].blocks[last_air_block_index.2][last_air_block_index.3][last_air_block_index.4]; + let grid = + self.chunk_grid[last_air_block_index.0][last_air_block_index.1].get_grid(); + check_and_set_block( + &mut self.set_blocks, + grid.0, + grid.1, + block_index.2, + block_index.3, + block_index.4, + selected_block, + ); + + let block = &mut self.chunk_grid[last_air_block_index.0] + [last_air_block_index.1] + .blocks[last_air_block_index.2][last_air_block_index.3] + [last_air_block_index.4]; block.visible = false; block.id = selected_block; - check_blocks_around_block(self, last_air_block_index.0, last_air_block_index.1, last_air_block_index.2, last_air_block_index.3, last_air_block_index.4); + check_blocks_around_block( + self, + last_air_block_index.0, + last_air_block_index.1, + last_air_block_index.2, + last_air_block_index.3, + last_air_block_index.4, + ); break; } } @@ -347,41 +492,68 @@ impl World{ } } - // returns values based on what the block type + // returns values based on what the block type // 0 for nothing // 1 for liquid // 2 solid block - pub fn move_to_direction(&self, &desired_position: &[f32;3], player_height: f32, margin_for_player: f32 ) -> usize { - let mut block_up:usize = 0; - let mut block_down:usize = 0; + pub fn move_to_direction( + &self, + &desired_position: &[f32; 3], + player_height: f32, + margin_for_player: f32, + ) -> usize { + let mut block_up: usize = 0; + let mut block_down: usize = 0; let mut block_index = get_block_or_water(self, &desired_position, margin_for_player); - if block_index.0 != 9999 && block_index.1 != 9999 && block_index.2 != 9999 && block_index.3 != 9999 && block_index.4 != 9999 { - if self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3][block_index.4].is_water(){ + if block_index.0 != 9999 + && block_index.1 != 9999 + && block_index.2 != 9999 + && block_index.3 != 9999 + && block_index.4 != 9999 + { + if self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3] + [block_index.4] + .is_water() + { block_up = 1; - }else{ + } else { block_up = 2; } } - block_index = get_block_or_water(self, &[desired_position[0], desired_position[1] - player_height, desired_position[2]], margin_for_player); - if block_index.0 != 9999 && block_index.1 != 9999 && block_index.2 != 9999 && block_index.3 != 9999 && block_index.4 != 9999 { - if self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3][block_index.4].is_water(){ + block_index = get_block_or_water( + self, + &[ + desired_position[0], + desired_position[1] - player_height, + desired_position[2], + ], + margin_for_player, + ); + if block_index.0 != 9999 + && block_index.1 != 9999 + && block_index.2 != 9999 + && block_index.3 != 9999 + && block_index.4 != 9999 + { + if self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3] + [block_index.4] + .is_water() + { block_down = 1; - }else{ + } else { block_down = 2; } } - - if block_up == 1 && block_down == 2{ + if block_up == 1 && block_down == 2 { return 3; - }else if block_up == 2 || block_down == 2{ + } else if block_up == 2 || block_down == 2 { return 2; - }else if block_up == 1 || block_down == 1{ + } else if block_up == 1 || block_down == 1 { return 1; } else { - return 0; } } @@ -391,85 +563,145 @@ impl World{ // 2 - solid block before // 1 - air/no block before // 0 - initial passed status - pub fn get_spawn_location(&self, camera_pos: &[f32;3], status: usize) -> [f32;3]{ + pub fn get_spawn_location(&self, camera_pos: &[f32; 3], status: usize) -> [f32; 3] { let block_index = get_block_or_air(self, &camera_pos); - if block_index.0 != 9999 && block_index.1 != 9999 && block_index.2 != 9999 && block_index.3 != 9999 && block_index.4 != 9999 { - let block = &self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2][block_index.3][block_index.4]; + if block_index.0 != 9999 + && block_index.1 != 9999 + && block_index.2 != 9999 + && block_index.3 != 9999 + && block_index.4 != 9999 + { + let block = &self.chunk_grid[block_index.0][block_index.1].blocks[block_index.2] + [block_index.3][block_index.4]; if Block::is_air(&block) { //Go down if status == 2 { - return [camera_pos[0], camera_pos[1]+3.0, camera_pos[2]] - }else{ - self.get_spawn_location(&[camera_pos[0], camera_pos[1]-1.0, camera_pos[2]], 1 as usize) + return [camera_pos[0], camera_pos[1] + 3.0, camera_pos[2]]; + } else { + self.get_spawn_location( + &[camera_pos[0], camera_pos[1] - 1.0, camera_pos[2]], + 1 as usize, + ) } - }else{ + } else { //Go up if status == 1 { - return [camera_pos[0], camera_pos[1]+3.0, camera_pos[2]] - }else{ - self.get_spawn_location(&[camera_pos[0], camera_pos[1]+1.0, camera_pos[2]], 2 as usize) + return [camera_pos[0], camera_pos[1] + 3.0, camera_pos[2]]; + } else { + self.get_spawn_location( + &[camera_pos[0], camera_pos[1] + 1.0, camera_pos[2]], + 2 as usize, + ) } } - }else{ + } else { //Go down if status == 2 { - return [camera_pos[0], camera_pos[1]+3.0, camera_pos[2]] - }else{ - self.get_spawn_location(&[camera_pos[0], camera_pos[1]-1.0, camera_pos[2]], 1 as usize) + return [camera_pos[0], camera_pos[1] + 3.0, camera_pos[2]]; + } else { + self.get_spawn_location( + &[camera_pos[0], camera_pos[1] - 1.0, camera_pos[2]], + 1 as usize, + ) } } } } -fn check_and_set_block(set_blocks: &mut HashMap, grid_x: i32, grid_z: i32, i: usize, k: usize, j: usize, id: u8){ - let key = [grid_x.to_string(), grid_z.to_string(), i.to_string(), k.to_string(), j.to_string()].join(""); +fn check_and_set_block( + set_blocks: &mut HashMap, + grid_x: i32, + grid_z: i32, + i: usize, + k: usize, + j: usize, + id: u8, +) { + let key = [ + grid_x.to_string(), + grid_z.to_string(), + i.to_string(), + k.to_string(), + j.to_string(), + ] + .join(""); if set_blocks.contains_key(&key) { set_blocks.remove_entry(&key); set_blocks.insert(key, id); - }else{ + } else { set_blocks.insert(key, id); } } -fn is_player_in_block_location(world: &World, camera_pos: &[f32;3], player_height: f32, i: usize, k:usize, j: usize, l: usize, m: usize) -> bool{ +fn is_player_in_block_location( + world: &World, + camera_pos: &[f32; 3], + player_height: f32, + i: usize, + k: usize, + j: usize, + l: usize, + m: usize, +) -> bool { let block_index_up = get_block_or_air(world, &camera_pos); - let block_index_down = get_block_or_air(world, &[camera_pos[0], camera_pos[1] - player_height, camera_pos[2]]); - - if block_index_up.0 == i && block_index_up.1 == k && block_index_up.2 == j && block_index_up.3 == l && block_index_up.4 == m{ + let block_index_down = get_block_or_air( + world, + &[camera_pos[0], camera_pos[1] - player_height, camera_pos[2]], + ); + + if block_index_up.0 == i + && block_index_up.1 == k + && block_index_up.2 == j + && block_index_up.3 == l + && block_index_up.4 == m + { return true; } - if block_index_down.0 == i && block_index_down.1 == k && block_index_down.2 == j && block_index_down.3 == l && block_index_down.4 == m{ + if block_index_down.0 == i + && block_index_down.1 == k + && block_index_down.2 == j + && block_index_down.3 == l + && block_index_down.4 == m + { return true; } return false; } - -fn distance_3d(arr1: &[f32;3], arr2: &[f32;3]) -> f32{ - (f32::powi(arr1[0] - arr2[0], 2) + f32::powi(arr1[1] - arr2[1], 2) + f32::powi(arr1[2] - arr2[2], 2)).sqrt() +fn distance_3d(arr1: &[f32; 3], arr2: &[f32; 3]) -> f32 { + (f32::powi(arr1[0] - arr2[0], 2) + + f32::powi(arr1[1] - arr2[1], 2) + + f32::powi(arr1[2] - arr2[2], 2)) + .sqrt() } -fn distance_2d(arr1: &[f32;2], arr2: &[f32;2]) -> f32{ +fn distance_2d(arr1: &[f32; 2], arr2: &[f32; 2]) -> f32 { (f32::powi(arr1[0] - arr2[0], 2) + f32::powi(arr1[1] - arr2[1], 2)).sqrt() } -fn get_block(world: &World, end: &[f32;3]) -> (usize, usize, usize, usize, usize){ +fn get_block(world: &World, end: &[f32; 3]) -> (usize, usize, usize, usize, usize) { let mut index_i: usize = 9999; let mut index_k: usize = 9999; - let mut index_j: usize = 9999; - let mut index_l: usize = 9999; + let mut index_j: usize = 9999; + let mut index_l: usize = 9999; let mut index_m: usize = 9999; - let mut min:f32 = 9999.0; - for i in 0..world.chunk_grid.len(){ - for k in 0..world.chunk_grid[i].len(){ + let mut min: f32 = 9999.0; + for i in 0..world.chunk_grid.len() { + for k in 0..world.chunk_grid[i].len() { let position = world.chunk_grid[i][k].position.clone(); - let distance = distance_2d(&[position[0], position[2]], &[end[0].clone(), end[2].clone()]); + let distance = distance_2d( + &[position[0], position[2]], + &[end[0].clone(), end[2].clone()], + ); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < world.chunk_width as f32 / 2.0 && distance_y < world.chunk_width as f32 / 2.0{ + if distance < min + && distance_x < world.chunk_width as f32 / 2.0 + && distance_y < world.chunk_width as f32 / 2.0 + { min = distance; index_i = i; index_k = k; @@ -480,49 +712,67 @@ fn get_block(world: &World, end: &[f32;3]) -> (usize, usize, usize, usize, usize break; } } - if min != 9999.0{ + if min != 9999.0 { min = 9999.0; for j in 0..world.chunk_grid[index_i][index_k].blocks.len() { for l in 0..world.chunk_grid[index_i][index_k].blocks[j].len() { for m in 0..world.chunk_grid[index_i][index_k].blocks[j][l].len() { - if world.chunk_grid[index_i][index_k].blocks[j][l][m].visible && world.chunk_grid[index_i][index_k].blocks[j][l][m].id != 3 { - let position = world.chunk_grid[index_i][index_k].blocks[j][l][m].position.clone(); + if world.chunk_grid[index_i][index_k].blocks[j][l][m].visible + && world.chunk_grid[index_i][index_k].blocks[j][l][m].id != 3 + { + let position = world.chunk_grid[index_i][index_k].blocks[j][l][m] + .position + .clone(); let distance = distance_3d(&position.clone(), &end.clone()); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[1] - end[1], 2)).sqrt(); let distance_z = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < 0.5 && distance_y < 0.5 && distance_z < 0.5{ - index_j = j; - index_l = l; - index_m = m; - return (index_i,index_k,index_j,index_l,index_m) + if distance < min + && distance_x < 0.5 + && distance_y < 0.5 + && distance_z < 0.5 + { + index_j = j; + index_l = l; + index_m = m; + return (index_i, index_k, index_j, index_l, index_m); } } } } } - return (index_i,index_k,index_j,index_l,index_m); - }else{ - return (index_i,index_k,index_j,index_l,index_m); + return (index_i, index_k, index_j, index_l, index_m); + } else { + return (index_i, index_k, index_j, index_l, index_m); } } -fn get_block_or_water(world: &World, end: &[f32;3], margin_for_player: f32) -> (usize, usize, usize, usize, usize){ +fn get_block_or_water( + world: &World, + end: &[f32; 3], + margin_for_player: f32, +) -> (usize, usize, usize, usize, usize) { let mut index_i: usize = 9999; let mut index_k: usize = 9999; - let mut index_j: usize = 9999; - let mut index_l: usize = 9999; + let mut index_j: usize = 9999; + let mut index_l: usize = 9999; let mut index_m: usize = 9999; - let mut min:f32 = 9999.0; - for i in 0..world.chunk_grid.len(){ - for k in 0..world.chunk_grid[i].len(){ + let mut min: f32 = 9999.0; + for i in 0..world.chunk_grid.len() { + for k in 0..world.chunk_grid[i].len() { let position = world.chunk_grid[i][k].position.clone(); - let distance = distance_2d(&[position[0], position[2]], &[end[0].clone(), end[2].clone()]); + let distance = distance_2d( + &[position[0], position[2]], + &[end[0].clone(), end[2].clone()], + ); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < world.chunk_width as f32 / 2.0 && distance_y < world.chunk_width as f32 / 2.0{ + if distance < min + && distance_x < world.chunk_width as f32 / 2.0 + && distance_y < world.chunk_width as f32 / 2.0 + { min = distance; index_i = i; index_k = k; @@ -533,49 +783,63 @@ fn get_block_or_water(world: &World, end: &[f32;3], margin_for_player: f32) -> ( break; } } - if min != 9999.0{ + if min != 9999.0 { min = 9999.0; for j in 0..world.chunk_grid[index_i][index_k].blocks.len() { for l in 0..world.chunk_grid[index_i][index_k].blocks[j].len() { for m in 0..world.chunk_grid[index_i][index_k].blocks[j][l].len() { - if world.chunk_grid[index_i][index_k].blocks[j][l][m].visible || world.chunk_grid[index_i][index_k].blocks[j][l][m].is_water(){ - let position = world.chunk_grid[index_i][index_k].blocks[j][l][m].position.clone(); + if world.chunk_grid[index_i][index_k].blocks[j][l][m].visible + || world.chunk_grid[index_i][index_k].blocks[j][l][m].is_water() + { + let position = world.chunk_grid[index_i][index_k].blocks[j][l][m] + .position + .clone(); let distance = distance_3d(&position.clone(), &end.clone()); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[1] - end[1], 2)).sqrt(); let distance_z = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < 0.5 + margin_for_player && distance_y < 0.5 && distance_z < 0.5 + margin_for_player{ - index_j = j; - index_l = l; - index_m = m; - return (index_i,index_k,index_j,index_l,index_m) + if distance < min + && distance_x < 0.5 + margin_for_player + && distance_y < 0.5 + && distance_z < 0.5 + margin_for_player + { + index_j = j; + index_l = l; + index_m = m; + return (index_i, index_k, index_j, index_l, index_m); } } } } } - return (index_i,index_k,index_j,index_l,index_m); - }else{ - return (index_i,index_k,index_j,index_l,index_m); + return (index_i, index_k, index_j, index_l, index_m); + } else { + return (index_i, index_k, index_j, index_l, index_m); } } -fn get_block_or_air(world: &World, end: &[f32;3]) -> (usize, usize, usize, usize, usize){ +fn get_block_or_air(world: &World, end: &[f32; 3]) -> (usize, usize, usize, usize, usize) { let mut index_i: usize = 9999; let mut index_k: usize = 9999; - let mut index_j: usize = 9999; - let mut index_l: usize = 9999; + let mut index_j: usize = 9999; + let mut index_l: usize = 9999; let mut index_m: usize = 9999; - let mut min:f32 = 9999.0; - for i in 0..world.chunk_grid.len(){ - for k in 0..world.chunk_grid[i].len(){ + let mut min: f32 = 9999.0; + for i in 0..world.chunk_grid.len() { + for k in 0..world.chunk_grid[i].len() { let position = world.chunk_grid[i][k].position.clone(); - let distance = distance_2d(&[position[0], position[2]], &[end[0].clone(), end[2].clone()]); + let distance = distance_2d( + &[position[0], position[2]], + &[end[0].clone(), end[2].clone()], + ); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < world.chunk_width as f32 / 2.0 && distance_y < world.chunk_width as f32 / 2.0{ + if distance < min + && distance_x < world.chunk_width as f32 / 2.0 + && distance_y < world.chunk_width as f32 / 2.0 + { min = distance; index_i = i; index_k = k; @@ -585,43 +849,51 @@ fn get_block_or_air(world: &World, end: &[f32;3]) -> (usize, usize, usize, usize break; } } - if min != 9999.0{ + if min != 9999.0 { min = 9999.0; for j in 0..world.chunk_grid[index_i][index_k].blocks.len() { for l in 0..world.chunk_grid[index_i][index_k].blocks[j].len() { for m in 0..world.chunk_grid[index_i][index_k].blocks[j][l].len() { - let position = world.chunk_grid[index_i][index_k].blocks[j][l][m].position.clone(); + let position = world.chunk_grid[index_i][index_k].blocks[j][l][m] + .position + .clone(); let distance = distance_3d(&position.clone(), &end.clone()); let distance_x = (f32::powi(position[0] - end[0], 2)).sqrt(); let distance_y = (f32::powi(position[1] - end[1], 2)).sqrt(); let distance_z = (f32::powi(position[2] - end[2], 2)).sqrt(); - if distance < min && distance_x < 0.5 && distance_y < 0.5 && distance_z < 0.5{ - index_j = j; - index_l = l; - index_m = m; - return (index_i,index_k,index_j,index_l,index_m) + if distance < min && distance_x < 0.5 && distance_y < 0.5 && distance_z < 0.5 { + index_j = j; + index_l = l; + index_m = m; + return (index_i, index_k, index_j, index_l, index_m); } } } } - return (index_i,index_k,index_j,index_l,index_m); - }else{ - return (index_i,index_k,index_j,index_l,index_m); + return (index_i, index_k, index_j, index_l, index_m); + } else { + return (index_i, index_k, index_j, index_l, index_m); } } -fn ray_step(end: &mut [f32;3], direction: &[f32;3], scale: f32){ - let new_end = add([end[0], end[1], end[2]], [scale * direction[0], scale * direction[1], scale * direction[2]]); +fn ray_step(end: &mut [f32; 3], direction: &[f32; 3], scale: f32) { + let new_end = add( + [end[0], end[1], end[2]], + [ + scale * direction[0], + scale * direction[1], + scale * direction[2], + ], + ); end[0] = new_end[0]; end[1] = new_end[1]; end[2] = new_end[2]; - } fn add(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { //Add two vectors - let mut result: [f32; 3] = [0.0,0.0,0.0]; + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; result[0] = arr1[0] + arr2[0]; result[1] = arr1[1] + arr2[1]; @@ -630,9 +902,22 @@ fn add(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { result } - -fn generate_chunks(display: &glium::Display, chunk_grid: &mut Vec>, change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, camera_position: &[f32;3], square_chunk_width: &usize, render_out_from_player: &usize, world_gen_seed: &u32, mid_height: &u8, set_blocks: &mut HashMap, underground_height: &u8, sky_height: &u8){ - let adjustment = (*render_out_from_player as f32 / 2.0).floor() as f32 * square_chunk_width.clone() as f32 + (*square_chunk_width as f32 / 2.0); +fn generate_chunks( + display: &glium::Display, + chunk_grid: &mut Vec>, + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + camera_position: &[f32; 3], + square_chunk_width: &usize, + render_out_from_player: &usize, + world_gen_seed: &u32, + mid_height: &u8, + set_blocks: &mut HashMap, + underground_height: &u8, + sky_height: &u8, +) { + let adjustment = (*render_out_from_player as f32 / 2.0).floor() as f32 + * square_chunk_width.clone() as f32 + + (*square_chunk_width as f32 / 2.0); let mut x_pos = camera_position[0] + adjustment; let mut z_pos = camera_position[2] + adjustment; let x_pos_temp = z_pos; @@ -640,28 +925,52 @@ fn generate_chunks(display: &glium::Display, chunk_grid: &mut Vec>, c let chunk_width; if render_out_from_player % 2 == 0 { chunk_width = render_out_from_player + 1; - }else { + } else { chunk_width = *render_out_from_player; } - for i in 0..chunk_width{ //Z line Go from positive to negative + for i in 0..chunk_width { + //Z line Go from positive to negative let collumn: Vec = vec![]; chunk_grid.push(collumn); - for k in 0..chunk_width{ //X line Go from positive to negative - let chunk = chunk::Chunk::init(change_block, i, k, i as i32, k as i32, [x_pos.clone(), -10.0, z_pos.clone()], square_chunk_width, world_gen_seed, mid_height, set_blocks, underground_height, sky_height, chunk_width, display); + for k in 0..chunk_width { + //X line Go from positive to negative + let chunk = chunk::Chunk::init( + change_block, + i, + k, + i as i32, + k as i32, + [x_pos.clone(), -10.0, z_pos.clone()], + square_chunk_width, + world_gen_seed, + mid_height, + set_blocks, + underground_height, + sky_height, + chunk_width, + display, + ); chunk_grid[i].push(chunk); x_pos -= *square_chunk_width as f32; } x_pos = x_pos_temp; - z_pos -= *square_chunk_width as f32 ; + z_pos -= *square_chunk_width as f32; } } -fn distance(max_distance: f32, point1: &[f32;3], point2: &[f32;3]) -> bool{ - return (f32::powi(point1[0].clone() - point2[0].clone(), 2)).sqrt().floor() <= max_distance && (f32::powi(point1[2].clone() - point2[2].clone(), 2)).sqrt().floor() <= max_distance +fn distance(max_distance: f32, point1: &[f32; 3], point2: &[f32; 3]) -> bool { + return (f32::powi(point1[0].clone() - point2[0].clone(), 2)) + .sqrt() + .floor() + <= max_distance + && (f32::powi(point1[2].clone() - point2[2].clone(), 2)) + .sqrt() + .floor() + <= max_distance; } -fn get_direction(point1: &[f32;3], point2: &[f32;3]) -> usize{ +fn get_direction(point1: &[f32; 3], point2: &[f32; 3]) -> usize { let mut x_distance = point1[0].clone() - point2[0].clone(); let mut z_distance = point1[2].clone() - point2[2].clone(); let mut x_negative: bool = false; @@ -679,26 +988,33 @@ fn get_direction(point1: &[f32;3], point2: &[f32;3]) -> usize{ z_distance *= -1.0; } - if x_distance < z_distance { bigger = true; } - - if !bigger && !x_negative{ // Positive x + + if !bigger && !x_negative { + // Positive x 1 - }else if !bigger && x_negative{ // Negative x + } else if !bigger && x_negative { + // Negative x 3 - }else if bigger && !z_negative{ // Positive z + } else if bigger && !z_negative { + // Positive z 2 - }else { // Negative z + } else { + // Negative z 4 } } fn setup_texture(world: &mut World, display: &glium::Display) { - // let image = image::open(&std::path::Path::new("resources\\TextureTemplate.png")).unwrap().into_rgba8(); - let image = image::load(Cursor::new(&include_bytes!("../resources/TextureTemplate.png")),image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../resources/TextureTemplate.png")), + image::ImageFormat::Png, + ) + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba(image.into_raw(), image_dimensions); let texture = glium::texture::SrgbTexture2d::new(display, image).unwrap(); @@ -706,8 +1022,7 @@ fn setup_texture(world: &mut World, display: &glium::Display) { world.loaded_textures = texture; } - -fn check_visibility(world: &mut World){ +fn check_visibility(world: &mut World) { for i in 0..world.chunk_grid.len() { for k in 0..world.chunk_grid[i].len() { check_chunk_visibility(world, i, k); @@ -715,18 +1030,26 @@ fn check_visibility(world: &mut World){ } } -fn check_chunk_visibility(world: &mut World, i: usize, k: usize){ +fn check_chunk_visibility(world: &mut World, i: usize, k: usize) { let mut layer_visibility: Vec = vec![]; - for _i in 0..world.chunk_grid[i][k].blocks[0][0].len(){ + for _i in 0..world.chunk_grid[i][k].blocks[0][0].len() { layer_visibility.push(false); } for m in 0..world.chunk_grid[i][k].blocks[0][0].len() { for j in 0..world.chunk_grid[i][k].blocks.len() { for l in 0..world.chunk_grid[i][k].blocks[j].len() { - let visible = check_block_sides(&mut world.chunk_grid , i.clone(), k.clone(), j.clone(), l.clone(), m.clone(), world.chunk_width as usize); - + let visible = check_block_sides( + &mut world.chunk_grid, + i.clone(), + k.clone(), + j.clone(), + l.clone(), + m.clone(), + world.chunk_width as usize, + ); + if visible && layer_visibility[m] == false { layer_visibility[m] = true; } @@ -737,219 +1060,373 @@ fn check_chunk_visibility(world: &mut World, i: usize, k: usize){ world.chunk_grid[i][k].set_layer_visibility(layer_visibility) } -fn check_block_sides(chunk_grid: &mut Vec>, i: usize, k: usize, j: usize, l: usize, m: usize, chunk_width: usize) -> bool{ +fn check_block_sides( + chunk_grid: &mut Vec>, + i: usize, + k: usize, + j: usize, + l: usize, + m: usize, + chunk_width: usize, +) -> bool { let block_id = chunk_grid[i][k].blocks[j][l][m].id; - + if chunk_grid[i][k].blocks[j][l][m].is_air() { chunk_grid[i][k].blocks[j][l][m].visible = false; return false; - }else { - let z_chunk_flag: u32; - let x_chunk_flag: u32; - let z_block_flag: u32; - let x_block_flag: u32; - let y_block_flag: u32; - - if i == 0 { z_chunk_flag = 0 }else if i == chunk_grid.len()-1 { z_chunk_flag = 2 } else { z_chunk_flag = 1 }; //Z axis - if k == 0 { x_chunk_flag = 0 }else if k == chunk_grid.len()-1 { x_chunk_flag = 2 } else { x_chunk_flag = 1 }; //X axis - if j == 0 { z_block_flag = 0 }else if j == chunk_width-1 { z_block_flag = 2 } else { z_block_flag = 1 }; //Z axis - if l == 0 { x_block_flag = 0 }else if l == chunk_width-1 { x_block_flag = 2 } else { x_block_flag = 1 }; //X axis - if m == 0 { y_block_flag = 0 }else if m == chunk_grid[i][k].blocks[j][l].len()-1 { y_block_flag = 2 } else { y_block_flag = 1 }; //Y axis + } else { + let z_chunk_flag: u32; + let x_chunk_flag: u32; + let z_block_flag: u32; + let x_block_flag: u32; + let y_block_flag: u32; + + if i == 0 { + z_chunk_flag = 0 + } else if i == chunk_grid.len() - 1 { + z_chunk_flag = 2 + } else { + z_chunk_flag = 1 + }; //Z axis + if k == 0 { + x_chunk_flag = 0 + } else if k == chunk_grid.len() - 1 { + x_chunk_flag = 2 + } else { + x_chunk_flag = 1 + }; //X axis + if j == 0 { + z_block_flag = 0 + } else if j == chunk_width - 1 { + z_block_flag = 2 + } else { + z_block_flag = 1 + }; //Z axis + if l == 0 { + x_block_flag = 0 + } else if l == chunk_width - 1 { + x_block_flag = 2 + } else { + x_block_flag = 1 + }; //X axis + if m == 0 { + y_block_flag = 0 + } else if m == chunk_grid[i][k].blocks[j][l].len() - 1 { + y_block_flag = 2 + } else { + y_block_flag = 1 + }; //Y axis // I = Z chunk, K = X chunk, J = Z block, L = X block, M = Y block let mut sides: Vec = vec![]; // //Z block go + - if z_block_flag == 2{ - if z_chunk_flag == 2 || chunk_grid.len()-1 == 0 && z_chunk_flag == 0{ + if z_block_flag == 2 { + if z_chunk_flag == 2 || chunk_grid.len() - 1 == 0 && z_chunk_flag == 0 { sides.push(false); - }else{ - if chunk_grid[i+1][k].blocks[0][l][m].is_air() || block_id != 3 && chunk_grid[i+1][k].blocks[0][l][m].is_water(){ - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i + 1][k].blocks[0][l][m].is_air() + || block_id != 3 && chunk_grid[i + 1][k].blocks[0][l][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - }else{ - if chunk_grid[i][k].blocks[j+1][l][m].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j+1][l][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j + 1][l][m].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j + 1][l][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - // Z block go - - if z_block_flag == 0{ - if z_chunk_flag == 0{ + if z_block_flag == 0 { + if z_chunk_flag == 0 { sides.push(false); - }else{ - if chunk_grid[i-1][k].blocks[chunk_width-1][l][m].is_air() || block_id != 3 && chunk_grid[i-1][k].blocks[chunk_width-1][l][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i - 1][k].blocks[chunk_width - 1][l][m].is_air() + || block_id != 3 + && chunk_grid[i - 1][k].blocks[chunk_width - 1][l][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - }else{ - if chunk_grid[i][k].blocks[j-1][l][m].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j-1][l][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j - 1][l][m].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j - 1][l][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } // X block go + - if x_block_flag == 2{ - if x_chunk_flag == 2 || chunk_grid.len()-1 == 0 && x_chunk_flag == 0{ + if x_block_flag == 2 { + if x_chunk_flag == 2 || chunk_grid.len() - 1 == 0 && x_chunk_flag == 0 { sides.push(false); - }else{ - if chunk_grid[i][k+1].blocks[j][0][m].is_air() || block_id != 3 && chunk_grid[i][k+1].blocks[j][0][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k + 1].blocks[j][0][m].is_air() + || block_id != 3 && chunk_grid[i][k + 1].blocks[j][0][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - }else{ - if chunk_grid[i][k].blocks[j][l+1][m].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j][l+1][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j][l + 1][m].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j][l + 1][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } // X block go - - if x_block_flag == 0{ - if x_chunk_flag == 0{ + if x_block_flag == 0 { + if x_chunk_flag == 0 { sides.push(false); - }else{ - if chunk_grid[i][k-1].blocks[j][chunk_width-1][m].is_air() || block_id != 3 && chunk_grid[i][k-1].blocks[j][chunk_width-1][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k - 1].blocks[j][chunk_width - 1][m].is_air() + || block_id != 3 + && chunk_grid[i][k - 1].blocks[j][chunk_width - 1][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - }else{ - if chunk_grid[i][k].blocks[j][l-1][m].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j][l-1][m].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j][l - 1][m].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j][l - 1][m].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - // Y block go - - if y_block_flag == 0{ + if y_block_flag == 0 { sides.push(true); - }else{ - if chunk_grid[i][k].blocks[j][l][m-1].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j][l][m-1].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j][l][m - 1].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j][l][m - 1].is_water() + { + sides.push(true); + } else { + sides.push(false); } } // Y block go + - if y_block_flag == 2 || 0 == chunk_grid[i][k].blocks[j][l].len()-1{ + if y_block_flag == 2 || 0 == chunk_grid[i][k].blocks[j][l].len() - 1 { sides.push(true); - }else{ - if chunk_grid[i][k].blocks[j][l][m+1].is_air() || block_id != 3 && chunk_grid[i][k].blocks[j][l][m+1].is_water() { - sides.push(true); - }else { - sides.push(false); + } else { + if chunk_grid[i][k].blocks[j][l][m + 1].is_air() + || block_id != 3 && chunk_grid[i][k].blocks[j][l][m + 1].is_water() + { + sides.push(true); + } else { + sides.push(false); } } - - //Assigning invisible or visisble let mut visible: u8 = 0; - for i in 0..sides.len(){ + for i in 0..sides.len() { if sides[i] == true { visible += 1; break; } } - - if visible == 1{ + + if visible == 1 { chunk_grid[i][k].blocks[j][l][m].visible = true; chunk_grid[i][k].blocks[j][l][m].sides = sides; - }else{ + } else { chunk_grid[i][k].blocks[j][l][m].visible = false; } return visible == 1; - } + } } -fn check_blocks_around_block(world: &mut World, i: usize, k: usize, j: usize, l: usize, m: usize){ - check_block_sides(&mut world.chunk_grid, i, k, j, l, m, world.chunk_width as usize); +fn check_blocks_around_block(world: &mut World, i: usize, k: usize, j: usize, l: usize, m: usize) { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j, + l, + m, + world.chunk_width as usize, + ); world.chunk_grid[i][k].changed_block_visibility(m); - if !world.unbuilt_models.contains(&(i,k,false,false,false)){ - push_unbuilt_to_start((i,k,false,false,false), &mut world.unbuilt_models); + if !world.unbuilt_models.contains(&(i, k, false, false, false)) { + push_unbuilt_to_start((i, k, false, false, false), &mut world.unbuilt_models); } - //Check up - if world.chunk_grid[i][k].blocks[j][l].len() != 1 && m != world.chunk_grid[i][k].blocks[j][l].len() -1 && m < world.chunk_grid[i][k].blocks[j][l].len() + 1 { - check_block_sides(&mut world.chunk_grid, i, k, j, l, m+1, world.chunk_width as usize); - world.chunk_grid[i][k].changed_block_visibility(m+1); + //Check up + if world.chunk_grid[i][k].blocks[j][l].len() != 1 + && m != world.chunk_grid[i][k].blocks[j][l].len() - 1 + && m < world.chunk_grid[i][k].blocks[j][l].len() + 1 + { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j, + l, + m + 1, + world.chunk_width as usize, + ); + world.chunk_grid[i][k].changed_block_visibility(m + 1); } //Check down if world.chunk_grid[i][k].blocks[j][l].len() != 1 && m != 0 { - check_block_sides(&mut world.chunk_grid, i, k, j, l, m-1, world.chunk_width as usize); - world.chunk_grid[i][k].changed_block_visibility(m-1); + check_block_sides( + &mut world.chunk_grid, + i, + k, + j, + l, + m - 1, + world.chunk_width as usize, + ); + world.chunk_grid[i][k].changed_block_visibility(m - 1); } - //Check left - if j != 0 { - check_block_sides(&mut world.chunk_grid, i, k, j-1, l, m, world.chunk_width as usize); + if j != 0 { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j - 1, + l, + m, + world.chunk_width as usize, + ); world.chunk_grid[i][k].changed_block_visibility(m); - } - else if i != 0 { - check_block_sides(&mut world.chunk_grid, i-1, k, world.chunk_width as usize-1, l, m, world.chunk_width as usize); - world.chunk_grid[i-1][k].changed_block_visibility(m); - if !world.unbuilt_models.contains(&(i-1,k,false,false,false)){ - push_unbuilt_to_start((i-1,k,false,false,false), &mut world.unbuilt_models); + } else if i != 0 { + check_block_sides( + &mut world.chunk_grid, + i - 1, + k, + world.chunk_width as usize - 1, + l, + m, + world.chunk_width as usize, + ); + world.chunk_grid[i - 1][k].changed_block_visibility(m); + if !world + .unbuilt_models + .contains(&(i - 1, k, false, false, false)) + { + push_unbuilt_to_start((i - 1, k, false, false, false), &mut world.unbuilt_models); } } //Check right - if j != world.chunk_width as usize -1 { - check_block_sides(&mut world.chunk_grid, i, k, j+1, l, m, world.chunk_width as usize); + if j != world.chunk_width as usize - 1 { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j + 1, + l, + m, + world.chunk_width as usize, + ); world.chunk_grid[i][k].changed_block_visibility(m); - }else if i != world.chunk_grid.len()-1 { - check_block_sides(&mut world.chunk_grid, i+1, k, 0, l, m, world.chunk_width as usize); - world.chunk_grid[i+1][k].changed_block_visibility(m); - if !world.unbuilt_models.contains(&(i+1,k,false,false,false)){ - push_unbuilt_to_start((i+1,k,false,false,false), &mut world.unbuilt_models); + } else if i != world.chunk_grid.len() - 1 { + check_block_sides( + &mut world.chunk_grid, + i + 1, + k, + 0, + l, + m, + world.chunk_width as usize, + ); + world.chunk_grid[i + 1][k].changed_block_visibility(m); + if !world + .unbuilt_models + .contains(&(i + 1, k, false, false, false)) + { + push_unbuilt_to_start((i + 1, k, false, false, false), &mut world.unbuilt_models); } - } - + } - //Check front - if l != 0 { - check_block_sides(&mut world.chunk_grid, i, k, j, l-1, m, world.chunk_width as usize); + //Check front + if l != 0 { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j, + l - 1, + m, + world.chunk_width as usize, + ); world.chunk_grid[i][k].changed_block_visibility(m); - - }else if k != 0{ - check_block_sides(&mut world.chunk_grid, i, k-1, j, world.chunk_width as usize-1, m, world.chunk_width as usize); - world.chunk_grid[i][k-1].changed_block_visibility(m); - if !world.unbuilt_models.contains(&(i,k-1,false,false,false)){ - push_unbuilt_to_start((i,k-1,false,false,false), &mut world.unbuilt_models); + } else if k != 0 { + check_block_sides( + &mut world.chunk_grid, + i, + k - 1, + j, + world.chunk_width as usize - 1, + m, + world.chunk_width as usize, + ); + world.chunk_grid[i][k - 1].changed_block_visibility(m); + if !world + .unbuilt_models + .contains(&(i, k - 1, false, false, false)) + { + push_unbuilt_to_start((i, k - 1, false, false, false), &mut world.unbuilt_models); } } //Check back - if l != world.chunk_width as usize -1{ - check_block_sides(&mut world.chunk_grid, i, k, j, l+1, m, world.chunk_width as usize); + if l != world.chunk_width as usize - 1 { + check_block_sides( + &mut world.chunk_grid, + i, + k, + j, + l + 1, + m, + world.chunk_width as usize, + ); world.chunk_grid[i][k].changed_block_visibility(m); - }else if k != world.chunk_grid[i].len()-1 { - check_block_sides(&mut world.chunk_grid, i, k+1, j, 0, m, world.chunk_width as usize); - world.chunk_grid[i][k+1].changed_block_visibility(m); - if !world.unbuilt_models.contains(&(i,k+1,false,false,false)){ - push_unbuilt_to_start((i,k+1,false,false,false), &mut world.unbuilt_models); + } else if k != world.chunk_grid[i].len() - 1 { + check_block_sides( + &mut world.chunk_grid, + i, + k + 1, + j, + 0, + m, + world.chunk_width as usize, + ); + world.chunk_grid[i][k + 1].changed_block_visibility(m); + if !world + .unbuilt_models + .contains(&(i, k + 1, false, false, false)) + { + push_unbuilt_to_start((i, k + 1, false, false, false), &mut world.unbuilt_models); } } } - -fn build_mesh(world: &mut World, display: &glium::Display){ +fn build_mesh(world: &mut World, display: &glium::Display) { for i in 0..world.chunk_grid.len() { for k in 0..world.chunk_grid[i].len() { Chunk::build_mesh(&mut world.chunk_grid[i][k], &world.block_model); @@ -958,13 +1435,15 @@ fn build_mesh(world: &mut World, display: &glium::Display){ } } -fn build_mesh_single(world: &mut World, i: usize, k: usize, display: &glium::Display){ +fn build_mesh_single(world: &mut World, i: usize, k: usize, display: &glium::Display) { Chunk::build_mesh(&mut world.chunk_grid[i][k], &world.block_model); Chunk::populate_mesh(&mut world.chunk_grid[i][k], display); - } -fn push_unbuilt_to_start(unbuilt: (usize, usize, bool, bool, bool), vector: &mut Vec<(usize, usize, bool, bool, bool)>){ +fn push_unbuilt_to_start( + unbuilt: (usize, usize, bool, bool, bool), + vector: &mut Vec<(usize, usize, bool, bool, bool)>, +) { let mut unbuilt_part: Vec<(usize, usize, bool, bool, bool)> = vec![unbuilt]; unbuilt_part.append(vector); *vector = unbuilt_part diff --git a/src/world/block_model.rs b/src/world/block_model.rs index a518db8..0827b33 100644 --- a/src/world/block_model.rs +++ b/src/world/block_model.rs @@ -1,147 +1,136 @@ - -pub struct BlockModel{ - px_pos: Vec<[f32;3]>, - nx_pos: Vec<[f32;3]>, - py_pos: Vec<[f32;3]>, - ny_pos: Vec<[f32;3]>, - pz_pos: Vec<[f32;3]>, - nz_pos: Vec<[f32;3]>, - px_pos_water: Vec<[f32;3]>, - nx_pos_water: Vec<[f32;3]>, - py_pos_water: Vec<[f32;3]>, - ny_pos_water: Vec<[f32;3]>, - pz_pos_water: Vec<[f32;3]>, - nz_pos_water: Vec<[f32;3]>, - px_uv: Vec<[f32;2]>, - nx_uv: Vec<[f32;2]>, - py_uv: Vec<[f32;2]>, - ny_uv: Vec<[f32;2]>, - pz_uv: Vec<[f32;2]>, - nz_uv: Vec<[f32;2]>, - brightness: Vec +pub struct BlockModel { + px_pos: Vec<[f32; 3]>, + nx_pos: Vec<[f32; 3]>, + py_pos: Vec<[f32; 3]>, + ny_pos: Vec<[f32; 3]>, + pz_pos: Vec<[f32; 3]>, + nz_pos: Vec<[f32; 3]>, + px_pos_water: Vec<[f32; 3]>, + nx_pos_water: Vec<[f32; 3]>, + py_pos_water: Vec<[f32; 3]>, + ny_pos_water: Vec<[f32; 3]>, + pz_pos_water: Vec<[f32; 3]>, + nz_pos_water: Vec<[f32; 3]>, + px_uv: Vec<[f32; 2]>, + nx_uv: Vec<[f32; 2]>, + py_uv: Vec<[f32; 2]>, + ny_uv: Vec<[f32; 2]>, + pz_uv: Vec<[f32; 2]>, + nz_uv: Vec<[f32; 2]>, + brightness: Vec, } - impl BlockModel { - - pub fn init() -> BlockModel{ - let px_pos: Vec<[f32;3]> = vec![ + pub fn init() -> BlockModel { + let px_pos: Vec<[f32; 3]> = vec![ [-0.5, -0.5, -0.5], - [ 0.5, 0.5, -0.5], + [0.5, 0.5, -0.5], [0.5, -0.5, -0.5], - [0.5, 0.5, -0.5], + [0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], - [-0.5, 0.5, -0.5], + [-0.5, 0.5, -0.5], ]; - let nx_pos: Vec<[f32;3]> = vec![ - [-0.5, -0.5, 0.5], - [0.5, -0.5, 0.5], - [0.5, 0.5, 0.5], - [0.5, 0.5, 0.5], - [-0.5, 0.5, 0.5], - [-0.5, -0.5, 0.5], + let nx_pos: Vec<[f32; 3]> = vec![ + [-0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [0.5, 0.5, 0.5], + [0.5, 0.5, 0.5], + [-0.5, 0.5, 0.5], + [-0.5, -0.5, 0.5], ]; - let py_pos: Vec<[f32;3]> = vec![ - [-0.5, 0.5, 0.5], - [-0.5, 0.5, -0.5], + let py_pos: Vec<[f32; 3]> = vec![ + [-0.5, 0.5, 0.5], + [-0.5, 0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], - [-0.5, -0.5, 0.5], - [-0.5, 0.5, 0.5], + [-0.5, -0.5, 0.5], + [-0.5, 0.5, 0.5], ]; - let ny_pos: Vec<[f32;3]> = vec![ - [0.5, 0.5, 0.5], + let ny_pos: Vec<[f32; 3]> = vec![ + [0.5, 0.5, 0.5], [0.5, -0.5, -0.5], - [0.5, 0.5, -0.5], + [0.5, 0.5, -0.5], [0.5, -0.5, -0.5], - [0.5, 0.5, 0.5], - [0.5, -0.5, 0.5], + [0.5, 0.5, 0.5], + [0.5, -0.5, 0.5], ]; - let pz_pos: Vec<[f32;3]> = vec![ + let pz_pos: Vec<[f32; 3]> = vec![ [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], - [0.5, -0.5, 0.5], - [0.5, -0.5, 0.5], - [-0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [-0.5, -0.5, 0.5], [-0.5, -0.5, -0.5], ]; - let nz_pos: Vec<[f32;3]> = vec![ - [-0.5, 0.5, -0.5], - [0.5, 0.5, 0.5], - [0.5, 0.5, -0.5], - [ 0.5, 0.5, 0.5], - [-0.5, 0.5, -0.5], - [-0.5, 0.5, 0.5], + let nz_pos: Vec<[f32; 3]> = vec![ + [-0.5, 0.5, -0.5], + [0.5, 0.5, 0.5], + [0.5, 0.5, -0.5], + [0.5, 0.5, 0.5], + [-0.5, 0.5, -0.5], + [-0.5, 0.5, 0.5], ]; - - - - let px_pos_water: Vec<[f32;3]> = vec![ + let px_pos_water: Vec<[f32; 3]> = vec![ [-0.5, -0.5, -0.5], - [ 0.5, 0.40, -0.5], + [0.5, 0.40, -0.5], [0.5, -0.5, -0.5], - [0.5, 0.40, -0.5], + [0.5, 0.40, -0.5], [-0.5, -0.5, -0.5], - [-0.5, 0.40, -0.5], + [-0.5, 0.40, -0.5], ]; - let nx_pos_water: Vec<[f32;3]> = vec![ - [-0.5, -0.5, 0.5], - [0.5, -0.5, 0.5], - [0.5, 0.40, 0.5], - [0.5, 0.40, 0.5], - [-0.5, 0.40, 0.5], - [-0.5, -0.5, 0.5], + let nx_pos_water: Vec<[f32; 3]> = vec![ + [-0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [0.5, 0.40, 0.5], + [0.5, 0.40, 0.5], + [-0.5, 0.40, 0.5], + [-0.5, -0.5, 0.5], ]; - let py_pos_water: Vec<[f32;3]> = vec![ - [-0.5, 0.40, 0.5], - [-0.5, 0.40, -0.5], + let py_pos_water: Vec<[f32; 3]> = vec![ + [-0.5, 0.40, 0.5], + [-0.5, 0.40, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], - [-0.5, -0.5, 0.5], - [-0.5, 0.40, 0.5], + [-0.5, -0.5, 0.5], + [-0.5, 0.40, 0.5], ]; - let ny_pos_water: Vec<[f32;3]> = vec![ - [0.5, 0.40, 0.5], + let ny_pos_water: Vec<[f32; 3]> = vec![ + [0.5, 0.40, 0.5], [0.5, -0.5, -0.5], - [0.5, 0.40, -0.5], + [0.5, 0.40, -0.5], [0.5, -0.5, -0.5], - [0.5, 0.40, 0.5], - [0.5, -0.5, 0.5], + [0.5, 0.40, 0.5], + [0.5, -0.5, 0.5], ]; - let pz_pos_water: Vec<[f32;3]> = vec![ + let pz_pos_water: Vec<[f32; 3]> = vec![ [-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], - [0.5, -0.5, 0.5], - [0.5, -0.5, 0.5], - [-0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [0.5, -0.5, 0.5], + [-0.5, -0.5, 0.5], [-0.5, -0.5, -0.5], ]; - let nz_pos_water: Vec<[f32;3]> = vec![ - [-0.5, 0.5, -0.5], - [0.5, 0.5, 0.5], - [0.5, 0.5, -0.5], - [ 0.5, 0.5, 0.5], - [-0.5, 0.5, -0.5], - [-0.5, 0.5, 0.5], + let nz_pos_water: Vec<[f32; 3]> = vec![ + [-0.5, 0.5, -0.5], + [0.5, 0.5, 0.5], + [0.5, 0.5, -0.5], + [0.5, 0.5, 0.5], + [-0.5, 0.5, -0.5], + [-0.5, 0.5, 0.5], ]; - - - - - - let px_uv: Vec<[f32;2]> = vec![ + let px_uv: Vec<[f32; 2]> = vec![ //GRASS [4.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], @@ -198,141 +187,136 @@ impl BlockModel { [9.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], - [1.0, 1.0], [0.0, 0.0], [0.0, 1.0], [0.0, 0.0], [1.0, 1.0], [1.0, 0.0], - ]; - let nx_uv: Vec<[f32;2]> = vec![ -//GRASS + let nx_uv: Vec<[f32; 2]> = vec![ + //GRASS [4.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], -//STONE + //STONE [2.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], -//DIRT + //DIRT [3.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], -//WATER + //WATER [5.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], -//WOOD PLANKS + //WOOD PLANKS [6.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], -//LOG SIDE + //LOG SIDE [7.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], -//SAND + //SAND [9.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], -//LEAVES + //LEAVES [10.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], - [4.0, 1.0], [3.0, 1.0], [3.0, 0.0], [3.0, 0.0], [4.0, 0.0], [4.0, 1.0], - ]; - let py_uv: Vec<[f32;2]> = vec![ -//GRASS + let py_uv: Vec<[f32; 2]> = vec![ + //GRASS [4.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], -//STONE + //STONE [2.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], -//DIRT + //DIRT [3.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], -//WATER + //WATER [5.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], -//WOOD PLANKS + //WOOD PLANKS [6.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], -//LOG SIDE + //LOG SIDE [7.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], -//SAND + //SAND [9.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], -//Leaves + //Leaves [10.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], - [1.0, 0.0], [0.0, 0.0], [0.0, 1.0], @@ -341,135 +325,128 @@ impl BlockModel { [1.0, 0.0], ]; - let ny_uv: Vec<[f32;2]> = vec![ -//GRASS + let ny_uv: Vec<[f32; 2]> = vec![ + //GRASS [3.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], -//STONE + //STONE [1.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], -//DIRT + //DIRT [2.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], -//WATER + //WATER [4.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], -//WOOD PLANKS + //WOOD PLANKS [5.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], -//LOG SIDE + //LOG SIDE [6.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], -//SAND + //SAND [8.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], -//Leaves - [9.0 / 16.0, 0.0 / 16.0], + //Leaves + [9.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], - [9.0 / 16.0, 0.0 / 16.0], - [9.0 / 16.0, 1.0 / 16.0], - + [9.0 / 16.0, 0.0 / 16.0], + [9.0 / 16.0, 1.0 / 16.0], [0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [1.0, 1.0], [0.0, 0.0], [0.0, 1.0], - ]; - let pz_uv: Vec<[f32;2]> = vec![ -//GRASS + let pz_uv: Vec<[f32; 2]> = vec![ + //GRASS [3.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], - -//STONE + //STONE [2.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], - -//DIRT + //DIRT [3.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], - -//WATER + //WATER [5.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], - -//WOOD PLANKS + //WOOD PLANKS [6.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], -//LOG TOP + //LOG TOP [8.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], -//SAND + //SAND [9.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], -//Leaves + //Leaves [10.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], - [1.0, 0.0], [0.0, 0.0], [0.0, 1.0], @@ -479,92 +456,81 @@ impl BlockModel { ]; let nz_uv: Vec<[f32; 2]> = vec![ -//GRASS + //GRASS [1.0 / 16.0, 1.0 / 16.0], [0.0 / 16.0, 0.0 / 16.0], [0.0 / 16.0, 1.0 / 16.0], [0.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], -//STONE + //STONE [2.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [1.0 / 16.0, 1.0 / 16.0], [1.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], -//DIRT + //DIRT [3.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [2.0 / 16.0, 1.0 / 16.0], [2.0 / 16.0, 0.0 / 16.0], [3.0 / 16.0, 1.0 / 16.0], [3.0 / 16.0, 0.0 / 16.0], -//WATER + //WATER [5.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [4.0 / 16.0, 1.0 / 16.0], [4.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], -//WOOD PLANKS + //WOOD PLANKS [6.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [5.0 / 16.0, 1.0 / 16.0], [5.0 / 16.0, 0.0 / 16.0], [6.0 / 16.0, 1.0 / 16.0], [6.0 / 16.0, 0.0 / 16.0], -//LOG TOP + //LOG TOP [8.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], [7.0 / 16.0, 1.0 / 16.0], [7.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], -//SAND + //SAND [9.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [8.0 / 16.0, 1.0 / 16.0], [8.0 / 16.0, 0.0 / 16.0], [9.0 / 16.0, 1.0 / 16.0], [9.0 / 16.0, 0.0 / 16.0], -//Leaves + //Leaves [10.0 / 16.0, 1.0 / 16.0], - [9.0 / 16.0, 0.0 / 16.0], - [9.0 / 16.0, 1.0 / 16.0], - [9.0 / 16.0, 0.0 / 16.0], + [9.0 / 16.0, 0.0 / 16.0], + [9.0 / 16.0, 1.0 / 16.0], + [9.0 / 16.0, 0.0 / 16.0], [10.0 / 16.0, 1.0 / 16.0], [10.0 / 16.0, 0.0 / 16.0], - - [0.0, 1.0], [1.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0], - [0.0, 0.0] - + [0.0, 0.0], ]; - - - let brightness: Vec = vec![ //Back - 0.6, - //Front - 0.8, - //Left - 0.65, - //Right - 0.75, - //Bottom - 0.45, - //Top - 0.95 + 0.6, //Front + 0.8, //Left + 0.65, //Right + 0.75, //Bottom + 0.45, //Top + 0.95, ]; - return BlockModel{ + return BlockModel { px_pos, nx_pos, py_pos, @@ -585,87 +551,85 @@ impl BlockModel { ny_uv, pz_uv, nz_uv, - brightness - } + brightness, + }; } - - pub fn get_px(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + + pub fn get_px(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.px_pos_water - }else{ + } else { &self.px_pos } } - pub fn get_nx(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + pub fn get_nx(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.nx_pos_water - }else{ + } else { &self.nx_pos } } - pub fn get_py(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + pub fn get_py(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.py_pos_water - }else{ + } else { &self.py_pos } } - pub fn get_ny(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + pub fn get_ny(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.ny_pos_water - }else{ + } else { &self.ny_pos } } - pub fn get_pz(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + pub fn get_pz(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.pz_pos_water - }else{ + } else { &self.pz_pos } } - pub fn get_nz(&self, id: u8) -> &Vec<[f32; 3]>{ - if id == 3{ + pub fn get_nz(&self, id: u8) -> &Vec<[f32; 3]> { + if id == 3 { &self.nz_pos_water - }else{ + } else { &self.nz_pos } } - //UV FOR EACH BLOCK - pub fn get_px_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_px_uv(&self) -> &Vec<[f32; 2]> { &self.px_uv } - pub fn get_nx_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_nx_uv(&self) -> &Vec<[f32; 2]> { &self.nx_uv } - pub fn get_py_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_py_uv(&self) -> &Vec<[f32; 2]> { &self.py_uv } - pub fn get_ny_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_ny_uv(&self) -> &Vec<[f32; 2]> { &self.ny_uv } - pub fn get_pz_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_pz_uv(&self) -> &Vec<[f32; 2]> { &self.pz_uv } - pub fn get_nz_uv(&self) -> &Vec<[f32; 2]>{ + pub fn get_nz_uv(&self) -> &Vec<[f32; 2]> { &self.nz_uv } - pub fn get_brightness(&self) -> &Vec{ + pub fn get_brightness(&self) -> &Vec { &self.brightness } - } diff --git a/src/world/chunk.rs b/src/world/chunk.rs index ef26dbb..e930364 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -1,16 +1,16 @@ extern crate noise; -use parking_lot::{Mutex}; -use rand::Rng; -use rand::{SeedableRng}; +use super::block_model::BlockModel; use noise::{Fbm, NoiseFn, Seedable, Worley}; +use parking_lot::Mutex; +use rand::Rng; +use rand::SeedableRng; use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; -use super::{block_model::BlockModel}; use std::collections::HashMap; pub mod block; use std::sync::Arc; pub struct Chunk { - pub position: [f32;3], + pub position: [f32; 3], pub blocks: Vec>>, pub grid_x: i32, pub grid_z: i32, @@ -27,8 +27,23 @@ pub struct Chunk { pub visible_layers: Vec, } -impl Chunk{ - pub fn init(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, i: usize, k:usize, grid_x: i32, grid_z: i32, position: [f32;3], square_chunk_width: &usize, world_gen_seed: &u32, mid_height: &u8, set_blocks: &mut HashMap, underground_height: &u8, sky_height: &u8, chunk_distance: usize, display: &glium::Display) -> Chunk{ +impl Chunk { + pub fn init( + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + i: usize, + k: usize, + grid_x: i32, + grid_z: i32, + position: [f32; 3], + square_chunk_width: &usize, + world_gen_seed: &u32, + mid_height: &u8, + set_blocks: &mut HashMap, + underground_height: &u8, + sky_height: &u8, + chunk_distance: usize, + display: &glium::Display, + ) -> Chunk { let mut blocks: Vec>> = vec![]; // Perlin smooth rolling hills @@ -42,19 +57,57 @@ impl Chunk{ // Open simplex is very very flat and low heigh variaton // Hybrd multi is very extreme. Maybe not use this one - let end_pos = generate_chunk(change_block, i, k, &mut blocks, *square_chunk_width, position, grid_x, grid_z, *world_gen_seed, *mid_height, *underground_height, *sky_height, false, set_blocks, chunk_distance); - let position_of_chunk = [(position[0] + end_pos[0]) / 2.0, (position[1] + end_pos[1]) / 2.0, (position[2] + end_pos[2]) / 2.0]; - - return Chunk{ + let end_pos = generate_chunk( + change_block, + i, + k, + &mut blocks, + *square_chunk_width, + position, + grid_x, + grid_z, + *world_gen_seed, + *mid_height, + *underground_height, + *sky_height, + false, + set_blocks, + chunk_distance, + ); + let position_of_chunk = [ + (position[0] + end_pos[0]) / 2.0, + (position[1] + end_pos[1]) / 2.0, + (position[2] + end_pos[2]) / 2.0, + ]; + + return Chunk { position: position_of_chunk, blocks, grid_x, grid_z, - vertices: vec![], + vertices: vec![], transparencies: vec![], - vertex_non_transparent: glium::VertexBuffer::new(display, &[block::Vertex{position: [0.0, 0.0, 0.0], tex_coords: [0.0,0.0], opacity: 0.8, brightness: 0.6}]).unwrap(), - vertex_transparent: glium::VertexBuffer::new(display, &[block::Vertex{position: [0.0, 0.0, 0.0], tex_coords: [0.0,0.0], opacity: 0.8, brightness: 0.6}]).unwrap(), - + vertex_non_transparent: glium::VertexBuffer::new( + display, + &[block::Vertex { + position: [0.0, 0.0, 0.0], + tex_coords: [0.0, 0.0], + opacity: 0.8, + brightness: 0.6, + }], + ) + .unwrap(), + vertex_transparent: glium::VertexBuffer::new( + display, + &[block::Vertex { + position: [0.0, 0.0, 0.0], + tex_coords: [0.0, 0.0], + opacity: 0.8, + brightness: 0.6, + }], + ) + .unwrap(), + world_gen_seed: *world_gen_seed, underground_height: *underground_height, sky_height: *sky_height, @@ -64,61 +117,91 @@ impl Chunk{ }; } - pub fn regenerate(&mut self, change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, i: usize, k:usize, set_blocks: &mut HashMap){ + pub fn regenerate( + &mut self, + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + i: usize, + k: usize, + set_blocks: &mut HashMap, + ) { let half_chunk_width = self.blocks[0].len() as f32 / 2.0; - let position = [self.position[0] + half_chunk_width - 0.5 , self.position[1], self.position[2] + half_chunk_width - 0.5]; + let position = [ + self.position[0] + half_chunk_width - 0.5, + self.position[1], + self.position[2] + half_chunk_width - 0.5, + ]; let chunk_length = self.blocks[0].len(); - generate_chunk(change_block, i, k, &mut self.blocks, chunk_length, position, self.grid_x, self.grid_z, self.world_gen_seed, self.mid_height, self.underground_height, self.sky_height, true, set_blocks, self.chunk_distance); - + generate_chunk( + change_block, + i, + k, + &mut self.blocks, + chunk_length, + position, + self.grid_x, + self.grid_z, + self.world_gen_seed, + self.mid_height, + self.underground_height, + self.sky_height, + true, + set_blocks, + self.chunk_distance, + ); } - pub fn get_grid(&self) -> (i32, i32){ + pub fn get_grid(&self) -> (i32, i32) { (self.grid_x, self.grid_z) } pub fn build_mesh(&mut self, block_model: &BlockModel) { - self.vertices.clear(); self.transparencies.clear(); for j in 0..self.blocks[0][0].len() { - if self.visible_layers[j]{ + if self.visible_layers[j] { for i in 0..self.blocks.len() { for k in 0..self.blocks[i].len() { - block::Block::get_mesh(&self.blocks[i][k][j], &mut self.vertices, block_model, &mut self.transparencies); + block::Block::get_mesh( + &self.blocks[i][k][j], + &mut self.vertices, + block_model, + &mut self.transparencies, + ); } } } } } - - pub fn populate_mesh(&mut self, display: &glium::Display){ + + pub fn populate_mesh(&mut self, display: &glium::Display) { let mut non_transparent_vertices: Vec = vec![]; let mut transparent_vertices: Vec = vec![]; for i in 0..self.vertices.len() { - if self.transparencies[i] != true{ + if self.transparencies[i] != true { non_transparent_vertices.push(self.vertices[i]); - }else{ + } else { transparent_vertices.push(self.vertices[i]); } } - - self.vertex_non_transparent = glium::VertexBuffer::new(display, &non_transparent_vertices).unwrap(); + + self.vertex_non_transparent = + glium::VertexBuffer::new(display, &non_transparent_vertices).unwrap(); self.vertex_transparent = glium::VertexBuffer::new(display, &transparent_vertices).unwrap(); self.vertices.clear(); } - pub fn set_layer_visibility(&mut self, visible_layers: Vec){ + pub fn set_layer_visibility(&mut self, visible_layers: Vec) { self.visible_layers = visible_layers; } - pub fn changed_block_visibility(&mut self, m: usize){ + pub fn changed_block_visibility(&mut self, m: usize) { let mut visible = false; for i in 0..self.blocks.len() { for k in 0..self.blocks[i].len() { - if self.blocks[i][k][m].visible{ + if self.blocks[i][k][m].visible { visible = true; break; } @@ -128,11 +211,27 @@ impl Chunk{ } } -fn map_value(value: f64, minimum: u8, maximum: u8) -> i32{ +fn map_value(value: f64, minimum: u8, maximum: u8) -> i32 { return ((maximum - minimum) as f64 * value).floor() as i32 + minimum as i32; } -fn generate_chunk(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chunk_i: usize, chunk_k:usize, blocks: &mut Vec>>, square_chunk_width: usize, position: [f32;3], grid_x: i32, grid_z: i32, world_gen_seed: u32, mid_height: u8, underground_height: u8, sky_height: u8, overwrite: bool, set_blocks: &mut HashMap, chunk_distance: usize) -> [f32;3]{ +fn generate_chunk( + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + chunk_i: usize, + chunk_k: usize, + blocks: &mut Vec>>, + square_chunk_width: usize, + position: [f32; 3], + grid_x: i32, + grid_z: i32, + world_gen_seed: u32, + mid_height: u8, + underground_height: u8, + sky_height: u8, + overwrite: bool, + set_blocks: &mut HashMap, + chunk_distance: usize, +) -> [f32; 3] { let water_level: u8 = 11 + underground_height; let set_blocks_arc = Arc::new(Mutex::new(set_blocks)); @@ -144,98 +243,124 @@ fn generate_chunk(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8) let mut worley = Worley::default().set_seed(world_gen_seed); worley.frequency = 0.01; - //Seed with values that will be used for biliniare interpolation - for i in 0..square_chunk_width{ + for i in 0..square_chunk_width { collumns.push(vec![]); for k in 0..square_chunk_width { - if square_chunk_width <= 8 && (i == 0 && k == 0 || i == 0 && k == square_chunk_width-1 || i == square_chunk_width-1 && k == 0 || i == square_chunk_width-1 && k == square_chunk_width-1) - || square_chunk_width == 10 && ( i % 3 == 0 && k % 3 == 0 ) - || square_chunk_width == 16 && ( i % 3 == 0 && k % 3 == 0 ){ - + if square_chunk_width <= 8 + && (i == 0 && k == 0 + || i == 0 && k == square_chunk_width - 1 + || i == square_chunk_width - 1 && k == 0 + || i == square_chunk_width - 1 && k == square_chunk_width - 1) + || square_chunk_width == 10 && (i % 3 == 0 && k % 3 == 0) + || square_chunk_width == 16 && (i % 3 == 0 && k % 3 == 0) + { let z = position[2] - 1.0 * i as f32; let x = position[0] - 1.0 * k as f32; - - let value_worley = (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - // 0 normal + + let value_worley = + (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + // 0 normal // 1 Ocean // 2 Dessert // 3 Mountains - - let type_biome: u8; + + let type_biome: u8; let max: u8; - if value_worley > 0.97{ + if value_worley > 0.97 { fbm.octaves = 6; fbm.lacunarity = 2.0; fbm.frequency = 0.02; - let value_fbm = (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; + let value_fbm = + (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; max = map_value(value_fbm.powf(1.0), 0, mid_height) as u8 + underground_height; type_biome = 3 - }else if value_worley > 0.9{ + } else if value_worley > 0.9 { fbm.octaves = 4; fbm.frequency = 0.01; fbm.lacunarity = 2.0; - let value_fbm = (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - max= map_value(value_fbm.powf(0.5)/2.0, 0, mid_height) as u8 + underground_height; + let value_fbm = + (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + max = map_value(value_fbm.powf(0.5) / 2.0, 0, mid_height) as u8 + + underground_height; type_biome = 2 - }else if value_worley > 0.5{ + } else if value_worley > 0.5 { fbm.octaves = 5; fbm.frequency = 0.01; fbm.lacunarity = 2.0; - let value_fbm = (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - max= map_value(f64::powi(value_fbm,3), 0, mid_height) as u8 + underground_height; + let value_fbm = + (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + max = map_value(f64::powi(value_fbm, 3), 0, mid_height) as u8 + + underground_height; type_biome = 1 - }else { + } else { fbm.octaves = 6; fbm.frequency = 0.01; fbm.lacunarity = 2.0; - let value_fbm = (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - max= map_value(f64::powi(value_fbm,3), 0, mid_height) as u8 + underground_height; + let value_fbm = + (fbm.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + max = map_value(f64::powi(value_fbm, 3), 0, mid_height) as u8 + + underground_height; type_biome = 0 } let mut has_tree = false; - let mut rng = rand_xoshiro::SplitMix64::seed_from_u64(world_gen_seed as u64 + f64::powi(x as f64, 2) as u64 + f64::powi(z as f64, 4) as u64); - if type_biome != 2 && rng.gen_range(1..50) == 1{ + let mut rng = rand_xoshiro::SplitMix64::seed_from_u64( + world_gen_seed as u64 + + f64::powi(x as f64, 2) as u64 + + f64::powi(z as f64, 4) as u64, + ); + if type_biome != 2 && rng.gen_range(1..50) == 1 { has_tree = true; - if max > water_level+2{ - trees.push((grid_x, grid_z, i, k, (max + underground_height + 6) as usize)); + if max > water_level + 2 { + trees.push(( + grid_x, + grid_z, + i, + k, + (max + underground_height + 6) as usize, + )); } } collumns[i].push((max, has_tree, z, x, type_biome)); - }else{ + } else { collumns[i].push((0, false, 0.0, 0.0, 0)); } } } - - let coll_temp = collumns.clone(); - for i in 0..square_chunk_width{ - if collumns[i][0].0 == 0{ + for i in 0..square_chunk_width { + if collumns[i][0].0 == 0 { continue; } for k in 0..square_chunk_width { - if collumns[i][k].0 == 0{ + if collumns[i][k].0 == 0 { let z = position[2] - 1.0 * i as f32; let x = position[0] - 1.0 * k as f32; - - let value_worley = (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - // 0 normal + + let value_worley = + (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + // 0 normal // 1 Ocean // 2 Dessert // 3 Mountains - - let type_biome: u8; + + let type_biome: u8; let mut next_distance: f32 = 0.0; let mut next_height: f32 = 0.0; - for j in k+1..square_chunk_width{ - if coll_temp[i][j].0 != 0{ + for j in k + 1..square_chunk_width { + if coll_temp[i][j].0 != 0 { next_distance = (j - k) as f32 + 1.0; next_height = coll_temp[i][j].0 as f32; break; @@ -243,88 +368,113 @@ fn generate_chunk(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8) } let mut before_distance: f32 = 0.0; let mut before_height: f32 = 0.0; - for j in (0..k).rev(){ - if coll_temp[i][j].0 != 0{ + for j in (0..k).rev() { + if coll_temp[i][j].0 != 0 { before_distance = (k - j) as f32; before_height = coll_temp[i][j].0 as f32; break; } } - let max: u8 = (next_height * (before_distance/(before_distance+next_distance)) + before_height * (next_distance/(before_distance+next_distance))).round() as u8; + let max: u8 = (next_height * (before_distance / (before_distance + next_distance)) + + before_height * (next_distance / (before_distance + next_distance))) + .round() as u8; - if value_worley > 0.97{ + if value_worley > 0.97 { type_biome = 3 - }else if value_worley > 0.9{ + } else if value_worley > 0.9 { type_biome = 2 - }else if value_worley > 0.5{ + } else if value_worley > 0.5 { type_biome = 6 - }else { + } else { type_biome = 0 } let mut has_tree = false; - let mut rng = rand_xoshiro::SplitMix64::seed_from_u64(world_gen_seed as u64 + f64::powi(x as f64, 2) as u64 + f64::powi(z as f64, 4) as u64); - if type_biome != 2 && rng.gen_range(1..50) == 1{ + let mut rng = rand_xoshiro::SplitMix64::seed_from_u64( + world_gen_seed as u64 + + f64::powi(x as f64, 2) as u64 + + f64::powi(z as f64, 4) as u64, + ); + if type_biome != 2 && rng.gen_range(1..50) == 1 { has_tree = true; - if max > water_level+2{ - trees.push((grid_x, grid_z, i, k, (max + underground_height + 6) as usize)); + if max > water_level + 2 { + trees.push(( + grid_x, + grid_z, + i, + k, + (max + underground_height + 6) as usize, + )); } } collumns[i][k] = (max, has_tree, z, x, type_biome); } - } } let coll_temp = collumns.clone(); - for k in 0..square_chunk_width{ + for k in 0..square_chunk_width { for i in 0..square_chunk_width { - if coll_temp[i][k].0 == 0{ + if coll_temp[i][k].0 == 0 { let z = position[2] - 1.0 * i as f32; let x = position[0] - 1.0 * k as f32; - - let value_worley = (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0)/2.0; - // 0 normal + + let value_worley = + (worley.get([(z as f64 + grid_z as f64), (x as f64 + grid_x as f64)]) + 1.0) + / 2.0; + // 0 normal // 1 Ocean // 2 Dessert // 3 Mountains - - let type_biome: u8; + + let type_biome: u8; let mut next_distance: f32 = 0.0; let mut next_height: f32 = 0.0; - for j in i+1..square_chunk_width{ - if coll_temp[j][k].0 != 0{ - next_distance = (j - i) as f32 +1.0; + for j in i + 1..square_chunk_width { + if coll_temp[j][k].0 != 0 { + next_distance = (j - i) as f32 + 1.0; next_height = coll_temp[j][k].0 as f32; break; } } let mut before_distance: f32 = 0.0; let mut before_height: f32 = 0.0; - for j in (0..i).rev(){ - if coll_temp[j][k].0 != 0{ + for j in (0..i).rev() { + if coll_temp[j][k].0 != 0 { before_distance = (i - j) as f32; before_height = coll_temp[j][k].0 as f32; break; } } - let max: u8 = (next_height * (before_distance/(before_distance+next_distance)) + before_height * (next_distance/(before_distance+next_distance))).round() as u8; - if value_worley > 0.97{ + let max: u8 = (next_height * (before_distance / (before_distance + next_distance)) + + before_height * (next_distance / (before_distance + next_distance))) + .round() as u8; + if value_worley > 0.97 { type_biome = 3 - }else if value_worley > 0.9{ + } else if value_worley > 0.9 { type_biome = 2 - }else if value_worley > 0.5{ + } else if value_worley > 0.5 { type_biome = 6 - }else { + } else { type_biome = 0 } let mut has_tree = false; - let mut rng = rand_xoshiro::SplitMix64::seed_from_u64(world_gen_seed as u64 + f64::powi(x as f64, 2) as u64 + f64::powi(z as f64, 4) as u64); - if type_biome != 2 && rng.gen_range(1..50) == 1{ + let mut rng = rand_xoshiro::SplitMix64::seed_from_u64( + world_gen_seed as u64 + + f64::powi(x as f64, 2) as u64 + + f64::powi(z as f64, 4) as u64, + ); + if type_biome != 2 && rng.gen_range(1..50) == 1 { has_tree = true; - if max > water_level+2{ - trees.push((grid_x, grid_z, i, k, (max + underground_height + 6) as usize)); + if max > water_level + 2 { + trees.push(( + grid_x, + grid_z, + i, + k, + (max + underground_height + 6) as usize, + )); } } collumns[i][k] = (max, has_tree, z, x, type_biome); @@ -333,15 +483,15 @@ fn generate_chunk(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8) } let collumns = Arc::new(Mutex::new(&collumns)); - if !overwrite { - for i in 0..square_chunk_width{ + if !overwrite { + for i in 0..square_chunk_width { let collumn: Vec> = vec![]; blocks.push(collumn); for k in 0..square_chunk_width { let row: Vec = vec![]; blocks[i].push(row); - for _j in 0..(mid_height + underground_height + sky_height) as usize{ + for _j in 0..(mid_height + underground_height + sky_height) as usize { blocks[i][k].push(block::Block::init([0.0, 0.0, 0.0], 1)); } } @@ -349,50 +499,89 @@ fn generate_chunk(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8) } // Fancy way to iterate over the vector. It makes a thread for each of the vectors in the vector - (blocks).par_iter_mut().enumerate().for_each(|(i, val)| { + (blocks).par_iter_mut().enumerate().for_each(|(i, val)| { let collumns_t = Arc::clone(&collumns); let set_blocks_arc_t = Arc::clone(&set_blocks_arc); let set_blocks_t = set_blocks_arc_t.lock().clone(); for k in 0..val.len() { let collumn_values = collumns_t.lock()[i][k].clone(); - for j in 0..val[k].len(){ + for j in 0..val[k].len() { let mut number: u8; number = get_set_block(&set_blocks_t, grid_x, grid_z, i, k, j); - if number == 7{ + if number == 7 { remove_key(&mut set_blocks_arc_t.lock(), grid_x, grid_z, i, k, j); } - if number == 241{ - number = get_block_type(j as u8, underground_height + collumn_values.0, water_level, collumn_values.1, underground_height, sky_height, mid_height + underground_height + sky_height, collumn_values.4); + if number == 241 { + number = get_block_type( + j as u8, + underground_height + collumn_values.0, + water_level, + collumn_values.1, + underground_height, + sky_height, + mid_height + underground_height + sky_height, + collumn_values.4, + ); } - - val[k as usize][j as usize].regenerate([collumn_values.3, position[1] + 1.0 * j as f32, collumn_values.2], number); + + val[k as usize][j as usize].regenerate( + [ + collumn_values.3, + position[1] + 1.0 * j as f32, + collumn_values.2, + ], + number, + ); } } }); - for i in 0..trees.len(){ - set_tree(change_block, chunk_i, chunk_k, blocks, &mut set_blocks_arc.lock(), trees[i].0, trees[i].1, trees[i].2, trees[i].3, trees[i].4, chunk_distance, (mid_height + underground_height + sky_height) as usize); + for i in 0..trees.len() { + set_tree( + change_block, + chunk_i, + chunk_k, + blocks, + &mut set_blocks_arc.lock(), + trees[i].0, + trees[i].1, + trees[i].2, + trees[i].3, + trees[i].4, + chunk_distance, + (mid_height + underground_height + sky_height) as usize, + ); } - return blocks[blocks.len()-1][blocks[blocks.len()-1].len()-1][0].position.clone(); + return blocks[blocks.len() - 1][blocks[blocks.len() - 1].len() - 1][0] + .position + .clone(); } -fn get_block_type(block_height: u8, max_collumn_height: u8, water_level: u8, has_plant: bool, underground_height: u8, _sky_height: u8,_height_limitt: u8, biome: u8) -> u8 { - if biome == 2{ - +fn get_block_type( + block_height: u8, + max_collumn_height: u8, + water_level: u8, + has_plant: bool, + underground_height: u8, + _sky_height: u8, + _height_limitt: u8, + biome: u8, +) -> u8 { + if biome == 2 { //Check if the j value is below undeground height. Everything underground is stone if block_height < underground_height { - return 1;//Stone + return 1; //Stone } - + //Check if j above collumn max if block_height > max_collumn_height { //Check if j is bellow water - if block_height < water_level{ - return 3;//Water - }else{ - return 240;// AIR + if block_height < water_level { + return 3; //Water + } else { + return 240; // AIR } } @@ -400,41 +589,46 @@ fn get_block_type(block_height: u8, max_collumn_height: u8, water_level: u8, has return 6; //Sand } - if max_collumn_height - block_height < 3 && block_height > water_level + 2{ + if max_collumn_height - block_height < 3 && block_height > water_level + 2 { return 6; //Sand } return 1; // Stone - }else{ + } else { //Check if the collumn has a plant, j is above water, the max height of the collumn is 2 blocks above water. J is up to 6 blocks bellow max collumn height and that j is aboce max collumn height - if has_plant && block_height > water_level && water_level + 2 < max_collumn_height && block_height < max_collumn_height + 6 && block_height > max_collumn_height { + if has_plant + && block_height > water_level + && water_level + 2 < max_collumn_height + && block_height < max_collumn_height + 6 + && block_height > max_collumn_height + { return 5; // Wood log } //Check if the j value is below undeground height. Everything underground is stone if block_height < underground_height { - return 1;//Stone + return 1; //Stone } - + //Check if j above collumn max if block_height > max_collumn_height { //Check if j is bellow water - if block_height < water_level{ - return 3;//Water - }else{ - return 240;// AIR + if block_height < water_level { + return 3; //Water + } else { + return 240; // AIR } } if block_height == max_collumn_height { - if block_height > water_level + 2{ + if block_height > water_level + 2 { return 0; //Grass - }else{ + } else { return 6; //Sand } } - if max_collumn_height - block_height < 3 && block_height > water_level + 2{ + if max_collumn_height - block_height < 3 && block_height > water_level + 2 { return 2; // Dirt bellow grass } @@ -442,21 +636,62 @@ fn get_block_type(block_height: u8, max_collumn_height: u8, water_level: u8, has } } -fn get_set_block(set_blocks: &HashMap, grid_x: i32, grid_z: i32, i: usize, k: usize, j: usize) -> u8{ - let key = [grid_x.to_string(), grid_z.to_string(), i.to_string(), k.to_string(), j.to_string()].join(""); - - match set_blocks.get(&key){ +fn get_set_block( + set_blocks: &HashMap, + grid_x: i32, + grid_z: i32, + i: usize, + k: usize, + j: usize, +) -> u8 { + let key = [ + grid_x.to_string(), + grid_z.to_string(), + i.to_string(), + k.to_string(), + j.to_string(), + ] + .join(""); + + match set_blocks.get(&key) { Some(value) => return *value, None => return 241, } } -fn remove_key(set_blocks: &mut HashMap, grid_x: i32, grid_z: i32, i: usize, k: usize, j: usize){ - let key = [grid_x.to_string(), grid_z.to_string(), i.to_string(), k.to_string(), j.to_string()].join(""); +fn remove_key( + set_blocks: &mut HashMap, + grid_x: i32, + grid_z: i32, + i: usize, + k: usize, + j: usize, +) { + let key = [ + grid_x.to_string(), + grid_z.to_string(), + i.to_string(), + k.to_string(), + j.to_string(), + ] + .join(""); set_blocks.remove(&key); } -fn set_tree(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chunk_i: usize, chunk_k:usize, blocks: &mut Vec>>, set_blocks: &mut HashMap, grid_x: i32, grid_z: i32, i: usize, k: usize, j: usize, chunk_distance: usize, j_max: usize){ +fn set_tree( + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + chunk_i: usize, + chunk_k: usize, + blocks: &mut Vec>>, + set_blocks: &mut HashMap, + grid_x: i32, + grid_z: i32, + i: usize, + k: usize, + j: usize, + chunk_distance: usize, + j_max: usize, +) { // UP is +X // Left is +Z // Right is -Z @@ -466,11 +701,76 @@ fn set_tree(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chu // XXX // X 5 - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize, j, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize, j, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize +1,j, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize -1,j, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize ,j, chunk_distance, j_max); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize, + j, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize, + j, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize + 1, + j, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize - 1, + j, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize, + j, + chunk_distance, + j_max, + ); // XXX // XXXXX @@ -478,36 +778,335 @@ fn set_tree(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chu // XXXXX // XXX 20 - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize +1, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize -1, j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize +1, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize -1, j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize , j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize +1 , j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize -1 , j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize +1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize -1, j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize +1, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize -1, j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize +1, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize -1, j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize ,k as isize -2, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize -2, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize -2, j-1, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize ,k as isize +2, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize +2, j-1, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize +2, j-1, chunk_distance, j_max); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize + 1, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize - 1, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize + 1, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize - 1, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize + 1, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize - 1, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize + 1, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize - 1, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize + 1, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize - 1, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize - 2, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize - 2, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize - 2, + j - 1, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize + 2, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize + 2, + j - 1, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize + 2, + j - 1, + chunk_distance, + j_max, + ); // XXXXX // XXXXX @@ -515,54 +1114,383 @@ fn set_tree(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chu // XXXXX // XXXXX 20 - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize +1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize -1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize +1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize, k as isize -1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize , j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize +1 , j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize -1 , j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize +1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize -1, j-2, chunk_distance, j_max); - - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize +1, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize -1, j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize ,k as isize -2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize -2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize -2, j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize ,k as isize +2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -1,k as isize +2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +1,k as isize +2, j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize +2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize +2, j-2, chunk_distance, j_max); - - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize +2,k as isize -2, j-2, chunk_distance, j_max); - set_tree_block(change_block, chunk_i, chunk_k, blocks, set_blocks, grid_x, grid_z, i as isize -2,k as isize -2, j-2, chunk_distance, j_max); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize + 1, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize - 1, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize - 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize - 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize - 2, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize, + k as isize + 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 1, + k as isize + 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 1, + k as isize + 2, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize + 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize + 2, + j - 2, + chunk_distance, + j_max, + ); + + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize + 2, + k as isize - 2, + j - 2, + chunk_distance, + j_max, + ); + set_tree_block( + change_block, + chunk_i, + chunk_k, + blocks, + set_blocks, + grid_x, + grid_z, + i as isize - 2, + k as isize - 2, + j - 2, + chunk_distance, + j_max, + ); } -fn set_tree_block(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, chunk_i: usize, chunk_k: usize, blocks: &mut Vec>>, set_blocks: &mut HashMap, grid_x: i32, grid_z: i32, i: isize, k: isize, j: usize, chunk_distance: usize, j_max: usize){ - if j_max > j{ - if i < 0 || k < 0 || i > (blocks[0].len() - 1) as isize || k > (blocks[0].len() - 1) as isize || j > (blocks[0][0].len() - 1){ +fn set_tree_block( + change_block: &mut Vec<(usize, usize, usize, usize, usize, u8)>, + chunk_i: usize, + chunk_k: usize, + blocks: &mut Vec>>, + set_blocks: &mut HashMap, + grid_x: i32, + grid_z: i32, + i: isize, + k: isize, + j: usize, + chunk_distance: usize, + j_max: usize, +) { + if j_max > j { + if i < 0 + || k < 0 + || i > (blocks[0].len() - 1) as isize + || k > (blocks[0].len() - 1) as isize + || j > (blocks[0][0].len() - 1) + { let mut chunk_i_mut = chunk_i as i32; - let mut chunk_k_mut = chunk_k as i32; + let mut chunk_k_mut = chunk_k as i32; let mut grid_x_set = grid_x; let mut grid_z_set = grid_z; let mut i_set = i; let mut k_set = k; - + if i < 0 { i_set = blocks[0].len() as isize + i; grid_x_set = grid_x_set - 1; chunk_i_mut = chunk_i_mut - 1; - } else if i > blocks[0].len() as isize - 1{ + } else if i > blocks[0].len() as isize - 1 { i_set = i - blocks[0].len() as isize; grid_x_set = grid_x_set + 1; chunk_i_mut = chunk_i_mut + 1; @@ -572,31 +1500,44 @@ fn set_tree_block(change_block: &mut Vec<(usize, usize, usize, usize, usize, u8) k_set = blocks[0].len() as isize + k; grid_z_set = grid_z_set - 1; chunk_k_mut = chunk_k_mut - 1; - }else if k > blocks[0].len() as isize - 1 { + } else if k > blocks[0].len() as isize - 1 { k_set = k - blocks[0].len() as isize; grid_z_set = grid_z_set + 1; chunk_k_mut = chunk_k_mut + 1; - } - + } + // If block is outisde the chunk border that is currently rendered add it as set block // If the block is inside the currently rendered chunk border put it in change blocks - if (chunk_distance as i32 - 1) >= chunk_k_mut && (chunk_distance as i32 - 1) >= chunk_i_mut && 0 <= chunk_k_mut && 0 <= chunk_i_mut{ - - change_block.push((chunk_i_mut as usize, chunk_k_mut as usize, i_set as usize, k_set as usize, j, 7)) - - }else{ - - let key = [grid_x_set.to_string(), grid_z_set.to_string(), i_set.to_string(), k_set.to_string(), j.to_string()].join(""); - - if !set_blocks.contains_key(&key){ + if (chunk_distance as i32 - 1) >= chunk_k_mut + && (chunk_distance as i32 - 1) >= chunk_i_mut + && 0 <= chunk_k_mut + && 0 <= chunk_i_mut + { + change_block.push(( + chunk_i_mut as usize, + chunk_k_mut as usize, + i_set as usize, + k_set as usize, + j, + 7, + )) + } else { + let key = [ + grid_x_set.to_string(), + grid_z_set.to_string(), + i_set.to_string(), + k_set.to_string(), + j.to_string(), + ] + .join(""); + + if !set_blocks.contains_key(&key) { set_blocks.insert(key, 7); } } - - }else{ + } else { blocks[i as usize][k as usize][j].id = 7; } } } - diff --git a/src/world/chunk/block.rs b/src/world/chunk/block.rs index de93926..c3e0c94 100644 --- a/src/world/chunk/block.rs +++ b/src/world/chunk/block.rs @@ -1,7 +1,7 @@ -use crate::{world::block_model::BlockModel}; +use crate::world::block_model::BlockModel; // pub enum BlockId{ - + // GRASS = 0, // STONE = 1, // DIRT = 2, @@ -23,110 +23,148 @@ pub struct Block { pub position: [f32; 3], pub id: u8, pub visible: bool, - pub sides: Vec + pub sides: Vec, } impl Block { - pub fn init(position: [f32; 3], id: u8) -> Block{ - - return Block{ + pub fn init(position: [f32; 3], id: u8) -> Block { + return Block { position, id, visible: true, - sides: vec![] + sides: vec![], }; } - pub fn regenerate(&mut self, position: [f32; 3], id: u8){ + pub fn regenerate(&mut self, position: [f32; 3], id: u8) { self.position = position; self.id = id; } - pub fn set_invisible(&mut self){ + pub fn set_invisible(&mut self) { self.visible = false; } - pub fn get_mesh(&self, vertices: &mut Vec, block_model: &BlockModel, transparencies: &mut Vec){ + pub fn get_mesh( + &self, + vertices: &mut Vec, + block_model: &BlockModel, + transparencies: &mut Vec, + ) { if self.visible { for i in 0..self.sides.len() { - if self.sides[i] == true{ + if self.sides[i] == true { match i { - 0 => for n in 0..6{ - vertices.push(Vertex { - position: add(BlockModel::get_px(block_model, self.id)[n], self.position), - tex_coords: BlockModel::get_px_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + 0 => { + for n in 0..6 { + vertices.push(Vertex { + position: add( + BlockModel::get_px(block_model, self.id)[n], + self.position, + ), + tex_coords: BlockModel::get_px_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - 1 => for n in 0..6{ - vertices.push(Vertex { - position: add(BlockModel::get_nx(block_model, self.id)[n], self.position), - tex_coords: BlockModel::get_nx_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + } + } + 1 => { + for n in 0..6 { + vertices.push(Vertex { + position: add( + BlockModel::get_nx(block_model, self.id)[n], + self.position, + ), + tex_coords: BlockModel::get_nx_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - 2 =>for n in 0..6{ - vertices.push(Vertex { - position: add(BlockModel::get_py(block_model, self.id)[n], self.position), - tex_coords: BlockModel::get_py_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + } + } + 2 => { + for n in 0..6 { + vertices.push(Vertex { + position: add( + BlockModel::get_py(block_model, self.id)[n], + self.position, + ), + tex_coords: BlockModel::get_py_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - 3 => for n in 0..6{ - vertices.push(Vertex { - position: add(BlockModel::get_ny(block_model, self.id)[n], self.position), - tex_coords: BlockModel::get_ny_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + } + } + 3 => { + for n in 0..6 { + vertices.push(Vertex { + position: add( + BlockModel::get_ny(block_model, self.id)[n], + self.position, + ), + tex_coords: BlockModel::get_ny_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - 4 => for n in 0..6{ - vertices.push(Vertex { - position: add(BlockModel::get_pz(block_model, self.id)[n], self.position), - tex_coords: BlockModel::get_pz_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + } + } + 4 => { + for n in 0..6 { + vertices.push(Vertex { + position: add( + BlockModel::get_pz(block_model, self.id)[n], + self.position, + ), + tex_coords: BlockModel::get_pz_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - 5 => for n in 0..6{ + } + } + 5 => { + for n in 0..6 { //Special case for water - let mut position_nz = add(BlockModel::get_nz(block_model, self.id)[n], self.position); + let mut position_nz = + add(BlockModel::get_nz(block_model, self.id)[n], self.position); position_nz[1] -= (self.is_water()) as i32 as f32 * 0.1; - vertices.push(Vertex { - position: position_nz, - tex_coords: BlockModel::get_nz_uv(block_model)[(self.id as usize * 6) + n], - opacity: if self.is_water(){0.9}else{1.0}, - brightness: BlockModel::get_brightness(block_model)[i] + vertices.push(Vertex { + position: position_nz, + tex_coords: BlockModel::get_nz_uv(block_model) + [(self.id as usize * 6) + n], + opacity: if self.is_water() { 0.9 } else { 1.0 }, + brightness: BlockModel::get_brightness(block_model)[i], }); transparencies.push(self.is_transparent()); - }, - _ => println!("Bad block") + } + } + _ => println!("Bad block"), } } } } } - - pub fn is_air(&self) -> bool{ + + pub fn is_air(&self) -> bool { return self.id == 240; } - pub fn is_water(&self) -> bool{ + pub fn is_water(&self) -> bool { return self.id == 3; } fn is_transparent(&self) -> bool { - if self.is_water(){ + if self.is_water() { return true; - }else{ + } else { return false; } } @@ -134,7 +172,7 @@ impl Block { fn add(arr1: [f32; 3], arr2: [f32; 3]) -> [f32; 3] { //Add two vectors - let mut result: [f32; 3] = [0.0,0.0,0.0]; + let mut result: [f32; 3] = [0.0, 0.0, 0.0]; result[0] = arr1[0] + arr2[0]; result[1] = arr1[1] + arr2[1];