From 4fceee38561777db7a9e59276663fdae818220ef Mon Sep 17 00:00:00 2001 From: deadvey Date: Fri, 21 Feb 2025 04:07:50 +0000 Subject: [PATCH] better error handling on loading above map, TODOL: Optimise map parsing --- data/save.json | 4 ++-- data/world_above.map | 26 +++++++++++++------------- data/world_ground.map | 24 ++++++++++++------------ src/function.rs | 16 ++++++++++++++++ src/main.rs | 2 +- src/output_map.rs | 11 ++++++++--- 6 files changed, 52 insertions(+), 31 deletions(-) diff --git a/data/save.json b/data/save.json index 4443f35..e62d000 100644 --- a/data/save.json +++ b/data/save.json @@ -5,8 +5,8 @@ "mining": 0 }, "coordinates": { - "x": 41, - "z": 30, + "x": 100, + "z": 29, "map": "world" }, "inventory": [ diff --git a/data/world_above.map b/data/world_above.map index b803e06..780c80c 100644 --- a/data/world_above.map +++ b/data/world_above.map @@ -1,20 +1,20 @@ #### #### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #### #### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ########################################### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - #### #%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%# #### ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# # ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ - # #%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%# # ^ ^^^^^^^^^^^^^^^^^^^^^ - # #%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%# # ^ ^^^^^^^^^^^^^^^^^ - # ###################%%%%########## # ^^^ ^^^^^^^^^ + #### # # # # # #### ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + # # # # ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ + # # # # # # # ^ ^^^^^^^^^^^^^^^^^^^^^ + # # # # # # # ^ ^^^^^^^^^^^^^^^^^ + # ################### ########## # ^^^ ^^^^^^^^^ # # ^ ^^^^^^^^^ ######### ########## ########## # ^^^ ^^^^^^^^^ - #%%%%%%%# #%%%%%%%%# #%%%%%%%%# # ^ ^^^^^^^^ - #%%%%%%%# #%%%%%%%%# #%%%%%%%%# # ^^ ^^^^^^^ - #######%# ####%%%%%# #%%%%%%%%# # ^ ^^^^^ - # #%%%%%# #####%#### # ^^^ ^^^^^ - # ####### #%%%%%# # ^^ ^^^^^ - #### ##%%%%%# ###%%## ####%### #### ^^^^^ ^^^^^ - #### #%%%%%%% #%%%%%%# #### ^ ^^^^^ + # # # # # # # ^ ^^^^^^^^ + # # # # # # # ^^ ^^^^^^^ + ####### # #### # # # # ^ ^^^^^ + # # # ##### #### # ^^^ ^^^^^ + # ####### # # # ^^ ^^^^^ + #### ## # ### ## #### ### #### ^^^^^ ^^^^^ + #### # # # #### ^ ^^^^^ ###################### ############## ^^^ ^^^^^ #### #### ♣ ^^^^^^^^^^^^^^^ ♣ ♣ ^^^^^^^^^^ @@ -31,7 +31,7 @@ ♣ ♣ ♣ ♣ ♣ - ♣ ♣ ♣ ♣ ♣ + ♣ ♣ ♣ ♣ ♣ ♣ ♣ ♣ ♣ ♣ diff --git a/data/world_ground.map b/data/world_ground.map index ce19f0b..2164b41 100644 --- a/data/world_ground.map +++ b/data/world_ground.map @@ -1,20 +1,20 @@ ''''''''''''####'''''''''''''''''''''''''''''''''''####''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ''''''''''''#;;#'''''''''''''''''''''''''''''''''''#;;#''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ''''''''''''#;;;###################################;;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''##;;;#;;;;;;;;#;;;;;;#;;;;;;;;;;#;;;;#;;;##''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;#;;;;;;;;#;;;;;;#;;;;;;;;;;#;;;;#;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;#;;;;;;;;#;;;;;;#;;;;;;;;;;#;;;;#;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;###################;;;;##########;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''##;;;#%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%#;;;##''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#;;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;#%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%#;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;#%%%%%%%%#%%%%%%#%%%%%%%%%%#%%%%#;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;###################%%%%##########;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ''''''''''''''#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ''''''''''''''#########;##########;;;;;;;##########;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;;;;;;#;#;;;;;;;;#;;;;;;;#;;;;;;;;#;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;;;;;;#;#;;;;;;;;#;;;;;;;#;;;;;;;;#;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#######;#;####;;;;;#;;;;;;;#;;;;;;;;#;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;;;;;;;;;;;#;;;;;#;;;;;;;#####;####;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''''#;;;#######;;#;;;;;#;;;;;;;;;;;;;;;;;;#''''''''''''''''''''''''''''''''''''''';;;;;;;;;'''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''###;;##;;;;;#;;###;;##;;;;;;;;####;###;####''''''''''''''''''''''''''''''''';;;;;;'''''';;;;;;;;'''''''''[[[[[[[[[[[[[[[[[[[[[[[[ -''''''''''''#;;;;#;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;#;;;;#'''''''''''''''''''''''''';;;;;;;'''''''''''''''''';;;;;''''''''';;;;[[[[[[[[[[[[[[[[[ +''''''''''''''#%%%%%%%#;#%%%%%%%%#;;;;;;;#%%%%%%%%#;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#%%%%%%%#;#%%%%%%%%#;;;;;;;#%%%%%%%%#;#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#######%#;####%%%%%#;;;;;;;#%%%%%%%%#;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;;;;;;;;;;;#%%%%%#;;;;;;;#####%####;#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''''#;;;#######;;#%%%%%#;;;;;;;;;;;;;;;;;;#''''''''''''''''''''''''''''''''''''''';;;;;;;;;'''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''###;;##%%%%%#;;###%%##;;;;;;;;####%###;####''''''''''''''''''''''''''''''''';;;;;;'''''';;;;;;;;'''''''''[[[[[[[[[[[[[[[[[[[[[[[[ +''''''''''''#;;;;#%%%%%%%;;;;;;;;;;;;;;;;;#%%%%%%#;;;;#'''''''''''''''''''''''''';;;;;;;'''''''''''''''''';;;;;''''''''';;;;[[[[[[[[[[[[[[[[[ ''''''''''''#;;###################;;;;;;;###########;;#'''''''''''''''''''''';;;;;;''''''''''''''''''''''''''';;;;;;;;;;;;;;[[[[[[[[[[[[[[[[[ ''''''''''''####'''''''''''''''''';;;;;;;''''''''''####''''''''''''';;;;;;;;;;'''''''''''''''''''''''''''''''''''''''';;;;;;''[[[[[[[[[[[[[[[ '''''''''''''''''''''''''''''''''';;;;;;;''''''''''''''''''''''''';;;''''''''''''''''''''''''''''''''''''''''''''''''''';;;''''''''[[[[[[[[[[ diff --git a/src/function.rs b/src/function.rs index 4144d14..035f38d 100644 --- a/src/function.rs +++ b/src/function.rs @@ -22,3 +22,19 @@ pub fn clear_screen() .status() .expect("Failed to clear screen"); } + +pub fn read_element_from_map(vector: &Vec>, index1: usize, index2: usize) -> Result +{ + if vector.len() <= index1 + { + Err(()) + } + else if vector[index1].len() <= index2 + { + Err(()) + } + else + { + Ok(vector[index1][index2]) + } +} diff --git a/src/main.rs b/src/main.rs index 57e7e95..7fe8240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,7 @@ fn main() { // Parse the map file into a vector if DEBUG_MODE == false { - function::clear_screen; + function::clear_screen(); } output_map::output_map // Call output_map fuctino from output_map.rs ( diff --git a/src/output_map.rs b/src/output_map.rs index 88c819f..adcd969 100644 --- a/src/output_map.rs +++ b/src/output_map.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use crate::Coordinates; use crate::DEBUG_MODE; - +use crate::function; // Output the map based on map vector pub fn output_map( @@ -51,7 +51,11 @@ pub fn output_map( if x >= 0 && x < ground_map[z as usize].len() as i16 { let character: char = ground_map[z as usize][x as usize]; - let above_character: char = above_map[z as usize][x as usize]; + let mut above_character: char = ' '; + if let Ok(above_element) = function::read_element_from_map(&above_map, z as usize, x as usize) + { + above_character = above_element; + } // Check if the character is at the current coordinates if z == player_coordinates.z && x == player_coordinates.x { @@ -64,7 +68,7 @@ pub fn output_map( ) } // Else check if there's something on the above y level - else if blocks.contains_key(&above_character) && above_character != ' ' + else if blocks.contains_key(&above_character) && above_character != ' ' { print! ( @@ -108,6 +112,7 @@ pub fn output_map( ) ) } + } } println!("");