aboutsummaryrefslogtreecommitdiff
path: root/src/spellcasting.cc
blob: 6315446b4f1430d033acb99591c505b22cb9278d (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
#include "spellcasting.h"
#include "creature.h"
#include "utils.h"
#include <string>
#include <sstream>
#include <vector>

using namespace std;

namespace entry {
    // Slot level serialization
    void to_json(nlohmann::json& j, const SlotLevel& sl) {
        std::vector<std::string> s;
        for(auto spell : sl.spells) {
            s.push_back(spell->getName());
        }
        j = {{"slots", sl.numSlots}, {"spells", s}};
    }

    void from_json(const nlohmann::json& j, SlotLevel& sl) {
        j.at("slots").get_to(sl.numSlots);
        sl.spells = utils::instantiateNames<Spell>("spells", j["spells"]);
    }

    shared_ptr<SlotLevel> SlotLevel::create(const json& data) {
        return shared_ptr<SlotLevel>(new SlotLevel(data));
    }

    vector<shared_ptr<Spell>> Spellcasting::getSpells() const {
        vector<shared_ptr<Spell>> ret;
        for(auto sl : getSlotLevels()) {
            for(auto spell : sl->spells) {
                ret.push_back(spell);
            }
        }
        return ret;
    }

    string Spellcasting::getText(const creature::Creature& c) const {
        stringstream text;
        text << getName() << " (" << getType() << "): ";
        text << "The " << c.getCreatureName() << "'s spellcasting ability is " << getAbility().getFull();
        text << " (spell save DC " << 8 + c.getBonus(getAbility()) + c.getProficiency();
        text << ", +" << c.getBonus(getAbility()) + c.getProficiency() << " to hit with spell attacks).";
        if(isInnate()) {
            text << " Spellcasting is innate.";
        }
        int levelNumber = -1;
        for(auto level : getSlotLevels()) {
            levelNumber++;
            // Skip if it's empty
            if(level->numSlots == 0 && level->spells.empty()) {
                continue;
            }
            text << endl;
            if(levelNumber == 0) {
                if(isInnate()) {
                    text << "    At will: ";
                } else {
                    text << "    Cantrips: ";
                }
            } else {
                if(isInnate()) {
                    text << "    " << level->numSlots << "/day each: ";
                } else {
                    text << "    " << utils::toOrdinal(levelNumber) << " level (" << level->numSlots << " slots): ";
                }
            }
            vector<string> names;
            for(auto spell : level->spells) {
                names.push_back(spell->getName());
            }
            text << utils::join(names, ", ");
        }

        return text.str();
    }
}