diff options
author | Your Name <you@example.com> | 2021-05-06 14:13:28 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-06 14:13:28 -0400 |
commit | 9f3802690f9dd9452e96d1d7a879291978d66e35 (patch) | |
tree | 6d6c17b39abdb9490119241bc4fc061744b46d7d /src/cmd_query.cc | |
parent | 2a9f262e6db5906db445d465e500d7ba8c90fab3 (diff) | |
download | dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.gz dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.bz2 dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.zip |
Refactoring
Diffstat (limited to 'src/cmd_query.cc')
-rw-r--r-- | src/cmd_query.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/cmd_query.cc b/src/cmd_query.cc new file mode 100644 index 0000000..7d4e6d6 --- /dev/null +++ b/src/cmd_query.cc @@ -0,0 +1,49 @@ +#include "cmd.h" +#include "utils.h" +#include "creature.h" +#include "dice.h" +#include "weapon.h" +#include <sstream> + +namespace cmd { + std::string attacks(std::vector<std::string> args) { + std::stringstream text; + auto c = utils::instantiate<creature::Creature>(getTruePath(args[0])); + for(auto w : creature::getAttacks(*c)) { + text << w->getName() << std::endl; + } + return text.str(); + } + + std::string roll(std::vector<std::string> args) { + std::stringstream text; + auto c = utils::instantiate<creature::Creature>(getTruePath(args[0])); + args.erase(args.begin()); + std::string rollName = utils::join(args, " "); + utils::lower(rollName); + int rolled = dice::roll(20); + auto printResults = [&text](std::string name, std::string type, int rolled, int bonus) { + text << name << " " << type << ": " << rolled << " (d20) + " << bonus << " (" << name << " " << type << " bonus) = " << rolled + bonus << std::endl; + }; + // Search through skills, saves, and attacks to roll + rules::Skill skill = rules::tryGetAbilityOrSkill<rules::Skill>(rollName); + rules::Ability ability = rules::tryGetAbilityOrSkill<rules::Ability>(rollName); + if(skill) { + printResults(skill.getName(), "check", rolled, c->getSkillBonus(skill)); + } else if(ability) { + printResults(ability.getFull(), "save", rolled, c->getAbilitySaveBonus(ability)); + } else { + for(auto w : creature::getAttacks(*c)) { + if(w->getName() == rollName) { + text << w->getText(*c) << std::endl; + int abilityBonus = c->getBonus(creature::getBestAbility(getAbilityOptions(*w), *c)); + int bonus = abilityBonus + c->getProficiency(); + printResults(w->getName(), "attack", rolled, bonus); + text << " on hit: " << entry::formatDmg(*w, *c) << std::endl; + break; + } + } + } + return text.str(); + } +} |