← All Workshops

MudEngine Part 3: File-Based World Loading

File loader

Read the game.toml as World State

mud-engine-repl/src/main.rs
impl World {
    fn from_file(path: &str) -> Self {
        let content = std::fs::read_to_string(path)
            .expect("Failed to read game.toml");
        let toml_world: TomlWorld = toml::from_str(&content)
            .expect("Failed to parse game.toml");

        let rooms: Vec<Room> = toml_world
            .rooms
            .iter()
            .map(|r| Room {
                id: r.id,
                name: r.name.clone(),
                description: r.description.clone(),
                exits: r.exits.iter().map(|e| (e.direction.clone(), e.destination)).collect(),
            })
            .collect();

        World { rooms, player_room: 1 }
    }
}
🔍 How room lookup works

The loader no longer needs a name-to-index map. Since both the TOML destination and the player_room use numeric room ids, the look and go methods use iter().find() to locate rooms:

let room = self.rooms.iter().find(|r| r.id == self.player_room).unwrap();

This linear scan is fine for a few rooms. For thousands of rooms, you would build a HashMap<usize, &Room> for O(1) lookups — but that is an optimisation you can add later when you need it.