From 444b6927797a4e0a79ad96189dabbfb19fe3772d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 19 Jan 2022 14:35:13 -0500 Subject: [PATCH] Obfuscate names of scrolls and potions in other scenarios --- src/gui/tooltip.rs | 8 ++++---- src/inventory_system.rs | 40 +++++++++++++++++++++++++++++++++++++-- src/map/dungeon.rs | 42 ++++++++++++++++++++++++++++++++++++++++- src/raws/rawmaster.rs | 27 +++++++++++++++++++++++++- 4 files changed, 109 insertions(+), 8 deletions(-) diff --git a/src/gui/tooltip.rs b/src/gui/tooltip.rs index 4eb0b01..45cf9cc 100644 --- a/src/gui/tooltip.rs +++ b/src/gui/tooltip.rs @@ -1,7 +1,7 @@ use ::rltk::Rltk; use ::specs::prelude::*; -use crate::components::{Attributes, Hidden, Name, Pools, Position}; +use crate::components::{Attributes, Hidden, Pools, Position}; use crate::{camera, colors, Map}; struct Tooltip { @@ -58,12 +58,12 @@ pub(super) fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { let (min_x, _max_x, min_y, _max_y) = camera::get_screen_bounds(ecs, ctx); let map = ecs.fetch::(); - let names = ecs.read_storage::(); let positions = ecs.read_storage::(); let hidden = ecs.read_storage::(); let attributes = ecs.read_storage::(); let pools = ecs.read_storage::(); let entities = ecs.entities(); + let mouse_pos = ctx.mouse_pos(); let mut mouse_map_pos = mouse_pos; mouse_map_pos.0 += min_x; @@ -82,10 +82,10 @@ pub(super) fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { } let mut tip_boxes: Vec = Vec::new(); - for (entity, name, position, _hidden) in (&entities, &names, &positions, !&hidden).join() { + for (entity, position, _hidden) in (&entities, &positions, !&hidden).join() { if position.x == mouse_map_pos.0 && position.y == mouse_map_pos.1 { let mut tip = Tooltip::new(); - tip.add(name.name.to_string()); + tip.add(super::get_item_display_name(ecs, entity)); // Comment on attributes if let Some(attr) = attributes.get(entity) { diff --git a/src/inventory_system.rs b/src/inventory_system.rs index 3f10071..eb46b13 100644 --- a/src/inventory_system.rs +++ b/src/inventory_system.rs @@ -5,6 +5,30 @@ use crate::game_log::GameLog; use crate::particle_system::ParticleBuilder; use crate::{colors, raws, spatial, Map, MasterDungeonMap, RunState}; +fn obfuscate_name( + item: Entity, + names: &ReadStorage, + magic_items: &ReadStorage, + obfuscated_names: &ReadStorage, + dm: &MasterDungeonMap, +) -> String { + if let Some(name) = names.get(item) { + if magic_items.get(item).is_some() { + if dm.identified_items.contains(&name.name) { + name.name.clone() + } else if let Some(obfuscated) = obfuscated_names.get(item) { + obfuscated.name.clone() + } else { + "Unidentified magic item".to_string() + } + } else { + name.name.clone() + } + } else { + "Nameless item (bug)".to_string() + } +} + pub struct ItemCollectionSystem {} impl<'a> System<'a> for ItemCollectionSystem { @@ -17,6 +41,9 @@ impl<'a> System<'a> for ItemCollectionSystem { ReadStorage<'a, Name>, WriteStorage<'a, InBackpack>, WriteStorage<'a, EquipmentChanged>, + ReadStorage<'a, MagicItem>, + ReadStorage<'a, ObfuscatedName>, + ReadExpect<'a, MasterDungeonMap>, ); fn run(&mut self, data: Self::SystemData) { @@ -28,6 +55,9 @@ impl<'a> System<'a> for ItemCollectionSystem { names, mut backpack, mut dirty, + magic_items, + obfuscated_names, + dm, ) = data; for pickup in wants_pickup.join() { @@ -47,7 +77,7 @@ impl<'a> System<'a> for ItemCollectionSystem { if pickup.collected_by == *player_entity { gamelog.append(format!( "You pick up the {}.", - names.get(pickup.item).unwrap().name + obfuscate_name(pickup.item, &names, &magic_items, &obfuscated_names, &dm) )); } } @@ -408,6 +438,9 @@ impl<'a> System<'a> for ItemDropSystem { WriteStorage<'a, Position>, WriteStorage<'a, InBackpack>, WriteStorage<'a, EquipmentChanged>, + ReadStorage<'a, MagicItem>, + ReadStorage<'a, ObfuscatedName>, + ReadExpect<'a, MasterDungeonMap>, ); fn run(&mut self, data: Self::SystemData) { @@ -420,6 +453,9 @@ impl<'a> System<'a> for ItemDropSystem { mut positions, mut backpack, mut dirty, + magic_items, + obfuscated_names, + dm, ) = data; for (entity, to_drop) in (&entities, &wants_drop).join() { @@ -449,7 +485,7 @@ impl<'a> System<'a> for ItemDropSystem { if entity == *player_entity { gamelog.append(format!( "You drop the {}.", - names.get(to_drop.item).unwrap().name + obfuscate_name(to_drop.item, &names, &magic_items, &obfuscated_names, &dm) )); } } diff --git a/src/map/dungeon.rs b/src/map/dungeon.rs index 5948802..a745e13 100644 --- a/src/map/dungeon.rs +++ b/src/map/dungeon.rs @@ -7,12 +7,14 @@ use ::specs::prelude::*; use crate::components::{OtherLevelPosition, Position, Viewshed}; use crate::map::{Map, TileType}; use crate::map_builders::level_builder; +use crate::raws; #[derive(Default, Serialize, Deserialize, Clone)] pub struct MasterDungeonMap { maps: HashMap, pub identified_items: HashSet, pub scroll_mappings: HashMap, + pub potion_mappings: HashMap, } impl MasterDungeonMap { @@ -21,15 +23,23 @@ impl MasterDungeonMap { maps: HashMap::new(), identified_items: HashSet::new(), scroll_mappings: HashMap::new(), + potion_mappings: HashMap::new(), }; let mut rng = RandomNumberGenerator::new(); - for scroll_tag in crate::raws::get_scroll_tags().iter() { + for scroll_tag in raws::get_scroll_tags().iter() { let masked_name = make_scroll_name(&mut rng); dm.scroll_mappings .insert(scroll_tag.to_string(), masked_name); } + let mut used_potion_names = HashSet::new(); + for potion_tag in raws::get_potion_tags().iter() { + let masked_name = make_potion_name(&mut rng, &mut used_potion_names); + dm.potion_mappings + .insert(potion_tag.to_string(), masked_name); + } + dm } @@ -89,6 +99,36 @@ fn make_scroll_name(rng: &mut rltk::RandomNumberGenerator) -> String { name } +const POTION_COLORS: &[&str] = &[ + "Red", "Orange", "Yellow", "Green", "Brown", "Indigo", "Violet", +]; +const POTION_ADJECTIVES: &[&str] = &[ + "Swirling", + "Effervescent", + "Slimey", + "Oiley", + "Viscous", + "Smelly", + "Glowing", +]; + +fn make_potion_name(rng: &mut RandomNumberGenerator, used_names: &mut HashSet) -> String { + loop { + let mut name = POTION_ADJECTIVES + [rng.roll_dice(1, POTION_ADJECTIVES.len() as i32) as usize - 1] + .to_string(); + name += " "; + name += POTION_COLORS[rng.roll_dice(1, POTION_COLORS.len() as i32) as usize - 1]; + name += " Potion"; + + if !used_names.contains(&name) { + used_names.insert(name.clone()); + + return name; + } + } +} + pub fn level_transition(ecs: &mut World, new_depth: i32, offset: i32) -> Option> { // Obtain the master dungeon map let dungeon_master = ecs.read_resource::(); diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index ce3201e..528ef7a 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -196,6 +196,21 @@ pub fn get_scroll_tags() -> Vec { result } +pub fn get_potion_tags() -> Vec { + let raws = &RAWS.lock().unwrap(); + let mut result = Vec::new(); + + for item in raws.raws.items.iter() { + if let Some(magic) = &item.magic { + if &magic.naming == "potion" { + result.push(item.name.clone()); + } + } + } + + result +} + pub fn is_tag_magic(tag: &str) -> bool { let raws = &RAWS.lock().unwrap(); if raws.item_index.contains_key(tag) { @@ -264,6 +279,7 @@ pub fn spawn_named_item( let dm = ecs.fetch::(); let scroll_names = dm.scroll_mappings.clone(); + let potion_names = dm.potion_mappings.clone(); let identified = dm.identified_items.clone(); std::mem::drop(dm); @@ -374,7 +390,16 @@ pub fn spawn_named_item( name: scroll_names[&item_template.name].clone(), }) } - _ => {} + "potion" => { + eb = eb.with(ObfuscatedName { + name: potion_names[&item_template.name].clone(), + }) + } + _ => { + eb = eb.with(ObfuscatedName { + name: magic.naming.clone(), + }) + } } } }