1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::{
    optional_data_line,
    Exit,
    ExitInstance,
    Instance,
    Position,
    RoomType,
    RoomFormat,
    Transition
};

use std::collections::HashMap;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Room {
    pub id: String,
    /// palette ID was optional in very early versions
    pub palette_id: Option<String>,
    pub name: Option<String>,
    /// tile IDs
    pub tiles: Vec<String>,
    pub items: Vec<Instance>,
    pub exits: Vec<ExitInstance>,
    pub endings: Vec<Instance>,
    /// old method of handling walls - a comma-separated list of tile IDs
    pub walls: Option<Vec<String>>,
}

impl Room {
    fn name_line(&self) -> String {
        optional_data_line("NAME", self.name.as_ref())
    }

    fn wall_line(&self) -> String {
        if let Some(walls) = &self.walls {
            optional_data_line("WAL", Some(walls.join(",")))
        } else {
            "".to_string()
        }
    }

    fn palette_line(&self) -> String {
        match &self.palette_id {
            Some(id) => optional_data_line("PAL", Some(id.clone())),
            None => "".to_string(),
        }
    }
}

impl From<String> for Room {
    fn from(string: String) -> Room {
        let string = string.replace("ROOM ", "");
        let string = string.replace("SET ", "");
        let mut lines: Vec<&str> = string.lines().collect();
        let id = lines[0].to_string();
        let mut name = None;
        let mut palette_id = None;
        let mut items: Vec<Instance> = Vec::new();
        let mut exits: Vec<ExitInstance> = Vec::new();
        let mut endings: Vec<Instance> = Vec::new();
        let mut walls = None;

        loop {
            let last_line = lines.pop().unwrap();

            if last_line.starts_with("WAL") {
                let last_line = last_line.replace("WAL ", "");
                let ids: Vec<&str> = last_line.split(',').collect();
                walls = Some(ids.iter().map(|&id| id.to_string()).collect());
            } else if last_line.starts_with("NAME") {
                name = Some(last_line.replace("NAME ", "").to_string());
            } else if last_line.starts_with("PAL") {
                palette_id = Some(last_line.replace("PAL ", ""));
            } 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];

                if let Ok(position) = Position::from_str(position) {
                    items.push(Instance { position, id: item_id.to_string() });
                }
            } else if last_line.starts_with("EXT") {
                let last_line = last_line.replace("EXT ", "");
                let parts: Vec<&str> = last_line.split(' ').collect();
                let position = Position::from_str(parts[0]);

                if let Ok(position) = position {
                    let exit = Exit::from_str(
                        &format!("{} {}", parts[1], parts[2])
                    );

                    if let Ok(exit) = exit {
                        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_str(chunk[1]).unwrap());
                            } else if chunk[0] == "DLG" {
                                dialogue_id = Some(chunk[1].to_string());
                            }
                        }

                        exits.push(ExitInstance { position, exit, transition, dialogue_id });
                    }
                }
            } 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();
                let position = ending_position[1];
                let position = Position::from_str(position);

                if let Ok(position) = position {
                    endings.push(Instance { position, id: ending });
                }
            } else {
                lines.push(last_line);
                break;
            }
        }

        let lines = &lines[1..];
        let dimension = lines.len(); // x or y, e.g. `16` for 16x16
        let mut tiles: Vec<String> = Vec::new();

        for line in lines.iter() {
            let comma_separated = line.contains(','); // old room format?
            let mut line: Vec<&str> = line
                .split(if comma_separated {","} else {""})
                .collect();

            if ! comma_separated { line = line[1..].to_owned(); }
            let line = line[..dimension].to_owned();

            for tile_id in line {
                tiles.push(tile_id.to_string());
            }
        }

        items.reverse();
        exits.reverse();
        endings.reverse();

        Room {
            id,
            palette_id,
            name,
            tiles,
            items,
            exits,
            endings,
            walls,
        }
    }
}

impl Room {
    pub fn to_string(&self, room_format: RoomFormat, room_type: RoomType) -> String {
        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) {
            for tile in line {
                tiles.push_str(tile);
                if room_format == RoomFormat::CommaSeparated {
                    tiles.push(',');
                }
            }

            if room_format == RoomFormat::CommaSeparated {
                tiles.pop(); // remove trailing comma
            }

            tiles.push('\n');
        }

        tiles.pop(); // remove trailing newline

        for instance in &self.items {
            items.push_str(&format!(
                "\nITM {} {}",
                instance.id,
                instance.position.to_string()
            ));
        }

        for instance in &self.exits {
            exits.push_str(&format!(
                "\nEXT {} {}{}{}{}",
                instance.position.to_string(),
                instance.exit.to_string(),
                match &instance.transition {
                    Some(transition) => transition,
                    None => &Transition::None,
                }.to_string(),
                if instance.dialogue_id.is_some() {" DLG "} else {""},
                instance.dialogue_id.as_ref().unwrap_or(&"".to_string()),
            ));
        }

        for instance in &self.endings {
            endings.push_str(&format!(
                "\nEND {} {}",
                instance.id,
                instance.position.to_string()
            ));
        }

        format!(
            "{} {}\n{}{}{}{}{}{}{}",
            room_type.to_string(),
            self.id,
            tiles,
            self.name_line(),
            self.wall_line(),
            items,
            exits,
            endings,
            self.palette_line()
        )
    }

    /// "changes" is a hash of old -> new tile IDs
    pub fn change_tile_ids(&mut self, changes: &HashMap<String, String>) {
        self.tiles = self.tiles.iter().map(|tile_id|
            changes.get(tile_id).unwrap_or(tile_id).clone()
        ).collect();
    }
}

#[cfg(test)]
mod test {
    use crate::{Room, RoomType, RoomFormat};

    #[test]
    fn room_from_string() {
        assert_eq!(
            Room::from(include_str!("test-resources/room").to_string()),
            crate::mock::room()
        );
    }

    #[test]
    fn room_to_string() {
        assert_eq!(
            crate::mock::room().to_string(RoomFormat::CommaSeparated, RoomType::Room),
            include_str!("test-resources/room").to_string()
        );
    }

    #[test]
    fn room_walls_array() {
        let output = Room::from(include_str!("test-resources/room-with-walls").to_string());

        assert_eq!(output.walls, Some(vec!["a".to_string(), "f".to_string()]));
    }
}