use std::collections::{HashMap, HashSet};
use std::hash::BuildHasher;
pub use crate::EntityId;
use crate::world::item_is_visible_item;
use crate::{Item, ItemId, Npc, NpcId, RoomId, View, ViewItem, WorldObject};
use colored::Colorize;
use thiserror::Error;
use variantly::Variantly;
use crate::{
AmbleWorld,
spinners::CoreSpinnerType,
style::GameStyle,
world::{nearby_reachable_items, nearby_vessel_items, nearby_visible_items},
};
#[derive(Debug, Clone, PartialEq)]
pub enum SearchScope {
AllVisible(RoomId),
AllTouchable(RoomId),
VisibleItems(RoomId),
VisibleNpcs(RoomId),
TouchableItems(RoomId),
TouchableNpcs(RoomId),
NearbyVessels(RoomId),
Inventory,
NpcInventory(NpcId),
ItemContents(ItemId),
}
#[derive(Debug, Error)]
pub enum SearchError {
#[error("no entity in scope name matching user input '{0}'")]
NoMatchingName(String),
#[error("no item found with the supplied id {0}")]
InvalidItemId(ItemId),
#[error("no npc found with the supplied id {0}")]
InvalidNpcId(NpcId),
#[error("found no room with the supplied id ({0})")]
InvalidRoomId(RoomId),
#[error("invalid {0} search scope: includes only {1}s")]
InvalidScope(String, String),
#[error("unknown error: {0}")]
Unknown(#[from] anyhow::Error),
}
#[derive(Debug, Variantly, Clone, Copy)]
pub enum WorldEntity<'a> {
Item(&'a Item),
Npc(&'a Npc),
}
impl WorldEntity<'_> {
pub fn name(&self) -> &str {
match self {
WorldEntity::Item(item) => item.name(),
WorldEntity::Npc(npc) => npc.name(),
}
}
pub fn entity_id(&self) -> EntityId {
match self {
WorldEntity::Item(item) => EntityId::Item(item.id.clone()),
WorldEntity::Npc(npc) => EntityId::Npc(npc.id.clone()),
}
}
pub fn symbol(&self) -> &str {
match self {
WorldEntity::Item(item) => item.symbol(),
WorldEntity::Npc(npc) => npc.symbol(),
}
}
}
pub fn find_world_entity<'a, S: BuildHasher>(
nearby_item_ids: impl IntoIterator<Item = &'a ItemId>,
nearby_npc_ids: impl IntoIterator<Item = &'a NpcId>,
world_items: &'a HashMap<ItemId, Item, S>,
world_npcs: &'a HashMap<NpcId, Npc, S>,
search_term: &str,
) -> Option<WorldEntity<'a>> {
let lc_term = search_term.to_lowercase();
for item_id in nearby_item_ids {
if let Some(found_item) = world_items.get(item_id)
&& (found_item.name().to_lowercase().contains(&lc_term)
|| found_item
.aliases
.iter()
.any(|alias| alias.to_lowercase().contains(&lc_term)))
{
return Some(WorldEntity::Item(found_item));
}
}
for npc_id in nearby_npc_ids {
if let Some(found_npc) = world_npcs.get(npc_id)
&& found_npc.name().to_lowercase().contains(&lc_term)
{
return Some(WorldEntity::Npc(found_npc));
}
}
None
}
pub fn entity_not_found(world: &AmbleWorld, view: &mut View, search_text: &str) {
view.push(ViewItem::Error(format!(
"\"{}\"? {}\n{}",
search_text.error_style(),
world.spin_core(CoreSpinnerType::EntityNotFound, "What's that?"),
"(word not understood in context)".to_string().italic().dimmed()
)));
}
pub fn find_item_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<ItemId, SearchError> {
let haystack: HashSet<_> = match scope {
SearchScope::VisibleItems(room_id) | SearchScope::AllVisible(room_id) => {
let room_items = nearby_visible_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id))?;
room_items.union(&world.player.inventory).cloned().collect()
},
SearchScope::TouchableItems(room_id) | SearchScope::AllTouchable(room_id) => {
let room_items =
nearby_reachable_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id))?;
room_items.union(&world.player.inventory).cloned().collect()
},
SearchScope::NearbyVessels(room_id) => {
nearby_vessel_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id.clone()))?
},
SearchScope::Inventory => world.player.inventory.clone(),
SearchScope::NpcInventory(npc_id) => {
let npc = world.npcs.get(&npc_id).ok_or(SearchError::InvalidNpcId(npc_id))?;
filter_visible_items(&npc.inventory, world)?
},
SearchScope::ItemContents(item_id) => {
let item = world.items.get(&item_id).ok_or(SearchError::InvalidItemId(item_id))?;
filter_visible_items(&item.contents, world)?
},
SearchScope::VisibleNpcs(_) | SearchScope::TouchableNpcs(_) => {
return Err(SearchError::InvalidScope("item".to_string(), "NPC".to_string()));
},
};
let Some(entity) = find_world_entity(
haystack.iter(),
std::iter::empty::<&NpcId>(),
&world.items,
&world.npcs,
pattern,
) else {
return Err(SearchError::NoMatchingName(pattern.to_string()));
};
match entity.entity_id() {
EntityId::Item(item_id) => Ok(item_id),
EntityId::Npc(_) => Err(SearchError::InvalidScope("item".into(), "NPC".into())),
}
}
pub(crate) fn filter_visible_items(
item_set: &HashSet<ItemId>,
world: &AmbleWorld,
) -> Result<HashSet<ItemId>, SearchError> {
let mut visible_items = HashSet::new();
for item_id in item_set {
let item = world
.items
.get(item_id)
.ok_or(SearchError::InvalidItemId(item_id.clone()))?;
if item_is_visible_item(item, world) {
visible_items.insert(item_id.clone());
}
}
Ok(visible_items)
}
pub fn find_entity_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<EntityId, SearchError> {
match find_item_match(world, pattern, scope.clone()) {
Ok(item_id) => return Ok(EntityId::Item(item_id)),
Err(SearchError::NoMatchingName(_) | SearchError::InvalidScope(_, _)) => (),
Err(e) => return Err(e),
}
match find_npc_match(world, pattern, scope) {
Ok(npc_id) => Ok(EntityId::Npc(npc_id)),
Err(e) => Err(e),
}
}
pub fn find_npc_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<NpcId, SearchError> {
let haystack = match scope {
SearchScope::VisibleNpcs(room_id)
| SearchScope::TouchableNpcs(room_id)
| SearchScope::NearbyVessels(room_id)
| SearchScope::AllVisible(room_id)
| SearchScope::AllTouchable(room_id) => {
let room = world.rooms.get(&room_id).ok_or(SearchError::InvalidRoomId(room_id))?;
room.npcs.clone()
},
SearchScope::VisibleItems(_)
| SearchScope::TouchableItems(_)
| SearchScope::Inventory
| SearchScope::NpcInventory(_)
| SearchScope::ItemContents(_) => {
return Err(SearchError::InvalidScope("npc".into(), "item".into()));
},
};
let Some(entity) = find_world_entity(
std::iter::empty::<&ItemId>(),
haystack.iter(),
&world.items,
&world.npcs,
pattern,
) else {
return Err(SearchError::NoMatchingName(pattern.to_string()));
};
match entity.entity_id() {
EntityId::Npc(npc_id) => Ok(npc_id),
EntityId::Item(_) => Err(SearchError::InvalidScope("npc".into(), "item".into())),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
AmbleWorld, Item, Npc, Room,
health::HealthState,
item::{ContainerState, Movability},
npc::NpcState,
scheduler::EventCondition,
trigger::TriggerCondition,
world::Location,
};
use std::collections::{HashMap, HashSet};
fn insert_room(world: &mut AmbleWorld, name: &str) -> RoomId {
let room_id = crate::idgen::new_room_id();
let room = Room {
id: room_id.clone(),
symbol: format!("room_{room_id}"),
name: name.to_string(),
base_description: format!("{name} description"),
overlays: Vec::new(),
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.clone(), room);
room_id
}
fn insert_item(
world: &mut AmbleWorld,
name: &str,
location: Location,
container_state: Option<ContainerState>,
) -> ItemId {
let item_id: ItemId = crate::idgen::new_id().into();
let item = Item {
id: item_id.clone(),
symbol: format!("item_{item_id}"),
name: name.to_string(),
description: format!("{name} item"),
location,
visibility: crate::item::ItemVisibility::Listed,
visible_when: None,
aliases: Vec::new(),
movability: Movability::Free,
container_state,
contents: HashSet::new(),
abilities: HashSet::new(),
interaction_requires: HashMap::new(),
text: None,
consumable: None,
};
world.items.insert(item_id.clone(), item);
item_id
}
fn insert_npc(world: &mut AmbleWorld, name: &str, location: Location) -> NpcId {
let npc_id: NpcId = crate::idgen::new_id().into();
let npc = Npc {
id: npc_id.clone(),
symbol: format!("npc_{npc_id}"),
name: name.to_string(),
description: format!("{name} npc"),
location,
inventory: HashSet::new(),
dialogue: HashMap::new(),
state: NpcState::Normal,
movement: None,
health: HealthState::new(),
};
world.npcs.insert(npc_id.clone(), npc);
npc_id
}
#[test]
fn find_item_match_includes_inventory_items_in_visible_scope() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Atrium");
let coin_id = insert_item(&mut world, "Lucky Coin", Location::Inventory, None);
world.player.inventory.insert(coin_id.clone());
let result = find_item_match(&world, "coin", SearchScope::VisibleItems(room_id)).unwrap();
assert_eq!(result, coin_id);
}
#[test]
fn find_item_match_returns_nearby_vessels() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Vault");
let chest_id = insert_item(
&mut world,
"Ancient Chest",
Location::Room(room_id.clone()),
Some(ContainerState::Open),
);
world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());
let result = find_item_match(&world, "chest", SearchScope::NearbyVessels(room_id)).unwrap();
assert_eq!(result, chest_id);
}
#[test]
fn find_item_match_returns_items_from_selected_container_only() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Vault");
let chest_id = insert_item(
&mut world,
"Ancient Chest",
Location::Room(room_id.clone()),
Some(ContainerState::Open),
);
let chest_gem_id = insert_item(&mut world, "Gem", Location::Item(chest_id.clone()), None);
let floor_gem_id = insert_item(&mut world, "Gem", Location::Room(room_id.clone()), None);
world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());
world.rooms.get_mut(&room_id).unwrap().contents.insert(floor_gem_id);
world
.items
.get_mut(&chest_id)
.unwrap()
.contents
.insert(chest_gem_id.clone());
let result = find_item_match(&world, "gem", SearchScope::ItemContents(chest_id)).unwrap();
assert_eq!(result, chest_gem_id);
}
#[test]
fn find_item_match_filters_hidden_items_from_npc_inventory() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Garden");
let npc_id = insert_npc(&mut world, "Caretaker", Location::Room(room_id.clone()));
world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());
let hidden_gem_id = insert_item(&mut world, "Gem", Location::Npc(npc_id.clone()), None);
world.items.get_mut(&hidden_gem_id).unwrap().visible_when = Some(EventCondition::Trigger(
TriggerCondition::HasFlag("npc_inventory_visible".to_string()),
));
let visible_coin_id = insert_item(&mut world, "Coin", Location::Npc(npc_id.clone()), None);
world.npcs.get_mut(&npc_id).unwrap().inventory.insert(hidden_gem_id);
world
.npcs
.get_mut(&npc_id)
.unwrap()
.inventory
.insert(visible_coin_id.clone());
let result = find_item_match(&world, "coin", SearchScope::NpcInventory(npc_id.clone())).unwrap();
assert_eq!(result, visible_coin_id);
let err = find_item_match(&world, "gem", SearchScope::NpcInventory(npc_id)).unwrap_err();
assert!(matches!(err, SearchError::NoMatchingName(name) if name == "gem"));
}
#[test]
fn find_item_match_filters_hidden_items_from_container_contents() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Vault");
let chest_id = insert_item(
&mut world,
"Ancient Chest",
Location::Room(room_id.clone()),
Some(ContainerState::Open),
);
let hidden_gem_id = insert_item(&mut world, "Gem", Location::Item(chest_id.clone()), None);
world.items.get_mut(&hidden_gem_id).unwrap().visible_when = Some(EventCondition::Trigger(
TriggerCondition::HasFlag("container_contents_visible".to_string()),
));
let visible_coin_id = insert_item(&mut world, "Coin", Location::Item(chest_id.clone()), None);
world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());
world.items.get_mut(&chest_id).unwrap().contents.insert(hidden_gem_id);
world
.items
.get_mut(&chest_id)
.unwrap()
.contents
.insert(visible_coin_id.clone());
let result = find_item_match(&world, "coin", SearchScope::ItemContents(chest_id.clone())).unwrap();
assert_eq!(result, visible_coin_id);
let err = find_item_match(&world, "gem", SearchScope::ItemContents(chest_id)).unwrap_err();
assert!(matches!(err, SearchError::NoMatchingName(name) if name == "gem"));
}
#[test]
fn find_item_match_errors_when_container_missing() {
let world = AmbleWorld::new_empty();
let item_id: ItemId = crate::idgen::new_id().into();
let err = find_item_match(&world, "gem", SearchScope::ItemContents(item_id.clone())).unwrap_err();
match err {
SearchError::InvalidItemId(id) => assert_eq!(id, item_id),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn find_item_match_errors_when_room_missing() {
let world = AmbleWorld::new_empty();
let room_id = crate::idgen::new_room_id();
let err = find_item_match(&world, "coin", SearchScope::VisibleItems(room_id.clone())).unwrap_err();
match err {
SearchError::InvalidRoomId(id) => assert_eq!(id, room_id),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn find_item_match_rejects_npc_scopes() {
let world = AmbleWorld::new_empty();
let scope = SearchScope::VisibleNpcs(crate::idgen::new_room_id());
let err = find_item_match(&world, "coin", scope).unwrap_err();
match err {
SearchError::InvalidScope(kind, only) => {
assert_eq!(kind, "item");
assert_eq!(only, "NPC");
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn find_npc_match_returns_visible_npc() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Garden");
let npc_id = insert_npc(&mut world, "Caretaker", Location::Room(room_id.clone()));
world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());
let result = find_npc_match(&world, "take", SearchScope::VisibleNpcs(room_id)).unwrap();
assert_eq!(result, npc_id);
}
#[test]
fn find_npc_match_errors_when_room_missing() {
let world = AmbleWorld::new_empty();
let room_id = crate::idgen::new_room_id();
let err = find_npc_match(&world, "caretaker", SearchScope::VisibleNpcs(room_id.clone())).unwrap_err();
match err {
SearchError::InvalidRoomId(id) => assert_eq!(id, room_id),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn find_npc_match_rejects_item_scopes() {
let world = AmbleWorld::new_empty();
let err = find_npc_match(&world, "caretaker", SearchScope::Inventory).unwrap_err();
match err {
SearchError::InvalidScope(kind, only) => {
assert_eq!(kind, "npc");
assert_eq!(only, "item");
},
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn find_entity_match_prefers_items() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Vault");
let vessel_id = insert_item(
&mut world,
"Guardian Chest",
Location::Room(room_id.clone()),
Some(ContainerState::Open),
);
world
.rooms
.get_mut(&room_id)
.unwrap()
.contents
.insert(vessel_id.clone());
let npc_id = insert_npc(&mut world, "Guardian", Location::Room(room_id.clone()));
world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());
let result = find_entity_match(&world, "guardian", SearchScope::NearbyVessels(room_id)).unwrap();
assert_eq!(result, EntityId::Item(vessel_id));
}
#[test]
fn find_entity_match_returns_npc_when_no_item_found() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Library");
let npc_id = insert_npc(&mut world, "Archivist", Location::Room(room_id.clone()));
world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());
let result = find_entity_match(&world, "archivist", SearchScope::VisibleNpcs(room_id)).unwrap();
assert_eq!(result, EntityId::Npc(npc_id));
}
#[test]
fn find_entity_match_returns_no_matching_name_when_none_found() {
let mut world = AmbleWorld::new_empty();
let room_id = insert_room(&mut world, "Workshop");
let vessel_id = insert_item(
&mut world,
"Toolbox",
Location::Room(room_id.clone()),
Some(ContainerState::Open),
);
world
.rooms
.get_mut(&room_id)
.unwrap()
.contents
.insert(vessel_id.clone());
let npc_id = insert_npc(&mut world, "Mechanic", Location::Room(room_id.clone()));
world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());
let err = find_entity_match(&world, "lantern", SearchScope::NearbyVessels(room_id)).unwrap_err();
match err {
SearchError::NoMatchingName(term) => assert_eq!(term, "lantern"),
other => panic!("unexpected error: {other:?}"),
}
}
}