#include "creature.h" #include "dice.h" #include "rules.h" #include "feature.h" #include "weapon.h" #include "armor.h" #include "attack.h" #include "spellcasting.h" #include #include #include #include using namespace std; namespace creature { void Creature::init() { if(hpMax == -1) { // Initialize hp hpMax = getBonus(rules::Ability::Con()) * hit_die_count; for(int i = 0; i < hit_die_count; i++) { hpMax += dice::roll(hit_die_sides); } hp = hpMax; } } // True if type without matching qualifiers is in subdata bool conditionApplies(const string& type, const vector& qualifiers, const vector subdata) { bool applies = false; for(dmgType con : subdata) { if(con.type == type) { applies = true; for(auto qualifier : qualifiers) { if(find(con.qualifiers.begin(), con.qualifiers.end(), qualifier) != con.qualifiers.end()) { applies = false; } } } } return applies; } void Creature::applyDamage(int amount, const string& type, const vector& qualifiers) { if(! conditionApplies(type, qualifiers, getDmgImmunities())) { if(conditionApplies(type, qualifiers, getDmgResistances())) { hp -= amount / 2; } else if(conditionApplies(type, qualifiers, getDmgVulnerabilities())) { hp -= amount * 2; } else { hp -= amount; } } } void Creature::applyHealing(int amount) { hp += amount; if(hp > hpMax) { hp = hpMax; } } int Creature::getSkillBonus(const rules::Skill& skill) const { int bonus = this->getBonus(skill.getAbility()); if(skills.contains(skill)) { bonus += skills.at(skill) * getProficiency(); } return bonus; } int Creature::getAbilitySaveBonus(const rules::Ability& ability) const { int bonus = this->getBonus(ability); if(find(saves.begin(), saves.end(), ability) != saves.end()) { bonus += getProficiency(); } return bonus; } void Creature::addInventoryItem(shared_ptr item) { inventory.push_back(item); } shared_ptr Creature::getSpellcasting() const { for(auto f : getFeatures()) { if(f->getType() == "spells") { return dynamic_pointer_cast(f); } } return shared_ptr(); } void Creature::addSpell(shared_ptr spell) { auto sc = getSpellcasting(); if(! sc) { throw runtime_error("Creature " + getName() + " has no spellcasting"); } std::size_t level = spell->getLevel(); while(sc->getSlotLevels().size() <= level) { sc->addSlotLevel(); } sc->getSlotLevels()[level]->spells.push_back(spell); } void Creature::removeSpell(shared_ptr spell) { auto sc = getSpellcasting(); if(sc) { if(sc->getSlotLevels().size() > (size_t) spell->getLevel()) { auto sl = sc->getSlotLevels()[spell->getLevel()]; for(auto it = sl->spells.begin(); it != sl->spells.end(); it++) { if(*it == spell) { sl->spells.erase(it); break; } } } } } void Creature::removeInventoryItem(shared_ptr item) { for(auto it = inventory.begin(); it != inventory.end(); it++) { if(*it == item) { inventory.erase(it); break; } } } map Creature::getSkills() const { map s; for(auto skill : skills) { s.insert({skill.first, getSkillBonus(skill.first)}); } return s; } map Creature::getSaves() const { map s; for(auto save : saves) { s.insert({save, this->getBonus(save) + getProficiency()}); } return s; } void Creature::setScore(const rules::Ability& ability, int score) { int initBonus = getBonus(ability); stats[ability] = score; if(ability == rules::Ability::Con()) { int delta = getBonus(ability) - initBonus; hpMax += delta * hit_die_count; hp += delta * hit_die_count; } } void Creature::setProfLevel(const rules::Skill& skill, int level) { skills[skill] = level; if(level == 0) { skills.erase(skill); } } void Creature::longRest() { hp = hpMax; } const int getAC(const Creature& c) { auto natArmor = c.getNaturalArmor(); if(! natArmor.name.empty()) { return natArmor.bonus; } int dex = c.getBonus(rules::Ability::Dex()); int baseBonus = 10 + dex; int miscBonus = 0; for(auto a : utils::castPtrs(c.getInventory())) { if(c.getScore(rules::Ability::Str()) < a->getStrRequirement()) { continue; } auto armorType = a->getArmorType(); if(armorType== "misc" || armorType == "shield") { miscBonus += a->getACBonus(); } else { baseBonus = a->getACBonus(); if(armorType == "light") { baseBonus += dex; } else if(armorType == "medium") { baseBonus += (dex > 2)? 2 : dex; } } } return baseBonus + miscBonus; } vector> getAttacks(const Creature& c) { vector> a = utils::castPtrs(c.getInventory()); for(auto attack : utils::castPtrs(c.getFeatures())) { a.push_back(shared_ptr(new entry::Weapon(attack->getWeapon()))); } return a; } rules::Ability getBestAbility(const std::vector& abilities, const Creature& c) { const rules::Ability* bestA = &abilities[0]; for(auto a : abilities) { if(c.getBonus(a) > c.getBonus(*bestA)) { bestA = &a; } } return *bestA; } template vector mapItems(const vector>& items) { vector out; for(auto i : items) { out.push_back(i->getName()); } return out; } vector dmgTypes2text(vector dmg) { vector ret; for(dmgType t : dmg) { ret.push_back(t.getText()); } return ret; } string formatDmgTypeVector(string title, vector dmg) { return title + ": " + utils::join(dmgTypes2text(dmg), "; "); } string Creature::getText() const { stringstream text; text << getGivenName() << " (" << getCreatureName() << "): " << getHP() << "/" << getHPMax() << " hp, " << getAC(*this) << " ac"; if(! getNaturalArmor().name.empty()) { text << " (" << getNaturalArmor().name << ")"; } else { string armor = utils::join(mapItems(utils::castPtrs(getInventory())), ", "); if(! armor.empty()) { text << " (" << armor << ")"; } } text << ", speed " << getSpeed() << endl; text << "A cr " << getCR() << " " << getAlignment() << " " << getSize() << " " << getType() << "." << endl; text << "Stats:" << endl; using namespace rules; vector abilities {Ability::Str(), Ability::Dex(), Ability::Con(), Ability::Int(), Ability::Wis(), Ability::Cha()}; for(auto ability : abilities) { text << " " << setw(6) << std::left << ability.getAbbrev(); } text << endl; for(auto ability : abilities) { text << setw(7) << std::left << (to_string(getScore(ability)) + "(" + to_string(getBonus(ability)) + ")"); } text << endl; text << "Senses: "; if(! getSenses().empty()) { text << utils::join(getSenses(), ", ") << ". "; } text << "Passive Perception " << 10 + getSkillBonus(rules::Skill::Perception()) << endl; if(! getLanguages().empty()) { text << "Languages: " << getLanguages() << endl; } text << "Proficiency: " << getProficiency() << endl; if(! getSkills().empty()) { text << endl << "Skills:" << endl; for(auto skill : getSkills()) { text << " * " << skill.first.getName() << " (+" << skill.second << ")" << endl; } } if(! getSaves().empty()) { text << endl << "Saves:" << endl; for(auto save : getSaves()) { text << " * " << save.first.getAbbrev() << " (+" << save.second << ")" << endl; } } if(! getDmgImmunities().empty()) { text << formatDmgTypeVector("\nDamage Immunities", getDmgImmunities()) << endl; } if(! getDmgResistances().empty()) { text << formatDmgTypeVector("\nDamage Resistances", getDmgResistances()) << endl; } if(! getDmgVulnerabilities().empty()) { text << formatDmgTypeVector("\nDamage Vulnerabilities", getDmgVulnerabilities()) << endl; } if(! getCondImmunities().empty()) { text << formatDmgTypeVector("\nCondition Immunities", getCondImmunities()) << endl; } if(! getFeatures().empty()) { text << endl << "Features:" << endl; for(auto f: getFeatures()) { text << " * " << f->getText(*this) << endl; } } if(! getInventory().empty()) { text << endl << "Inventory:" << endl; for(auto i : getInventory()) { text << " * " << i->getText(*this) << endl; } } if(! Entry::getText().empty()) { text << endl << Entry::getText() << endl; } return text.str(); } }