in-system name obfuscation (picking up, dropping items, etc.)

This commit is contained in:
Llywelwyn 2023-08-14 20:21:53 +01:00
parent 1319ed16e0
commit 80c34a72b5
5 changed files with 97 additions and 25 deletions

View file

@ -1,5 +1,4 @@
use crate::{gamelog, Attributes, Burden, EquipmentChanged, Equipped, InBackpack, Item, Pools}; use crate::{gamelog, Attributes, Burden, EquipmentChanged, Equipped, InBackpack, Item, Pools};
use rltk::prelude::*;
use specs::prelude::*; use specs::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;

View file

@ -1,7 +1,7 @@
use super::{ use super::{
ai::CARRY_CAPACITY_PER_STRENGTH, camera, gamelog, gamesystem, rex_assets::RexAssets, ArmourClassBonus, Attributes, ai::CARRY_CAPACITY_PER_STRENGTH, camera, gamelog, gamesystem, rex_assets::RexAssets, ArmourClassBonus, Attributes,
Burden, Equipped, Hidden, HungerClock, HungerState, InBackpack, MagicItem, Map, Name, ObfuscatedName, Player, Burden, Equipped, Hidden, HungerClock, HungerState, InBackpack, MagicItem, Map, MasterDungeonMap, Name,
Point, Pools, Position, Prop, Renderable, RunState, Skill, Skills, State, Viewshed, ObfuscatedName, Player, Point, Pools, Position, Prop, Renderable, RunState, Skill, Skills, State, Viewshed, Wand,
}; };
use rltk::{Rltk, VirtualKeyCode, RGB}; use rltk::{Rltk, VirtualKeyCode, RGB};
use specs::prelude::*; use specs::prelude::*;
@ -365,6 +365,43 @@ pub fn get_max_inventory_width(inventory: &BTreeMap<UniqueInventoryItem, i32>) -
return width; return width;
} }
// Inside the ECS
pub fn obfuscate_name(
item: Entity,
names: &ReadStorage<Name>,
magic_items: &ReadStorage<MagicItem>,
obfuscated_names: &ReadStorage<ObfuscatedName>,
dm: &MasterDungeonMap,
wand: Option<&ReadStorage<Wand>>,
) -> (String, String) {
let (mut singular, mut plural) = ("nameless item (bug)".to_string(), "nameless items (bug)".to_string());
if let Some(name) = names.get(item) {
if magic_items.get(item).is_some() {
if dm.identified_items.contains(&name.name) {
(singular, plural) = (name.name.clone(), name.plural.clone());
} else if let Some(obfuscated) = obfuscated_names.get(item) {
(singular, plural) = (obfuscated.name.clone(), obfuscated.plural.clone());
} else {
(singular, plural) = ("unid magic item".to_string(), "unid magic items".to_string());
}
} else {
(singular, plural) = (name.name.clone(), name.plural.clone());
}
}
if wand.is_some() {
let wands = wand.unwrap();
if let Some(wand) = wands.get(item) {
let used = wand.max_uses - wand.uses;
for _i in 0..used {
singular.push_str("*");
plural.push_str("*");
}
}
}
return (singular, plural);
}
// Outside the ECS
pub fn get_item_display_name(ecs: &World, item: Entity) -> (String, String) { pub fn get_item_display_name(ecs: &World, item: Entity) -> (String, String) {
let (mut singular, mut plural) = ("nameless item (bug)".to_string(), "nameless items (bug)".to_string()); let (mut singular, mut plural) = ("nameless item (bug)".to_string(), "nameless items (bug)".to_string());
if let Some(name) = ecs.read_storage::<Name>().get(item) { if let Some(name) = ecs.read_storage::<Name>().get(item) {
@ -381,7 +418,7 @@ pub fn get_item_display_name(ecs: &World, item: Entity) -> (String, String) {
(singular, plural) = (name.name.clone(), name.plural.clone()); (singular, plural) = (name.name.clone(), name.plural.clone());
} }
} }
if let Some(wand) = ecs.read_storage::<crate::Wand>().get(item) { if let Some(wand) = ecs.read_storage::<Wand>().get(item) {
let used = wand.max_uses - wand.uses; let used = wand.max_uses - wand.uses;
for _i in 0..used { for _i in 0..used {
singular.push_str("*"); singular.push_str("*");

View file

@ -1,4 +1,4 @@
use super::{camera::get_screen_bounds, Attributes, Hidden, Map, Name, Pools, Position, Renderable, Rltk, World, RGB}; use super::{camera::get_screen_bounds, Attributes, Hidden, Map, Pools, Position, Renderable, Rltk, World, RGB};
use rltk::prelude::*; use rltk::prelude::*;
use specs::prelude::*; use specs::prelude::*;
@ -43,7 +43,6 @@ impl Tooltip {
pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
let (min_x, _max_x, min_y, _max_y, x_offset, y_offset) = get_screen_bounds(ecs, ctx); let (min_x, _max_x, min_y, _max_y, x_offset, y_offset) = get_screen_bounds(ecs, ctx);
let map = ecs.fetch::<Map>(); let map = ecs.fetch::<Map>();
let names = ecs.read_storage::<Name>();
let positions = ecs.read_storage::<Position>(); let positions = ecs.read_storage::<Position>();
let renderables = ecs.read_storage::<Renderable>(); let renderables = ecs.read_storage::<Renderable>();
let hidden = ecs.read_storage::<Hidden>(); let hidden = ecs.read_storage::<Hidden>();
@ -70,10 +69,10 @@ pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
} }
let mut tooltips: Vec<Tooltip> = Vec::new(); let mut tooltips: Vec<Tooltip> = Vec::new();
for (entity, name, position, renderable, _hidden) in (&entities, &names, &positions, &renderables, !&hidden).join() { for (entity, position, renderable, _hidden) in (&entities, &positions, &renderables, !&hidden).join() {
if position.x == mouse_pos_adjusted.0 && position.y == mouse_pos_adjusted.1 { if position.x == mouse_pos_adjusted.0 && position.y == mouse_pos_adjusted.1 {
let mut tip = Tooltip::new(); let mut tip = Tooltip::new();
tip.add(name.name.to_string(), renderable.fg); tip.add(crate::gui::get_item_display_name(ecs, entity).0, renderable.fg);
// Attributes // Attributes
let attr = attributes.get(entity); let attr = attributes.get(entity);
if let Some(a) = attr { if let Some(a) = attr {

View file

@ -1,9 +1,9 @@
use super::{ use super::{
gamelog, Confusion, Consumable, Cursed, Destructible, Digger, EquipmentChanged, Equippable, Equipped, HungerClock, gamelog, gui::obfuscate_name, Confusion, Consumable, Cursed, Destructible, Digger, EquipmentChanged, Equippable,
HungerState, IdentifiedItem, InBackpack, InflictsDamage, MagicMapper, Map, Name, ParticleBuilder, Point, Pools, Equipped, HungerClock, HungerState, IdentifiedItem, InBackpack, InflictsDamage, MagicItem, MagicMapper, Map,
Position, ProvidesHealing, ProvidesNutrition, RandomNumberGenerator, RunState, SufferDamage, TileType, Viewshed, MasterDungeonMap, Name, ObfuscatedName, ParticleBuilder, Point, Pools, Position, ProvidesHealing,
Wand, WantsToDropItem, WantsToPickupItem, WantsToRemoveItem, WantsToUseItem, AOE, DEFAULT_PARTICLE_LIFETIME, ProvidesNutrition, RandomNumberGenerator, RunState, SufferDamage, TileType, Viewshed, Wand, WantsToDropItem,
LONG_PARTICLE_LIFETIME, WantsToPickupItem, WantsToRemoveItem, WantsToUseItem, AOE, DEFAULT_PARTICLE_LIFETIME, LONG_PARTICLE_LIFETIME,
}; };
use specs::prelude::*; use specs::prelude::*;
@ -18,10 +18,25 @@ impl<'a> System<'a> for ItemCollectionSystem {
ReadStorage<'a, Name>, ReadStorage<'a, Name>,
WriteStorage<'a, InBackpack>, WriteStorage<'a, InBackpack>,
WriteStorage<'a, EquipmentChanged>, WriteStorage<'a, EquipmentChanged>,
ReadStorage<'a, MagicItem>,
ReadStorage<'a, ObfuscatedName>,
ReadExpect<'a, MasterDungeonMap>,
ReadStorage<'a, Wand>,
); );
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
let (player_entity, mut wants_pickup, mut positions, names, mut backpack, mut equipment_changed) = data; let (
player_entity,
mut wants_pickup,
mut positions,
names,
mut backpack,
mut equipment_changed,
magic_items,
obfuscated_names,
dm,
wands,
) = data;
for pickup in wants_pickup.join() { for pickup in wants_pickup.join() {
positions.remove(pickup.item); positions.remove(pickup.item);
@ -33,7 +48,10 @@ impl<'a> System<'a> for ItemCollectionSystem {
if pickup.collected_by == *player_entity { if pickup.collected_by == *player_entity {
gamelog::Logger::new() gamelog::Logger::new()
.append("You pick up the") .append("You pick up the")
.item_name_n(format!("{}", &names.get(pickup.item).unwrap().name)) .item_name_n(format!(
"{}",
obfuscate_name(pickup.item, &names, &magic_items, &obfuscated_names, &dm, Some(&wands)).0
))
.period() .period()
.log(); .log();
} }
@ -48,7 +66,7 @@ impl<'a> System<'a> for ItemCollectionSystem {
// systems. // systems.
type EquipComponents<'a> = type EquipComponents<'a> =
(ReadStorage<'a, Equippable>, WriteStorage<'a, Equipped>, WriteStorage<'a, EquipmentChanged>); (ReadStorage<'a, Equippable>, WriteStorage<'a, Equipped>, WriteStorage<'a, EquipmentChanged>);
type NameComponents<'a> = (WriteStorage<'a, Name>, WriteStorage<'a, IdentifiedItem>); type NameComponents<'a> = (ReadStorage<'a, Name>, WriteStorage<'a, IdentifiedItem>);
pub struct ItemUseSystem {} pub struct ItemUseSystem {}
impl<'a> System<'a> for ItemUseSystem { impl<'a> System<'a> for ItemUseSystem {
@ -89,7 +107,7 @@ impl<'a> System<'a> for ItemUseSystem {
mut rng, mut rng,
entities, entities,
mut wants_to_use, mut wants_to_use,
(mut names, mut identified_items), (names, mut identified_items),
mut consumables, mut consumables,
mut wands, mut wands,
destructibles, destructibles,
@ -461,10 +479,26 @@ impl<'a> System<'a> for ItemDropSystem {
WriteStorage<'a, Position>, WriteStorage<'a, Position>,
WriteStorage<'a, InBackpack>, WriteStorage<'a, InBackpack>,
WriteStorage<'a, EquipmentChanged>, WriteStorage<'a, EquipmentChanged>,
ReadStorage<'a, MagicItem>,
ReadStorage<'a, ObfuscatedName>,
ReadExpect<'a, MasterDungeonMap>,
ReadStorage<'a, Wand>,
); );
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
let (player_entity, entities, mut wants_drop, names, mut positions, mut backpack, mut equipment_changed) = data; let (
player_entity,
entities,
mut wants_drop,
names,
mut positions,
mut backpack,
mut equipment_changed,
magic_items,
obfuscated_names,
dm,
wands,
) = data;
for (entity, to_drop) in (&entities, &wants_drop).join() { for (entity, to_drop) in (&entities, &wants_drop).join() {
equipment_changed.insert(entity, EquipmentChanged {}).expect("Unable to insert EquipmentChanged."); equipment_changed.insert(entity, EquipmentChanged {}).expect("Unable to insert EquipmentChanged.");
@ -482,7 +516,10 @@ impl<'a> System<'a> for ItemDropSystem {
if entity == *player_entity { if entity == *player_entity {
gamelog::Logger::new() gamelog::Logger::new()
.append("You drop the") .append("You drop the")
.item_name_n(format!("{}", &names.get(to_drop.item).unwrap().name)) .item_name_n(format!(
"{}",
obfuscate_name(to_drop.item, &names, &magic_items, &obfuscated_names, &dm, Some(&wands)).0
))
.period() .period()
.log(); .log();
} }

View file

@ -1,7 +1,7 @@
use super::{ use super::{
gamelog, Attributes, BlocksTile, BlocksVisibility, Bystander, Door, EntityMoved, Hidden, HungerClock, HungerState, gamelog, gui::get_item_display_name, Attributes, BlocksTile, BlocksVisibility, Bystander, Door, EntityMoved,
Item, Map, Monster, Name, ParticleBuilder, Player, Pools, Position, Renderable, RunState, State, SufferDamage, Hidden, HungerClock, HungerState, Item, Map, Monster, Name, ParticleBuilder, Player, Pools, Position, Renderable,
Telepath, TileType, Viewshed, WantsToMelee, WantsToPickupItem, RunState, State, SufferDamage, Telepath, TileType, Viewshed, WantsToMelee, WantsToPickupItem,
}; };
use rltk::{Point, RandomNumberGenerator, Rltk, VirtualKeyCode}; use rltk::{Point, RandomNumberGenerator, Rltk, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
@ -351,9 +351,9 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> bool {
for entity in map.tile_content[destination_idx].iter() { for entity in map.tile_content[destination_idx].iter() {
if let Some(_hidden) = hidden.get(*entity) { if let Some(_hidden) = hidden.get(*entity) {
} else { } else {
if let Some(name) = names.get(*entity) { if let Some(_) = names.get(*entity) {
let item_name = &name.name; let item_name = get_item_display_name(ecs, *entity).0;
item_names.push(item_name.to_string()); item_names.push(item_name);
some = true; some = true;
} }
} }