diff options
author | Your Name <you@example.com> | 2021-05-09 13:56:46 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-09 13:56:46 -0400 |
commit | d13358b71ec15085f2638fd9c3fc634df62dfc94 (patch) | |
tree | 467c643a068bf2d83da3632823a6434244ae004e /src/creature.cc | |
parent | e3aaa68a2ea1a403256150121c57a0287014162f (diff) | |
download | dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.tar.gz dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.tar.bz2 dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.zip |
Reduced dependency on json-related hacks
Diffstat (limited to 'src/creature.cc')
-rw-r--r-- | src/creature.cc | 120 |
1 files changed, 56 insertions, 64 deletions
diff --git a/src/creature.cc b/src/creature.cc index dcd2324..d4cf8c4 100644 --- a/src/creature.cc +++ b/src/creature.cc @@ -16,16 +16,8 @@ typedef nlohmann::json json; using namespace std; namespace creature { - template<typename T> map<T, int> makeMap(map<string, int> src) { - map<T, int> ret; - for(auto& [abilityStr, val] : src) { - ret.insert({T(abilityStr), val}); - } - return ret; - } - Creature::Creature(const json& data, const json& base) - : Entry(base), inventory(utils::json2ptrvec<entry::Item>(data["inventory"])), stats(makeMap<rules::Ability>(data["stats"])), skills(makeMap<rules::Skill>(data["skills"])), proficiency(data["prof"]), size(data["size"]), alignment(data["alignment"]), hdCount(data["hit_die_count"]), hdSides(data["hit_die_sides"]), speed(data["speed"]), saves(utils::json2vec<rules::Ability>(data["saves"])), langs(data["langs"]), cr(data["cr"]), natArmorName(data["natural_armor"]["name"]), natArmorBonus(data["natural_armor"]["bonus"]), dmgImmunities(utils::json2vec<dmgType>(data["d_immunities"])), dmgResistances(utils::json2vec<dmgType>(data["d_resistances"])), dmgVulnerabilities(utils::json2vec<dmgType>(data["d_vulnerabilities"])), condImmunities(utils::json2vec<dmgType>(data["c_immunities"])), features(utils::json2ptrvec<entry::Feature>(data["features"])) + : Entry(base), inventory(data["inventory"]), stats(data["stats"]), skills(data["skills"]), prof(data["prof"]), size(data["size"]), alignment(data["alignment"]), hit_die_count(data["hit_die_count"]), hit_die_sides(data["hit_die_sides"]), speed(data["speed"]), saves(data["saves"]), langs(data["langs"]), cr(data["cr"]), observant(data["observant"]), natural_armor(data["natural_armor"]), d_immunities(data["d_immunities"]), d_resistances(data["d_resistances"]), d_vulnerabilities(data["d_vulnerabilities"]), c_immunities(data["c_immunities"]), features(data["features"]) { // Initialize names and hp if(((map<string, json>) data).contains("givenName")) { @@ -34,9 +26,9 @@ namespace creature { hp = data["hp"]; } else { givenName = "Jerry"; //TODO: Autogenerate - hpMax = this->getBonus(rules::Ability::Con()) * hdCount; - for(int i = 0; i < hdCount; i++) { - hpMax += dice::roll(hdSides); + hpMax = this->getBonus(rules::Ability::Con()) * hit_die_count; + for(int i = 0; i < hit_die_count; i++) { + hpMax += dice::roll(hit_die_sides); } hp = hpMax; } @@ -46,26 +38,26 @@ namespace creature { nlohmann::json data = Entry::toJson(); data["size"] = size; data["alignment"] = alignment; - data["hit_die_count"] = hdCount; - data["hit_die_sides"] = hdSides; + data["hit_die_count"] = hit_die_count; + data["hit_die_sides"] = hit_die_sides; data["speed"] = speed; data["stats"] = stats; data["skills"] = skills; data["saves"] = saves; data["langs"] = langs; data["cr"] = cr; - data["prof"] = proficiency; - data["natural_armor"]["name"] = natArmorName; - data["natural_armor"]["bonus"] = natArmorBonus; - data["d_immunities"] = dmgImmunities; - data["d_resistances"] = dmgResistances; - data["d_vulnerabilities"] = dmgVulnerabilities; - data["c_immunities"] = condImmunities; + data["observant"] = observant; + data["prof"] = prof; + data["natural_armor"] = natural_armor; + data["d_immunities"] = d_immunities; + data["d_resistances"] = d_resistances; + data["d_vulnerabilities"] = d_vulnerabilities; + data["c_immunities"] = c_immunities; data["givenName"] = givenName; data["hpMax"] = hpMax; data["hp"] = hp; - data["inventory"] = utils::ptrvec2json(inventory); - data["features"] = utils::ptrvec2json(features); + data["inventory"] = inventory; + data["features"] = features; return data; } @@ -86,10 +78,10 @@ namespace creature { } void Creature::applyDamage(int amount, const string& type, const vector<rules::Qualifier>& qualifiers) { - if(! conditionApplies(type, qualifiers, dmgImmunities)) { - if(conditionApplies(type, qualifiers, dmgResistances)) { + if(! conditionApplies(type, qualifiers, getDmgImmunities())) { + if(conditionApplies(type, qualifiers, getDmgResistances())) { hp -= amount / 2; - } else if(conditionApplies(type, qualifiers, dmgVulnerabilities)) { + } else if(conditionApplies(type, qualifiers, getDmgVulnerabilities())) { hp -= amount * 2; } else { hp -= amount; @@ -190,8 +182,8 @@ namespace creature { stats[ability] = score; if(ability == rules::Ability::Con()) { int delta = getBonus(ability) - initBonus; - hpMax += delta * hdCount; - hp += delta * hdCount; + hpMax += delta * hit_die_count; + hp += delta * hit_die_count; } } @@ -208,8 +200,8 @@ namespace creature { const int getAC(const Creature& c) { auto natArmor = c.getNaturalArmor(); - if(! natArmor.first.empty()) { - return natArmor.second; + if(! natArmor.name.empty()) { + return natArmor.bonus; } int dex = c.getBonus(rules::Ability::Dex()); int baseBonus = 10 + dex; @@ -271,19 +263,19 @@ namespace creature { return title + ": " + utils::join(dmgTypes2text(dmg), "; "); } - string genText(const Creature& c) { + string Creature::getText() const { stringstream text; - text << c.getGivenName() << " (" << c.getCreatureName() << "): " << c.getHP() << "/" << c.getHPMax() << " hp, " << getAC(c) << " ac"; - if(! c.getNaturalArmor().first.empty()) { - text << " (" << c.getNaturalArmor().first << ")"; + text << getGivenName() << " (" << getCreatureName() << "): " << getHP() << "/" << getHPMax() << " hp, " << getAC(*this) << " ac"; + if(! getNaturalArmor().name.empty()) { + text << " (" << getNaturalArmor().name << ")"; } else { - string armor = utils::join(mapItems(utils::castPtrs<entry::Item, entry::Armor>(c.getInventory())), ", "); + string armor = utils::join(mapItems(utils::castPtrs<entry::Item, entry::Armor>(getInventory())), ", "); if(! armor.empty()) { text << " (" << armor << ")"; } } - text << ", speed " << c.getSpeed() << endl; - text << "A cr " << c.getCR() << " " << c.getAlignment() << " " << c.getSize() << " " << c.getType() << "." << endl; + text << ", speed " << getSpeed() << endl; + text << "A cr " << getCR() << " " << getAlignment() << " " << getSize() << " " << getType() << "." << endl; text << "Stats:" << endl; using namespace rules; vector<rules::Ability> abilities {Ability::Str(), Ability::Dex(), Ability::Con(), Ability::Int(), Ability::Wis(), Ability::Cha()}; @@ -292,56 +284,56 @@ namespace creature { } text << endl; for(auto ability : abilities) { - text << setw(7) << std::left << (to_string(c.getScore(ability)) + "(" + to_string(c.getBonus(ability)) + ")"); + text << setw(7) << std::left << (to_string(getScore(ability)) + "(" + to_string(getBonus(ability)) + ")"); } text << endl; text << "Senses: "; - if(! c.getSenses().empty()) { - text << utils::join(c.getSenses(), ", ") << ". "; + if(! getSenses().empty()) { + text << utils::join(getSenses(), ", ") << ". "; } - text << "Passive Perception " << 10 + c.getSkillBonus(rules::Skill::Perception()) << endl; - if(! c.getLanguages().empty()) { - text << "Languages: " << c.getLanguages() << endl; + text << "Passive Perception " << 10 + getSkillBonus(rules::Skill::Perception()) << endl; + if(! getLanguages().empty()) { + text << "Languages: " << getLanguages() << endl; } - text << "Proficiency: " << c.getProficiency() << endl; - if(! c.getSkills().empty()) { + text << "Proficiency: " << getProficiency() << endl; + if(! getSkills().empty()) { text << endl << "Skills:" << endl; - for(auto skill : c.getSkills()) { + for(auto skill : getSkills()) { text << " * " << skill.first.getName() << " (+" << skill.second << ")" << endl; } } - if(! c.getSaves().empty()) { + if(! getSaves().empty()) { text << endl << "Saves:" << endl; - for(auto save : c.getSaves()) { + for(auto save : getSaves()) { text << " * " << save.first.getAbbrev() << " (+" << save.second << ")" << endl; } } - if(! c.getDmgImmunities().empty()) { - text << formatDmgTypeVector("\nDamage Immunities", c.getDmgImmunities()) << endl; + if(! getDmgImmunities().empty()) { + text << formatDmgTypeVector("\nDamage Immunities", getDmgImmunities()) << endl; } - if(! c.getDmgResistances().empty()) { - text << formatDmgTypeVector("\nDamage Resistances", c.getDmgResistances()) << endl; + if(! getDmgResistances().empty()) { + text << formatDmgTypeVector("\nDamage Resistances", getDmgResistances()) << endl; } - if(! c.getDmgVulnerabilities().empty()) { - text << formatDmgTypeVector("\nDamage Vulnerabilities", c.getDmgVulnerabilities()) << endl; + if(! getDmgVulnerabilities().empty()) { + text << formatDmgTypeVector("\nDamage Vulnerabilities", getDmgVulnerabilities()) << endl; } - if(! c.getCondImmunities().empty()) { - text << formatDmgTypeVector("\nCondition Immunities", c.getCondImmunities()) << endl; + if(! getCondImmunities().empty()) { + text << formatDmgTypeVector("\nCondition Immunities", getCondImmunities()) << endl; } - if(! c.getFeatures().empty()) { + if(! getFeatures().empty()) { text << endl << "Features:" << endl; - for(auto f: c.getFeatures()) { - text << " * " << f->getText(c) << endl; + for(auto f: getFeatures()) { + text << " * " << f->getText(*this) << endl; } } - if(! c.getInventory().empty()) { + if(! getInventory().empty()) { text << endl << "Inventory:" << endl; - for(auto i : c.getInventory()) { - text << " * " << i->getText(c) << endl; + for(auto i : getInventory()) { + text << " * " << i->getText(*this) << endl; } } - if(! c.Entry::getText().empty()) { - text << endl << c.Entry::getText() << endl; + if(! Entry::getText().empty()) { + text << endl << Entry::getText() << endl; } return text.str(); |