use crate::{
AmbleWorld, View, ViewItem, WorldObject,
entity_search::{EntityId, SearchError, SearchScope, entity_not_found, find_entity_match, find_item_match},
item::ItemAbility,
room::{Room, RoomScenery},
style::GameStyle,
trigger::{TriggerAction, TriggerCondition, check_triggers},
view::{ContentLine, ViewMode},
};
use anyhow::{Context, Result, bail};
use log::info;
pub fn look_handler(world: &mut AmbleWorld, view: &mut View) -> Result<bool> {
let room = world.player_room_ref()?;
room.show(
world,
view,
if view.mode == ViewMode::Brief {
Some(ViewMode::Verbose)
} else {
None
},
)?;
info!(
"{} looked around {} ({})",
world.player.name(),
room.name(),
room.symbol()
);
let _fired = check_triggers(world, view, &[]);
Ok(true)
}
pub fn look_at_handler(world: &mut AmbleWorld, view: &mut View, thing: &str) -> Result<bool> {
let room_id = world.player_room_id();
let entity_id = match find_entity_match(world, thing, SearchScope::AllVisible(room_id)) {
Ok(id) => id,
Err(SearchError::NoMatchingName(input)) => {
let room = world.player_room_ref()?;
if let Some(entry) = room.find_scenery(&input) {
let desc = describe_scenery(room, world, entry);
view.push(ViewItem::ItemDescription {
name: entry.name.clone(),
description: desc,
});
info!("player looked at scenery item \"{}\"", entry.name);
return Ok(true);
}
entity_not_found(world, view, input.as_str());
return Ok(false);
},
Err(e) => bail!(e),
};
match entity_id {
EntityId::Item(item_id) => {
let item = world.items.get(&item_id).expect("known OK from find_entity_match");
info!("{} looked at {} ({})", world.player.name(), item.name(), item.symbol());
item.show(world, view);
let _fired = check_triggers(world, view, &[TriggerCondition::LookAt(item.id())]);
},
EntityId::Npc(npc_id) => {
let npc = world.npcs.get(&npc_id).expect("known OK from find_entity_match");
info!("{} looked at {} ({})", world.player.name(), npc.name(), npc.symbol());
npc.show(world, view);
let _fired = check_triggers(world, view, &[]);
},
}
Ok(true)
}
fn markup_scenery_item(item_name: &str) -> String {
format!("[[item]][[i]]{}[[/i]][[/item]]", item_name.to_lowercase())
}
fn create_default_scenery_description(world: &AmbleWorld, entry: &RoomScenery) -> String {
let default_desc = world.spin_core(
crate::spinners::CoreSpinnerType::NothingSpecial,
"Just a plain {thing}, you see nothing curious about it.",
);
let thing_substitute = markup_scenery_item(&entry.name);
default_desc.replace("{thing}", &thing_substitute)
}
fn describe_scenery(room: &Room, world: &AmbleWorld, entry: &RoomScenery) -> String {
entry
.desc
.clone()
.or_else(|| {
room.scenery_default
.clone()
.map(|text| text.replace("{thing}", &markup_scenery_item(&entry.name)))
})
.unwrap_or_else(|| create_default_scenery_description(world, entry))
}
pub fn inv_handler(world: &AmbleWorld, view: &mut View) -> Result<()> {
info!("{} checked inventory.", world.player.name());
view.push(ViewItem::Inventory(
world
.player
.inventory
.iter()
.filter_map(|item_id| world.items.get(item_id))
.map(|item| ContentLine {
item_name: item.name.clone(),
restricted: false,
})
.collect(),
));
Ok(())
}
pub fn read_handler(world: &mut AmbleWorld, view: &mut View, pattern: &str) -> Result<bool> {
let room_id = world.player_room_id();
let item_id = match find_item_match(world, pattern, SearchScope::VisibleItems(room_id)) {
Ok(uuid) => uuid,
Err(SearchError::NoMatchingName(input)) => {
let room = world.player_room_ref()?;
if let Some(entry) = room.find_scenery(&input) {
let desc = describe_scenery(room, world, entry);
view.push(ViewItem::ItemText(desc));
info!("{} read scenery \"{}\"", world.player.name(), entry.name);
return Ok(true);
}
entity_not_found(world, view, input.as_str());
return Ok(false);
},
Err(e) => bail!(e),
};
let item = world.items.get(&item_id).expect("item_id known to be valid here");
if item.text.is_none() || !item.abilities.contains(&ItemAbility::Read) {
view.push(ViewItem::ActionFailure(format!(
"You see nothing special about the {}, and nothing legible on it.",
item.name().item_style()
)));
info!(
"{} examined a detail-less item {} ({})",
world.player.name(),
item.name(),
item.symbol()
);
return Ok(false);
}
let fired = check_triggers(
world,
view,
&[TriggerCondition::UseItem {
item_id: item.id.clone(),
ability: ItemAbility::Read,
}],
)?;
let denied = fired.iter().any(|trigger| {
trigger
.actions
.iter()
.any(|action| matches!(&action.action, TriggerAction::DenyRead(_)))
});
if !denied {
let item = world
.items
.get(&item_id)
.with_context(|| format!("item_id ({item_id}) not found in world items"))?;
view.push(ViewItem::ItemText(
item.text.clone().unwrap_or_else(|| "(Nothing legible.)".to_string()),
));
info!("{} read '{}' ({})", world.player.name(), item.name(), item.symbol());
}
Ok(true)
}