bitsy-parser/src/room.rs

256 lines
8.2 KiB
Rust
Raw Normal View History

2020-04-18 15:58:30 +00:00
use crate::{from_base36, optional_data_line, Exit, ExitInstance, Instance, Position, ToBase36};
use crate::game::{RoomType, RoomFormat};
use crate::exit::Transition;
2020-04-29 17:33:22 +00:00
use std::str::FromStr;
2020-04-12 13:28:11 +00:00
2020-05-31 15:12:23 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
2020-04-12 13:28:11 +00:00
pub struct Room {
2020-06-18 13:44:20 +00:00
pub id: String,
/// palette ID was optional in very early versions
pub palette_id: Option<String>,
pub name: Option<String>,
2020-06-18 13:44:20 +00:00
/// tile IDs
pub tiles: Vec<String>,
pub items: Vec<Instance>,
pub exits: Vec<ExitInstance>,
pub endings: Vec<Instance>,
2020-06-18 13:44:20 +00:00
/// old method of handling walls - a comma-separated list of tile IDs
pub walls: Vec<String>,
2020-04-12 13:28:11 +00:00
}
2020-04-13 18:14:57 +00:00
impl Room {
#[inline]
2020-04-13 18:14:57 +00:00
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
2020-04-18 13:39:17 +00:00
#[inline]
2020-04-18 13:39:17 +00:00
fn wall_line(&self) -> String {
if self.walls.len() > 0 {
2020-06-18 13:44:20 +00:00
optional_data_line("WAL", Some(self.walls.join(",")))
2020-04-18 13:39:17 +00:00
} else {
"".to_string()
}
}
2020-04-18 14:50:01 +00:00
#[inline]
2020-04-18 14:50:01 +00:00
fn palette_line(&self) -> String {
if self.palette_id.is_some() {
2020-06-18 13:44:20 +00:00
optional_data_line("PAL", Some(self.palette_id.as_ref().unwrap()))
2020-04-18 14:50:01 +00:00
} else {
"".to_string()
}
}
2020-04-13 18:14:57 +00:00
}
2020-04-12 13:28:11 +00:00
impl From<String> for Room {
#[inline]
2020-04-12 13:28:11 +00:00
fn from(string: String) -> Room {
2020-04-18 14:33:04 +00:00
let string = string.replace("ROOM ", "");
let string = string.replace("SET ", "");
2020-04-12 13:28:11 +00:00
let mut lines: Vec<&str> = string.lines().collect();
2020-06-18 13:44:20 +00:00
let id = lines[0].to_string();
2020-04-12 13:28:11 +00:00
let mut name = None;
2020-04-18 14:50:01 +00:00
let mut palette_id = None;
2020-04-12 13:28:11 +00:00
let mut items: Vec<Instance> = Vec::new();
let mut exits: Vec<ExitInstance> = Vec::new();
let mut endings: Vec<Instance> = Vec::new();
2020-06-18 13:44:20 +00:00
let mut walls: Vec<String> = Vec::new();
2020-04-12 13:28:11 +00:00
loop {
let last_line = lines.pop().unwrap();
2020-04-18 13:39:17 +00:00
if last_line.starts_with("WAL") {
let last_line = last_line.replace("WAL ", "");
let ids: Vec<&str> = last_line.split(",").collect();
2020-06-18 13:44:20 +00:00
walls = ids.iter().map(|&id| id.to_string()).collect();
2020-04-18 13:39:17 +00:00
} else if last_line.starts_with("NAME") {
2020-04-12 13:28:11 +00:00
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("PAL") {
2020-06-18 13:44:20 +00:00
palette_id = Some(last_line.replace("PAL ", ""));
2020-04-12 13:28:11 +00:00
} else if last_line.starts_with("ITM") {
let last_line = last_line.replace("ITM ", "");
let item_position: Vec<&str> = last_line.split(' ').collect();
let item_id = item_position[0];
let position = item_position[1];
2020-04-29 17:33:22 +00:00
let position = Position::from_str(position);
2020-04-12 13:28:11 +00:00
2020-04-29 17:33:22 +00:00
if position.is_ok() {
let position = position.unwrap();
items.push(Instance { position, id: item_id.to_string() });
}
2020-04-12 13:28:11 +00:00
} else if last_line.starts_with("EXT") {
let last_line = last_line.replace("EXT ", "");
let parts: Vec<&str> = last_line.split(' ').collect();
2020-04-29 17:33:22 +00:00
let position = Position::from_str(parts[0]);
if position.is_ok() {
let position = position.unwrap();
2020-04-29 19:43:49 +00:00
let exit = Exit::from_str(
&format!("{} {}", parts[1], parts[2])
);
if exit.is_ok() {
let exit = exit.unwrap();
let mut transition = None;
let mut dialogue_id = None;
let chunks = parts[3..].chunks(2);
for chunk in chunks {
if chunk[0] == "FX" {
transition = Some(Transition::from(chunk[1]));
} else if chunk[0] == "DLG" {
dialogue_id = Some(chunk[1].to_string());
}
2020-04-29 17:33:22 +00:00
}
2020-04-29 19:43:49 +00:00
exits.push(ExitInstance { position, exit, transition, dialogue_id });
}
}
2020-04-12 13:28:11 +00:00
} else if last_line.starts_with("END") {
let last_line = last_line.replace("END ", "");
let ending_position: Vec<&str> = last_line.split(' ').collect();
let ending = ending_position[0].to_string();
2020-04-29 17:33:22 +00:00
let position = ending_position[1];
let position = Position::from_str(position);
2020-04-12 13:28:11 +00:00
2020-04-29 17:33:22 +00:00
if position.is_ok() {
let position = position.unwrap();
endings.push(Instance { position, id: ending });
}
2020-04-12 13:28:11 +00:00
} else {
lines.push(last_line);
break;
}
}
let lines = &lines[1..];
2020-04-28 18:30:18 +00:00
let dimension = lines.len(); // x or y, e.g. `16` for 16x16
2020-04-12 13:28:11 +00:00
let mut tiles: Vec<String> = Vec::new();
for line in lines.into_iter() {
2020-04-24 17:07:32 +00:00
let comma_separated = line.contains(","); // old room format?
let mut line: Vec<&str> = line
.split(if comma_separated {","} else {""})
2020-04-23 07:57:46 +00:00
.collect();
2020-04-12 13:28:11 +00:00
2020-04-24 17:07:32 +00:00
if ! comma_separated { line = line[1..].to_owned(); }
2020-04-28 18:30:18 +00:00
let line = line[..dimension].to_owned();
2020-04-24 17:07:32 +00:00
2020-04-12 13:28:11 +00:00
for tile_id in line {
tiles.push(tile_id.to_string());
}
}
2020-04-13 18:26:11 +00:00
items.reverse();
exits.reverse();
endings.reverse();
2020-04-18 15:58:30 +00:00
Room {
id,
palette_id,
name,
tiles,
items,
exits,
endings,
walls,
}
2020-04-12 13:28:11 +00:00
}
}
impl Room {
#[inline]
pub fn to_string(&self, room_format: RoomFormat, room_type: RoomType) -> String {
2020-04-12 13:28:11 +00:00
let mut tiles = String::new();
let mut items = String::new();
let mut exits = String::new();
let mut endings = String::new();
for line in self.tiles.chunks(16) {
2020-04-12 13:28:11 +00:00
for tile in line {
2020-04-24 17:07:32 +00:00
tiles.push_str(tile);
if room_format == RoomFormat::CommaSeparated {
tiles.push(',');
}
}
if room_format == RoomFormat::CommaSeparated {
tiles.pop(); // remove trailing comma
2020-04-12 13:28:11 +00:00
}
2020-04-24 17:07:32 +00:00
2020-04-12 13:28:11 +00:00
tiles.push_str("\n");
}
tiles.pop(); // remove trailing newline
for instance in &self.items {
2020-04-18 15:58:30 +00:00
items.push_str(&format!(
"\nITM {} {}",
instance.id,
instance.position.to_string()
));
2020-04-12 13:28:11 +00:00
}
for instance in &self.exits {
2020-04-18 15:58:30 +00:00
exits.push_str(&format!(
"\nEXT {} {}{}{}{}",
2020-04-18 15:58:30 +00:00
instance.position.to_string(),
instance.exit.to_string(),
if instance.transition.is_some() {
instance.transition.as_ref().unwrap().to_string()
} else {"".to_string()},
if instance.dialogue_id.is_some() {" DLG "} else {""},
instance.dialogue_id.as_ref().unwrap_or(&"".to_string()),
2020-04-18 15:58:30 +00:00
));
2020-04-12 13:28:11 +00:00
}
for instance in &self.endings {
2020-04-18 15:58:30 +00:00
endings.push_str(&format!(
"\nEND {} {}",
instance.id,
instance.position.to_string()
));
2020-04-12 13:28:11 +00:00
}
format!(
"{} {}\n{}{}{}{}{}{}{}",
room_type.to_string(),
2020-06-18 13:44:20 +00:00
self.id,
2020-04-12 13:28:11 +00:00
tiles,
2020-04-13 18:14:57 +00:00
self.name_line(),
2020-04-18 13:39:17 +00:00
self.wall_line(),
2020-04-12 13:28:11 +00:00
items,
exits,
endings,
2020-04-18 14:50:01 +00:00
self.palette_line()
2020-04-12 13:28:11 +00:00
)
}
}
2020-04-19 07:13:55 +00:00
#[cfg(test)]
mod test {
use crate::room::Room;
use crate::game::{RoomType, RoomFormat};
2020-04-19 07:13:55 +00:00
#[test]
fn test_room_from_string() {
assert_eq!(
Room::from(include_str!("test-resources/room").to_string()),
crate::mock::room()
);
}
#[test]
fn test_room_to_string() {
assert_eq!(
crate::mock::room().to_string(RoomFormat::CommaSeparated, RoomType::Room),
2020-04-19 07:13:55 +00:00
include_str!("test-resources/room").to_string()
);
}
2020-04-18 13:39:17 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_room_walls_array() {
let output = Room::from(include_str!("test-resources/room-with-walls").to_string());
2020-04-18 13:39:17 +00:00
2020-06-18 13:44:20 +00:00
assert_eq!(output.walls, vec!["a".to_string(), "f".to_string()]);
2020-04-19 07:13:55 +00:00
}
2020-04-18 13:39:17 +00:00
}