From 18012c7223f9bc26fa11b7bb5aeb88c08ea42882 Mon Sep 17 00:00:00 2001 From: deadvey Date: Thu, 13 Feb 2025 00:00:15 +0000 Subject: [PATCH] seperate files --- data/save.json | 3 + data/world_ground.map | 50 +++++----- src/main.rs | 207 ++++++++++-------------------------------- src/output_map.rs | 100 ++++++++++++++++++++ src/parse_map.rs | 52 +++++++++++ 5 files changed, 227 insertions(+), 185 deletions(-) create mode 100644 data/save.json create mode 100644 src/output_map.rs create mode 100644 src/parse_map.rs diff --git a/data/save.json b/data/save.json new file mode 100644 index 0000000..0be5d7a --- /dev/null +++ b/data/save.json @@ -0,0 +1,3 @@ +{ + "coordinates": ["13","23"] +} diff --git a/data/world_ground.map b/data/world_ground.map index 6171c68..d4997e4 100644 --- a/data/world_ground.map +++ b/data/world_ground.map @@ -6,13 +6,13 @@ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''"'""'"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'""'""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ +"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ +"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ @@ -24,18 +24,18 @@ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[ '''''''';;;;;'''''''''''';;;;;;''''';;;;;''''''''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[ '''''''';;;;;''''''''''''';;;;'''''';;;;;''''''';;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[ -''''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[ -''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''''''''''''''''''''''';;;;;[[[[[[[[[[[ -'''''''''''''''''''''''''''';;;;;;;''';;;''''''''''''''''''''''''''''''''''''''''''''';;;;;[[[[[[[[[[ -'''''''''''''''''''''''''''''''';;;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''';;[[[[[[[[[[ -''''''''''''''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''''''''''''''''''''''';''[[[[[[[[ -'''''''''''''''''''''''''''''''''';;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''';;''[[[[[[[ +"'''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[ +''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[ +'''''''''''''''"'''''''''''';;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[ +'''''''''''''''''''''''''''''''';;;;;;;;"'"'''"''''"""''"'"''"''"''"'"'"'""'"''""'"'"'""';;[[[[[[[[[[ +"''''''''''''''''"''''''''''''''';;;;;'""'"''"'""'''"'''''''''"'''"'"'""''"'"'''''"'''"'"';''[[[[[[[[ +"''''''''''''''''''''''''''''''''';;;;;;;'"''''''''''''''"''''''''''''''''''''''''''"''"'';;''[[[[[[[ '''''''''''''''''''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''''''''''''''''''';''''''[[[ -''''''''''''''''''''''''''''''''''''''''';;;''''''''''''''''''''''''''''''''''''''''''''''';''''''''[ -'''''''''''''''''''''''''''''''''''''''~'';;;;''''''~''''''''''''''''''''''''''''''''''''';'''''''''' +''''''''''''''''''''''''''''''''''''''''';;;'''''''''''''''''''''''''''''''''''''''''''''"';''''''''[ +'''''''''''"'''''''''''''''''''''''''''~'';;;;''''''~''''''''''''''''''''''''''''''''''''';'''''''''' '''''''''''''''''''''''''''''''''''''''~'''';;''''''~'''''''''''''''''''''''''''''''''''';''''''''''' -''''''''~''''''''''''''''''''''''''''''~''''';''''~~'''''''''''''''''''''''''''''''''''';;''''''''''' -''''''''~~~~~~~~~~''''''''''''''''''''~~''''';;''~~'''''''''''''''''''''''''''''''''''';;'''''''''''' +"'''''''~''''''''''''''''''''''''''''''~''''';''''~~'''''''''''''''''''''''''''''''''''';;''''''''''' +''''''''~~~~~~~~~~''''''''''''''"'''''~~''''';;''~~'''''''''''''''''''''''''''''''''''';;'''''''''''' '''''''''''''''''~~~~'''''''''''''''''~''''''';'~~'''''''''''''''''''''''''''''''''''';'''''''''''''' ''''''''''''''''''''~~'''''''''''''''~~''''''';'~~''''''''''''''''''''''''''''''''''';''''''''''''''' '''''''''''''''''''''~''''''''''''''~~~~~~~~~~%~~'''''''''''''''''''''''''''''''''''';''''''''''''''' @@ -45,14 +45,14 @@ ''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;''''' ''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;'''' ''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;; -''''''''''''''''''''''~~'''''''''''''''''''''''';;;''''''''''''''''''''''''''';;;'''''''''''''''''''' -'''''''''''''''''''''~~''''''''''''''''''''''''''';;;;;;;''''''''''''''''';;;;;'''''''''''''''''''''' -'''''''''''''''''''~~~'''''''''''''''''''''''''''''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~ -''''''''''''''''''~~~'''''''''''''''''''''''''''''''''''';;'''''''''''''''''''''''''''''''''''''''''~ -'''''''''''''''::~~~~''''''''''''''''''''''''''''''''''';;''''''''''''''''''''''''''''''''''''''''''~ -''''''''''''':::~~~~~:'''''''''''''''''''''''''''''''';;;;''''''''''''''''''''''''''''''''''''''''''~ -:'''''''''''::::~~~~~~::'''''''''''''''''''''''''';;;;;;''''''''''''''''''''''''''''''''''''''''''''' -::::'''::::::~~~~;~~~~::::::'''''''''''''''';;;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''' +''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;'''''''''''''''''''' +'''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;'''''''''''''''''''''' +'''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~ +''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''''''''''''''''''''''''~ +'''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''''''''''''''''''''''''~ +''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;''''''''''''''''''''''''''''''''''''''''''~ +:'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;''''''''''''''''''''''''''''''''''''''''''''' +::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''' :::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''''''''' :::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''''''''~~~~~~~~~~~ :~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~''''' diff --git a/src/main.rs b/src/main.rs index 7031a7d..7c8e123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,17 @@ -use std::fs; // For reading the map and save files -use colored::Colorize; // For coloring the output -use std::collections::HashMap; +use std::io::{stdin,stdout,Write}; use std::process::{Command}; +// Declare the modules from these files +mod parse_map; // parse_map.rs +mod output_map; // output_map.rs + +const HELP_STRING: &str = "'help' = output's this help text +'travel' = travel to a given location +"; + struct Coordinates { x: i16, - y: i16, +// y: i16, z: i16, } @@ -15,153 +21,19 @@ fn clear_screen() .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(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 - 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 - } - } - 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 Ok((ground_map, above_map)) +fn input() -> String{ + let mut s=String::new(); + let _=stdout().flush(); + stdin().read_line(&mut s).expect("Did not enter a correct string"); + if let Some('\n')=s.chars().next_back() { + s.pop(); + } + if let Some('\r')=s.chars().next_back() { + s.pop(); + } + return s; } -// Output the map based on map vector -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 - ('C', [0, 0, 0]), // Coal - ('i', [138,74 ,24]), // Iron - ('%', [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 - { - let character: char = ground_map[i as usize][j as usize]; - 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 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], - blocks.get(&character).unwrap()[1], - blocks.get(&character).unwrap()[2] - ) - ) - } - // If the character is invalid print a null value - else - { - print! - ( - "{}", - "X" - .to_string() - .truecolor( - blocks.get(&'X').unwrap()[0], - blocks.get(&'X').unwrap()[1], - blocks.get(&'X').unwrap()[2] - ) - ) - } - } - } - println!(""); - } - } -} fn main() { @@ -182,23 +54,38 @@ fn main() let player_coordinates: Coordinates = Coordinates { x: 30, - y: 1, + //y: 1, z: 30, }; - if let Ok((ground_map, above_map)) = parse_map(map) + if let Ok((ground_map, above_map)) = parse_map::parse_map(map) // Call the parse map function from parse_map.rs { // Parse the map file into a vector - if ! debug_mode - { - clear_screen() - } - output_map + output_map::output_map // Call output_map fuctino from output_map.rs ( - ground_map, - above_map, - player_coordinates, - distance_you_can_see + &ground_map, + &above_map, + &player_coordinates, + &distance_you_can_see ); // Output the map + 'game_loop: loop + { + if ! debug_mode + { + clear_screen() + } + print!("> "); + let user_input: String = input(); + if user_input == "help" + { + print!("\n{}\n", HELP_STRING); + } + else if user_input == "exit" + { + // TO DO: SAVE + print!("Exiting...\n"); + break 'game_loop; + } + } } else { diff --git a/src/output_map.rs b/src/output_map.rs new file mode 100644 index 0000000..ff793dd --- /dev/null +++ b/src/output_map.rs @@ -0,0 +1,100 @@ +use colored::Colorize; // For coloring the output +use std::collections::HashMap; + +use crate::Coordinates; + +// Output the map based on map vector +pub 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 + ('C', [0, 0, 0]), // Coal + ('i', [138,74 ,24]), // Iron + ('%', [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 + { + let character: char = ground_map[i as usize][j as usize]; + 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 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], + blocks.get(&character).unwrap()[1], + blocks.get(&character).unwrap()[2] + ) + ) + } + // If the character is invalid print a null value + else + { + print! + ( + "{}", + "X" + .to_string() + .truecolor( + blocks.get(&'X').unwrap()[0], + blocks.get(&'X').unwrap()[1], + blocks.get(&'X').unwrap()[2] + ) + ) + } + } + } + println!(""); + } + } +} diff --git a/src/parse_map.rs b/src/parse_map.rs new file mode 100644 index 0000000..69c41a3 --- /dev/null +++ b/src/parse_map.rs @@ -0,0 +1,52 @@ +use std::fs; // For reading the map and save files + +// 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 +pub 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 + 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 + } + } + 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 Ok((ground_map, above_map)) +}