#include "weapon.h" #include "creature.h" #include "rules.h" #include "dice.h" #include #include #include #include using namespace std; namespace entry { struct weaponImpl { std::vector damage; std::set properties; std::string weapon_type; std::pair range; int reach; std::optional toHitOverride; std::optional dmgBonusOverride; std::optional abilityOverride; }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(weaponImpl, damage, properties, weapon_type, range, reach, toHitOverride, dmgBonusOverride, abilityOverride); NLOHMANN_FRIEND_DEFS(Item, Weapon, data); Weapon::Weapon() : data(new weaponImpl()) {} std::vector Weapon::getDamage(void) const {return data->damage;} std::set Weapon::getProperties(void) const {return data->properties;} std::string Weapon::getWeaponType(void) const {return data->weapon_type;} std::pair Weapon::getRange(void) const {return data->range;} int Weapon::getReach(void) const {return data->reach;} string getTextHelper(const Weapon& w, string toHitBonus, string damageBonus) { stringstream text; text << "+" << toHitBonus << " to hit, "; if(w.getReach() > 0 or w.getRange().second == 0) { text << "reach " << w.getReach() << " ft."; if(w.getRange().second > 0) { text << " or "; } } if(w.getRange().second > 0) { text << "range " << w.getRange().first << "/" << w.getRange().second << " ft."; } text << " Hit: "; auto dmgs = w.getDamage(); for(size_t i = 0; i < dmgs.size(); i++) { const Damage& d = dmgs[i]; text << d.dmg_die_count; if(d.dmg_die_sides > 1) { text << "d" << d.dmg_die_sides; } if((i == 0 or dmgs[i].is_or) and w.getType() == "weapons" and d.dmg_die_sides != 1) { if(w.getProperties().count("versatile")) { text << " (or " << d.dmg_die_count << "d" << d.dmg_die_sides + 2 << " if two-handed)"; } try { int dmgBonusInt = stoi(damageBonus); if(dmgBonusInt > 0) { text << " + " << dmgBonusInt; } else if(dmgBonusInt < 0) { text << " - " << dmgBonusInt * -1; } // Else it's zero } catch(exception& e) { text << " + " << damageBonus; } } text << " " << d.dmg_type << " damage"; if(i < dmgs.size()-1) { if(dmgs[i+1].is_or) { text << " or "; } else { text << " plus "; } } } if(w.Entry::getText().empty()) { text << "."; } else { char first = w.Entry::getText()[0]; if('a' <= first and 'z' >= first) { text << ' '; } else if('A' <= first and 'Z' >= first) { text << ". "; } text << w.Entry::getText(); } auto props = w.getProperties(); // We don't care about finesse nor versatile because they're already handled props.erase("finesse"); props.erase("versatile"); if(! props.empty()) { text << " Additional properties: " << utils::join(props, ", ") << "."; } if(! w.getCostWeightText().empty()) { text << " " << w.getCostWeightText(); } return text.str(); } vector rollDmg(const Weapon& w, bool versatile) { vector dmgs = w.getDamage(); bool first = true; for(Damage& d : dmgs) { d.rolled = 0; if(first && versatile && w.getProperties().count("versatile")) { d.dmg_die_sides += 2; } first = false; for(int i = 0; i < d.dmg_die_count; i++) { d.rolled += dice::roll(d.dmg_die_sides); } } return dmgs; } string formatDmgHelper(const Weapon& w, const creature::Creature& c, const std::vector& dmgsNoVersatile, const std::vector& dmgsVersatile) { stringstream text; int abilityBonus = w.getDamageBonus(c); for(size_t i = 0; i < dmgsNoVersatile.size(); i++) { if(i == 0 and w.getType() == "weapons" and dmgsNoVersatile[0].dmg_die_sides != 1) { text << dmgsNoVersatile[i].rolled + abilityBonus; if(w.getProperties().count("versatile") and ! dmgsVersatile.empty()) { text << " (or " << dmgsVersatile[i].rolled + abilityBonus << " if two-handed)"; } } else { text << dmgsNoVersatile[i].rolled; } text << " " << dmgsNoVersatile[i].dmg_type << " damage"; if(i < dmgsNoVersatile.size()-1) { if(dmgsNoVersatile[i].is_or) { text << " or "; } else { text << " plus "; } } } return text.str(); } string formatDmg(const Weapon& w, const creature::Creature& c) { vector dmgsNoVersatile = rollDmg(w, false); vector dmgsVersatile = rollDmg(w, true); return formatDmgHelper(w, c, dmgsNoVersatile, dmgsVersatile); } string formatDmg(const Weapon& w, const creature::Creature& c, const std::vector& dmgs) { return formatDmgHelper(w, c, dmgs, {}); } vector getAbilityOptions(const Weapon& w) { // Do finesse if(w.getProperties().count("finesse")) { return {rules::Ability::Str(), rules::Ability::Dex()}; } // Do melee weapons if(w.getReach() > 0) { return {rules::Ability::Str()}; } // Do range weapons (thrown melee were done above) if(w.getRange().second > 0) { return {rules::Ability::Dex()}; } //cerr << "Error processing weapon: " << w.getName() << "!" << endl; // Default to str return {rules::Ability::Str()}; } int getAbilityBonus(const creature::Creature& c, const Weapon& w, std::shared_ptr data) { if(data->abilityOverride) { return c.getBonus(*data->abilityOverride); } return c.getBonus(creature::getBestAbility(getAbilityOptions(w), c)); } int Weapon::getToHitBonus(const creature::Creature& c) const { if(data->toHitOverride) { return *data->toHitOverride; } return getAbilityBonus(c, *this, data) + c.getProficiency(); } int Weapon::getDamageBonus(const creature::Creature& c) const { if(data->dmgBonusOverride) { return *data->dmgBonusOverride; } return getAbilityBonus(c, *this, data); } string Weapon::getText() const { auto abilities = getAbilityOptions(*this); string abilityString; if(data->dmgBonusOverride) { abilityString = to_string(*data->dmgBonusOverride); } else if(data->abilityOverride) { abilityString = data->abilityOverride->getAbbrev(); } else if(abilities.size() == 1) { abilityString = string(abilities[0]); } else { abilityString = "max(" + utils::join(abilities, ", ") + ")"; } string toHitString = data->toHitOverride ? to_string(*data->toHitOverride) : "(" + abilityString + " + prof)"; return getTextHelper(*this, toHitString, abilityString); } string Weapon::getText(const creature::Creature& c) const { stringstream text; text << getName() << " (" << getType() << "): "; string toHitString = to_string(getToHitBonus(c)); string dmgBonus = to_string(getDamageBonus(c)); text << getTextHelper(*this, toHitString, dmgBonus); return text.str(); } }