2020-04-05 20:02:45 +00:00
|
|
|
use std::collections::HashMap;
|
2020-04-06 07:36:52 +00:00
|
|
|
use std::fmt;
|
2020-04-05 17:58:04 +00:00
|
|
|
|
2020-04-05 21:26:47 +00:00
|
|
|
const IMAGE_DIMENSION_SD: usize = 8;
|
|
|
|
const IMAGE_DIMENSION_HD: usize = 16;
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 17:58:04 +00:00
|
|
|
struct Colour {
|
|
|
|
red: u8,
|
|
|
|
green: u8,
|
|
|
|
blue: u8,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 17:58:04 +00:00
|
|
|
struct Palette {
|
|
|
|
id: String, // base36 string (why??)
|
|
|
|
name: Option<String>,
|
|
|
|
colours: Vec<Colour>,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 17:58:04 +00:00
|
|
|
struct Image {
|
2020-04-05 21:28:23 +00:00
|
|
|
pixels: Vec<u8>, // 64 for SD, 256 for HD
|
2020-04-05 17:58:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 17:58:04 +00:00
|
|
|
struct Tile {
|
|
|
|
id: String, // base36 string
|
|
|
|
name: Option<String>,
|
|
|
|
wall: bool,
|
|
|
|
animation_frames: Vec<Image>,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:00:15 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Hash)]
|
2020-04-05 19:46:55 +00:00
|
|
|
struct Position {
|
|
|
|
x: u8,
|
|
|
|
y: u8,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 19:46:55 +00:00
|
|
|
struct Dialogue {
|
|
|
|
id: String,
|
|
|
|
contents: String,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 19:46:55 +00:00
|
|
|
struct Sprite {
|
|
|
|
id: String, // lowercase base36
|
|
|
|
name: Option<String>,
|
|
|
|
animation_frames: Vec<Image>,
|
2020-04-05 22:58:10 +00:00
|
|
|
dialogue: Option<String>, /// dialogue id
|
2020-04-06 11:27:29 +00:00
|
|
|
room: String, /// room id
|
2020-04-05 19:46:55 +00:00
|
|
|
position: Position,
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:54:43 +00:00
|
|
|
/// avatar is a "sprite" in the game data but with a specific id
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 19:46:55 +00:00
|
|
|
struct Avatar {
|
|
|
|
animation_frames: Vec<Image>,
|
2020-04-06 11:27:29 +00:00
|
|
|
room: String, /// room id
|
2020-04-05 19:46:55 +00:00
|
|
|
position: Position,
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 19:46:55 +00:00
|
|
|
struct Item {
|
|
|
|
id: String,
|
|
|
|
animation_frames: Vec<Image>,
|
|
|
|
name: Option<String>,
|
2020-04-06 07:54:43 +00:00
|
|
|
dialogue: Option<String>, // dialogue id
|
2020-04-05 19:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 20:02:45 +00:00
|
|
|
struct Exit {
|
|
|
|
/// destination
|
|
|
|
room: String, /// id
|
|
|
|
position: Position,
|
|
|
|
}
|
|
|
|
|
|
|
|
// same as a dialogue basically
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 20:02:45 +00:00
|
|
|
struct Ending {
|
2020-04-06 13:11:56 +00:00
|
|
|
id: String,
|
|
|
|
dialogue: String,
|
2020-04-05 20:02:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 22:02:03 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-04-05 20:02:45 +00:00
|
|
|
struct Room {
|
|
|
|
id: String,
|
|
|
|
palette: String, /// id
|
|
|
|
name: Option<String>,
|
2020-04-05 21:33:06 +00:00
|
|
|
tiles: Vec<String>, /// tile ids
|
2020-04-06 13:24:35 +00:00
|
|
|
items: HashMap<Position, String>, /// item id
|
2020-04-05 20:02:45 +00:00
|
|
|
exits: HashMap<Position, Exit>,
|
|
|
|
endings: HashMap<Position, Ending>,
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:22:35 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
struct Variable {
|
|
|
|
id: String,
|
|
|
|
initial_value: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-04-05 17:58:04 +00:00
|
|
|
struct Game {
|
|
|
|
name: String,
|
|
|
|
version: f64,
|
2020-04-06 11:27:58 +00:00
|
|
|
room_format: u8,
|
2020-04-05 17:58:04 +00:00
|
|
|
palettes: Vec<Palette>,
|
2020-04-06 13:22:35 +00:00
|
|
|
variables: Vec<Variable>,
|
2020-04-05 17:58:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 11:26:26 +00:00
|
|
|
fn test_image_chequers_1() -> Image {
|
|
|
|
Image {
|
|
|
|
pixels: vec![
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_image_chequers_2() -> Image {
|
|
|
|
Image {
|
|
|
|
pixels: vec![
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
0,1,0,1,0,1,0,1,
|
|
|
|
1,0,1,0,1,0,1,0,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_sprite() -> Sprite {
|
|
|
|
Sprite {
|
|
|
|
id: "a".to_string(),
|
|
|
|
name: Some("hatch".to_string()),
|
|
|
|
animation_frames: vec![
|
|
|
|
Image {
|
|
|
|
pixels: vec![
|
|
|
|
0,0,0,0,0,0,0,0,
|
|
|
|
0,1,1,1,1,0,0,0,
|
|
|
|
0,1,0,0,1,0,0,0,
|
|
|
|
0,0,1,1,1,1,0,0,
|
|
|
|
0,0,1,1,1,1,0,0,
|
|
|
|
0,1,0,1,1,1,1,0,
|
|
|
|
0,1,0,1,1,1,1,0,
|
|
|
|
0,1,1,0,1,1,1,1,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
dialogue: Some("SPR_0".to_string()),
|
|
|
|
room: "4".to_string(),
|
|
|
|
position: Position {
|
|
|
|
x: 9,
|
|
|
|
y: 7
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_item() -> Item {
|
|
|
|
Item {
|
|
|
|
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,0,1,0,0,
|
|
|
|
0,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,0,1,0,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: Some("door".to_string()),
|
|
|
|
dialogue: Some("ITM_2".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
fn image_from_string(string: String) -> Image {
|
|
|
|
let string = string.replace("\n", "");
|
|
|
|
let pixels: Vec<&str> = string.split("").collect();
|
|
|
|
// the above seems to add an extra "" at the start and end of the vec, so strip them below
|
2020-04-05 21:28:23 +00:00
|
|
|
let pixels = &pixels[1..(pixels.len() - 1)];
|
|
|
|
let pixels: Vec<u8> = pixels.iter().map(|&pixel| { pixel.parse::<u8>().unwrap() }).collect();
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
Image { pixels }
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:28:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_image_from_string() {
|
|
|
|
let output = image_from_string(
|
|
|
|
"11111111\n11001111\n10111111\n11111111\n11111111\n11111111\n11111111\n11111111".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = Image {
|
|
|
|
pixels: vec![
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,0,0,1,1,1,1,
|
|
|
|
1,0,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected)
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
fn image_to_string(image: Image) -> String {
|
2020-04-06 07:36:52 +00:00
|
|
|
image_to_string_opts(image, false)
|
2020-04-05 21:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn image_to_string_opts(image: Image, hd: bool) -> String {
|
2020-04-05 17:58:04 +00:00
|
|
|
let mut string = String::new();
|
2020-04-05 21:26:47 +00:00
|
|
|
let chunk_size = if hd {IMAGE_DIMENSION_HD} else {IMAGE_DIMENSION_SD};
|
2020-04-05 17:58:04 +00:00
|
|
|
|
2020-04-05 21:26:47 +00:00
|
|
|
for line in image.pixels.chunks(chunk_size) {
|
2020-04-05 17:58:04 +00:00
|
|
|
for pixel in line {
|
2020-04-06 07:36:52 +00:00
|
|
|
string.push_str(&format!("{}", *pixel));
|
2020-04-05 17:58:04 +00:00
|
|
|
}
|
|
|
|
string.push('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
string.pop(); // remove trailing newline
|
|
|
|
|
|
|
|
string
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_image_to_string() {
|
2020-04-06 07:27:08 +00:00
|
|
|
let output = image_to_string(test_image_chequers_1());
|
2020-04-05 17:58:04 +00:00
|
|
|
let expected = "10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101".to_string();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
2020-04-06 06:23:20 +00:00
|
|
|
fn animation_frames_to_string(animation_frames: Vec<Image>) -> String {
|
|
|
|
let mut string = String::new();
|
|
|
|
let last_frame = animation_frames.len() - 1;
|
|
|
|
|
|
|
|
for (i, frame) in animation_frames.into_iter().enumerate() {
|
|
|
|
string.push_str(&image_to_string(frame));
|
|
|
|
|
|
|
|
if i < last_frame {
|
|
|
|
string.push_str(&"\n>\n".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
fn tile_from_string(string: String) -> Tile {
|
|
|
|
let mut lines: Vec<&str> = string.split("\n").collect();
|
|
|
|
|
|
|
|
let id = lines[0].replace("TIL ", "");
|
|
|
|
|
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
let wall = match last_line == "WAL true" {
|
|
|
|
true => true,
|
|
|
|
false => {
|
|
|
|
lines.push(last_line);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
let name = match last_line.starts_with("NAME") {
|
|
|
|
true => Some(last_line.replace("NAME ", "").to_string()),
|
|
|
|
false => {
|
|
|
|
lines.push(last_line);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let animation_frames = lines[1..].join("");
|
|
|
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
|
|
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
|
|
|
image_from_string(frame.to_string())
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
Tile {id, name, wall, animation_frames}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tile_from_string() {
|
|
|
|
let output = tile_from_string("TIL z\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\nNAME concrete 1\nWAL true".to_string());
|
|
|
|
|
|
|
|
let expected = Tile {
|
|
|
|
id: "z".to_string(),
|
|
|
|
name: Some("concrete 1".to_string()),
|
|
|
|
wall: true,
|
|
|
|
animation_frames: vec![
|
|
|
|
Image {
|
2020-04-05 21:28:23 +00:00
|
|
|
pixels: vec![1; 64]
|
2020-04-05 17:58:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tile_to_string(tile: Tile) -> String {
|
|
|
|
format!(
|
|
|
|
"TIL {}\n{}{}{}",
|
|
|
|
tile.id,
|
2020-04-06 06:23:20 +00:00
|
|
|
animation_frames_to_string(tile.animation_frames),
|
2020-04-05 17:58:04 +00:00
|
|
|
if tile.name.is_some() {format!("\nNAME {}", tile.name.unwrap())} else {"".to_string()},
|
|
|
|
if tile.wall {"\nWAL true"} else {""}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-05 19:09:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tile_to_string() {
|
|
|
|
let output = tile_to_string(Tile {
|
|
|
|
id: "7a".to_string(),
|
|
|
|
name: Some("chequers".to_string()),
|
|
|
|
wall: false,
|
|
|
|
animation_frames: vec![
|
2020-04-06 07:27:08 +00:00
|
|
|
test_image_chequers_1(),
|
|
|
|
test_image_chequers_2(),
|
2020-04-05 19:09:03 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
let expected = "TIL 7a\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n>\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\nNAME chequers".to_string();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-05 17:58:04 +00:00
|
|
|
|
|
|
|
fn colour_from_string(colour: String) -> Colour {
|
|
|
|
let values: Vec<&str> = colour.split(',').collect();
|
|
|
|
|
|
|
|
let red: u8 = values[0].parse().unwrap_or(0);
|
|
|
|
let green: u8 = values[1].parse().unwrap_or(0);
|
|
|
|
let blue: u8 = values[2].parse().unwrap_or(0);
|
|
|
|
|
|
|
|
Colour { red, green, blue }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_colour_from_string() {
|
|
|
|
assert_eq!(
|
|
|
|
colour_from_string("0,255,0".to_string()),
|
|
|
|
Colour { red: 0, green: 255, blue: 0 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:28:35 +00:00
|
|
|
fn colour_to_string(colour: Colour) -> String {
|
|
|
|
format!("{},{},{}", colour.red, colour.green, colour.blue)
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
#[test]
|
|
|
|
fn test_colour_to_string() {
|
|
|
|
assert_eq!(
|
|
|
|
colour_to_string(Colour { red: 22, green: 33, blue: 44 }),
|
|
|
|
"22,33,44".to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn palette_from_string(palette: String) -> Palette {
|
|
|
|
let lines: Vec<&str> = palette.split('\n').collect();
|
|
|
|
|
|
|
|
let id = lines[0].replace("PAL ", "");
|
|
|
|
|
|
|
|
let name = match lines[1].starts_with("NAME") {
|
|
|
|
true => Some(lines[1].replace("NAME ", "").to_string()),
|
|
|
|
false => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let colour_start_index = if name.is_some() {2} else {1};
|
|
|
|
|
|
|
|
let colours = lines[colour_start_index..].iter().map(|&line| {
|
|
|
|
colour_from_string(line.to_string())
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
Palette { id, name, colours }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_palette_from_string() {
|
|
|
|
let output = palette_from_string(
|
|
|
|
"PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = Palette {
|
|
|
|
id: "1".to_string(),
|
|
|
|
name: Some("lamplight".to_string()),
|
|
|
|
colours: vec![
|
|
|
|
Colour {red: 45, green: 45, blue: 59},
|
|
|
|
Colour {red: 66, green: 60, blue: 39},
|
|
|
|
Colour {red: 140, green: 94, blue: 1 },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_palette_from_string_no_name() {
|
|
|
|
let output = palette_from_string(
|
|
|
|
"PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = Palette {
|
|
|
|
id: "9".to_string(),
|
|
|
|
name: None,
|
|
|
|
colours: vec![
|
|
|
|
Colour {red: 45, green: 45, blue: 59},
|
|
|
|
Colour {red: 66, green: 60, blue: 39},
|
|
|
|
Colour {red: 140, green: 94, blue: 1 },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:00:15 +00:00
|
|
|
fn position_from_string(string: String) -> Position {
|
2020-04-06 11:27:29 +00:00
|
|
|
// e.g. "2,5"
|
|
|
|
let xy: Vec<&str> = string.split(',').collect();
|
2020-04-05 22:00:15 +00:00
|
|
|
let x = xy[0].parse().unwrap();
|
|
|
|
let y = xy[1].parse().unwrap();
|
|
|
|
|
2020-04-06 11:27:29 +00:00
|
|
|
Position {x, y}
|
2020-04-05 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_position_from_string() {
|
|
|
|
assert_eq!(
|
2020-04-06 11:27:29 +00:00
|
|
|
position_from_string("4,12".to_string()),
|
|
|
|
Position { x: 4, y: 12 }
|
2020-04-05 22:00:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn position_to_string(position: Position) -> String {
|
2020-04-06 11:27:29 +00:00
|
|
|
format!("{},{}", position.x, position.y)
|
2020-04-05 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_position_to_string() {
|
|
|
|
assert_eq!(
|
2020-04-06 11:27:29 +00:00
|
|
|
position_to_string(Position { x: 4, y: 12 }),
|
|
|
|
"4,12".to_string()
|
2020-04-05 22:00:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:58:10 +00:00
|
|
|
fn sprite_from_string(string: String) -> Sprite {
|
|
|
|
let mut lines: Vec<&str> = string.split("\n").collect();
|
|
|
|
|
|
|
|
let id = lines[0].replace("SPR ", "");
|
|
|
|
let mut name = None;
|
|
|
|
let mut dialogue = None;
|
2020-04-06 11:27:29 +00:00
|
|
|
let mut room: Option<String> = None;
|
2020-04-05 22:58:10 +00:00
|
|
|
let mut position: Option<Position> = None;
|
|
|
|
|
|
|
|
for _ in 0..3 {
|
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
|
|
|
|
if last_line.starts_with("NAME") {
|
|
|
|
name = Some(last_line.replace("NAME ", "").to_string());
|
|
|
|
} else if last_line.starts_with("DLG") {
|
|
|
|
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
|
|
|
} else if last_line.starts_with("POS") {
|
2020-04-06 11:27:29 +00:00
|
|
|
let last_line = last_line.replace("POS ", "");
|
|
|
|
let room_position: Vec<&str> = last_line.split(' ').collect();
|
|
|
|
room = Some(room_position[0].to_string());
|
|
|
|
position = Some(position_from_string(room_position[1].to_string()));
|
2020-04-05 22:58:10 +00:00
|
|
|
} else {
|
|
|
|
lines.push(last_line); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:27:29 +00:00
|
|
|
let room = room.unwrap();
|
2020-04-05 22:58:10 +00:00
|
|
|
let position = position.unwrap();
|
|
|
|
|
|
|
|
// todo dedupe
|
|
|
|
let animation_frames = lines[1..].join("");
|
|
|
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
|
|
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
|
|
|
image_from_string(frame.to_string())
|
|
|
|
}).collect();
|
|
|
|
|
2020-04-06 11:27:29 +00:00
|
|
|
Sprite { id, name, animation_frames, dialogue, room, position }
|
2020-04-05 22:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sprite_from_string() {
|
|
|
|
let output = sprite_from_string("SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string());
|
2020-04-06 07:27:08 +00:00
|
|
|
let expected = test_sprite();
|
2020-04-05 22:58:10 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-05 19:46:55 +00:00
|
|
|
|
2020-04-06 06:23:20 +00:00
|
|
|
fn sprite_to_string(sprite: Sprite) -> String {
|
|
|
|
format!(
|
2020-04-06 07:27:08 +00:00
|
|
|
"SPR {}\n{}{}{}\nPOS {}",
|
2020-04-06 06:23:20 +00:00
|
|
|
sprite.id,
|
|
|
|
animation_frames_to_string(sprite.animation_frames),
|
2020-04-06 07:27:08 +00:00
|
|
|
if sprite.name.is_some() {format!("\nNAME {}", sprite.name.unwrap())} else {"".to_string()},
|
|
|
|
if sprite.dialogue.is_some() {format!("\nDLG {}", sprite.dialogue.unwrap())} else {"".to_string()},
|
2020-04-06 06:23:20 +00:00
|
|
|
position_to_string(sprite.position),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:27:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_sprite_to_string() {
|
|
|
|
let output = sprite_to_string(test_sprite());
|
|
|
|
let expected = "SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:54:43 +00:00
|
|
|
fn item_from_string(string: String) -> Item {
|
|
|
|
let mut lines: Vec<&str> = string.split("\n").collect();
|
|
|
|
|
|
|
|
let id = lines[0].replace("ITM ", "");
|
|
|
|
let mut name = None;
|
|
|
|
let mut dialogue = None;
|
|
|
|
|
|
|
|
for _ in 0..2 {
|
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
|
|
|
|
if last_line.starts_with("NAME") {
|
|
|
|
name = Some(last_line.replace("NAME ", "").to_string());
|
|
|
|
} else if last_line.starts_with("DLG") {
|
|
|
|
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
|
|
|
} else {
|
|
|
|
lines.push(last_line); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 08:02:52 +00:00
|
|
|
// todo dedupe
|
2020-04-06 07:54:43 +00:00
|
|
|
let animation_frames = lines[1..].join("");
|
|
|
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
|
|
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
|
|
|
image_from_string(frame.to_string())
|
|
|
|
}).collect();
|
|
|
|
|
2020-04-06 08:02:52 +00:00
|
|
|
Item { id, name, animation_frames, dialogue }
|
2020-04-06 07:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_item_from_string() {
|
|
|
|
let output = item_from_string("ITM 6\n01000000\n00000000\n00000000\n00000100\n00100000\n00000000\n00000000\n00000010\nNAME door\nDLG ITM_2".to_string());
|
2020-04-06 08:02:52 +00:00
|
|
|
let expected = test_item();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item_to_string(item: Item) -> String {
|
|
|
|
format!(
|
|
|
|
"ITM {}\n{}{}{}",
|
|
|
|
item.id,
|
|
|
|
animation_frames_to_string(item.animation_frames),
|
|
|
|
if item.name.is_some() {format!("\nNAME {}", item.name.unwrap())} else {"".to_string()},
|
|
|
|
if item.dialogue.is_some() {format!("\nDLG {}", item.dialogue.unwrap())} else {"".to_string()},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_item_to_string() {
|
|
|
|
let output = item_to_string(test_item());
|
|
|
|
let expected = "ITM 6\n01000000\n00000000\n00000000\n00000100\n00100000\n00000000\n00000000\n00000010\nNAME door\nDLG ITM_2".to_string();
|
2020-04-06 07:54:43 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:21:59 +00:00
|
|
|
fn exit_from_string(string: String) -> Exit {
|
|
|
|
// e.g. "4 3,3"
|
|
|
|
let room_position: Vec<&str> = string.split(' ').collect();
|
|
|
|
let room = room_position[0].to_string();
|
|
|
|
let position = position_from_string(room_position[1].to_string());
|
|
|
|
|
|
|
|
Exit { room, position }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_exit_from_string() {
|
|
|
|
assert_eq!(
|
|
|
|
exit_from_string("a 12,13".to_string()),
|
|
|
|
Exit { room: "a".to_string(), position: Position { x: 12, y: 13}}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_to_string(exit: Exit) -> String {
|
|
|
|
format!("{} {}", exit.room, position_to_string(exit.position))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_exit_to_string() {
|
|
|
|
assert_eq!(
|
|
|
|
exit_to_string(Exit { room: "8".to_string(), position: Position { x: 5, y: 6 }}),
|
|
|
|
"8 5,6".to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ending_from_string(string: String) -> Ending {
|
2020-04-06 13:11:56 +00:00
|
|
|
let string = string.replace("END ", "");
|
|
|
|
let id_dialogue: Vec<&str> = string.split('\n').collect();
|
|
|
|
let id = id_dialogue[0].to_string();
|
|
|
|
let dialogue = id_dialogue[1].to_string();
|
|
|
|
|
|
|
|
Ending { id, dialogue }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ending_from_string() {
|
|
|
|
assert_eq!(
|
|
|
|
ending_from_string("END a\nThis is a long line of dialogue. Blah blah blah".to_string()),
|
|
|
|
Ending { id: "a".to_string(), dialogue: "This is a long line of dialogue. Blah blah blah".to_string() }
|
|
|
|
);
|
2020-04-06 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ending_to_string(ending: Ending) -> String {
|
2020-04-06 13:11:56 +00:00
|
|
|
format!("END {}\n{}", ending.id, ending.dialogue)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ending_to_string() {
|
|
|
|
assert_eq!(
|
|
|
|
ending_to_string(
|
|
|
|
Ending {
|
|
|
|
id: "7".to_string(),
|
|
|
|
dialogue: "This is another long ending. So long, farewell, etc.".to_string()
|
|
|
|
}
|
|
|
|
),
|
|
|
|
"END 7\nThis is another long ending. So long, farewell, etc.".to_string()
|
|
|
|
);
|
2020-04-06 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn room_from_string(string: String) -> Room {
|
|
|
|
// todo handle room_format?
|
|
|
|
let mut lines: Vec<&str> = string.split("\n").collect();
|
|
|
|
let id = lines[0].replace("ROOM ", "");
|
|
|
|
let mut name = None;
|
|
|
|
let mut palette = "0".to_string();
|
|
|
|
let mut items: HashMap<Position, String> = HashMap::new();
|
2020-04-06 13:21:58 +00:00
|
|
|
let mut exits: HashMap<Position, Exit> = HashMap::new();
|
2020-04-06 12:21:59 +00:00
|
|
|
let endings: HashMap<Position, Ending> = HashMap::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
|
|
|
|
if last_line.starts_with("NAME") {
|
|
|
|
name = Some(last_line.replace("NAME ", "").to_string());
|
|
|
|
} else if last_line.starts_with("PAL") {
|
|
|
|
palette = last_line.replace("PAL ", "").to_string();
|
|
|
|
} else if last_line.starts_with("ITM") {
|
|
|
|
let last_line = last_line.replace("ITM ", "");
|
2020-04-06 13:23:37 +00:00
|
|
|
let item_position: Vec<&str> = last_line.split(' ').collect();
|
|
|
|
let item_id = item_position[0];
|
|
|
|
let position = item_position[1];
|
|
|
|
let position = position_from_string(position.to_string());
|
|
|
|
|
|
|
|
items.insert(position, item_id.to_string());
|
2020-04-06 12:21:59 +00:00
|
|
|
} else if last_line.starts_with("EXT") {
|
2020-04-06 13:21:58 +00:00
|
|
|
let last_line = last_line.replace("EXT ", "");
|
|
|
|
let parts: Vec<&str> = last_line.split(' ').collect();
|
|
|
|
let position = position_from_string(parts[0].to_string());
|
|
|
|
let exit = exit_from_string(format!("{} {}", parts[1], parts[2]));
|
2020-04-06 12:21:59 +00:00
|
|
|
|
2020-04-06 13:21:58 +00:00
|
|
|
exits.insert(position, exit);
|
2020-04-06 12:21:59 +00:00
|
|
|
} else if last_line.starts_with("END") {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
lines.push(last_line); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Room {
|
|
|
|
id,
|
|
|
|
palette,
|
|
|
|
name,
|
|
|
|
tiles: vec![],
|
2020-04-06 13:23:37 +00:00
|
|
|
items,
|
2020-04-06 13:21:58 +00:00
|
|
|
exits,
|
2020-04-06 12:21:59 +00:00
|
|
|
endings: Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
// fn game_from_string(game: String ) -> Game {
|
2020-04-05 19:46:55 +00:00
|
|
|
// // probably needs to split the game data into different segments starting from the end
|
|
|
|
// // e.g. VAR... then END... then DLG...
|
|
|
|
// // then split all these up into their individual items
|
2020-04-05 17:58:04 +00:00
|
|
|
//
|
2020-04-06 07:54:43 +00:00
|
|
|
// // no - let's try going from the beginning and popping off elements from the start?
|
|
|
|
// }
|
|
|
|
|
2020-04-05 17:58:04 +00:00
|
|
|
// fn game_to_string(game: Game) -> String {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
fn main() {
|
2020-04-06 13:23:37 +00:00
|
|
|
println!("why does a crate need a main function?")
|
2020-04-05 17:58:04 +00:00
|
|
|
}
|