← All Workshops

MudEngine Part 6: Multiplayer

Step 7 / 19

Static Room Data

The 9 rooms and their connections are pure data — they do not change while the game runs, and both sides need them:

  • The server validates movement against the exit table (it stays authoritative).
  • The client uses the same table to highlight and allow clicking reachable rooms, and reads room names/descriptions for display.

Because the data is identical on both sides, we store it once in src/game/room_data.rs and share it between the server and the client. ROOM_NAMES and ROOM_DESCS are pub static (readable from any component), and EXITS is a LazyLock<Vec<Vec<Exit>>> — each room's exits are the shared Exit struct, not raw (direction, destination) tuples.

mud-engine/src/game/room_data.rs
use std::sync::LazyLock;

// ── Static room data (shared by client and server) ──

pub static ROOM_NAMES: &[&str] = &[
    "Forest Path", "Hilltop", "Abandoned Tower",
    "Dark Forest", "Town Square", "Temple Courtyard",
    "Riverbank", "Old Bridge", "Graveyard",
];

pub static ROOM_DESCS: &[&str] = &[
    "A winding path leads through ancient oaks. Sunlight filters through the canopy.",
    "From this vantage point you can see the entire valley. A cool breeze carries the scent of pine.",
    "A crumbling stone tower stands alone. Vines crawl up its walls and crows nest in the windows.",
    "Twisted trees block out most of the light. Strange sounds echo through the undergrowth.",
    "A bustling town square with a fountain at its center. Cobblestones gleam from the morning rain.",
    "Ancient stone pillars surround a quiet courtyard. Moss clings to weathered statues.",
    "A slow-moving river borders a muddy bank. Frogs croak from the reeds.",
    "A weathered stone bridge crosses the river. Moss covers the ancient masonry.",
    "Rows of moss-covered headstones stretch into the fog. An iron gate creaks in the wind.",
];

/// A single exit — the shared `{ direction, destination }` struct.
/// This is the same `Exit` type the persistence layer uses in later parts
/// (Part 7's `Game.toml` rooms carry `exits: Vec<Exit>`), so the exit shape
/// stays identical whether it comes from a static table or from a file.
#[derive(Debug, Clone, PartialEq)]
pub struct Exit {
    pub direction: String,
    pub destination: usize,
}

/// Exit table: for each room index, a list of `Exit`s.
/// It is a `LazyLock` (not a `const`) because `Exit.direction` is a `String` — a
/// runtime-owned type that cannot be built in a `const`/`static` initializer.
/// The client uses it to highlight and allow clicking reachable rooms;
/// the server uses it to validate moves.
pub static EXITS: LazyLock<Vec<Vec<Exit>>> = LazyLock::new(|| {
    vec![
        vec![Exit { direction: "south".into(), destination: 3 }, Exit { direction: "east".into(), destination: 1 }],      // 0 Forest Path
        vec![Exit { direction: "south".into(), destination: 4 }, Exit { direction: "west".into(), destination: 0 }, Exit { direction: "east".into(), destination: 2 }], // 1 Hilltop
        vec![Exit { direction: "south".into(), destination: 5 }, Exit { direction: "west".into(), destination: 1 }],      // 2 Abandoned Tower
        vec![Exit { direction: "north".into(), destination: 0 }, Exit { direction: "south".into(), destination: 6 }, Exit { direction: "east".into(), destination: 4 }], // 3 Dark Forest
        vec![Exit { direction: "north".into(), destination: 1 }, Exit { direction: "south".into(), destination: 7 }, Exit { direction: "west".into(), destination: 3 }, Exit { direction: "east".into(), destination: 5 }], // 4 Town Square
        vec![Exit { direction: "north".into(), destination: 2 }, Exit { direction: "south".into(), destination: 8 }, Exit { direction: "west".into(), destination: 4 }], // 5 Temple Courtyard
        vec![Exit { direction: "north".into(), destination: 3 }, Exit { direction: "east".into(), destination: 7 }],      // 6 Riverbank
        vec![Exit { direction: "north".into(), destination: 4 }, Exit { direction: "west".into(), destination: 6 }, Exit { direction: "east".into(), destination: 8 }], // 7 Old Bridge
        vec![Exit { direction: "north".into(), destination: 5 }, Exit { direction: "west".into(), destination: 7 }],      // 8 Graveyard
    ]
});
Step 7 / 19