aboutsummaryrefslogtreecommitdiff
path: root/src/creature.h
blob: cdbd2b9eaef434bd347a563ab8f593d8991403f4 (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
#pragma once
#include "json.hpp"
#include "rules.h"
#include "utils.h"
#include "entry.h"
#include <cmath>

namespace entry {
    class Feature;
    class Item;
    class Weapon;
    class Attack;
    class Spell;
    class Spellcasting;
}

typedef nlohmann::json json;

namespace creature {
    class Creature;

    class dmgType : public Jsonable {
        public:
            dmgType(const json& data) : type(data["type"]), qualifiers(json2vec<rules::Qualifier>(data["qualifiers"])) {}
            std::string type;
            std::vector<rules::Qualifier> qualifiers;
            std::string getText() const {
                if(qualifiers.empty()) {
                    return type;
                }
                return utils::join(qualifiers, ", ") + " " + type;
            }
            json toJson(void) const {
                return json({
                        {"type", type},
                        {"qualifiers", qualifiers}
                        });
            }

    };

    // Convenience function to calculate AC
    const int getAC(const Creature& c);

    // Convenience function to spit out everything about it
    std::string genText(const Creature& c);

    class Creature : public entry::Entry {
        public:
            Creature(const json& data, const json& base);
            virtual ~Creature() {};

            // Getters
            int getSkillBonus(const rules::Skill& skill) const;
            int getAbilitySaveBonus(const rules::Ability& ability) const;
            std::map<rules::Skill, int> getSkills(void) const;
            std::map<rules::Ability, int> getSaves(void) const;

            //Override getText
            virtual std::string getText() const override {return genText(*this);}

            // Inline getters
            std::string getCreatureName(void) const {return getName();}
            std::string getGivenName(void) const {return givenName;}
            //std::string getType(void) const {return getType();}
            std::string getSize(void) const {return size;}
            std::string getAlignment(void) const {return alignment;}
            double getCR(void) const {return cr;}
            std::pair<std::string, int> getNaturalArmor(void) const {return {natArmorName, natArmorBonus};}
            std::string getLanguages(void) const {return langs;}
            int getHP(void) const {return hp;}
            int getHPMax(void) const {return hpMax;}
            std::vector<std::string> getSenses(void) const {return senses;}
            std::string getSpeed(void) const {return speed;}
            int getScore(const rules::Ability& ability) const {return stats.at(ability);}
            int getBonus(const rules::Ability& ability) const {return std::floor((getScore(ability) - 10) / 2.0);}
            int getProficiency(void) const {return proficiency;}
            std::vector<std::shared_ptr<entry::Feature>> getFeatures(void) const {return features;}
            std::shared_ptr<entry::Spellcasting> getSpellcasting(void) const;
            std::vector<std::shared_ptr<entry::Item>> getInventory(void) const {return inventory;}
            std::vector<dmgType> getDmgImmunities(void) const {return dmgImmunities;}
            std::vector<dmgType> getDmgResistances(void) const {return dmgResistances;}
            std::vector<dmgType> getDmgVulnerabilities(void) const {return dmgVulnerabilities;}
            std::vector<dmgType> getCondImmunities(void) const {return condImmunities;}


            // Setters (mutators)
            void setGivenName(std::string name) {givenName = name;}
            void applyDamage(int amount, const std::string& type, const std::vector<rules::Qualifier>& qualifiers);
            void applyHealing(int amount);
            void setScore(const rules::Ability& ability, int score);
            void setProfLevel(const rules::Skill& skill, int level);
            void setProficiency(int prof) {proficiency = prof;}
            void addInventoryItem(std::shared_ptr<entry::Item> item);
            void addSpell(std::shared_ptr<entry::Spell> spell);
            void removeSpell(std::shared_ptr<entry::Spell> spell);
            void removeInventoryItem(std::shared_ptr<entry::Item> item);
            void longRest(void);

            virtual json toJson(void) const;

        private:
            // Mutable variables
            std::string givenName;
            int hpMax;
            int hp;
            std::vector<std::shared_ptr<entry::Item>> inventory;
            std::map<rules::Ability, int> stats;
            std::map<rules::Skill, int> skills;
            int proficiency;
            
            //Immutable variables
            //const std::string creatureName;
            const std::string size;
            //const std::string type;
            const std::string alignment;
            const int hdCount;
            const int hdSides;
            const std::string speed;
            const std::vector<rules::Ability> saves;
            const std::vector<std::string> senses;
            const std::string langs;
            const double cr;
            const std::string natArmorName;
            const int natArmorBonus;
            const std::vector<dmgType> dmgImmunities;
            const std::vector<dmgType> dmgResistances;
            const std::vector<dmgType> dmgVulnerabilities;
            const std::vector<dmgType> condImmunities;
            const std::vector<std::shared_ptr<entry::Feature>> features;

    };

    // Helper function to get all attacks (weapons and pseudo-weapons included)
    std::vector<std::shared_ptr<entry::Weapon>> getAttacks(const Creature& c);

    // Helper function to get the best ability for this creature (chooses arbitrarily in case of ties)
    rules::Ability getBestAbility(const std::vector<rules::Ability>& abilities, const Creature& c);
}