MudEngine Part 9: The Twin Guardians
Server Quest Logic
Now we add the core quest engine — two functions in a new server/quest.rs file (declared with mod quest; in server/mod.rs):
update_quest— after any player move or disconnect, check whether the pedestals are occupied, open or close the portal (add/remove the dynamic exits), teleport vault strays home, and return the event to broadcastcheck_quest_completion— after any item change, check whether theretrieval_itemsits in thecompletion_room(on the floor or in a player's inventory)
Both run inside the GAME_STATE lock and mutate in-memory state only. The dynamic exits live on RoomInfo (server-side) — they shape the live world without ever touching Game.toml.
The GAME_STATE initializer in server/mod.rs also needs a small update to seed the quest from the two files:
/// The authoritative game state — rooms, connected players, item positions, quest. /// The world and quest config come from Game.toml (static); players, ground, /// and quest completion come from Players.toml (live). #[cfg(feature = "server")] pub static GAME_STATE: LazyLock<Mutex<GameState>> = LazyLock::new(|| { // Start the background autosave task before any player connects. let _ = LazyLock::force(&AUTOSAVE); // Live state from the last session (players + inventories + ground + quest flag) let live = persistence::load_players("Players.toml"); // Seed the ground from the static item layout, then apply live changes let mut ground = persistence::seed_ground("Game.toml"); for (room, items) in live.ground { ground.insert(room, items); } // Static quest config from Game.toml + the persisted completion flag let quest = persistence::load_quest("Game.toml").map(|config| persistence::QuestStatus { config, previously_active: false, completed: live.quest_completed.unwrap_or(false), }); Mutex::new(GameState { rooms: persistence::load_world("Game.toml"), players: live.players, ground, quest, }) });
use crate::game::persistence::{Exit, GameState}; /// Evaluate quest conditions and mutate game state accordingly. /// Returns the event to broadcast, or None if nothing changed. pub fn update_quest(state: &mut GameState) -> Option<(bool, String)> { let quest = state.quest.as_mut()?; // Once the fragment reached the King, the quest is won for good. if quest.completed { return None; } // Both pedestals need at least one player — and a second player exists. let all_occupied = quest .config .guardians .iter() .all(|g| state.players.values().any(|p| p.room == g.room_id)); let now_active = all_occupied && state.players.len() >= 2; if now_active == quest.previously_active { return None; // no change — nothing to broadcast } quest.previously_active = now_active; if now_active { // Open the portal: room 4 gains a "down" exit to the vault, // and the vault gains an "up" exit back to room 4. let reward = &quest.config.reward; if let Some(room) = state .rooms .iter_mut() .find(|r| r.id == reward.source_room_id) && !room.exits.iter().any(|e| e.direction == reward.direction) { room.exits.push(Exit { direction: reward.direction.clone(), destination: reward.target_room_id, }); } if let Some(room) = state .rooms .iter_mut() .find(|r| r.id == reward.target_room_id) && !room.exits.iter().any(|e| e.direction == "up") { room.exits.push(Exit { direction: "up".into(), destination: reward.source_room_id, }); } Some(( true, format!( "☀️🌙 The Twin Guardians are united! A shimmering portal opens in {}!", state .rooms .iter() .find(|r| r.id == reward.source_room_id) .map(|r| &r.name[..]) .unwrap_or("the source room") ), )) } else { // Close the portal and teleport anyone in the vault back home. let reward = &quest.config.reward; let source_id = reward.source_room_id; if let Some(room) = state.rooms.iter_mut().find(|r| r.id == source_id) { room.exits.retain(|e| e.direction != reward.direction); } if let Some(room) = state .rooms .iter_mut() .find(|r| r.id == reward.target_room_id) { room.exits.retain(|e| e.direction != "up"); } let victims: Vec<String> = state .players .iter() .filter(|(_, p)| p.room == reward.target_room_id) .map(|(id, _)| id.clone()) .collect(); for victim_id in &victims { if let Some(player) = state.players.get_mut(victim_id) { player.room = source_id; } } Some(( false, format!( "🌙☀️ The Twin Guardians part ways! The portal to the {} seals shut!", reward.target_name ), )) } } /// Check whether the retrieval item has arrived in the completion room. /// If so, mark the quest completed and return a celebration message. pub fn check_quest_completion(state: &mut GameState) -> Option<String> { let quest = state.quest.as_mut()?; if quest.completed { return None; } let item = &quest.config.retrieval_item; let room_id = quest.config.completion_room; // On the floor? (the live ground map, not the static Game.toml rooms) let on_floor = state .ground .get(&room_id) .is_some_and(|items| items.iter().any(|i| i == item)); // In a player's inventory while they stand in the completion room? let in_inventory = state .players .values() .any(|p| p.room == room_id && p.items.iter().any(|i| i == item)); if on_floor || in_inventory { quest.completed = true; Some(format!( "👑 King Aldric beams with joy! 'The Star Fragment is returned! The realm is saved!' All hail the heroes who reclaimed the {}!", item )) } else { None } }
update_quest — the gate
Runs inside the GAME_STATE lock after every move and disconnect.
- If the quest is
completed, return None — the quest is won, guardians are free - Are all guardian rooms occupied? Requires
state.players.len() >= 2, so a single player can't activate both pedestals alone - No change since the last call? Return None (nothing to broadcast)
- Otherwise flip
previously_active, add or remove the dynamicdown/upexits onRoomInfo, and teleport vault strays back to Town Square - Return an event the caller broadcasts to every client
check_quest_completion — the goal
Runs where items move (take, drop, and every successful move). Checks the completion_room: is the retrieval_item on the floor (the live ground map) or in a player's inventory there? Marks completed = true and returns a celebration message.
Why two functions?
The gate and the goal are different concerns — update_quest after moves/disconnects, check_quest_completion after item or position changes. Splitting them keeps each trigger point explicit in the WebSocket handler.
Where does the state live?
All of it is in-memory mutation on GameState. The portal exits (on RoomInfo) are session state — Game.toml never knows about them. Only completed crosses a restart, and only through the autosave flush to Players.toml.