This commit is contained in:
deadvey 2025-02-11 21:07:55 +00:00
parent 3dbc3f3400
commit 589fe0e1c0
4 changed files with 207 additions and 86 deletions

View File

@ -1,49 +0,0 @@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

71
data/world_above.map Normal file
View File

@ -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 ♣ ^
♣ ♣ ^
♣ ^
♣ ♣ ♣ ^ ^^
♣ ♣ ♣ ♣ ^^^^^^^^^^
♣ ♣ ♣ ♣ ^^^^^^^^
♣ ♣ ♣ ♣ ^^^^^^^
♣ ♣ ♣ ♣ ♣ ^^^
♣ ♣ ♣ ^
♣ ♣ ♣
♣ ♣
♣ ♣
♣ ♣ ♣ ♣ ♣
♣ ♣
♣ ♣ ♣
♣ ♣ ♣
♣ ♣ ♣
♣ ♣
♣ ♣
♣ ♣
♣ ♣
♣ ♣
♣ ♣
%%%%
% %
%%%%

View File

@ -1,6 +1,7 @@
use std::fs; // For reading the map and save files use std::fs; // For reading the map and save files
use colored::Colorize; // For coloring the output use colored::Colorize; // For coloring the output
use std::collections::HashMap; use std::collections::HashMap;
use std::process::{Command};
struct Coordinates { struct Coordinates {
x: i16, x: i16,
@ -8,32 +9,72 @@ struct Coordinates {
z: i16, 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 // 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 // Refers to a block (see top comment) and the function returns this vector
fn parse_map() -> Vec<Vec<char>> { fn parse_map(map: &str) -> Result<(Vec<Vec<char>>, Vec<Vec<char>>), u8>
let mut ground_map: Vec<Vec<char>> = Vec::new(); // Initialises the map vector {
let mut ground_map: Vec<Vec<char>> = Vec::new(); // Initialises the Ground map vector
let mut above_map: Vec<Vec<char>> = 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 // Read the ground map file
let file_contents: String = fs::read_to_string("data/ground_map.data").unwrap(); // Reads the file if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file)
let ground_rows = file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows {
let ground_file_contents = parsed_ground_file_contents;
let ground_rows = ground_file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows
// Loop over each row to parse each column of it // Loop over each row to parse each column of it
for row in 0..ground_rows.len() { for row in 0..ground_rows.len()
{
// Turn each column into an integer and collect it into a row vector // Turn each column into an integer and collect it into a row vector
let current_row = ground_rows[row].chars().collect(); let current_row = ground_rows[row].chars().collect();
ground_map.push(current_row); // Then push this vector to the full map vector 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::<Vec<_>>(); // Split the file contents into rows
return ground_map // 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))
} }
// Output the map based on map vector // Output the map based on map vector
fn output_map(ground_map: Vec<Vec<char>>, player_coordinates: Coordinates, distance_you_can_see: [i16; 2]) { fn output_map(ground_map: Vec<Vec<char>>, above_map: Vec<Vec<char>>, player_coordinates: Coordinates, distance_you_can_see: [i16; 2])
print!("{}[2J", 27 as char); // Clear screen {
let blocks: HashMap<char, [u8; 3]> =
let blocks: HashMap<char, [u8; 3]> = [ [
('X', [255,0, 0]), // Null ('X', [255,0, 0]), // Null
('\'', [0, 255,0]), // Grass ('\'', [0, 255,0]), // Grass
('"', [153,102,0]), // Dirt
(';', [128,128,128]), // Cobbles (';', [128,128,128]), // Cobbles
('~', [0, 0, 255]), // Water/sea ('~', [0, 0, 255]), // Water/sea
('D', [0, 128,128]), // Diamond ('D', [0, 128,128]), // Diamond
@ -42,23 +83,56 @@ fn output_map(ground_map: Vec<Vec<char>>, player_coordinates: Coordinates, dista
('%', [195,162,103]), // Wood planks ('%', [195,162,103]), // Wood planks
(':', [195,162,103]), // Sand (':', [195,162,103]), // Sand
('[', [196,196,196]), // Stone ('[', [196,196,196]), // Stone
('^', [196,128,196]), // Mountain
('8', [255,196,196]), // Stone bricks
('♣', [0, 153,51]), // Oak tree
] ]
.iter().cloned().collect(); .iter().cloned().collect();
// Loop over array and print each block // 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] { 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 i >= 0 && i < ground_map.len() as i16
if j >= 0 && j < ground_map[i as usize].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 character: char = ground_map[i as usize][j as usize];
if i == player_coordinates.z && j == player_coordinates.x { let above_character: char = above_map[i as usize][j as usize];
print!("{}"," " // Check if the character is at the current coordinates
if i == player_coordinates.z && j == player_coordinates.x
{
print!
(
"{}",
" "
.on_truecolor(40,235,181) .on_truecolor(40,235,181)
.truecolor(0,0,0) .truecolor(0,0,0)
) )
} }
else if blocks.contains_key(&character) { // Else check if there's something on the above y level
print!("{}",character // Output's the current block 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() .to_string()
.truecolor( .truecolor(
blocks.get(&character).unwrap()[0], blocks.get(&character).unwrap()[0],
@ -67,8 +141,13 @@ fn output_map(ground_map: Vec<Vec<char>>, player_coordinates: Coordinates, dista
) )
) )
} }
else { // If the character is invalid print a null value
print!("{}","X" else
{
print!
(
"{}",
"X"
.to_string() .to_string()
.truecolor( .truecolor(
blocks.get(&'X').unwrap()[0], blocks.get(&'X').unwrap()[0],
@ -84,25 +163,45 @@ fn output_map(ground_map: Vec<Vec<char>>, player_coordinates: Coordinates, dista
} }
} }
fn main() { fn main()
{
let debug_mode = true; 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, x: 30,
y: 1, y: 1,
z: 30, z: 30,
}; };
let (screen_width, screen_height) = termion::terminal_size().unwrap();
if debug_mode { if let Ok((ground_map, above_map)) = parse_map(map)
println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); { // 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<Vec<char>> = parse_map(); // Parse the map file into a vector
output_map(ground_map, player_coordinates, distance_you_can_see); // Output the map
} }