diff options
| author | Your Name <you@example.com> | 2021-04-13 15:14:34 -0400 | 
|---|---|---|
| committer | Your Name <you@example.com> | 2021-04-13 15:14:34 -0400 | 
| commit | 2ab51e507d620c4479e07ca0ec47d22c8c66bc90 (patch) | |
| tree | 90906ecb043c01034280c767b83a88eb6df6956f /src/weapon.cc | |
| download | dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.tar.gz dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.tar.bz2 dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.zip  | |
Initial commit
Diffstat (limited to 'src/weapon.cc')
| -rw-r--r-- | src/weapon.cc | 91 | 
1 files changed, 91 insertions, 0 deletions
diff --git a/src/weapon.cc b/src/weapon.cc new file mode 100644 index 0000000..b12c2cb --- /dev/null +++ b/src/weapon.cc @@ -0,0 +1,91 @@ +#include "json.hpp" +#include "weapon.h" +#include "creature.h" +#include <string> +#include <sstream> +#include <algorithm> + +using namespace std; + +namespace item { +    Weapon::Weapon(const nlohmann::json& data) +        : Item(data), damageType(data["damage"]["dmg_type"]), damageDieCount(data["damage"]["dmg_die_count"]), damageDieSides(data["damage"]["dmg_die_sides"]), properties(data["properties"]), weaponType(data["type"]), range(data["range"][0], data["range"][1]), reach(data["reach"]) {} + +    string Weapon::getDamageType() const { +        return damageType; +    } + +    int Weapon::getDamageDieCount() const { +        return damageDieCount; +    } + +    int Weapon::getDamageDieSides(bool versatile) const { +        if(versatile && getProperties().count("versatile")) { +            return damageDieSides + 2; +        } +        return damageDieSides; +    } + +    set<string> Weapon::getProperties() const { +        return properties; +    } + +    string Weapon::getWeaponType() const { +        return weaponType; +    } + +    pair<int, int> Weapon::getRange() const { +        return range; +    } + +    int Weapon::getReach() const { +        return reach; +    } + +    nlohmann::json Weapon::toJson() const { +        auto data = Item::toJson(); +        data["damage"]["dmg_type"] = damageType; +        data["damage"]["dmg_die_count"] = damageDieCount; +        data["damage"]["dmg_die_sides"] = damageDieSides; +        data["properties"] = properties; +        data["type"] = weaponType; +        data["range"] = range; +        data["reach"] = reach; +        return data; +    } + +    std::string genActionText(const Weapon& w, const creature::Creature& c) { +        stringstream text; +        // Determine best ability bonus +        int abilityBonus = c.getBonus("str"); +        if(w.getProperties().count("finesse")) { +            abilityBonus = max(abilityBonus, c.getBonus("dex")); +        } +        text << "+" << abilityBonus + c.getProficiency() << " to hit, "; +        if(w.getReach() > 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 << "\nHit: " << w.getDamageDieCount() << "d" << w.getDamageDieSides() << " + " << abilityBonus << " " << w.getDamageType() << " damage"; +        if(w.getProperties().count("versatile")) { +            text << " (or " << w.getDamageDieCount() << "d" << w.getDamageDieSides(true) << " + " << abilityBonus << " " << w.getDamageType() << " damage if two-handed)"; +        } +        text << "."; +        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 << "\nAdditional properties:\n"; +            for(string prop : props) { +                text << "\t" << prop << "\n"; +            } +        } +        return text.str(); +    } +}  | 
