From fe6f3f5c841b5a3d291c3ff50bf1492eb1fbb4c6 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 18 Jun 2020 14:44:20 +0100 Subject: [PATCH] convert int IDs to strings --- src/exit.rs | 14 +++++++------- src/game.rs | 4 ++-- src/item.rs | 8 ++++---- src/mock.rs | 32 ++++++++++++++++---------------- src/palette.rs | 13 ++++++------- src/room.rs | 28 +++++++++++++++------------- src/sprite.rs | 14 +++++++------- 7 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/exit.rs b/src/exit.rs index f7eb20c..c523f1a 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -54,7 +54,7 @@ impl ToString for Transition { #[derive(Clone, Debug, Eq, PartialEq)] pub struct Exit { /// destination - pub room_id: u64, + pub room_id: String, /// id pub position: Position, pub effect: Transition, @@ -67,7 +67,7 @@ impl FromStr for Exit { fn from_str(s: &str) -> Result { let mut parts = s.split_whitespace(); - let room_id = from_base36(parts.next().unwrap()); + let room_id = parts.next().unwrap().to_string(); let position = Position::from_str(parts.next().unwrap()); if position.is_err() { @@ -92,7 +92,7 @@ impl fmt::Display for Exit { write!( f, "{} {}{}", - self.room_id.to_base36(), + self.room_id, self.position.to_string(), self.effect.to_string() ) @@ -110,7 +110,7 @@ mod test { assert_eq!( Exit::from_str("a 12,13").unwrap(), Exit { - room_id: 10, + room_id: "a".to_string(), position: Position { x: 12, y: 13 }, effect: Transition::None } @@ -122,7 +122,7 @@ mod test { assert_eq!( Exit::from_str("a 12,13 FX slide_u").unwrap(), Exit { - room_id: 10, + room_id: "a".to_string(), position: Position { x: 12, y: 13 }, effect: Transition::SlideUp } @@ -133,7 +133,7 @@ mod test { fn test_exit_to_string() { assert_eq!( Exit { - room_id: 8, + room_id: "8".to_string(), position: Position { x: 5, y: 6 }, effect: Transition::None }.to_string(), @@ -145,7 +145,7 @@ mod test { fn test_exit_to_string_with_fx() { assert_eq!( Exit { - room_id: 8, + room_id: "8".to_string(), position: Position { x: 5, y: 6 }, effect: Transition::FadeToWhite }.to_string(), diff --git a/src/game.rs b/src/game.rs index 83f9ab7..60b2efd 100644 --- a/src/game.rs +++ b/src/game.rs @@ -273,7 +273,7 @@ impl ToString for Game { } for sprite in &self.sprites { - if is_string_numeric(sprite.id.to_base36()) { + if is_string_numeric(sprite.id.clone()) { segments.push(sprite.to_string()); } } @@ -281,7 +281,7 @@ impl ToString for Game { segments.push(self.avatar.to_string().replace("SPR a", "SPR A")); for sprite in &self.sprites { - if !is_string_numeric(sprite.id.to_base36()) { + if !is_string_numeric(sprite.id.clone()) { segments.push(sprite.to_string()); } } diff --git a/src/item.rs b/src/item.rs index 91a032e..78a0592 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,9 +1,9 @@ -use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36}; +use crate::{optional_data_line, AnimationFrames, Image}; use crate::image::animation_frames_from_string; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Item { - pub id: u64, + pub id: String, pub animation_frames: Vec, pub name: Option, pub dialogue_id: Option, @@ -32,7 +32,7 @@ impl From for Item { fn from(string: String) -> Item { let mut lines: Vec<&str> = string.lines().collect(); - let id = from_base36(&lines[0].replace("ITM ", "")); + let id = lines[0].replace("ITM ", ""); let mut name = None; let mut dialogue_id = None; let mut colour_id: Option = None; @@ -71,7 +71,7 @@ impl ToString for Item { fn to_string(&self) -> String { format!( "ITM {}\n{}{}{}{}", - self.id.to_base36(), + self.id, self.animation_frames.to_string(), self.name_line(), self.dialogue_line(), diff --git a/src/mock.rs b/src/mock.rs index 6dbd0e0..1a5b8c2 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -84,7 +84,7 @@ pub mod image { #[inline] pub fn avatar() -> Sprite { Sprite { - id: 0, + id: "0".to_string(), animation_frames: vec![ Image { pixels: vec![ @@ -102,7 +102,7 @@ pub fn avatar() -> Sprite { }, ], name: None, - room_id: Some(0), + room_id: Some("0".to_string()), position: Some(Position { x: 2, y: 5 }), colour_id: None, dialogue_id: None, @@ -130,7 +130,7 @@ pub fn tile_default() -> Tile { #[inline] pub fn sprite() -> Sprite { Sprite { - id: 10, + id: "a".to_string(), name: Some("hatch".to_string()), animation_frames: vec![Image { pixels: vec![ @@ -140,7 +140,7 @@ pub fn sprite() -> Sprite { ], }], dialogue_id: Some("SPR_0".to_string()), - room_id: Some(4), + room_id: Some("4".to_string()), position: Some(Position { x: 9, y: 7 }), colour_id: None, items: vec![] @@ -150,7 +150,7 @@ pub fn sprite() -> Sprite { #[inline] pub fn item() -> Item { Item { - id: 6, + id: "6".to_string(), animation_frames: vec![Image { pixels: vec![ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -167,8 +167,8 @@ pub fn item() -> Item { #[inline] pub fn room() -> Room { Room { - id: 10, - palette_id: Some(9), + id: "a".to_string(), + palette_id: Some("9".to_string()), name: Some("cellar 7".to_string()), tiles: vec![ "0".to_string(), @@ -453,7 +453,7 @@ pub fn room() -> Room { exits: vec![ExitInstance { position: Position { x: 3, y: 3 }, exit: Exit { - room_id: 3, + room_id: "3".to_string(), position: Position { x: 10, y: 6 }, effect: Transition::None, }, @@ -479,7 +479,7 @@ pub fn game_default() -> Game { custom_font: None, text_direction: TextDirection::LeftToRight, palettes: vec![Palette { - id: 0, + id: "0".to_string(), name: None, colours: vec![ Colour { @@ -500,8 +500,8 @@ pub fn game_default() -> Game { ], }], rooms: vec![Room { - id: 0, - palette_id: Some(0), + id: "0".to_string(), + palette_id: Some("0".to_string()), name: None, tiles: vec![ "0".to_string(), @@ -768,7 +768,7 @@ pub fn game_default() -> Game { }], tiles: vec![self::tile_default()], avatar: Sprite { - id: 10, + id: "A".to_string(), animation_frames: vec![Image { pixels: vec![ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, @@ -777,14 +777,14 @@ pub fn game_default() -> Game { ], }], name: None, - room_id: Some(0), + room_id: Some("0".to_string()), position: Option::from(Position { x: 4, y: 4 }), colour_id: None, dialogue_id: None, items: vec![] }, sprites: vec![Sprite { - id: 10, + id: "a".to_string(), name: None, animation_frames: vec![Image { pixels: vec![ @@ -794,13 +794,13 @@ pub fn game_default() -> Game { ], }], dialogue_id: Some("SPR_0".to_string()), - room_id: Some(0), + room_id: Some("0".to_string()), position: Some(Position { x: 8, y: 12 }), colour_id: None, items: vec![] }], items: vec![Item { - id: 0, + id: "0".to_string(), animation_frames: vec![Image { pixels: vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/src/palette.rs b/src/palette.rs index 5fde93c..f776910 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -1,9 +1,8 @@ use crate::colour::Colour; -use crate::{from_base36, ToBase36}; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Palette { - pub id: u64, + pub id: String, pub name: Option, pub colours: Vec, } @@ -13,7 +12,7 @@ impl From for Palette { fn from(string: String) -> Palette { let lines: Vec<&str> = string.lines().collect(); - let id = from_base36(&lines[0].replace("PAL ", "")); + let id = lines[0].replace("PAL ", ""); let name = match lines[1].starts_with("NAME") { true => Some(lines[1].replace("NAME ", "").to_string()), @@ -46,7 +45,7 @@ impl ToString for Palette { } colours.pop(); - format!("PAL {}\n{}{}", self.id.to_base36(), name, colours) + format!("PAL {}\n{}{}", self.id, name, colours) } } @@ -60,7 +59,7 @@ mod test { let output = Palette::from("PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string()); let expected = Palette { - id: 1, + id: "1".to_string(), name: Some("lamplight".to_string()), colours: vec![ Colour { @@ -89,7 +88,7 @@ mod test { let output = Palette::from("PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string()); let expected = Palette { - id: 9, + id: "9".to_string(), name: None, colours: vec![ Colour { @@ -116,7 +115,7 @@ mod test { #[test] fn test_palette_to_string() { let output = Palette { - id: 16, + id: "g".to_string(), name: Some("moss".to_string()), colours: vec![ Colour { diff --git a/src/room.rs b/src/room.rs index 94a3a90..0f4733c 100644 --- a/src/room.rs +++ b/src/room.rs @@ -5,14 +5,17 @@ use std::str::FromStr; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Room { - pub id: u64, - pub palette_id: Option, // optional in very early versions + pub id: String, + /// palette ID was optional in very early versions + pub palette_id: Option, pub name: Option, - pub tiles: Vec, // tile ids + /// tile IDs + pub tiles: Vec, pub items: Vec, pub exits: Vec, pub endings: Vec, - pub walls: Vec, // old way of handling walls... + /// old method of handling walls - a comma-separated list of tile IDs + pub walls: Vec, } impl Room { @@ -24,8 +27,7 @@ impl Room { #[inline] fn wall_line(&self) -> String { if self.walls.len() > 0 { - let ids: Vec = self.walls.iter().map(|&id| id.to_base36()).collect(); - optional_data_line("WAL", Some(ids.join(","))) + optional_data_line("WAL", Some(self.walls.join(","))) } else { "".to_string() } @@ -34,7 +36,7 @@ impl Room { #[inline] fn palette_line(&self) -> String { if self.palette_id.is_some() { - optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36())) + optional_data_line("PAL", Some(self.palette_id.as_ref().unwrap())) } else { "".to_string() } @@ -47,13 +49,13 @@ impl From for Room { let string = string.replace("ROOM ", ""); let string = string.replace("SET ", ""); let mut lines: Vec<&str> = string.lines().collect(); - let id = from_base36(&lines[0]); + let id = lines[0].to_string(); let mut name = None; let mut palette_id = None; let mut items: Vec = Vec::new(); let mut exits: Vec = Vec::new(); let mut endings: Vec = Vec::new(); - let mut walls: Vec = Vec::new(); + let mut walls: Vec = Vec::new(); loop { let last_line = lines.pop().unwrap(); @@ -61,11 +63,11 @@ impl From for Room { if last_line.starts_with("WAL") { let last_line = last_line.replace("WAL ", ""); let ids: Vec<&str> = last_line.split(",").collect(); - walls = ids.iter().map(|&id| from_base36(id)).collect(); + walls = ids.iter().map(|&id| id.to_string()).collect(); } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); } else if last_line.starts_with("PAL") { - palette_id = Some(from_base36(&last_line.replace("PAL ", ""))); + palette_id = Some(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(); @@ -211,7 +213,7 @@ impl Room { format!( "{} {}\n{}{}{}{}{}{}{}", room_type.to_string(), - self.id.to_base36(), + self.id, tiles, self.name_line(), self.wall_line(), @@ -248,6 +250,6 @@ mod test { fn test_room_walls_array() { let output = Room::from(include_str!("test-resources/room-with-walls").to_string()); - assert_eq!(output.walls, vec![10, 15]); + assert_eq!(output.walls, vec!["a".to_string(), "f".to_string()]); } } diff --git a/src/sprite.rs b/src/sprite.rs index 1bdc3e8..f42fd2e 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -4,11 +4,11 @@ use std::str::FromStr; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Sprite { - pub id: u64, + pub id: String, pub name: Option, pub animation_frames: Vec, pub dialogue_id: Option, - pub room_id: Option, + pub room_id: Option, pub position: Option, pub colour_id: Option, pub items: Vec, @@ -30,7 +30,7 @@ impl Sprite { if self.room_id.is_some() && self.position.is_some() { format!( "\nPOS {} {}", - self.room_id.unwrap().to_base36(), + self.room_id.as_ref().unwrap(), self.position.as_ref().unwrap().to_string() ) } else { @@ -59,10 +59,10 @@ impl From for Sprite { fn from(string: String) -> Sprite { let mut lines: Vec<&str> = string.lines().collect(); - let id = from_base36(&lines[0].replace("SPR ", "")); + let id = lines[0].replace("SPR ", ""); let mut name = None; let mut dialogue_id: Option = None; - let mut room_id: Option = None; + let mut room_id: Option = None; let mut position: Option = None; let mut colour_id: Option = None; let mut items: Vec = Vec::new(); @@ -77,7 +77,7 @@ impl From for Sprite { } else if last_line.starts_with("POS") { let last_line = last_line.replace("POS ", ""); let room_position: Vec<&str> = last_line.split(' ').collect(); - room_id = Some(from_base36(&room_position[0])); + room_id = Some(room_position[0].to_string()); if room_position.len() < 2 { panic!("Bad room/position for sprite: {}", string); @@ -118,7 +118,7 @@ impl ToString for Sprite { fn to_string(&self) -> String { format!( "SPR {}\n{}{}{}{}{}{}", - self.id.to_base36(), + self.id, self.animation_frames.to_string(), self.name_line(), self.dialogue_line(),