aboutsummaryrefslogtreecommitdiff
path: root/src/weapon.cc
blob: 053dbc10b79f7fa49618fc1113150f395db68b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "weapon.h"
#include "creature.h"
#include "rules.h"
#include "dice.h"
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

namespace entry {
    struct weaponImpl {
        std::vector<Damage> damage;
        std::set<std::string> properties;
        std::string weapon_type;
        std::pair<int, int> range;
        int reach;
        int cost;
        double weight;
    };

    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(weaponImpl, damage, properties, weapon_type, range, reach, cost, weight);

    NLOHMANN_FRIEND_DEFS(Item, Weapon, data);

    Weapon::Weapon() : data(new weaponImpl()) {}

    std::vector<Damage> Weapon::getDamage(void) const {return data->damage;}
    std::set<std::string> Weapon::getProperties(void) const {return data->properties;}
    std::string Weapon::getWeaponType(void) const {return data->weapon_type;}
    std::pair<int, int> Weapon::getRange(void) const {return data->range;}
    int Weapon::getReach(void) const {return data->reach;}
    int Weapon::getCost(void) const {return data->cost;}
    double Weapon::getWeight(void) const {return data->weight;}

    string getTextHelper(const Weapon& w, string toHitBonus, string damageBonus) {
        stringstream text;
        text << "+" << toHitBonus << " 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 << " Hit: ";
        auto dmgs = w.getDamage();
        for(size_t i = 0; i < dmgs.size(); i++) {
            const Damage& d = dmgs[i];
            text << d.dmg_die_count << "d" << d.dmg_die_sides;
            if(i == 0) {
                if(w.getProperties().count("versatile")) {
                    text << " (or " << d.dmg_die_count << "d" << d.dmg_die_sides + 2 << " if two-handed)";
                }
                text << " + " << damageBonus;
            }
            text << " " << d.dmg_type << " damage";
            if(i < dmgs.size()-1) {
                if(d.is_or) {
                    text << " or ";
                } else {
                    text << " plus ";
                }
            }
        }
        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 << " Additional properties: " << utils::join(props, ", ") << ".";
        }
        if(! w.Entry::getText().empty()) {
            text << " " << w.Entry::getText();
        }
        text << " " << w.Substantial::getText();
        return text.str();
    }

    vector<Damage> rollDmg(const Weapon& w, bool versatile) {
        vector<Damage> dmgs = w.getDamage();
        bool first = true;
        for(Damage& d : dmgs) {
            d.rolled = 0;
            int sides = d.dmg_die_sides;
            if(first && versatile && w.getProperties().count("versatile")) {
                sides += 2;
            }
            first = false;
            for(int i = 0; i < d.dmg_die_count; i++) {
                d.rolled += dice::roll(sides);
            }
        }
        return dmgs;
    }

    string formatDmg(const Weapon& w, const creature::Creature& c) {
        stringstream text;
        vector<Damage> dmgsNoVersatile = rollDmg(w, false);
        vector<Damage> dmgsVersatile = rollDmg(w, true);
        int abilityBonus = c.getBonus(creature::getBestAbility(getAbilityOptions(w), c));
        for(size_t i = 0; i < dmgsNoVersatile.size(); i++) {
            if(i == 0) {
                text << dmgsNoVersatile[i].rolled + abilityBonus;
                if(w.getProperties().count("versatile")) {
                    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();
    }

    vector<rules::Ability> 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!" << endl;
        return {rules::Ability::Str()};
    }

    string Weapon::getText() const {
        auto abilities = getAbilityOptions(*this);
        string abilityString;
        if(abilities.size() == 1) {
            abilityString = string(abilities[0]);
        } else {
            abilityString = "max(" + utils::join(abilities, ", ") + ")";
        }
        return getTextHelper(*this, "(" + abilityString + " + prof)", abilityString);

    }

    string Weapon::getText(const creature::Creature& c) const {
        stringstream text;
        text << getName() << " (" << getType() << "): ";
        // Determine best ability bonus
        int abilityBonus = c.getBonus(creature::getBestAbility(getAbilityOptions(*this), c));
        text << getTextHelper(*this, to_string(abilityBonus + c.getProficiency()), to_string(abilityBonus));
        return text.str();
    }
}