handle room formats

This commit is contained in:
Max Bradbury 2020-04-24 18:07:32 +01:00
parent 1c6e3eb515
commit ebb172df7e
3 changed files with 17 additions and 8 deletions

View File

@ -65,8 +65,6 @@ some more practical uses would be things like:
* handle old out-of-bounds editor errors (extra image pixels, exits/endings with position -1,-0 etc.) * handle old out-of-bounds editor errors (extra image pixels, exits/endings with position -1,-0 etc.)
* it's ok if the output of these does not match the input as we're fixing it * it's ok if the output of these does not match the input as we're fixing it
* "unquote" function for dialogues, game title, anything else that supports """ * "unquote" function for dialogues, game title, anything else that supports """
* if old room format, export game in old room format
* handle windows line endings (cr+lf) - https://crates.io/crates/loe
* add Bitsy HD and Bitsy 7.0 test data * add Bitsy HD and Bitsy 7.0 test data
### tidy up ### tidy up

View File

@ -242,7 +242,7 @@ impl ToString for Game {
} }
for room in &self.rooms { for room in &self.rooms {
segments.push(room.to_string()); segments.push(room.to_string(self.room_format(), self.room_type));
} }
for tile in &self.tiles { for tile in &self.tiles {

View File

@ -100,10 +100,14 @@ impl From<String> for Room {
let mut tiles: Vec<String> = Vec::new(); let mut tiles: Vec<String> = Vec::new();
for line in lines.into_iter() { for line in lines.into_iter() {
let line: Vec<&str> = line let comma_separated = line.contains(","); // old room format?
.split(if line.contains(",") {","} else {""}) // old room format? let mut line: Vec<&str> = line
.split(if comma_separated {","} else {""})
.collect(); .collect();
if ! comma_separated { line = line[1..].to_owned(); }
let line = line[..16].to_owned();
for tile_id in line { for tile_id in line {
tiles.push(tile_id.to_string()); tiles.push(tile_id.to_string());
} }
@ -136,9 +140,16 @@ impl Room {
let sqrt = (self.tiles.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD let sqrt = (self.tiles.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in self.tiles.chunks(sqrt) { for line in self.tiles.chunks(sqrt) {
for tile in line { for tile in line {
tiles.push_str(&format!("{},", tile)); tiles.push_str(tile);
if room_format == RoomFormat::CommaSeparated {
tiles.push(',');
}
} }
tiles.pop(); // remove trailing comma
if room_format == RoomFormat::CommaSeparated {
tiles.pop(); // remove trailing comma
}
tiles.push_str("\n"); tiles.push_str("\n");
} }
tiles.pop(); // remove trailing newline tiles.pop(); // remove trailing newline