diff options
author | Your Name <you@example.com> | 2021-05-02 10:12:49 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-02 10:12:49 -0400 |
commit | 77b43093c7b157e8ff0b68af4efd0eff226da35a (patch) | |
tree | af051e78ebe6c26ea373b50fb266935e0b51f9fa /src | |
parent | 9e6936d325751a7e0670e8cc6aa61faf048cb241 (diff) | |
download | dmtool-77b43093c7b157e8ff0b68af4efd0eff226da35a.tar.gz dmtool-77b43093c7b157e8ff0b68af4efd0eff226da35a.tar.bz2 dmtool-77b43093c7b157e8ff0b68af4efd0eff226da35a.zip |
Added damage and heal commands
Diffstat (limited to 'src')
-rw-r--r-- | src/creature.cc | 13 | ||||
-rw-r--r-- | src/creature.h | 1 | ||||
-rw-r--r-- | src/dmtool.cc | 69 | ||||
-rw-r--r-- | src/rules.cc | 10 | ||||
-rw-r--r-- | src/rules.h | 3 | ||||
-rw-r--r-- | src/utils.h | 2 |
6 files changed, 89 insertions, 9 deletions
diff --git a/src/creature.cc b/src/creature.cc index 88b26e7..e1f2b61 100644 --- a/src/creature.cc +++ b/src/creature.cc @@ -104,6 +104,13 @@ namespace creature { } } + void Creature::applyHealing(int amount) { + hp += amount; + if(hp > hpMax) { + hp = hpMax; + } + } + int Creature::getSkillBonus(const rules::Skill& skill) const { int bonus = this->getBonus(skill.getAbility()); if(skills.contains(skill)) { @@ -150,7 +157,13 @@ namespace creature { } void Creature::setScore(const rules::Ability& ability, int score) { + int initBonus = getBonus(ability); stats.insert({ability, score}); + if(ability == rules::Ability::Con()) { + int delta = getBonus(ability) - initBonus; + hpMax += delta * hdCount; + hp += delta * hdCount; + } } void Creature::setProfLevel(const rules::Skill& skill, int level) { diff --git a/src/creature.h b/src/creature.h index e89978e..ee4d5ba 100644 --- a/src/creature.h +++ b/src/creature.h @@ -84,6 +84,7 @@ namespace creature { // Setters (mutators) void setGivenName(std::string name) {givenName = name;} void applyDamage(int amount, const std::string& type, const std::vector<std::string>& qualifiers); + void applyHealing(int amount); void setScore(const rules::Ability& ability, int score); void setProfLevel(const rules::Skill& skill, int level); void addInventoryItem(std::shared_ptr<entry::Item> item); diff --git a/src/dmtool.cc b/src/dmtool.cc index c644026..18af9e8 100644 --- a/src/dmtool.cc +++ b/src/dmtool.cc @@ -161,18 +161,12 @@ void cp(fs::path src, fs::path dest) { } void cp(std::vector<std::string> args) { - if(args.size() != 2) { - throw std::runtime_error("Subcommand 'cp' expected 2 arguments but got " + std::to_string(args.size())); - } // Operate by intantiating and saving // We do recursive! cp(getTruePath(args[0]), getTruePath(args[1])); } void mv(std::vector<std::string> args) { - if(args.size() != 2) { - throw std::runtime_error("Subcommand 'mv' expected 2 arguments but got " + std::to_string(args.size())); - } fs::rename(getTruePath(args[0]), getTruePath(args[1])); } @@ -218,12 +212,70 @@ void roll(std::vector<std::string> args) { } } -void damage(std::vector<std::string> args) {} -void heal(std::vector<std::string> args) {} +void damage(std::vector<std::string> args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr<entry::Entry> e = instantiate(p); + std::shared_ptr<creature::Creature> c = std::dynamic_pointer_cast<creature::Creature>(e); + if(! c) { + throw std::runtime_error("Subcommand 'damage' expected a creature but was given an instance of " + e->getType()); + } + int dmg = 0; + try { + dmg = std::stoi(args[1]); + } catch(std::exception& e) { + throw std::runtime_error("Subcommand 'damage' expected an integer but was given " + args[1]); + } + std::string dmgType = "force"; + if(args.size() == 3) { + dmgType = args[2]; + } + std::vector<std::string> qualifiers; // TODO + int initHP = c->getHP(); + c->applyDamage(dmg, dmgType, qualifiers); + std::cout << "Applying " << dmg << " " << dmgType << " damage to " << c->getGivenName() << " the " << c->getCreatureName() << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl; + save(c, p); +} + +void heal(std::vector<std::string> args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr<entry::Entry> e = instantiate(p); + std::shared_ptr<creature::Creature> c = std::dynamic_pointer_cast<creature::Creature>(e); + if(! c) { + throw std::runtime_error("Subcommand 'heal' expected a creature but was given an instance of " + e->getType()); + } + int amnt = 0; + try { + amnt = std::stoi(args[1]); + } catch(std::exception& e) { + throw std::runtime_error("Subcommand 'heal' expected an integer but was given " + args[1]); + } + int initHP = c->getHP(); + c->applyHealing(amnt); + std::cout << "Healing " << c->getGivenName() << " the " << c->getCreatureName() << " by " << amnt << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl; + save(c, p); +} + + void reset(std::vector<std::string> args) {} void set(std::vector<std::string> args) {} void add(std::vector<std::string> args) {} +const std::map<std::string, std::vector<int>> nargs({ + {"cp", {2}}, + {"mv", {2}}, + {"damage", {2, 3}}, + {"heal", {2}} + }); + +void checkArgs(std::string cmd, std::vector<std::string> args) { + if(nargs.contains(cmd)) { + auto& allowed = nargs.at(cmd); + if(std::find(allowed.begin(), allowed.end(), args.size()) == allowed.end()) { + throw std::runtime_error("Subcommand '" + cmd + "' expected " + utils::join(allowed, ", ") + " arguments but got " + std::to_string(args.size())); + } + } +} + int main(int argc, char *argv[]) { std::string exename = argv[0]; std::vector<std::string> args(&argv[1], &argv[argc]); @@ -241,6 +293,7 @@ int main(int argc, char *argv[]) { std::vector<std::string> argsOrig(args); args.erase(args.begin()); try { + checkArgs(cmd, args); if(cmd == "ls") list(args); else if(cmd == "cp") cp(args); else if(cmd == "mkdir") mkdir(args); diff --git a/src/rules.cc b/src/rules.cc index a9ca395..15dcb34 100644 --- a/src/rules.cc +++ b/src/rules.cc @@ -30,4 +30,14 @@ namespace rules { {"Performance", "cha"}, {"Persuasion", "cha"} }; + + std::ostream& operator<<(std::ostream& os, const Ability& a) { + os << std::string(a); + return os; + } + + std::ostream& operator<<(std::ostream& os, const Skill& s) { + os << std::string(s); + return os; + } } diff --git a/src/rules.h b/src/rules.h index d49f17c..18cdef0 100644 --- a/src/rules.h +++ b/src/rules.h @@ -100,4 +100,7 @@ namespace rules { static const std::map<std::string, std::string> skill2ability; }; + + std::ostream& operator<<(std::ostream& os, const Ability& a); + std::ostream& operator<<(std::ostream& os, const Skill& s); } diff --git a/src/utils.h b/src/utils.h index e44bdf2..2a66101 100644 --- a/src/utils.h +++ b/src/utils.h @@ -44,7 +44,7 @@ namespace utils { template<typename Container> std::string join(Container parts, std::string joiner) { std::stringstream out; bool isFirst = true; - for(std::string p : parts) { + for(auto p : parts) { if(! isFirst) { out << joiner; } |