aboutsummaryrefslogtreecommitdiff
path: root/src/creature.cc
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-04-19 13:46:14 -0400
committerYour Name <you@example.com>2021-04-19 13:46:14 -0400
commit2cae1aa33f80ce0844fb54a84ce103146a7fe7ad (patch)
tree26945223a2672c5a80975a7b1998622da6edcc28 /src/creature.cc
parent4618763c0e3a723bf4bb43c7b9edbce87240e0af (diff)
downloaddmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.tar.gz
dmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.tar.bz2
dmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.zip
Started earnest work on cli
Diffstat (limited to 'src/creature.cc')
-rw-r--r--src/creature.cc118
1 files changed, 92 insertions, 26 deletions
diff --git a/src/creature.cc b/src/creature.cc
index 13b54d3..53a4994 100644
--- a/src/creature.cc
+++ b/src/creature.cc
@@ -7,13 +7,15 @@
#include "armor.h"
#include <algorithm>
#include <iostream>
+#include <sstream>
+#include <iomanip>
typedef nlohmann::json json;
using namespace std;
namespace creature {
Creature::Creature(const json& data)
- : inventory(json2ptrvec<entry::Item>(data["inventory"])), creatureName(data["name"]), size(data["size"]), type(data["type"]), alignment(data["alignment"]), hdCount(data["hit_die_count"]), hdSides(data["hit_die_sides"]), speed(data["speed"]), stats(data["stats"]), skills(data["skills"]), saves(data["saves"]), langs(data["langs"]), cr(data["cr"]), proficiency(data["prof"]), dmgImmunities(json2vec<dmgType>(data["d_immunities"])), dmgResistances(json2vec<dmgType>(data["d_resistances"])), dmgVulnerabilities(json2vec<dmgType>(data["d_vulnerabilities"])), condImmunities(json2vec<dmgType>(data["c_immunities"])), features(json2ptrvec<entry::Feature>(data["features"]))
+ : inventory(json2ptrvec<entry::Item>(data["inventory"])), stats(data["stats"]), skills(data["skills"]), creatureName(data["name"]), size(data["size"]), type(data["type"]), alignment(data["alignment"]), hdCount(data["hit_die_count"]), hdSides(data["hit_die_sides"]), speed(data["speed"]), saves(data["saves"]), langs(data["langs"]), cr(data["cr"]), proficiency(data["prof"]), dmgImmunities(json2vec<dmgType>(data["d_immunities"])), dmgResistances(json2vec<dmgType>(data["d_resistances"])), dmgVulnerabilities(json2vec<dmgType>(data["d_vulnerabilities"])), condImmunities(json2vec<dmgType>(data["c_immunities"])), features(json2ptrvec<entry::Feature>(data["features"]))
{
// Initialize names and hp
if(((map<string, json>) data).contains("givenName")) {
@@ -37,32 +39,32 @@ namespace creature {
}
return ret;
}
-
+
nlohmann::json Creature::toJson() const {
return nlohmann::json({
- {"name", creatureName},
- {"size", size},
- {"type", type},
- {"alignment", alignment},
- {"hit_die_count", hdCount},
- {"hit_die_sides", hdSides},
- {"speed", speed},
- {"stats", stats},
- {"skills", skills},
- {"saves", saves},
- {"langs", langs},
- {"cr", cr},
- {"prof", proficiency},
- {"d_immunities", dmgImmunities},
- {"d_resistances", dmgResistances},
- {"d_vulnerabilities", dmgVulnerabilities},
- {"c_immunities", condImmunities},
- {"givenName", givenName},
- {"hpMax", hpMax},
- {"hp", hp},
- {"inventory", getJsonVectP(inventory)},
- {"features", getJsonVectP(features)}
- });
+ {"name", creatureName},
+ {"size", size},
+ {"type", type},
+ {"alignment", alignment},
+ {"hit_die_count", hdCount},
+ {"hit_die_sides", hdSides},
+ {"speed", speed},
+ {"stats", stats},
+ {"skills", skills},
+ {"saves", saves},
+ {"langs", langs},
+ {"cr", cr},
+ {"prof", proficiency},
+ {"d_immunities", dmgImmunities},
+ {"d_resistances", dmgResistances},
+ {"d_vulnerabilities", dmgVulnerabilities},
+ {"c_immunities", condImmunities},
+ {"givenName", givenName},
+ {"hpMax", hpMax},
+ {"hp", hp},
+ {"inventory", getJsonVectP(inventory)},
+ {"features", getJsonVectP(features)}
+ });
}
// True if type without matching qualifiers is in subdata
@@ -94,7 +96,7 @@ namespace creature {
}
int Creature::getSkillBonus(const string& skill) const {
- int bonus = this->getBonus(skill2ability[skill]);
+ int bonus = this->getBonus(rules::skill2ability[skill]);
if(skills.contains(skill)) {
bonus += skills.at(skill) * getProficiency();
}
@@ -138,6 +140,14 @@ namespace creature {
return s;
}
+ void Creature::setScore(const string& ability, int score) {
+ stats[ability] = score;
+ }
+
+ void Creature::setProfLevel(const string& skill, int level) {
+ skills[skill] = level;
+ }
+
const int getAC(const Creature& c) {
int baseBonus = 10 + c.getBonus("dex");
int miscBonus = 0;
@@ -159,4 +169,60 @@ namespace creature {
}
return baseBonus + miscBonus;
}
+
+ template<typename T> vector<string> mapItems(const vector<shared_ptr<T>>& items) {
+ vector<string> out;
+ for(auto i : items) {
+ out.push_back(i->getName());
+ }
+ return out;
+ }
+
+ string genText(const Creature& c) {
+ stringstream text;
+ text << c.getGivenName() << " (" << c.getCreatureName() << "): " << c.getHP() << "/" << c.getHPMax() << " hp, " << getAC(c) << " ac";
+ string armor = utils::join(mapItems(creature::getItems<entry::Armor>(c)), ", ");
+ if(! armor.empty()) {
+ text << " (" << armor << ")";
+ }
+ text << ", speed " << c.getSpeed() << "\n";
+ text << "A cr " << c.getCR() << " " << c.getAlignment() << " " << c.getSize() << " " << c.getType() << ".\n";
+ text << "Stats:\n";
+ //text << setiosflags(ios::fixed) << setw(6);
+ for(auto ability : rules::abilities) {
+ text << " " << setw(6) << std::left << ability;
+ }
+ text << "\n";
+ for(auto ability : rules::abilities) {
+ text << setw(7) << std::left << (to_string(c.getScore(ability)) + "(" + to_string(c.getBonus(ability)) + ")");
+ }
+ text << "\n";
+ text << "Senses: ";
+ if(! c.getSenses().empty()) {
+ text << utils::join(c.getSenses(), ", ") << ". ";
+ }
+ text << "Passive Perception " << 10 + c.getBonus("wis") << "\n";
+ if(! c.getLanguages().empty()) {
+ text << "Languages: " << c.getLanguages() << "\n";
+ }
+
+ text << "\nSkills:\n";
+ for(auto skill : c.getSkills()) {
+ text << skill.first << " (+" << skill.second << ")\n";
+ }
+ text << "\nSaves:\n";
+ for(auto save : c.getSaves()) {
+ text << save.first << " (+" << save.second << ")\n";
+ }
+ text << "\nFeatures:\n";
+ for(auto f: c.getFeatures()) {
+ text << f->getText(c) << "\n";
+ }
+ text << "\nInventory:\n";
+ for(auto i : c.getInventory()) {
+ text << i->getText(c) << "\n";
+ }
+
+ return text.str();
+ }
}