From ae68e84557a7e56fd7210c1009aa1313dcc78adf Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 22 Nov 2023 15:28:00 -0500 Subject: Increased capacity for creating items from command line --- src/creature.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) (limited to 'src/creature.cc') diff --git a/src/creature.cc b/src/creature.cc index 4343b6d..955da4d 100644 --- a/src/creature.cc +++ b/src/creature.cc @@ -152,6 +152,19 @@ namespace creature { return bonus; } + void Creature::setProficientSave(const rules::Ability& ability) { + if(find(data->saves.begin(), data->saves.end(), ability) == data->saves.end()) { + data->saves.push_back(ability); + } + } + + void Creature::removeProficientSave(const rules::Ability& ability) { + for(auto it = data->saves.begin(); it != data->saves.end();) { + if(*it == ability) it = data->saves.erase(it); + else ++it; + } + } + void Creature::addInventoryItem(std::shared_ptr item) { data->inventory.push_back(item); } @@ -209,6 +222,19 @@ namespace creature { } } + void Creature::addFeature(std::shared_ptr feature) { + data->features.push_back(feature); + } + + void Creature::removeFeature(std::shared_ptr feature) { + for(auto it = data->features.begin(); it != data->features.end(); it++) { + if(*it == feature) { + data->features.erase(it); + break; + } + } + } + std::map Creature::getSkills() const { std::map s; for(auto skill : data->skills) { @@ -309,6 +335,46 @@ namespace creature { return out; } + bool addEntry(std::shared_ptr entry, Creature& c) { + auto i = std::dynamic_pointer_cast(entry); + if(i) { + c.addInventoryItem(i); + return true; + } + auto s = std::dynamic_pointer_cast(entry); + if(s) { + c.addSpell(s); + return true; + } + auto f = std::dynamic_pointer_cast(entry); + if(f) { + c.addFeature(f); + return true; + } + return false; + } + + template std::shared_ptr tryRemove(std::string entryName, F remover, std::vector entries) { + for(auto e : entries) { + std::string name = e->getName(); + if(utils::lower(name) == entryName) { + remover(e); + return e; + } + } + return std::shared_ptr(); + } + + std::shared_ptr removeEntry(std::string entryName, Creature& c) { + entryName = utils::lower(entryName); + if(auto e = tryRemove(entryName, [c](auto e) mutable {c.removeInventoryItem(e);}, c.getInventory())) return e; + if(auto e = tryRemove(entryName, [c](auto e) mutable {c.removeFeature(e);}, c.getFeatures())) return e; + if(c.getSpellcasting()) { + if(auto e = tryRemove(entryName, [c](auto e) mutable {c.removeSpell(e);}, c.getSpellcasting()->getSpells())) return e; + } + return std::shared_ptr(); + } + std::vector dmgTypes2text(std::vector dmg) { std::vector ret; for(dmgType t : dmg) { @@ -361,7 +427,9 @@ namespace creature { if(! getSkills().empty()) { text << std::endl << "Skills:" << std::endl; for(auto skill : getSkills()) { - text << " * " << skill.first.getName() << " (+" << skill.second << ")" << std::endl; + text << " * " << skill.first.getName(); + if(data->skills.at(skill.first) == 2) text << " Expert"; + text << " (+" << skill.second << ")" << std::endl; } } if(! getSaves().empty()) { @@ -389,9 +457,21 @@ namespace creature { } } if(! getInventory().empty()) { - text << std::endl << "Inventory:" << std::endl; + std::map inventoryText; + double totalWeight = 0; + int totalCost = 0; for(auto i : getInventory()) { - text << " * " << i->getText(*this) << std::endl; + totalWeight += i->getWeight(); + totalCost += i->getCost(); + auto t = i->getText(*this); + if(inventoryText.contains(t)) inventoryText[t]++; + else inventoryText.insert({t, 1}); + } + text << std::endl << "Inventory (" << totalWeight << " lbs, value " << utils::getCostString(totalCost) << "):" << std::endl; + for(auto const& [t, count] : inventoryText) { + text << " * "; + if(count > 1) text << count << "x "; + text << t << std::endl; } } if(! Entry::getText().empty()) { -- cgit v1.2.3