2020-04-24 17:06:17 +00:00
|
|
|
use crate::{Avatar, Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings};
|
2020-04-18 16:48:29 +00:00
|
|
|
use std::error::Error;
|
2020-04-24 17:06:17 +00:00
|
|
|
use loe::TransformMode;
|
|
|
|
|
|
|
|
/// in very early versions of Bitsy, room tiles were defined as single characters
|
|
|
|
/// so, only 36 tiles total. later versions are comma-separated
|
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
|
|
|
pub enum RoomFormat {Contiguous, CommaSeparated}
|
|
|
|
|
|
|
|
impl RoomFormat {
|
|
|
|
fn from(str: &str) -> Result<RoomFormat, &'static dyn Error> {
|
|
|
|
match str {
|
|
|
|
"0" => Ok(RoomFormat::Contiguous),
|
|
|
|
"1" => Ok(RoomFormat::CommaSeparated),
|
|
|
|
_ => panic!(format!("Invalid room format: {}", str)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match &self {
|
|
|
|
RoomFormat::Contiguous => "0",
|
|
|
|
RoomFormat::CommaSeparated => "1",
|
|
|
|
}.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// in very early versions of Bitsy, a room was called a "set"
|
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
|
|
|
pub enum RoomType {Room, Set}
|
|
|
|
|
|
|
|
impl From<&str> for RoomType {
|
|
|
|
fn from(string: &str) -> RoomType {
|
|
|
|
match string {
|
|
|
|
"ROOM" => RoomType::Room,
|
|
|
|
"SET" => RoomType::Set,
|
|
|
|
_ => panic!("Unrecognised room type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for RoomType {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match &self {
|
|
|
|
RoomType::Set => "SET",
|
|
|
|
RoomType::Room => "ROOM",
|
|
|
|
}.to_string()
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 13:38:07 +00:00
|
|
|
|
2020-04-23 11:03:39 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
2020-04-18 10:03:24 +00:00
|
|
|
pub struct Version {
|
|
|
|
pub major: u8,
|
|
|
|
pub minor: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Version {
|
|
|
|
fn from(str: &str) -> Version {
|
|
|
|
let parts: Vec<&str> = str.split(".").collect();
|
|
|
|
assert_eq!(parts.len(), 2);
|
2020-04-18 15:58:30 +00:00
|
|
|
Version {
|
|
|
|
major: parts[0].parse().unwrap(),
|
|
|
|
minor: parts[1].parse().unwrap(),
|
|
|
|
}
|
2020-04-18 10:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 11:03:39 +00:00
|
|
|
pub version: Option<Version>,
|
2020-04-24 17:06:17 +00:00
|
|
|
pub room_format: Option<RoomFormat>,
|
|
|
|
pub(crate) room_type: RoomType,
|
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-23 06:43:52 +00:00
|
|
|
pub font_data: Option<String>, // todo make this an actual struct for parsing
|
2020-04-23 11:07:45 +00:00
|
|
|
pub(crate) line_endings_crlf: bool, // otherwise lf (unix/mac)
|
2020-04-12 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 16:48:29 +00:00
|
|
|
impl Game {
|
2020-04-18 17:07:25 +00:00
|
|
|
pub fn from(string: String) -> Result<Game, &'static dyn Error> {
|
2020-04-24 17:06:17 +00:00
|
|
|
let line_endings_crlf = string.contains("\r\n");
|
|
|
|
let mut string = string;
|
|
|
|
if line_endings_crlf {
|
|
|
|
string = transform_line_endings(string, TransformMode::LF)
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:32:50 +00:00
|
|
|
let mut string = format!("{}\n\n", string.trim_matches('\n'));
|
2020-04-13 23:34:03 +00:00
|
|
|
|
|
|
|
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();
|
2020-04-23 06:43:52 +00:00
|
|
|
let mut font_data: Option<String> = None;
|
2020-04-12 13:38:07 +00:00
|
|
|
let main_split: Vec<&str> = string.split("\n\nDLG").collect();
|
|
|
|
let main = main_split[0].to_string();
|
2020-04-23 06:43:52 +00:00
|
|
|
let mut extra: String = main_split[1..].join("\n\nDLG");
|
2020-04-12 13:38:07 +00:00
|
|
|
|
2020-04-23 06:43:52 +00:00
|
|
|
if extra.contains("\n\nFONT") {
|
|
|
|
let parts = extra.clone();
|
|
|
|
let parts: Vec<&str> = parts.split("\n\nFONT").collect();
|
|
|
|
font_data = Some(format!("FONT{}", parts[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
let variable_segments = extra.clone();
|
2020-04-12 13:38:07 +00:00
|
|
|
let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect();
|
|
|
|
if variable_segments.len() > 0 {
|
2020-04-23 06:43:52 +00:00
|
|
|
extra = variable_segments[0].to_string();
|
2020-04-12 13:38:07 +00:00
|
|
|
let variable_segments = variable_segments[1..].to_owned();
|
|
|
|
|
|
|
|
for segment in variable_segments {
|
|
|
|
let segment = format!("VAR{}", segment);
|
|
|
|
variables.push(Variable::from(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 06:43:52 +00:00
|
|
|
let ending_segments = extra.clone();
|
2020-04-12 13:38:07 +00:00
|
|
|
let ending_segments: Vec<&str> = ending_segments.split("\n\nEND").collect();
|
|
|
|
if ending_segments.len() > 0 {
|
2020-04-23 06:43:52 +00:00
|
|
|
extra = ending_segments[0].to_string();
|
2020-04-12 13:38:07 +00:00
|
|
|
let ending_segments = ending_segments[1..].to_owned();
|
|
|
|
|
|
|
|
for segment in ending_segments {
|
|
|
|
let segment = format!("END{}", segment);
|
|
|
|
endings.push(Ending::from(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:58:30 +00:00
|
|
|
let dialogue_segments =
|
2020-04-23 06:43:52 +00:00
|
|
|
format!("\n\nDLG{}", extra.trim_matches('\n'));
|
2020-04-18 15:32:50 +00:00
|
|
|
|
2020-04-12 13:38:07 +00:00
|
|
|
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-23 11:03:39 +00:00
|
|
|
let mut version = None;
|
2020-04-24 17:06:17 +00:00
|
|
|
let mut room_format = None;
|
|
|
|
let mut room_type = RoomType::Room;
|
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 ", "");
|
2020-04-23 11:03:39 +00:00
|
|
|
version = Some(Version::from(&segment));
|
2020-04-12 13:38:07 +00:00
|
|
|
} else if segment.starts_with("! ROOM_FORMAT") {
|
2020-04-24 17:06:17 +00:00
|
|
|
let segment = segment.replace("! ROOM_FORMAT ", "");
|
|
|
|
room_format = Some(RoomFormat::from(&segment).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));
|
2020-04-18 14:33:04 +00:00
|
|
|
} else if segment.starts_with("ROOM") || segment.starts_with("SET") {
|
2020-04-24 17:06:17 +00:00
|
|
|
if segment.starts_with("SET") {
|
|
|
|
room_type = RoomType::Set;
|
|
|
|
}
|
2020-04-12 13:38:07 +00:00
|
|
|
rooms.push(Room::from(segment));
|
|
|
|
} else if segment.starts_with("TIL") {
|
|
|
|
tiles.push(Tile::from(segment));
|
|
|
|
} else if segment.starts_with("SPR A") {
|
2020-04-18 16:42:32 +00:00
|
|
|
avatar = Some(Avatar::from(segment).unwrap());
|
2020-04-12 13:38:07 +00:00
|
|
|
} 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();
|
|
|
|
|
2020-04-18 17:08:16 +00:00
|
|
|
Ok(Game {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
room_format,
|
2020-04-24 17:06:17 +00:00
|
|
|
room_type,
|
2020-04-18 17:08:16 +00:00
|
|
|
font,
|
|
|
|
custom_font,
|
|
|
|
text_direction,
|
|
|
|
palettes,
|
|
|
|
rooms,
|
|
|
|
tiles,
|
|
|
|
avatar,
|
|
|
|
sprites,
|
|
|
|
items,
|
|
|
|
dialogues,
|
|
|
|
endings,
|
|
|
|
variables,
|
2020-04-23 06:43:52 +00:00
|
|
|
font_data,
|
2020-04-24 17:06:17 +00:00
|
|
|
line_endings_crlf
|
2020-04-18 17:08:16 +00:00
|
|
|
})
|
2020-04-12 13:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-04-24 17:07:32 +00:00
|
|
|
segments.push(room.to_string(self.room_format(), self.room_type));
|
2020-04-12 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for tile in &self.tiles {
|
|
|
|
segments.push(tile.to_string());
|
|
|
|
}
|
|
|
|
|
2020-04-18 12:38:20 +00:00
|
|
|
// for some reason the sprites with numeric IDs go first,
|
|
|
|
// then SPR A (avatar), then all the non-numeric IDs
|
|
|
|
fn is_string_numeric(str: String) -> bool {
|
|
|
|
for c in str.chars() {
|
2020-04-18 15:58:30 +00:00
|
|
|
if !c.is_numeric() {
|
2020-04-18 12:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for sprite in &self.sprites {
|
|
|
|
if is_string_numeric(sprite.id.to_base36()) {
|
|
|
|
segments.push(sprite.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:38:07 +00:00
|
|
|
segments.push(self.avatar.to_string());
|
|
|
|
|
|
|
|
for sprite in &self.sprites {
|
2020-04-18 15:58:30 +00:00
|
|
|
if !is_string_numeric(sprite.id.to_base36()) {
|
2020-04-18 12:38:41 +00:00
|
|
|
segments.push(sprite.to_string());
|
|
|
|
}
|
2020-04-12 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-04-23 06:43:52 +00:00
|
|
|
if self.font_data.is_some() {
|
|
|
|
segments.push(self.font_data.to_owned().unwrap())
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:08:21 +00:00
|
|
|
transform_line_endings(
|
|
|
|
format!(
|
|
|
|
"{}{}{}{}{}\n\n{}\n\n",
|
|
|
|
&self.name,
|
|
|
|
&self.version_line(),
|
|
|
|
&self.room_format_line(),
|
|
|
|
&self.font_line(),
|
|
|
|
&self.text_direction_line(),
|
|
|
|
segments.join("\n\n"),
|
|
|
|
),
|
|
|
|
if self.line_endings_crlf {TransformMode::CRLF} else {TransformMode::LF}
|
2020-04-12 13:38:07 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:01:42 +00:00
|
|
|
impl Game {
|
2020-04-18 09:46:33 +00:00
|
|
|
pub fn tile_ids(&self) -> Vec<u64> {
|
2020-04-18 15:58:30 +00:00
|
|
|
self.tiles.iter().map(|tile| tile.id).collect()
|
2020-04-13 15:19:59 +00:00
|
|
|
}
|
|
|
|
|
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-18 09:46:33 +00:00
|
|
|
pub fn new_tile_id(&self) -> u64 {
|
2020-04-13 15:19:59 +00:00
|
|
|
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
|
2020-04-18 09:46:33 +00:00
|
|
|
pub fn add_tile(&mut self, mut tile: Tile) -> u64 {
|
2020-04-13 16:44:51 +00:00
|
|
|
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 {
|
2020-04-23 11:03:39 +00:00
|
|
|
if self.version.is_some() {
|
|
|
|
format!(
|
|
|
|
"\n# BITSY VERSION {}.{}",
|
|
|
|
self.version.as_ref().unwrap().major, self.version.as_ref().unwrap().minor
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
2020-04-17 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn room_format_line(&self) -> String {
|
|
|
|
optional_data_line("! ROOM_FORMAT", Some(self.room_format))
|
|
|
|
}
|
2020-04-18 11:46:46 +00:00
|
|
|
|
|
|
|
fn font_line(&self) -> String {
|
|
|
|
if self.font == Font::AsciiSmall {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
if self.font == Font::Custom {
|
2020-04-18 12:37:26 +00:00
|
|
|
format!("\n\nDEFAULT_FONT {}", self.custom_font.as_ref().unwrap())
|
2020-04-18 11:46:46 +00:00
|
|
|
} else {
|
2020-04-18 12:37:26 +00:00
|
|
|
format!("\n\nDEFAULT_FONT {}", self.font.to_string().unwrap())
|
2020-04-18 11:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn text_direction_line(&self) -> &str {
|
2020-04-18 15:58:30 +00:00
|
|
|
if self.text_direction == TextDirection::RightToLeft {
|
|
|
|
"\n\nTEXT_DIRECTION RTL"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
}
|
2020-04-18 11:46:46 +00:00
|
|
|
}
|
2020-04-23 11:03:39 +00:00
|
|
|
|
|
|
|
/// older bitsy games do not specify a version, but we can infer 1.0
|
|
|
|
pub fn version(&self) -> Version {
|
|
|
|
self.version.unwrap_or(Version { major: 1, minor: 0 })
|
|
|
|
}
|
2020-04-13 16:44:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::game::{Version, Game};
|
|
|
|
use crate::text::{TextDirection, Font};
|
|
|
|
use crate::tile::Tile;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_game_from_string() {
|
|
|
|
let output = Game::from(include_str!["test-resources/default.bitsy"].to_string()).unwrap();
|
|
|
|
let expected = crate::mock::game_default();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 13:38:07 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_game_to_string() {
|
|
|
|
let output = crate::mock::game_default().to_string();
|
|
|
|
let expected = include_str!["test-resources/default.bitsy"].to_string();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tile_ids() {
|
|
|
|
assert_eq!(crate::mock::game_default().tile_ids(), vec![10]);
|
|
|
|
}
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_new_tile_id() {
|
|
|
|
// default tile has an id of 10 ("a"), so 0 is available
|
|
|
|
assert_eq!(crate::mock::game_default().new_tile_id(), 0);
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
let mut game = crate::mock::game_default();
|
|
|
|
let mut tiles: Vec<Tile> = Vec::new();
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
for n in 0..9 {
|
|
|
|
if n != 4 {
|
|
|
|
let mut new_tile = crate::mock::tile_default();
|
|
|
|
new_tile.id = n;
|
|
|
|
tiles.push(new_tile);
|
|
|
|
}
|
2020-04-13 15:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
game.tiles = tiles;
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(game.new_tile_id(), 4);
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
// fill in the space created above, and test that tile IDs get sorted
|
2020-04-13 15:19:59 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
let mut new_tile = crate::mock::tile_default();
|
|
|
|
new_tile.id = 4;
|
|
|
|
game.tiles.push(new_tile);
|
2020-04-13 23:41:05 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(game.new_tile_id(), 10);
|
2020-04-18 15:46:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_add_tile() {
|
|
|
|
let mut game = crate::mock::game_default();
|
|
|
|
let new_id = game.add_tile(crate::mock::tile_default());
|
|
|
|
assert_eq!(new_id, 0);
|
|
|
|
assert_eq!(game.tiles.len(), 2);
|
|
|
|
let new_id = game.add_tile(crate::mock::tile_default());
|
|
|
|
assert_eq!(new_id, 1);
|
|
|
|
assert_eq!(game.tiles.len(), 3);
|
|
|
|
}
|
2020-04-13 23:41:05 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bitsy_omnibus() {
|
|
|
|
let acceptable_failures: Vec<String> = vec![
|
|
|
|
// fails because of sprite colours but also because a tile contains "NaN"
|
|
|
|
"src/test-resources/omnibus/DA88C287.bitsy.txt".to_string(),
|
|
|
|
// SET instead of ROOM? todo investigate
|
|
|
|
"src/test-resources/omnibus/1998508E.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/046871F8.bitsy.txt".to_string(),
|
|
|
|
// not sure about this one but it uses room wall array
|
|
|
|
"src/test-resources/omnibus/748F77B5.bitsy.txt".to_string(),
|
|
|
|
// bad game data
|
|
|
|
"src/test-resources/omnibus/14C48FA0.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/C63A0633.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/C63A0633.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/013B3CDE.bitsy.txt".to_string(), // NaN in image
|
|
|
|
// this one has font data appended to the end of the game data - is this valid?
|
|
|
|
"src/test-resources/omnibus/4B4EB988.bitsy.txt".to_string(),
|
|
|
|
// has an ending position of -1
|
|
|
|
"src/test-resources/omnibus/593BD9A6.bitsy.txt".to_string(),
|
|
|
|
// extra line between dialogues
|
|
|
|
"src/test-resources/omnibus/DB59A848.bitsy.txt".to_string(),
|
|
|
|
// something going on with dialogues? todo investigate
|
|
|
|
"src/test-resources/omnibus/807805CC.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/C36E27E5.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/354DA56F.bitsy.txt".to_string(),
|
|
|
|
// this avatar has `ITM 0 1` - can the player start with an item? todo investigate
|
|
|
|
"src/test-resources/omnibus/CC5085BE.bitsy.txt".to_string(),
|
|
|
|
"src/test-resources/omnibus/20D06BD1.bitsy.txt".to_string(),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut passes = 0;
|
|
|
|
let mut skips = 0;
|
|
|
|
|
|
|
|
let files = std::fs::read_dir("src/test-resources/omnibus");
|
|
|
|
|
|
|
|
if !files.is_ok() {
|
|
|
|
return;
|
2020-04-13 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
for file in files.unwrap() {
|
|
|
|
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!("Testing: {}...", path.display());
|
|
|
|
let game_data = std::fs::read_to_string(path).unwrap();
|
|
|
|
let game = Game::from(game_data.clone()).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
game.to_string().trim_matches('\n'),
|
|
|
|
game_data.trim_matches('\n')
|
|
|
|
);
|
|
|
|
passes += 1;
|
|
|
|
println!("Success! {} passes, {} skips.", passes, skips);
|
|
|
|
}
|
2020-04-13 23:41:05 +00:00
|
|
|
}
|
2020-04-18 11:45:43 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_arabic() {
|
|
|
|
let game = Game::from(include_str!("test-resources/arabic.bitsy").to_string()).unwrap();
|
2020-04-18 16:48:29 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(game.font, Font::Arabic);
|
|
|
|
assert_eq!(game.text_direction, TextDirection::RightToLeft);
|
|
|
|
}
|
2020-04-18 11:49:51 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_version_formatting() {
|
|
|
|
let mut game = crate::mock::game_default();
|
2020-04-23 11:03:39 +00:00
|
|
|
game.version = Some(Version { major: 5, minor: 0 });
|
2020-04-19 07:13:55 +00:00
|
|
|
assert!(game.to_string().contains("# BITSY VERSION 5.0"))
|
|
|
|
}
|
2020-04-18 11:49:51 +00:00
|
|
|
}
|