diff --git a/src/mock.rs b/src/mock.rs index 33b5842..6daa80b 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -143,7 +143,7 @@ pub fn item() -> Item { pub fn room() -> Room { Room { id: 10, - palette_id: 9, + palette_id: Some(9), name: Some("cellar 7".to_string()), tiles: vec![ "0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"1l".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(), @@ -209,7 +209,7 @@ pub fn game_default() -> Game { rooms: vec![ Room { id: 0, - palette_id: 0, + palette_id: Some(0), name: None, tiles: vec![ "0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(), diff --git a/src/room.rs b/src/room.rs index 4199e0a..cd48432 100644 --- a/src/room.rs +++ b/src/room.rs @@ -3,7 +3,7 @@ use crate::{Exit, ExitInstance, Instance, Position, from_base36, ToBase36, optio #[derive(Debug, Eq, PartialEq)] pub struct Room { pub id: u64, - pub palette_id: u64, // id + pub palette_id: Option, // optional in very early versions pub name: Option, pub tiles: Vec, // tile ids pub items: Vec, @@ -25,6 +25,14 @@ impl Room { "".to_string() } } + + fn palette_line(&self) -> String { + if self.palette_id.is_some() { + optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36())) + } else { + "".to_string() + } + } } impl From for Room { @@ -34,7 +42,7 @@ impl From for Room { let mut lines: Vec<&str> = string.lines().collect(); let id = from_base36(&lines[0]); let mut name = None; - let mut palette_id = 0; + let mut palette_id = None; let mut items: Vec = Vec::new(); let mut exits: Vec = Vec::new(); let mut endings: Vec = Vec::new(); @@ -50,7 +58,7 @@ impl From for Room { } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); } else if last_line.starts_with("PAL") { - palette_id = from_base36(&last_line.replace("PAL ", "")); + palette_id = Some(from_base36(&last_line.replace("PAL ", ""))); } else if last_line.starts_with("ITM") { let last_line = last_line.replace("ITM ", ""); let item_position: Vec<&str> = last_line.split(' ').collect(); @@ -152,7 +160,7 @@ impl ToString for Room { } format!( - "ROOM {}\n{}{}{}{}{}{}\nPAL {}", + "ROOM {}\n{}{}{}{}{}{}{}", self.id.to_base36(), tiles, self.name_line(), @@ -160,7 +168,7 @@ impl ToString for Room { items, exits, endings, - self.palette_id.to_base36() + self.palette_line() ) } }