2020-04-18 09:45:01 +00:00
|
|
|
use crate::{
|
|
|
|
Avatar,
|
|
|
|
Dialogue,
|
|
|
|
Ending,
|
|
|
|
Font,
|
|
|
|
Item,
|
|
|
|
Palette,
|
|
|
|
Room,
|
|
|
|
Sprite,
|
|
|
|
TextDirection,
|
|
|
|
Tile,
|
|
|
|
Variable,
|
|
|
|
optional_data_line
|
|
|
|
};
|
2020-04-12 13:38:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-04-12 16:13:08 +00:00
|
|
|
pub struct Game {
|
|
|
|
pub name: String,
|
2020-04-17 08:37:03 +00:00
|
|
|
pub major_version: u8,
|
|
|
|
pub minor_version: u8,
|
2020-04-13 12:30:26 +00:00
|
|
|
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
|
2020-04-18 09:45:01 +00:00
|
|
|
pub font: Font,
|
|
|
|
pub custom_font: Option<String>, // used if font is Font::Custom
|
|
|
|
pub text_direction: TextDirection,
|
2020-04-12 16:13:08 +00:00
|
|
|
pub palettes: Vec<Palette>,
|
|
|
|
pub rooms: Vec<Room>,
|
|
|
|
pub tiles: Vec<Tile>,
|
|
|
|
pub avatar: Avatar,
|
|
|
|
pub sprites: Vec<Sprite>,
|
|
|
|
pub items: Vec<Item>,
|
|
|
|
pub dialogues: Vec<Dialogue>,
|
|
|
|
pub endings: Vec<Ending>,
|
|
|
|
pub variables: Vec<Variable>,
|
2020-04-12 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Game {
|
|
|
|
fn from(string: String) -> Game {
|
2020-04-13 23:34:03 +00:00
|
|
|
let mut string = format!("{}\n\n", string.trim());
|
|
|
|
|
|
|
|
if string.starts_with("# BITSY VERSION") {
|
|
|
|
string = format!("\n\n{}", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
let string = string;
|
|
|
|
|
2020-04-12 13:38:07 +00:00
|
|
|
// dialogues and endings can have 2+ line breaks inside, so deal with these separately
|
|
|
|
// otherwise, everything can be split on a double line break (\n\n)
|
|
|
|
let mut dialogues: Vec<Dialogue> = Vec::new();
|
|
|
|
let mut endings: Vec<Ending> = Vec::new();
|
|
|
|
let mut variables: Vec<Variable> = Vec::new();
|
|
|
|
let main_split: Vec<&str> = string.split("\n\nDLG").collect();
|
|
|
|
let main = main_split[0].to_string();
|
|
|
|
let mut dialogues_endings_variables: String = main_split[1..].join("\n\nDLG");
|
|
|
|
|
|
|
|
let variable_segments = dialogues_endings_variables.clone();
|
|
|
|
let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect();
|
|
|
|
if variable_segments.len() > 0 {
|
|
|
|
dialogues_endings_variables = variable_segments[0].to_string();
|
|
|
|
let variable_segments = variable_segments[1..].to_owned();
|
|
|
|
|
|
|
|
for segment in variable_segments {
|
|
|
|
let segment = format!("VAR{}", segment);
|
|
|
|
variables.push(Variable::from(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ending_segments = dialogues_endings_variables.clone();
|
|
|
|
let ending_segments: Vec<&str> = ending_segments.split("\n\nEND").collect();
|
|
|
|
if ending_segments.len() > 0 {
|
|
|
|
dialogues_endings_variables = ending_segments[0].to_string();
|
|
|
|
let ending_segments = ending_segments[1..].to_owned();
|
|
|
|
|
|
|
|
for segment in ending_segments {
|
|
|
|
let segment = format!("END{}", segment);
|
|
|
|
endings.push(Ending::from(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dialogue_segments = format!("\n\nDLG {}", dialogues_endings_variables.trim());
|
|
|
|
let dialogue_segments: Vec<&str> = dialogue_segments.split("\n\nDLG").collect();
|
|
|
|
for segment in dialogue_segments[1..].to_owned() {
|
|
|
|
let segment = format!("DLG{}", segment);
|
|
|
|
dialogues.push(Dialogue::from(segment));
|
|
|
|
}
|
|
|
|
|
|
|
|
let segments: Vec<&str> = main.split("\n\n").collect();
|
|
|
|
|
|
|
|
let name = segments[0].to_string();
|
2020-04-17 08:37:03 +00:00
|
|
|
let mut major_version: u8 = 1;
|
|
|
|
let mut minor_version: u8 = 0;
|
2020-04-12 13:38:07 +00:00
|
|
|
let mut room_format: u8 = 1;
|
2020-04-18 09:45:01 +00:00
|
|
|
let mut font = Font::AsciiSmall;
|
|
|
|
let mut custom_font = None;
|
|
|
|
let mut text_direction = TextDirection::LeftToRight;
|
2020-04-12 13:38:07 +00:00
|
|
|
let mut palettes: Vec<Palette> = Vec::new();
|
|
|
|
let mut rooms: Vec<Room> = Vec::new();
|
|
|
|
let mut tiles: Vec<Tile> = Vec::new();
|
|
|
|
let mut avatar: Option<Avatar> = None; // unwrap this later
|
|
|
|
let mut sprites: Vec<Sprite> = Vec::new();
|
|
|
|
let mut items: Vec<Item> = Vec::new();
|
|
|
|
|
|
|
|
for segment in segments[1..].to_owned() {
|
|
|
|
let segment = segment.to_string();
|
|
|
|
|
|
|
|
if segment.starts_with("# BITSY VERSION") {
|
2020-04-17 08:37:03 +00:00
|
|
|
let segment = segment.replace("# BITSY VERSION ", "");
|
|
|
|
let parts = Game::version_from_string(segment);
|
|
|
|
major_version = parts.0;
|
|
|
|
minor_version = parts.1;
|
2020-04-12 13:38:07 +00:00
|
|
|
} else if segment.starts_with("! ROOM_FORMAT") {
|
|
|
|
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
|
2020-04-18 09:45:01 +00:00
|
|
|
} else if segment.starts_with("DEFAULT_FONT") {
|
|
|
|
let segment = segment.replace("DEFAULT_FONT ", "");
|
|
|
|
|
|
|
|
font = Font::from(&segment);
|
|
|
|
|
|
|
|
if font == Font::Custom {
|
|
|
|
custom_font = Some(segment.to_string());
|
|
|
|
}
|
|
|
|
} else if segment.trim() == "TEXT_DIRECTION RTL".to_string() {
|
|
|
|
text_direction = TextDirection::RightToLeft;
|
2020-04-12 13:38:07 +00:00
|
|
|
} else if segment.starts_with("PAL") {
|
|
|
|
palettes.push(Palette::from(segment));
|
|
|
|
} else if segment.starts_with("ROOM") {
|
|
|
|
rooms.push(Room::from(segment));
|
|
|
|
} else if segment.starts_with("TIL") {
|
|
|
|
tiles.push(Tile::from(segment));
|
|
|
|
} else if segment.starts_with("SPR A") {
|
|
|
|
avatar = Some(Avatar::from(segment));
|
|
|
|
} else if segment.starts_with("SPR") {
|
|
|
|
sprites.push(Sprite::from(segment));
|
|
|
|
} else if segment.starts_with("ITM") {
|
|
|
|
items.push(Item::from(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(avatar.is_some());
|
|
|
|
let avatar = avatar.unwrap();
|
|
|
|
|
|
|
|
Game {
|
|
|
|
name,
|
2020-04-17 08:37:03 +00:00
|
|
|
major_version,
|
|
|
|
minor_version,
|
2020-04-12 13:38:07 +00:00
|
|
|
room_format,
|
2020-04-18 09:45:01 +00:00
|
|
|
font,
|
|
|
|
custom_font,
|
|
|
|
text_direction,
|
2020-04-12 13:38:07 +00:00
|
|
|
palettes,
|
|
|
|
rooms,
|
|
|
|
tiles,
|
|
|
|
avatar,
|
|
|
|
sprites,
|
|
|
|
items,
|
|
|
|
dialogues,
|
|
|
|
endings,
|
|
|
|
variables,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Game {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
let mut segments: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
// todo refactor
|
|
|
|
|
|
|
|
for palette in &self.palettes {
|
|
|
|
segments.push(palette.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for room in &self.rooms {
|
|
|
|
segments.push(room.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for tile in &self.tiles {
|
|
|
|
segments.push(tile.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
segments.push(self.avatar.to_string());
|
|
|
|
|
|
|
|
for sprite in &self.sprites {
|
|
|
|
segments.push(sprite.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for item in &self.items {
|
|
|
|
segments.push(item.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for dialogue in &self.dialogues {
|
|
|
|
segments.push(dialogue.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for ending in &self.endings {
|
|
|
|
segments.push(ending.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
for variable in &self.variables {
|
|
|
|
segments.push(variable.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
format!(
|
2020-04-17 08:37:03 +00:00
|
|
|
"{}\n{}\n\n! ROOM_FORMAT {}\n\n{}\n\n",
|
2020-04-12 13:38:07 +00:00
|
|
|
&self.name,
|
2020-04-17 08:37:03 +00:00
|
|
|
&self.version_line(),
|
2020-04-12 13:38:07 +00:00
|
|
|
&self.room_format,
|
|
|
|
segments.join("\n\n"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:01:42 +00:00
|
|
|
impl Game {
|
2020-04-13 15:19:59 +00:00
|
|
|
fn tile_ids(&self) -> Vec<u64> {
|
|
|
|
self.tiles.iter().map(|tile| {tile.id}).collect()
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:01:42 +00:00
|
|
|
/// first available tile ID.
|
|
|
|
/// e.g. if current tile IDs are [0, 2, 3] the result will be `1`
|
|
|
|
/// if current tile IDs are [0, 1, 2] the result will be `3`
|
2020-04-13 15:19:59 +00:00
|
|
|
fn new_tile_id(&self) -> u64 {
|
|
|
|
let mut new_id = 0;
|
|
|
|
|
|
|
|
let mut ids = self.tile_ids();
|
|
|
|
ids.sort();
|
|
|
|
|
|
|
|
for id in ids {
|
|
|
|
if new_id == id {
|
|
|
|
new_id += 1;
|
|
|
|
} else {
|
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_id + 1
|
|
|
|
}
|
2020-04-13 16:44:51 +00:00
|
|
|
/// adds a tile safely and returns the new tile ID
|
|
|
|
fn add_tile(&mut self, mut tile: Tile) -> u64 {
|
|
|
|
let new_id = self.new_tile_id();
|
|
|
|
tile.id = new_id;
|
|
|
|
self.tiles.push(tile);
|
|
|
|
new_id
|
|
|
|
}
|
2020-04-17 08:37:03 +00:00
|
|
|
|
|
|
|
fn version_line(&self) -> String {
|
|
|
|
format!("\n# BITSY VERSION {}.{}", self.major_version, self.minor_version)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn room_format_line(&self) -> String {
|
|
|
|
optional_data_line("! ROOM_FORMAT", Some(self.room_format))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version_from_string(string: String) -> (u8, u8) {
|
|
|
|
let parts: Vec<&str> = string.split(".").collect();
|
|
|
|
assert_eq!(parts.len(), 2);
|
|
|
|
(parts[0].parse().unwrap(),parts[1].parse().unwrap())
|
|
|
|
}
|
2020-04-13 16:44:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:38:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_game_from_string() {
|
|
|
|
let output = Game::from(
|
2020-04-13 23:17:40 +00:00
|
|
|
include_str!["test-resources/default.bitsy"].to_string()
|
2020-04-12 13:38:07 +00:00
|
|
|
);
|
|
|
|
|
2020-04-18 09:45:01 +00:00
|
|
|
let expected = crate::mock::game_default();
|
2020-04-12 13:38:07 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_game_to_string() {
|
2020-04-18 09:45:01 +00:00
|
|
|
let output = crate::mock::game_default().to_string();
|
2020-04-13 23:17:40 +00:00
|
|
|
let expected = include_str!["test-resources/default.bitsy"].to_string();
|
2020-04-12 13:38:07 +00:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-13 15:19:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tile_ids() {
|
2020-04-18 09:45:01 +00:00
|
|
|
assert_eq!(crate::mock::game_default().tile_ids(), vec![10]);
|
2020-04-13 15:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_tile_id() {
|
|
|
|
// default tile has an id of 10 ("a"), so 0 is available
|
2020-04-18 09:45:01 +00:00
|
|
|
assert_eq!(crate::mock::game_default().new_tile_id(), 0);
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-18 09:45:01 +00:00
|
|
|
let mut game = crate::mock::game_default();
|
2020-04-13 15:19:59 +00:00
|
|
|
let mut tiles : Vec<Tile> = Vec::new();
|
|
|
|
|
|
|
|
for n in 0..9 {
|
|
|
|
if n != 4 {
|
2020-04-18 09:45:01 +00:00
|
|
|
let mut new_tile = crate::mock::tile_default();
|
2020-04-13 15:19:59 +00:00
|
|
|
new_tile.id = n;
|
|
|
|
tiles.push(new_tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
game.tiles = tiles;
|
|
|
|
|
|
|
|
assert_eq!(game.new_tile_id(), 4);
|
|
|
|
|
|
|
|
// fill in the space created above, and test that tile IDs get sorted
|
|
|
|
|
2020-04-18 09:45:01 +00:00
|
|
|
let mut new_tile = crate::mock::tile_default();
|
2020-04-13 15:19:59 +00:00
|
|
|
new_tile.id = 4;
|
|
|
|
game.tiles.push(new_tile);
|
|
|
|
|
|
|
|
assert_eq!(game.new_tile_id(), 10);
|
|
|
|
}
|
2020-04-13 16:44:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_tile() {
|
2020-04-18 09:45:01 +00:00
|
|
|
let mut game = crate::mock::game_default();
|
|
|
|
let new_id = game.add_tile(crate::mock::tile_default());
|
2020-04-13 16:44:51 +00:00
|
|
|
assert_eq!(new_id, 0);
|
|
|
|
assert_eq!(game.tiles.len(), 2);
|
2020-04-18 09:45:01 +00:00
|
|
|
let new_id = game.add_tile(crate::mock::tile_default());
|
2020-04-13 16:44:51 +00:00
|
|
|
assert_eq!(new_id, 1);
|
|
|
|
assert_eq!(game.tiles.len(), 3);
|
|
|
|
}
|
2020-04-13 23:41:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bitsy_omnibus() {
|
|
|
|
let acceptable_failures: Vec<String> = vec![
|
|
|
|
// avatar ordering issues
|
|
|
|
"src/test-resources/omnibus/682993AC.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/0D901EE6.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/7FEF71E4.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/245E93CB.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/A643C5F4.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/7533372B.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/DBD5D375.bitsy.txt".to_string(),
|
|
|
|
|
|
|
|
// fails because of sprite colours but also because a tile contains "NaN"
|
|
|
|
"src/test-resources/omnibus/DA88C287.bitsy.txt".to_string(),
|
|
|
|
|
|
|
|
// fails because room wall array is not implemented - @todo investigate
|
|
|
|
// (this game uses room_format 1 - I thought it'd be using 0...)
|
|
|
|
"src/test-resources/omnibus/76EB6E4A.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/DC053B1A.bitsy.txt".to_string(),
|
|
|
|
|
|
|
|
// todo handle fonts!
|
|
|
|
"src/test-resources/omnibus/4B4EB988.bitsy.txt".to_string(),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut passes = 0;
|
|
|
|
let mut skips = 0;
|
|
|
|
|
2020-04-18 09:45:01 +00:00
|
|
|
for file in std::fs::read_dir("src/test-resources/omnibus").unwrap() {
|
2020-04-13 23:41:05 +00:00
|
|
|
let path = file.unwrap().path();
|
|
|
|
let nice_name = format!("{}", path.display());
|
|
|
|
|
|
|
|
if ! nice_name.contains("bitsy") || acceptable_failures.contains(&nice_name) {
|
|
|
|
skips += 1;
|
|
|
|
println!("Skipping: {}", nice_name);
|
|
|
|
println!("Skipped. {} passes, {} skips.", passes, skips);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("\nTesting: {}...", path.display());
|
2020-04-18 09:45:01 +00:00
|
|
|
let game_data = std::fs::read_to_string(path).unwrap();
|
2020-04-13 23:41:05 +00:00
|
|
|
let game = Game::from(game_data.clone());
|
|
|
|
assert_eq!(game.to_string().trim(), game_data.trim());
|
|
|
|
passes += 1;
|
|
|
|
println!("Success! {} passes, {} skips.", passes, skips);
|
|
|
|
}
|
|
|
|
}
|