1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
#include "cmd.h"
#include "../utils.h"
#include "../creature.h"
#include "../item.h"
#include "../spellcasting.h"
#include "../settings.h"
#include "../weapon.h"
#include "../dice.h"
#include "../armor.h"
#include <sstream>
#include <memory>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
namespace fs = std::filesystem;
namespace cmd {
// Call after applying to format printing
std::string formatHealingDamage(const std::shared_ptr<creature::Creature>& c, int initHP, bool heal, int amnt, const std::string& dmgType, const std::vector<rules::Qualifier>& qualifiers) {
std::stringstream text;
text << (heal? "Healing " : "Damaging ") << c->getGivenName() << " the " << c->getCreatureName() << " by " << amnt;
if(! heal) {
std::string qualsString;
std::vector<std::string> positiveQuals;
for(auto qual : qualifiers) {
positiveQuals.push_back(qual.getPositive());
}
if(! positiveQuals.empty()) {
qualsString = " " + utils::join(positiveQuals, ", ");
}
text << qualsString << " " << dmgType << " damage";
}
text << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl;
return text.str();
}
std::string healOrDamageProgrammatic(fs::path p, bool heal, int amnt, std::string dmgType, const std::vector<rules::Qualifier>& qualifiers) {
auto c = utils::instantiate<creature::Creature>(p);
int initHP = c->getHP();
if(heal) {
c->applyHealing(amnt);
} else {
c->applyDamage(amnt, dmgType, qualifiers);
}
utils::saveJson(c->serialize(), p);
return formatHealingDamage(c, initHP, heal, amnt, dmgType, qualifiers);
}
std::string healOrDamage(bool heal, std::vector<std::string> args, std::map<std::string, std::string> flags) {
auto qualifiers = parseQualifiers(flags);
fs::path p = getTruePath(args[0]);
int amnt = utils::parseInt(args[1]);
std::string dmgType = "force";
if(args.size() == 3) {
dmgType = args[2];
}
return healOrDamageProgrammatic(p, heal, amnt, dmgType, qualifiers);
}
std::string attack(std::vector<std::string> args, std::map<std::string, std::string> flags) {
std::stringstream text;
bool is2h = flags.find("2") != flags.end();
bool is1h = flags.find("1") != flags.end();
if(is2h and is1h) {
text << "ERROR: Cannot be both 1 handed and 2 handed!" << std::endl;
return text.str();
}
auto c1 = utils::instantiate<creature::Creature>(getTruePath(args[0]));
args.erase(args.begin());
fs::path p2 = getTruePath(args.back());
auto c2 = utils::instantiate<creature::Creature>(p2);
args.erase(args.end()-1);
std::string attackName = utils::join(args, " ");
utils::lower(attackName);
std::shared_ptr<entry::Weapon> w;
for(auto weap : creature::getAttacks(*c1)) {
if(weap->getName() == attackName) {
w = weap;
break;
}
}
text << w->getText(*c1) << std::endl;
int rolled = dice::roll(20);
int bonus = w->getToHitBonus(*c1);
text << formatRoll(w->getName(), "attack", rolled, bonus);
int ac = creature::getAC(*c2);
if(rolled + bonus >= ac) {
text << " Hit (" << (rolled + bonus) << " to hit >= " << ac << " ac): ";
bool wants2h = true;
for(auto a : utils::castPtrs<entry::Item, entry::Armor>(c1->getInventory())) {
if(a->getArmorType() == "shield") {
wants2h = false;
}
}
if(is2h) {
wants2h = true;
} else if(is1h) {
wants2h = false;
}
auto dmg = entry::rollDmg(*w, wants2h);
text << entry::formatDmg(*w, *c1, dmg) << std::endl;
bool isFirst = true;
for(auto d : dmg) {
int amnt = d.rolled;
if(isFirst) {
amnt += w->getDamageBonus(*c1);
isFirst = false;
}
text << " " << healOrDamageProgrammatic(p2, false, amnt, d.dmg_type, {});
}
} else {
text << " Miss (" << (rolled + bonus) << " to hit < " << ac << " ac)" << std::endl;
}
return text.str();
}
std::string heal(std::vector<std::string> args) {
return healOrDamage(true, args, {});
}
std::string damage(std::vector<std::string> args, std::map<std::string, std::string> flags) {
return healOrDamage(false, args, flags);
}
std::string save(std::vector<std::string> args, std::map<std::string, std::string> flags) {
if(args.size() < 3) {
throw std::runtime_error("Subcommand 'save' requires at least 3 arguments");
}
std::stringstream text;
rules::Ability ability = rules::tryGetAbilityOrSkill<rules::Ability>(args[0]);
if(! ability) {
throw std::runtime_error("Requires a valid ability name but received \"" + args[0] + "\".");
}
args.erase(args.begin());
int DC = utils::parseInt(args[0]);
args.erase(args.begin());
// Now iterate over the paths
for(std::string s : args) {
fs::path p = getTruePath(s);
auto c = utils::instantiate<creature::Creature>(p);
int initHP = c->getHP();
int rolled = dice::roll(20);
int bonus = c->getAbilitySaveBonus(ability);
int damage = 0;
std::string type = "force";
bool halves = flags.find("halves") != flags.end();
if(flags.find("damage") != flags.end()) {
damage = utils::parseInt(flags.at("damage"));
if(flags.find("type") != flags.end()) type = flags.at("type");
auto qualifiers = parseQualifiers(flags);
rolled = c->saveOrDamage(ability, DC, damage, type, qualifiers, halves);
rolled -= bonus; // It's combined in creature
}
bool passed = rolled + bonus >= DC;
text << c->getName() << " " << (passed? "PASS" : "FAIL") << ": ";
text << formatRoll(ability.getFull(), "save", rolled, bonus);
if(flags.find("damage") != flags.end() and (halves or ! passed)) {
text << formatHealingDamage(c, initHP, false, damage, type, parseQualifiers(flags));
}
utils::saveJson(c->serialize(), p);
}
return text.str();
}
std::string reset(std::vector<std::string> args) {
for(std::string s : args) {
fs::path p = getTruePath(s);
auto c = utils::instantiate<creature::Creature>(p);
c->longRest();
utils::saveJson(c->serialize(), p);
}
return "";
}
std::string set(std::vector<std::string> args) {
if(args.size() < 3) {
throw std::runtime_error("Subcommand 'set' requires at least 3 arguments");
}
fs::path p = getTruePath(args[0]);
std::stringstream text;
args.erase(args.begin()); // remove path from args
if(args[0] == "name") {
args.erase(args.begin()); // remove "name" from args
auto newname = utils::join(args, " ");
auto e = utils::instantiate<entry::Entry>(p);
if(e->getEntryType() == "creatures") { // creature, we should do given name instead
auto c = utils::instantiate<creature::Creature>(p);
c->setGivenName(newname);
text << "Set the given name of the " << c->getName() << " to " << c->getGivenName() << std::endl;
utils::saveJson(c->serialize(), p);
} else { // Standard entry, no given name so we set name
e->setName(newname);
text << "Set the name of the " << e->getType() << " to " << e->getName() << std::endl;
utils::saveJson(e->serialize(), p);
}
} else if(args[0] == "proficiency") {
auto c = utils::instantiate<creature::Creature>(p);
c->setProficiency(utils::parseInt(args[1]));
text << "Set the proficiency of " << c->getGivenName() << " the " << c->getName() << " to " << c->getProficiency() << std::endl;
utils::saveJson(c->serialize(), p);
} else if(args[0] == "cost") {
auto i = utils::instantiate<entry::Item>(p);
i->setCost(utils::parseInt(args[1]));
text << "Set the cost of the " << i->getType() << " " << i->getName() << " to " << utils::getCostString(i->getCost()) << std::endl;
utils::saveJson(i->serialize(), p);
} else if(args[0] == "weight") {
auto i = utils::instantiate<entry::Item>(p);
i->setWeight(utils::parseDouble(args[1]));
text << "Set the weight of the " << i->getType() << " " << i->getName() << " to " << i->getWeight() << " lbs" << std::endl;
utils::saveJson(i->serialize(), p);
} else {
auto c = utils::instantiate<creature::Creature>(p);
// Either an ability or a skill. If skill, then it could be multiple words long.
std::string toSet = args.back();
args.erase(--args.end());
std::string abilityOrSkill = utils::join(args, " ");
rules::Skill skill = rules::tryGetAbilityOrSkill<rules::Skill>(abilityOrSkill);
rules::Ability ability = rules::tryGetAbilityOrSkill<rules::Ability>(abilityOrSkill);
if(skill) {
// ensure lower case
utils::lower(toSet);
int level = -1;
if(toSet == "none") {
level = 0;
text << "Removed proficiency in " << skill << " for " << c->getGivenName() << " the " << c->getName() << std::endl;
} else if(toSet == "proficient") {
level = 1;
text << "Granted proficiency in " << skill << " for " << c->getGivenName() << " the " << c->getName() << std::endl;
} else if(toSet == "expert") {
level = 2;
text << "Granted expertise in " << skill << " for " << c->getGivenName() << " the " << c->getName() << std::endl;
}
if(level == -1) {
throw std::runtime_error("Skill levels can be set to none, proficient, or expert, but " + toSet + " was given.");
}
c->setProfLevel(skill, level);
} else if(ability) {
if(toSet == "proficient") {
c->setProficientSave(ability);
text << "Granted proficiency in " << ability << " saves for " << c->getGivenName() << " the " << c->getName() << std::endl;
} else if(toSet == "none") {
c->removeProficientSave(ability);
text << "Removed proficiency in " << ability << " saves for " << c->getGivenName() << " the " << c->getName() << std::endl;
} else {
c->setScore(ability, utils::parseInt(toSet));
text << "Set " << ability << " score to " << c->getScore(ability) << " for " << c->getGivenName() << " the " << c->getName() << std::endl;
}
} else {
throw std::runtime_error("Subcommand 'set' expected an ability, skill, proficiency, or name field to set, but was given " + abilityOrSkill);
}
utils::saveJson(c->serialize(), p);
}
return text.str();
}
std::string add(std::vector<std::string> args) {
std::stringstream text;
fs::path p = getTruePath(args[0]);
args.erase(args.begin()); // remove path from args
auto c = utils::instantiate<creature::Creature>(p);
std::string addName = utils::join(args, " ");
std::shared_ptr<entry::Entry> ent;
fs::path path = getTruePath(addName);
if(fs::directory_entry(path).exists()) {
ent = utils::instantiate<entry::Entry>(path);
} else {
ent = entry::Entry::create(utils::findByName(addName));
}
if(! creature::addEntry(ent, *c)) {
throw std::runtime_error("Could not add the " + ent->getType() + " " + ent->getName() + " to " + c->getGivenName() + " the " + c->getName() + ": Requires a weapon, armor, or spell, but received object of type " + ent->getType());
}
utils::saveJson(c->serialize(), p);
text << "Added the " << ent->getType() << " " << ent->getName() << " to " << c->getGivenName() << " the " << c->getName() << std::endl;
return text.str();
}
std::string del(std::vector<std::string> args) {
std::stringstream text;
fs::path p = getTruePath(args[0]);
args.erase(args.begin()); // remove path from args
auto c = utils::instantiate<creature::Creature>(p);
//Atempt to load the item if it's a path
std::string itemName = utils::join(args, " ");
try {
auto i = utils::instantiate<entry::Entry>(getTruePath(itemName));
if(i) {
itemName = i->getName();
}
} catch(std::exception& e) {} // eat.
auto removed = creature::removeEntry(itemName, *c);
utils::saveJson(c->serialize(), p);
if(removed) {
text << "Successfully removed the " << removed->Entry::getType() << " " << removed->getName() << std::endl;
} else {
text << "Could not find any entry by that name" << std::endl;
}
return text.str();
}
std::string edit(std::vector<std::string> args) {
auto p = getTruePath(args[0]);
auto e = utils::instantiate<entry::Entry>(p);
auto editor = settings::getString("editor");
// General workflow: copy notes (text) from e to a temp file, edit it, then copy back.
fs::path tmp("/tmp/dmtool.tmp");
std::ofstream out(tmp);
out << e->Entry::getText();
out.close();
std::system((editor + " " + tmp.string()).c_str());
std::ifstream in(tmp);
std::string newText(std::istreambuf_iterator<char>{in}, {});
e->setText(newText);
utils::saveJson(e->serialize(), p);
return "";
}
std::string spellcasting(std::vector<std::string> args) {
std::stringstream text;
auto p = getTruePath(args[0]);
auto c = utils::instantiate<creature::Creature>(p);
auto subcommand = args[1];
if(subcommand != "init" && subcommand != "ability" && subcommand != "level") {
throw std::runtime_error("Unknown option \"" + subcommand + "\"");
}
if(subcommand == "init") {
c->addSpellcasting();
} else {
auto sc = c->getSpellcasting();
if(! sc) {
throw std::runtime_error("Creature " + c->getName() + " has no spellcasting");
}
text << "Added spellcasting to " << c->getName() << std::endl;
if(subcommand == "ability") {
if(args.size() != 3) {
throw std::runtime_error("Subcommand \"spellcasting ability\" requires an additional parameter, but none was given");
}
sc->setAbility(args[2]);
text << "Set " << c->getName() << " spellcasting ability to " << args[2] << std::endl;
} else { // subcommand == "level"
if(args.size() != 4) {
throw std::runtime_error("Subcommand \"spellcasting level\" requires more parameters");
}
int level = utils::parseInt(args[2]);
int slots = utils::parseInt(args[3]);
if(level <= 0 || slots < 0) {
throw std::runtime_error("Spellcasting target out of range");
}
while(sc->getSlotLevels().size() <= (std::size_t) level) {
sc->addSlotLevel();
}
sc->getSlotLevels()[level]->numSlots = slots;
text << "Gave " << c->getName() << " " << slots << " " << utils::toOrdinal(level) << " level spell slots" << std::endl;
}
}
utils::saveJson(c->serialize(), p);
return text.str();
}
std::string create(std::vector<std::string> args) {
auto p = getTruePath(args[0]);
args.erase(args.begin()); // remove path from args
auto name = args[1];
if(args[0] == "item") {
nlohmann::json j = entry::Entry("item", name, "item", "");
j["cost"] = 0;
j["weight"] = 0.0;
utils::saveJson(j, p);
} else if(args[0] == "weapon" or args[0] == "feature_attack") {
nlohmann::json j = entry::Entry((args[0] == "weapon")? "item" : "feature", name, "weapons", "");
j["cost"] = 0;
j["weight"] = 0.0;
j["damage"] = {{{"dmg_die_count", 1}, {"dmg_die_sides", 6}, {"dmg_type", "bludgeoning"}, {"is_or", false}}};
j["properties"] = std::vector<std::string>();
j["weapon_type"] = "";
j["range"] = {0, 0};
j["reach"] = 5;
j["toHitOverride"] = {}; // Set to null
j["dmgBonusOverride"] = {};
j["abilityOverride"] = {};
utils::saveJson(j, p);
} else if(args[0] == "armor") {
nlohmann::json j = entry::Entry("item", name, "armor", "");
j["cost"] = 0;
j["weight"] = 0.0;
j["ac"] = 10;
j["strength"] = 0;
j["disadvantage"] = false;
j["armor_type"] = "light";
utils::saveJson(j, p);
} else if(args[0] == "spell") {
nlohmann::json j = entry::Entry("spells", name, "UNKNOWN", "");
j["level"] = 0;
j["classes"] = std::vector<std::string>();
j["casting_time"] = "1 action";
j["range"] = "Touch";
j["components"] = "V, S, M";
j["duration"] = "Instantaneous";
utils::saveJson(j, p);
} else if(args[0] == "feature") {
nlohmann::json j = entry::Entry("feature", name, "feature", "");
utils::saveJson(j, p);
} else if(args[0] == "creature") {
nlohmann::json j = entry::Entry("creatures", name, "UNKNOWN", "");
j["givenName"] = "NAME";
j["hpMax"] = j["hp"] = -1;
j["inventory"] = j["saves"] = j["senses"] = j["d_resistances"] = j["d_vulnerabilities"] = j["d_immunities"] = j["c_immunities"] = j["features"] = std::vector<std::string>();
j["skills"] = std::map<std::string, std::string>();
j["stats"] = {{"str", 10}, {"dex", 10}, {"con", 10}, {"int", 10}, {"wis", 10}, {"cha", 10}};
j["prof"] = 2;
j["size"] = "Medium";
j["alignment"] = "any alignment";
j["hit_die_count"] = 1;
j["hit_die_sides"] = 8;
j["speed"] = "30 ft.";
j["langs"] = "any one language (usually Common)";
j["cr"] = 0.0;
j["observant"] = false;
j["natural_armor"] = {{"name", ""}, {"bonus", 0}};
utils::saveJson(j, p);
}
return "Successfully created " + args[0] + " " + args[1] + ".\n";
}
std::string git(std::vector<std::string> args) {
std::string root = getTruePath("").string();
std::system(("cd " + root + " && " + utils::join(args, " ")).c_str());
return "";
}
}
|