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

using namespace std;

namespace entry {
    string Spellcasting::getText(const creature::Creature& c) const {
        return genText(*this, c);
    }

    string toOrdinal(size_t number) {
        string suffix = "th";
        if (number % 100 < 11 || number % 100 > 13) {
            switch (number % 10) {
                case 1:
                    suffix = "st";
                    break;
                case 2:
                    suffix = "nd";
                    break;
                case 3:
                    suffix = "rd";
                    break;
            }
        }
        return to_string(number) + suffix;
    }

    string genText(const Spellcasting& s, const creature::Creature& c) {
        stringstream text;
        text << genText(static_cast<const Feature&>(s), c);
        text << ": Spellcasting ability: " << s.getAbility();
        text << " (spell save DC " << 8 + c.getBonus(s.getAbility()) + c.getProficiency();
        text << ", +" << c.getBonus(s.getAbility()) + c.getProficiency() << " to hit with spell attacks).";
        if(s.isInnate()) {
            text << " (spellcasting is innate).";
        }
        int levelNumber = 0;
        for(auto level : s.getSpellsBySlot()) {
            text << "\n";
            if(level.numSlots == 0) {
                if(s.isInnate()) {
                    text << "At will: ";
                } else {
                    text << "Cantrips: ";
                }
            } else {
                if(s.isInnate()) {
                    text << level.numSlots << "/day each: ";
                } else {
                    text << toOrdinal(levelNumber) << " level (" << level.numSlots << " slots): ";
                }
            }
            vector<string> names;
            for(auto spell : level.spells) {
                names.push_back(spell.getName());
            }
            text << utils::join(names, ", ");
            levelNumber++;
        }

        return text.str();
    }
}