use std::collections::HashSet;
use crate::Player;
use crate::{
AmbleWorld, ItemId, View, ViewItem, WorldObject,
player::Flag,
room::Exit,
spinners::CoreSpinnerType,
style::GameStyle,
trigger::{TriggerCondition, check_triggers},
view::ViewMode,
};
use anyhow::{Context, Result, anyhow};
use log::info;
pub fn go_back_handler(world: &mut AmbleWorld, view: &mut View) -> Result<bool> {
let leaving_id = world.player.location.room_id().inspect_err(|_| {
view.push(ViewItem::ActionFailure("You're not in a room.".to_string()));
})?;
if let Some(previous_room_id) = world.player.go_back() {
world.player_path.push(previous_room_id.clone());
let travel_message = world.spin_core(CoreSpinnerType::Movement, "You retrace your steps...");
let previous_room = world
.rooms
.get(&previous_room_id)
.ok_or_else(|| anyhow!("invalid previous room in history ({previous_room_id})"))?;
info!(
"{} went back to {} ({})",
world.player.name(),
previous_room.name(),
previous_room.symbol()
);
view.push(ViewItem::TransitionMessage(travel_message));
previous_room.show(world, view, None)?;
check_triggers(
world,
view,
&[
TriggerCondition::Leave(leaving_id),
TriggerCondition::Enter(previous_room_id),
],
)?;
return Ok(true);
} else {
view.push(ViewItem::ActionFailure(
"You haven't been anywhere else yet.".to_string(),
));
}
Ok(false)
}
pub fn move_to_handler(world: &mut AmbleWorld, view: &mut View, input_dir: &str) -> Result<bool> {
let player_name = world.player.name.clone();
let travel_message = world.spin_core(CoreSpinnerType::Movement, "You head that way...");
let leaving_id = world.player.location.room_id().inspect_err(|_| {
view.push(ViewItem::ActionFailure("You're not in a room.".to_string()));
})?;
let input_dir_normalized = input_dir.to_lowercase();
let destination_exit = {
let current_room = world.player_room_ref()?;
let direction = current_room
.exits
.iter()
.find(|(dir, exit)| !exit.hidden && dir.to_lowercase().contains(&input_dir_normalized));
if let Some((_, exit)) = direction {
Some(exit)
} else {
view.push(ViewItem::Error(format!(
"{}? {}",
input_dir.error_style(),
world.spin_core(CoreSpinnerType::DestinationUnknown, "Which direction is that?")
)));
return Ok(false);
}
};
if let Some(destination_exit) = destination_exit {
if let Some(denial_reason) = exit_access_restriction(destination_exit, &world.player) {
handle_barred_exit(world, view, &denial_reason, destination_exit)?;
return Ok(true);
}
let destination_id = destination_exit.to.clone();
world.player.move_to_room(destination_id.clone());
world.player_path.push(destination_id.clone());
let new_room = world
.rooms
.get(&destination_id)
.ok_or_else(|| anyhow!("invalid move destination ({destination_id})"))?;
info!("{} moving to {} ({})", player_name, new_room.name(), new_room.symbol());
view.push(ViewItem::TransitionMessage(travel_message));
if new_room.visited {
new_room.show(world, view, None)?;
} else {
world.player.score = world.player.score.saturating_add(1);
new_room.show(
world,
view,
if matches!(view.mode, ViewMode::Brief) {
Some(ViewMode::Verbose)
} else {
None
},
)?;
}
if let Some(new_room) = world.rooms.get_mut(&destination_id) {
new_room.visited = true;
}
check_triggers(
world,
view,
&[
TriggerCondition::Leave(leaving_id),
TriggerCondition::Enter(destination_id),
],
)?;
return Ok(true);
}
Ok(false)
}
#[derive(Debug, Clone, Default)]
struct AccessDenial<'a> {
unmet_flags: HashSet<&'a Flag>,
unmet_items: HashSet<&'a ItemId>,
locked: bool,
}
impl AccessDenial<'_> {
fn log_format(&self) -> String {
let unmet_item_list = self
.unmet_items
.iter()
.map(|&item| item.as_str())
.collect::<Vec<_>>()
.join(", ");
let unmet_flag_list = self
.unmet_flags
.iter()
.map(|&flag| flag.name())
.collect::<Vec<&str>>()
.join(", ");
format!(
"items ( {unmet_item_list} ), flags( {unmet_flag_list} ), locked( {} )",
self.locked
)
}
}
fn exit_access_restriction<'a>(exit: &'a Exit, player: &'a Player) -> Option<AccessDenial<'a>> {
let unmet_flags: HashSet<_> = exit.required_flags.difference(&player.flags).collect();
let unmet_items: HashSet<_> = exit.required_items.difference(&player.inventory).collect();
if unmet_flags.is_empty() && unmet_items.is_empty() && !exit.locked {
None
} else {
Some(AccessDenial {
unmet_flags,
unmet_items,
locked: exit.locked,
})
}
}
fn handle_barred_exit(
world: &AmbleWorld,
view: &mut View,
denial: &AccessDenial,
destination_exit: &crate::room::Exit,
) -> Result<()> {
let msg = match (&destination_exit.barred_message, &denial.locked) {
(Some(msg), _) => msg,
(None, true) => "You can't go that way: it's locked.",
(None, false) => "You can't go that way: something is missing or undone.",
};
view.push(ViewItem::ActionFailure(msg.denied_style().to_string()));
let (dest_name, dest_sym) = world
.rooms
.get(&destination_exit.to)
.map(|rm| (rm.name(), rm.symbol()))
.with_context(|| format!("accessing room {}", destination_exit.to))?;
info!(
"player denied access to {dest_name} ({dest_sym}) !>> {}",
denial.log_format(),
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::RoomId;
use crate::player::Flag;
use crate::room::{Exit, Room};
use crate::world::{AmbleWorld, Location};
use std::collections::{HashMap, HashSet};
fn build_test_world() -> (AmbleWorld, RoomId, RoomId, View) {
let view = View::new();
let mut world = AmbleWorld::new_empty();
let start = crate::idgen::new_room_id();
let dest = crate::idgen::new_room_id();
let mut start_room = Room {
id: start.clone(),
symbol: "start".into(),
name: "Start".into(),
base_description: String::new(),
overlays: vec![],
scenery: Vec::new(),
scenery_default: None,
location: Location::Nowhere,
visited: true,
exits: HashMap::new(),
contents: HashSet::new(),
npcs: HashSet::new(),
};
start_room.exits.insert("north".into(), Exit::new(dest.clone()));
let dest_room = Room {
id: dest.clone(),
symbol: "dest".into(),
name: "Dest".into(),
base_description: String::new(),
overlays: vec![],
scenery: Vec::new(),
scenery_default: None,
location: Location::Nowhere,
visited: false,
exits: HashMap::new(),
contents: HashSet::new(),
npcs: HashSet::new(),
};
world.rooms.insert(start.clone(), start_room);
world.rooms.insert(dest.clone(), dest_room);
world.player.location = Location::Room(start.clone());
(world, start, dest, view)
}
#[test]
fn move_to_hidden_exit_blocked() {
let (mut world, start, dest, mut view) = build_test_world();
{
world
.rooms
.get_mut(&start)
.unwrap()
.exits
.get_mut("north")
.unwrap()
.hidden = true;
}
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.score, initial);
assert!(!world.rooms.get(&dest).unwrap().visited);
}
#[test]
fn move_to_locked_exit_blocked() {
let (mut world, start, dest, mut view) = build_test_world();
world
.rooms
.get_mut(&start)
.unwrap()
.exits
.get_mut("north")
.unwrap()
.locked = true;
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.score, initial);
assert!(!world.rooms.get(&dest).unwrap().visited);
}
#[test]
fn move_requires_item() {
let (mut world, start, dest, mut view) = build_test_world();
let item_id: ItemId = crate::idgen::new_id().into();
world
.rooms
.get_mut(&start)
.unwrap()
.exits
.get_mut("north")
.unwrap()
.required_items
.insert(item_id.clone());
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.score, initial);
assert!(!world.rooms.get(&dest).unwrap().visited);
world.player.inventory.insert(item_id.clone());
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &dest));
assert_eq!(world.player.score, initial + 1);
assert!(world.rooms.get(&dest).unwrap().visited);
}
#[test]
fn move_requires_flag() {
let (mut world, start, dest, mut view) = build_test_world();
let flag = Flag::simple("alpha", world.turn_count);
world
.rooms
.get_mut(&start)
.unwrap()
.exits
.get_mut("north")
.unwrap()
.required_flags
.insert(flag.clone());
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.score, initial);
assert!(!world.rooms.get(&dest).unwrap().visited);
world.player.flags.insert(flag);
let initial = world.player.score;
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &dest));
assert_eq!(world.player.score, initial + 1);
assert!(world.rooms.get(&dest).unwrap().visited);
}
#[test]
fn go_back_with_no_history_fails() {
let (mut world, start, _dest, mut view) = build_test_world();
assert!(go_back_handler(&mut world, &mut view).is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
}
#[test]
fn go_back_with_history_works() {
let (mut world, start, dest, mut view) = build_test_world();
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &dest));
assert_eq!(world.player.location_history.len(), 1);
assert_eq!(world.player.location_history[0], start);
assert!(go_back_handler(&mut world, &mut view).is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.location_history.len(), 0);
}
#[test]
fn move_to_handler_errors_when_not_in_room() {
let (mut world, _start, _dest, mut view) = build_test_world();
world.player.location = Location::Inventory;
let result = move_to_handler(&mut world, &mut view, "north");
assert!(result.is_err());
assert_eq!(view.items.len(), 1);
assert!(matches!(view.items[0].view_item, ViewItem::ActionFailure(ref msg) if msg == "You're not in a room."));
}
#[test]
fn go_back_handler_errors_when_not_in_room() {
let (mut world, _start, _dest, mut view) = build_test_world();
world.player.location = Location::Inventory;
let result = go_back_handler(&mut world, &mut view);
assert!(result.is_err());
assert_eq!(view.items.len(), 1);
assert!(matches!(view.items[0].view_item, ViewItem::ActionFailure(ref msg) if msg == "You're not in a room."));
}
#[test]
fn location_history_maintains_max_size() {
let (mut world, start, dest, _view) = build_test_world();
let room3 = crate::idgen::new_room_id();
let room4 = crate::idgen::new_room_id();
let room5 = crate::idgen::new_room_id();
let room6 = crate::idgen::new_room_id();
let room7 = crate::idgen::new_room_id();
for room_id in [&room3, &room4, &room5, &room6, &room7] {
let room_id = room_id.clone();
let short_id = room_id.as_str().get(0..8).unwrap_or(room_id.as_str());
let room = Room {
id: room_id.clone(),
symbol: format!("room_{short_id}"),
name: format!("Room {short_id}"),
base_description: String::new(),
overlays: vec![],
scenery: Vec::new(),
scenery_default: None,
location: Location::Nowhere,
visited: false,
exits: HashMap::new(),
contents: HashSet::new(),
npcs: HashSet::new(),
};
world.rooms.insert(room_id, room);
}
world.player.move_to_room(dest.clone());
world.player.move_to_room(room3.clone());
world.player.move_to_room(room4.clone());
world.player.move_to_room(room5.clone());
world.player.move_to_room(room6.clone());
world.player.move_to_room(room7.clone());
assert_eq!(world.player.location_history.len(), 5);
assert!(!world.player.location_history.contains(&start)); assert!(world.player.location_history.contains(&dest));
}
#[test]
fn go_back_multiple_times() {
let (mut world, start, dest, mut view) = build_test_world();
let room3 = crate::idgen::new_room_id();
let room3_obj = Room {
id: room3.clone(),
symbol: "room3".into(),
name: "Room3".into(),
base_description: String::new(),
overlays: vec![],
scenery: Vec::new(),
scenery_default: None,
location: Location::Nowhere,
visited: false,
exits: HashMap::new(),
contents: HashSet::new(),
npcs: HashSet::new(),
};
world
.rooms
.get_mut(&dest)
.unwrap()
.exits
.insert("east".into(), Exit::new(room3.clone()));
world.rooms.insert(room3.clone(), room3_obj);
assert!(move_to_handler(&mut world, &mut view, "north").is_ok());
assert!(move_to_handler(&mut world, &mut view, "east").is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &room3));
assert_eq!(world.player.location_history, vec![start.clone(), dest.clone()]);
assert!(go_back_handler(&mut world, &mut view).is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &dest));
assert_eq!(world.player.location_history, vec![start.clone()]);
assert!(go_back_handler(&mut world, &mut view).is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
assert_eq!(world.player.location_history.len(), 0);
assert!(go_back_handler(&mut world, &mut view).is_ok());
assert!(matches!(&world.player.location, Location::Room(id) if id == &start));
}
}