pluralised names

This commit is contained in:
Llywelwyn 2023-07-23 02:10:29 +01:00
parent b4420ba538
commit 050923fab0
3 changed files with 70 additions and 42 deletions

View file

@ -57,6 +57,7 @@ pub struct Telepath {
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct Name {
pub name: String,
pub plural: String,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]

View file

@ -145,37 +145,49 @@ pub fn show_inventory(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option
// FIXME: This is unwieldy. Having a separate data structure for (items, id) and (items, count) is not good.
// But it works, and this might get cut anyway as I get further along in the design, so leaving as is atm.
let mut inventory_ids: BTreeMap<String, Entity> = BTreeMap::new();
let mut player_inventory: BTreeMap<String, i32> = BTreeMap::new();
let mut player_inventory: BTreeMap<(String, String), i32> = BTreeMap::new();
for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity) {
player_inventory.entry(name.name.to_string()).and_modify(|count| *count += 1).or_insert(1);
player_inventory
.entry((name.name.to_string(), name.plural.to_string()))
.and_modify(|count| *count += 1)
.or_insert(1);
inventory_ids.entry(name.name.to_string()).or_insert(entity);
}
let count = player_inventory.len();
let mut y = (25 - (count / 2)) as i32;
ctx.draw_box(15, y - 2, 37, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
ctx.draw_box(15, y - 2, 45, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
ctx.print_color(18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory");
ctx.print_color(18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESC to cancel");
let mut j = 0;
for (name, item_count) in &player_inventory {
// Print the character required to access this item. i.e. (a)
ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType);
ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
let mut x = 21;
let vowels = ['a', 'e', 'i', 'o', 'u'];
let mut x = 21;
if item_count > &1 {
// If more than one, print the number and pluralise
// i.e. (a) 3 daggers
ctx.print(x, y, item_count);
x += 2;
} else if vowels.iter().any(|&v| name.starts_with(v)) {
ctx.print(x, y, "an");
x += 3;
ctx.print(x, y, name.1.to_string());
} else {
ctx.print(x, y, "a");
x += 2;
if ['a', 'e', 'i', 'o', 'u'].iter().any(|&v| name.0.starts_with(v)) {
// If one and starts with a vowel, print 'an'
// i.e. (a) an apple
ctx.print(x, y, "an");
x += 3;
} else {
// If one and not a vowel, print 'a'
// i.e. (a) a dagger
ctx.print(x, y, "a");
x += 2;
}
ctx.print(x, y, name.0.to_string());
}
ctx.print(x, y, name.to_string());
y += 1;
j += 1;
}
@ -202,37 +214,49 @@ pub fn drop_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option
let entities = gs.ecs.entities();
let mut inventory_ids: BTreeMap<String, Entity> = BTreeMap::new();
let mut player_inventory: BTreeMap<String, i32> = BTreeMap::new();
let mut player_inventory: BTreeMap<(String, String), i32> = BTreeMap::new();
for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity) {
player_inventory.entry(name.name.to_string()).and_modify(|count| *count += 1).or_insert(1);
player_inventory
.entry((name.name.to_string(), name.plural.to_string()))
.and_modify(|count| *count += 1)
.or_insert(1);
inventory_ids.entry(name.name.to_string()).or_insert(entity);
}
let count = player_inventory.len();
let mut y = (25 - (count / 2)) as i32;
ctx.draw_box(15, y - 2, 37, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
ctx.draw_box(15, y - 2, 45, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
ctx.print_color(18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop what?");
ctx.print_color(18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESC to cancel");
let mut j = 0;
for (name, item_count) in &player_inventory {
// Print the character required to access this item. i.e. (a)
ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType);
ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
let mut x = 21;
let vowels = ['a', 'e', 'i', 'o', 'u'];
let mut x = 21;
if item_count > &1 {
// If more than one, print the number and pluralise
// i.e. (a) 3 daggers
ctx.print(x, y, item_count);
x += 2;
} else if vowels.iter().any(|&v| name.starts_with(v)) {
ctx.print(x, y, "an");
x += 3;
ctx.print(x, y, name.1.to_string());
} else {
ctx.print(x, y, "a");
x += 2;
if ['a', 'e', 'i', 'o', 'u'].iter().any(|&v| name.0.starts_with(v)) {
// If one and starts with a vowel, print 'an'
// i.e. (a) an apple
ctx.print(x, y, "an");
x += 3;
} else {
// If one and not a vowel, print 'a'
// i.e. (a) a dagger
ctx.print(x, y, "a");
x += 2;
}
ctx.print(x, y, name.0.to_string());
}
ctx.print(x, y, name.to_string());
y += 1;
j += 1;
}

View file

@ -23,7 +23,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
})
.with(Player {})
.with(Viewshed { visible_tiles: Vec::new(), range: 12, dirty: true })
.with(Name { name: "wanderer".to_string() })
.with(Name { name: "wanderer".to_string(), plural: "wanderers".to_string() })
.with(CombatStats { max_hp: 8, hp: 8, defence: 0, power: 4 })
.with(HungerClock { state: HungerState::Satiated, duration: 50 })
.with(Attributes {
@ -47,7 +47,7 @@ fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy
.with(Viewshed { visible_tiles: Vec::new(), range: 12, dirty: true })
.with(Monster {})
.with(Mind {})
.with(Name { name: name.to_string() })
.with(Name { name: name.to_string(), plural: format!("{}s", name.to_string()) })
.with(BlocksTile {})
.with(CombatStats { max_hp: rolled_hp, hp: rolled_hp, defence: 0, power: power })
.marked::<SimpleMarker<SerializeMe>>()
@ -236,7 +236,7 @@ fn health_potion(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "potion of health".to_string() })
.with(Name { name: "potion of health".to_string(), plural: "potions of health".to_string() })
.with(Item {})
.with(Consumable {})
.with(Destructible {})
@ -254,7 +254,7 @@ fn weak_health_potion(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "potion of lesser health".to_string() })
.with(Name { name: "potion of lesser health".to_string(), plural: "potions of lesser health".to_string() })
.with(Item {})
.with(Consumable {})
.with(Destructible {})
@ -294,7 +294,7 @@ fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "scroll of magic missile".to_string() })
.with(Name { name: "scroll of magic missile".to_string(), plural: "scrolls of magic missile".to_string() })
.with(Item {})
.with(Consumable {})
.with(Destructible {})
@ -313,7 +313,7 @@ fn fireball_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "scroll of fireball".to_string() })
.with(Name { name: "scroll of fireball".to_string(), plural: "scrolls of fireball".to_string() })
.with(Item {})
.with(Consumable {})
.with(Destructible {})
@ -333,7 +333,7 @@ fn cursed_fireball_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "cursed scroll of fireball".to_string() })
.with(Name { name: "cursed scroll of fireball".to_string(), plural: "cursed scrolls of fireball".to_string() })
.with(Item {})
.with(Cursed {})
.with(Consumable {})
@ -354,7 +354,7 @@ fn confusion_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "scroll of confusion".to_string() })
.with(Name { name: "scroll of confusion".to_string(), plural: "scrolls of confusion".to_string() })
.with(Item {})
.with(Consumable {})
.with(Destructible {})
@ -373,7 +373,7 @@ fn magic_map_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "scroll of magic mapping".to_string() })
.with(Name { name: "scroll of magic mapping".to_string(), plural: "scrolls of magic mapping".to_string() })
.with(Item {})
.with(MagicMapper {})
.with(Consumable {})
@ -391,7 +391,10 @@ fn cursed_magic_map_scroll(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "cursed scroll of magic mapping".to_string() })
.with(Name {
name: "cursed scroll of magic mapping".to_string(),
plural: "cursed scrolls of magic mapping".to_string(),
})
.with(Item {})
.with(Cursed {})
.with(MagicMapper {})
@ -411,7 +414,7 @@ fn dagger(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "dagger".to_string() })
.with(Name { name: "dagger".to_string(), plural: "daggers".to_string() })
.with(Item {})
.with(Equippable { slot: EquipmentSlot::Melee })
.with(MeleePowerBonus { amount: 1 })
@ -427,7 +430,7 @@ fn shortsword(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "shortsword".to_string() })
.with(Name { name: "shortsword".to_string(), plural: "shortswords".to_string() })
.with(Item {})
.with(Equippable { slot: EquipmentSlot::Melee })
.with(MeleePowerBonus { amount: 2 })
@ -444,7 +447,7 @@ fn buckler(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "buckler".to_string() })
.with(Name { name: "buckler".to_string(), plural: "bucklers".to_string() })
.with(Item {})
.with(DefenceBonus { amount: 1 })
.with(Equippable { slot: EquipmentSlot::Shield })
@ -461,7 +464,7 @@ fn shield(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "shield".to_string() })
.with(Name { name: "shield".to_string(), plural: "shields".to_string() })
.with(Item {})
.with(DefenceBonus { amount: 2 })
.with(MeleePowerBonus { amount: -1 })
@ -481,7 +484,7 @@ fn rations(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "rations".to_string() })
.with(Name { name: "rations".to_string(), plural: "rations".to_string() })
.with(Item {})
.with(ProvidesNutrition {})
.with(Consumable {})
@ -500,7 +503,7 @@ fn fireball_wand(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "wand of fireball".to_string() })
.with(Name { name: "wand of fireball".to_string(), plural: "wands of fireball".to_string() })
.with(Item {})
.with(Wand { uses: 3, max_uses: 3 })
.with(Destructible {})
@ -520,7 +523,7 @@ fn magic_missile_wand(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "wand of magic missile".to_string() })
.with(Name { name: "wand of magic missile".to_string(), plural: "wands of magic missile".to_string() })
.with(Item {})
.with(Wand { uses: 3, max_uses: 3 })
.with(Destructible {})
@ -539,7 +542,7 @@ fn confusion_wand(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "wand of confusion".to_string() })
.with(Name { name: "wand of confusion".to_string(), plural: "wands of confusion".to_string() })
.with(Item {})
.with(Wand { uses: 3, max_uses: 3 })
.with(Destructible {})
@ -559,7 +562,7 @@ fn bear_trap(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "bear trap".to_string() })
.with(Name { name: "bear trap".to_string(), plural: "bear traps".to_string() })
.with(Hidden {})
.with(EntryTrigger {})
.with(SingleActivation {})
@ -577,7 +580,7 @@ fn confusion_trap(ecs: &mut World, x: i32, y: i32) {
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name { name: "magic trap".to_string() })
.with(Name { name: "magic trap".to_string(), plural: "magic traps".to_string() })
.with(Hidden {})
.with(EntryTrigger {})
.with(SingleActivation {})