convert int IDs to strings

This commit is contained in:
Max Bradbury 2020-06-18 14:44:20 +01:00
parent e895f932a6
commit fe6f3f5c84
7 changed files with 57 additions and 56 deletions

View File

@ -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<Self, Self::Err> {
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(),

View File

@ -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());
}
}

View File

@ -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<Image>,
pub name: Option<String>,
pub dialogue_id: Option<String>,
@ -32,7 +32,7 @@ impl From<String> 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<u64> = 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(),

View File

@ -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,

View File

@ -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<String>,
pub colours: Vec<Colour>,
}
@ -13,7 +12,7 @@ impl From<String> 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 {

View File

@ -5,14 +5,17 @@ use std::str::FromStr;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Room {
pub id: u64,
pub palette_id: Option<u64>, // optional in very early versions
pub id: String,
/// palette ID was optional in very early versions
pub palette_id: Option<String>,
pub name: Option<String>,
pub tiles: Vec<String>, // tile ids
/// tile IDs
pub tiles: Vec<String>,
pub items: Vec<Instance>,
pub exits: Vec<ExitInstance>,
pub endings: Vec<Instance>,
pub walls: Vec<u64>, // old way of handling walls...
/// old method of handling walls - a comma-separated list of tile IDs
pub walls: Vec<String>,
}
impl Room {
@ -24,8 +27,7 @@ impl Room {
#[inline]
fn wall_line(&self) -> String {
if self.walls.len() > 0 {
let ids: Vec<String> = 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<String> 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<Instance> = Vec::new();
let mut exits: Vec<ExitInstance> = Vec::new();
let mut endings: Vec<Instance> = Vec::new();
let mut walls: Vec<u64> = Vec::new();
let mut walls: Vec<String> = Vec::new();
loop {
let last_line = lines.pop().unwrap();
@ -61,11 +63,11 @@ impl From<String> 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()]);
}
}

View File

@ -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<String>,
pub animation_frames: Vec<Image>,
pub dialogue_id: Option<String>,
pub room_id: Option<u64>,
pub room_id: Option<String>,
pub position: Option<Position>,
pub colour_id: Option<u64>,
pub items: Vec<String>,
@ -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<String> 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<String> = None;
let mut room_id: Option<u64> = None;
let mut room_id: Option<String> = None;
let mut position: Option<Position> = None;
let mut colour_id: Option<u64> = None;
let mut items: Vec<String> = Vec::new();
@ -77,7 +77,7 @@ impl From<String> 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(),