From 77b43093c7b157e8ff0b68af4efd0eff226da35a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 May 2021 10:12:49 -0400 Subject: Added damage and heal commands --- src/creature.cc | 13 +++++++++++ src/creature.h | 1 + src/dmtool.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++------- src/rules.cc | 10 +++++++++ src/rules.h | 3 +++ src/utils.h | 2 +- 6 files changed, 89 insertions(+), 9 deletions(-) (limited to 'src') 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& 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 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 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 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 args) { } } -void damage(std::vector args) {} -void heal(std::vector args) {} +void damage(std::vector args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr e = instantiate(p); + std::shared_ptr c = std::dynamic_pointer_cast(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 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 args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr e = instantiate(p); + std::shared_ptr c = std::dynamic_pointer_cast(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 args) {} void set(std::vector args) {} void add(std::vector args) {} +const std::map> nargs({ + {"cp", {2}}, + {"mv", {2}}, + {"damage", {2, 3}}, + {"heal", {2}} + }); + +void checkArgs(std::string cmd, std::vector 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 args(&argv[1], &argv[argc]); @@ -241,6 +293,7 @@ int main(int argc, char *argv[]) { std::vector 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 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 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; } -- cgit v1.2.3