From 589fe0e1c0a1ebfc413489d50396ef414ffd142d Mon Sep 17 00:00:00 2001 From: deadvey Date: Tue, 11 Feb 2025 21:07:55 +0000 Subject: [PATCH] stuff --- data/test.data | 49 ------ data/world_above.map | 71 +++++++++ data/{ground_map.data => world_ground.map} | 0 src/main.rs | 173 ++++++++++++++++----- 4 files changed, 207 insertions(+), 86 deletions(-) delete mode 100644 data/test.data create mode 100644 data/world_above.map rename data/{ground_map.data => world_ground.map} (100%) diff --git a/data/test.data b/data/test.data deleted file mode 100644 index 8caace7..0000000 --- a/data/test.data +++ /dev/null @@ -1,49 +0,0 @@ -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' -'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' diff --git a/data/world_above.map b/data/world_above.map new file mode 100644 index 0000000..7bbda81 --- /dev/null +++ b/data/world_above.map @@ -0,0 +1,71 @@ + 88888 88888 ♣ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 8 8888888888888888888888888 8 ^^^^^^^^^^^^^^^^^^^^^^^^^ + 8 8 ^^^^^^^^^^^^^^^^^^^^^^ + 88888888 88888888 888 888888 ^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 ♣ ♣ ^^^^^^^^^^^^^^^^^^^^^^^ + 8888888888888888 88888888888 ^^^^^^^^^^^^^^^^^^ ^^ + 8 8 ^^^^^^^^^^^^^^ ^^^ + 8 8888888 88888888 88888888 ^^^^^^^^^^^ ^^^^^ + 8 8 8 8 8 8 8 ♣ ^^^^^^^ ^^^ + 8 8 8 8 8 8 ^^^^ + ♣ 8 8 8 88888888 8 ♣ ^^ + 8 88888 8 8 8 ^ + 8 8888 8 88 88888888 ^^ + 8 88888 8 8 8 8 8 ^^^ + 8 8 8 8 8 8 8 ^^^ + 8 88888888 8 88888888 8 ♣ ♣ ^^^^ + 8 8 888888 8 ♣ ^^^ + 88888 8 8 8 888 88888 ♣ ^^^^ + 8 8 8 8 8 ♣ ♣ ^^ + 8 88888888888888 8888888 8 ♣ ^ + 8 8 8 8 ♣ ^ + 88888 88888 ♣ ^ + ♣ ♣ ^ + ♣ ^ + ♣ ♣ ♣ ^ ^^ + ♣ ♣ ♣ ♣ ^^^^^^^^^^ + ♣ ♣ ♣ ♣ ^^^^^^^^ + ♣ ♣ ♣ ♣ ^^^^^^^ + ♣ ♣ ♣ ♣ ♣ ^^^ + ♣ ♣ ♣ ^ + ♣ ♣ ♣ + ♣ ♣ + ♣ ♣ + ♣ ♣ ♣ ♣ ♣ + ♣ ♣ + ♣ ♣ ♣ + ♣ ♣ ♣ + ♣ ♣ ♣ + ♣ ♣ + ♣ ♣ + ♣ ♣ + ♣ ♣ + ♣ ♣ + ♣ + ♣ + ♣ + + ♣ + + + ♣ ♣ + + + + + + + + + + + + %%%% + % % + %%%% + + diff --git a/data/ground_map.data b/data/world_ground.map similarity index 100% rename from data/ground_map.data rename to data/world_ground.map diff --git a/src/main.rs b/src/main.rs index c263d63..7031a7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::fs; // For reading the map and save files use colored::Colorize; // For coloring the output use std::collections::HashMap; +use std::process::{Command}; struct Coordinates { x: i16, @@ -8,32 +9,72 @@ struct Coordinates { z: i16, } +fn clear_screen() +{ + Command::new("clear") + .status() + .expect("Failed to clear screen"); +} + // This function reads the map file and puts it into a 2d vector of integers, each integer // Refers to a block (see top comment) and the function returns this vector -fn parse_map() -> Vec> { - let mut ground_map: Vec> = Vec::new(); // Initialises the map vector +fn parse_map(map: &str) -> Result<(Vec>, Vec>), u8> +{ + let mut ground_map: Vec> = Vec::new(); // Initialises the Ground map vector + let mut above_map: Vec> = Vec::new(); // Initialises the Above Ground map vector + let ground_file = format!("data/{}_ground.map",map); + let above_file = format!("data/{}_above.map",map); // Read the ground map file - let file_contents: String = fs::read_to_string("data/ground_map.data").unwrap(); // Reads the file - let ground_rows = file_contents.split("\n").collect::>(); // Split the file contents into rows + if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file) + { + let ground_file_contents = parsed_ground_file_contents; + let ground_rows = ground_file_contents.split("\n").collect::>(); // Split the file contents into rows - // Loop over each row to parse each column of it - for row in 0..ground_rows.len() { - // Turn each column into an integer and collect it into a row vector - let current_row = ground_rows[row].chars().collect(); - ground_map.push(current_row); // Then push this vector to the full map vector + // Loop over each row to parse each column of it + for row in 0..ground_rows.len() + { + // Turn each column into an integer and collect it into a row vector + let current_row = ground_rows[row].chars().collect(); + ground_map.push(current_row); // Then push this vector to the full map vector + } + } + else + { + eprintln!("Error: Could not read file: {}", ground_file); + return Err(1) + } + // Read the above ground map file + if let Ok(parsed_above_file_contents) = fs::read_to_string(&above_file) + { + let above_file_contents = parsed_above_file_contents; + let above_rows = above_file_contents.split("\n").collect::>(); // Split the file contents into rows + + // Loop over each row to parse each column of it + for row in 0..above_rows.len() + { + // Turn each column into an integer and collect it into a row vector + let current_row = above_rows[row].chars().collect(); + above_map.push(current_row); // Then push this vector to the full map vector + } + } + else + { + eprintln!("Error: Could not read file: {}", ground_file); + return Err(1) } - return ground_map + return Ok((ground_map, above_map)) } // Output the map based on map vector -fn output_map(ground_map: Vec>, player_coordinates: Coordinates, distance_you_can_see: [i16; 2]) { - print!("{}[2J", 27 as char); // Clear screen - - let blocks: HashMap = [ +fn output_map(ground_map: Vec>, above_map: Vec>, player_coordinates: Coordinates, distance_you_can_see: [i16; 2]) +{ + let blocks: HashMap = + [ ('X', [255,0, 0]), // Null ('\'', [0, 255,0]), // Grass + ('"', [153,102,0]), // Dirt (';', [128,128,128]), // Cobbles ('~', [0, 0, 255]), // Water/sea ('D', [0, 128,128]), // Diamond @@ -42,23 +83,56 @@ fn output_map(ground_map: Vec>, player_coordinates: Coordinates, dista ('%', [195,162,103]), // Wood planks (':', [195,162,103]), // Sand ('[', [196,196,196]), // Stone + ('^', [196,128,196]), // Mountain + ('8', [255,196,196]), // Stone bricks + ('♣', [0, 153,51]), // Oak tree ] .iter().cloned().collect(); // Loop over array and print each block - for i in player_coordinates.z - distance_you_can_see[1]..player_coordinates.z + distance_you_can_see[1] { - if i >= 0 && i < ground_map.len() as i16 { - for j in player_coordinates.x - distance_you_can_see[0]..player_coordinates.x+distance_you_can_see[0] { - if j >= 0 && j < ground_map[i as usize].len() as i16 { + for i in player_coordinates.z - distance_you_can_see[1]..player_coordinates.z + distance_you_can_see[1] + { + if i >= 0 && i < ground_map.len() as i16 + { + for j in player_coordinates.x - distance_you_can_see[0]..player_coordinates.x+distance_you_can_see[0] + { + if j >= 0 && j < ground_map[i as usize].len() as i16 + { let character: char = ground_map[i as usize][j as usize]; - if i == player_coordinates.z && j == player_coordinates.x { - print!("{}"," " + let above_character: char = above_map[i as usize][j as usize]; + // Check if the character is at the current coordinates + if i == player_coordinates.z && j == player_coordinates.x + { + print! + ( + "{}", + " " .on_truecolor(40,235,181) .truecolor(0,0,0) ) } - else if blocks.contains_key(&character) { - print!("{}",character // Output's the current block + // Else check if there's something on the above y level + else if blocks.contains_key(&above_character) && above_character != ' ' + { + print! + ( + "{}", + above_character // Output's the current block + .to_string() + .truecolor( + blocks.get(&above_character).unwrap()[0], + blocks.get(&above_character).unwrap()[1], + blocks.get(&above_character).unwrap()[2] + ) + ) + } + // Else print what is below (the ground) + else if blocks.contains_key(&character) + { + print! + ( + "{}", + character // Output's the current block .to_string() .truecolor( blocks.get(&character).unwrap()[0], @@ -67,8 +141,13 @@ fn output_map(ground_map: Vec>, player_coordinates: Coordinates, dista ) ) } - else { - print!("{}","X" + // If the character is invalid print a null value + else + { + print! + ( + "{}", + "X" .to_string() .truecolor( blocks.get(&'X').unwrap()[0], @@ -84,25 +163,45 @@ fn output_map(ground_map: Vec>, player_coordinates: Coordinates, dista } } -fn main() { +fn main() +{ let debug_mode = true; + let (screen_width, screen_height) = termion::terminal_size().unwrap(); + let distance_you_can_see: [i16; 2] = + [ + (screen_width as f32 / 2.0) as i16, + (screen_height as f32 / 2.0) as i16 + ]; + if debug_mode + { + println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); + } - let player_coordinates: Coordinates = Coordinates { + // Player data + let map: &str = "world"; + let player_coordinates: Coordinates = Coordinates + { x: 30, y: 1, z: 30, }; - let (screen_width, screen_height) = termion::terminal_size().unwrap(); - if debug_mode { - println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); + if let Ok((ground_map, above_map)) = parse_map(map) + { // Parse the map file into a vector + if ! debug_mode + { + clear_screen() + } + output_map + ( + ground_map, + above_map, + player_coordinates, + distance_you_can_see + ); // Output the map + } + else + { + eprintln!("Error parsing map files."); } - - let distance_you_can_see: [i16; 2] = [ - (screen_width as f32 / 2.0) as i16, - (screen_height as f32 / 2.0) as i16 - ]; - - let ground_map: Vec> = parse_map(); // Parse the map file into a vector - output_map(ground_map, player_coordinates, distance_you_can_see); // Output the map }