aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYour Name <you@example.com>2022-08-09 16:57:53 -0400
committerYour Name <you@example.com>2022-08-09 16:57:53 -0400
commit5937d893da5656be3f486c863ae54e08bbe66579 (patch)
tree15aedaf0753fcf73fe85e90e88a7d0af33036eca /src
parent947d83c59ea86615e3a81a2ec122d843b5eceee9 (diff)
downloaddmtool-5937d893da5656be3f486c863ae54e08bbe66579.tar.gz
dmtool-5937d893da5656be3f486c863ae54e08bbe66579.tar.bz2
dmtool-5937d893da5656be3f486c863ae54e08bbe66579.zip
Added generic items, worked on features
Diffstat (limited to 'src')
-rw-r--r--src/armor.cc8
-rw-r--r--src/armor.h4
-rw-r--r--src/battlescape/battlescape.h50
-rw-r--r--src/features/feature.cc2
-rw-r--r--src/item.cc26
-rw-r--r--src/item.h18
-rw-r--r--src/settings.cc1
-rw-r--r--src/utils.h2
-rw-r--r--src/weapon.cc10
-rw-r--r--src/weapon.h4
10 files changed, 96 insertions, 29 deletions
diff --git a/src/armor.cc b/src/armor.cc
index 3a429b3..cb73cb1 100644
--- a/src/armor.cc
+++ b/src/armor.cc
@@ -13,10 +13,8 @@ namespace entry{
std::string armor_type;
int strength;
bool disadvantage;
- int cost;
- double weight;
};
- NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(armorImpl, ac, armor_type, strength, disadvantage, cost, weight);
+ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(armorImpl, ac, armor_type, strength, disadvantage);
NLOHMANN_FRIEND_DEFS(Item, Armor, data);
@@ -26,8 +24,6 @@ namespace entry{
std::string Armor::getArmorType(void) const {return data->armor_type;}
int Armor::getStrRequirement(void) const {return data->strength;}
bool Armor::stealthDisadvantage(void) const {return data->disadvantage;}
- int Armor::getCost(void) const {return data->cost;}
- double Armor::getWeight(void) const {return data->weight;}
string getTextHelper(const Armor& a, string dexBonusLight, string dexBonusMedium) {
stringstream text;
@@ -43,7 +39,7 @@ namespace entry{
if(a.stealthDisadvantage()) {
text << ", Imposes stealth disadvantage";
}
- text << ". " << a.Substantial::getText();
+ text << ". " << a.getCostWeightText();
return text.str();
}
diff --git a/src/armor.h b/src/armor.h
index c8bdb87..43bd002 100644
--- a/src/armor.h
+++ b/src/armor.h
@@ -5,15 +5,13 @@
namespace entry {
struct armorImpl;
- class Armor : public Item , public Substantial {
+ class Armor : public Item {
public:
Armor();
int getACBonus(void) const;
std::string getArmorType(void) const;
int getStrRequirement(void) const;
bool stealthDisadvantage(void) const;
- int getCost(void) const;
- double getWeight(void) const;
virtual std::string getText() const override;
virtual std::string getText(const creature::Creature& c) const override;
diff --git a/src/battlescape/battlescape.h b/src/battlescape/battlescape.h
new file mode 100644
index 0000000..a170ad3
--- /dev/null
+++ b/src/battlescape/battlescape.h
@@ -0,0 +1,50 @@
+#pragma once
+#include <vector>
+#include <memory>
+#include <string>
+#include <nlohmann/json.hpp>
+#include <tuple>
+#include <cstddef> // size_t
+#include "../entry.h"
+
+namespace battlescape {
+ struct battlescapeImpl;
+
+ struct Tile {
+ std::string name;
+ std::string description;
+ char look;
+ int color;
+ };
+ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Tile, name, description, look, color);
+
+ typedef std::tuple<std::size_t,std::size_t,std::size_t> coord3d;
+
+ class Battlescape {
+ public:
+ Battlescape();
+ // Also can be created programmatically
+ Battlescape(std::vector<std::vector<std::vector<Tile>>> terrain);
+
+ // Getters
+ std::map<std::shared_ptr<entry::Entry>, coord3d> getContents(void) const;
+
+ // Setters
+ bool place(std::shared_ptr<entry::Entry> thing, const coord3d& position);
+ bool remove(std::shared_ptr<entry::Entry> thing, const coord3d& position);
+ bool move(std::shared_ptr<entry::Entry> thing, const coord3d& from, const coord3d& to) {
+ if(place(thing, to)) {
+ if(remove(thing, from)) {
+ return true;
+ }
+ remove(thing, to);
+ }
+ return false;
+ }
+
+ NLOHMANN_FRIEND_DECLARES(Battlescape);
+
+ private:
+ std::shared_ptr<battlescapeImpl> data;
+ };
+}
diff --git a/src/features/feature.cc b/src/features/feature.cc
index 0fd2d6c..a941ecf 100644
--- a/src/features/feature.cc
+++ b/src/features/feature.cc
@@ -2,8 +2,10 @@
#include "../spellcasting.h"
#include "../attack.h"
#include "../utils.h"
+#include "../creature.h"
#include <nlohmann/json.hpp>
#include <memory>
+#include <string>
using namespace std;
diff --git a/src/item.cc b/src/item.cc
index 5ecdb0c..24498db 100644
--- a/src/item.cc
+++ b/src/item.cc
@@ -20,10 +20,24 @@ namespace entry {
} else if(data["type"] == "armor") {
return utils::loadDFromJson<Item, Armor>(data);
}
- return shared_ptr<Item>(new Item(data));
+ return utils::loadDFromJson<Item, Item>(data);
}
- string Substantial::getText() const {
+ struct itemImpl {
+ int cost;
+ double weight;
+ };
+ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(itemImpl, cost, weight);
+
+ NLOHMANN_FRIEND_DEFS(Entry, Item, data);
+
+ Item::Item() : data(new itemImpl()) {}
+
+ int Item::getCost() const {return data->cost;}
+
+ double Item::getWeight() const {return data->weight;}
+
+ string Item::getCostWeightText() const {
stringstream text;
if(getCost() >= 0) {
text << "Cost: ";
@@ -39,4 +53,12 @@ namespace entry {
}
return text.str();
}
+
+ string Item::getText() const {
+ return Entry::getText() + " " + getCostWeightText();
+ }
+
+ string Item::getText(const creature::Creature& c) const {
+ return getText();
+ }
}
diff --git a/src/item.h b/src/item.h
index fc43a40..e8a40b9 100644
--- a/src/item.h
+++ b/src/item.h
@@ -4,17 +4,21 @@
#include <memory>
namespace entry {
+ struct itemImpl;
+
class Item : public Entry {
public:
+ Item();
static std::shared_ptr<Item> create(const nlohmann::json& data);
virtual ~Item() {}
- };
+ virtual int getCost(void) const;
+ virtual double getWeight(void) const;
+ virtual std::string getCostWeightText() const;
+ virtual std::string getText() const override;
+ virtual std::string getText(const creature::Creature& c) const override;
- class Substantial {
- public:
- virtual int getCost(void) const = 0;
- virtual double getWeight(void) const = 0;
- std::string getText() const;
- virtual ~Substantial() {}
+ NLOHMANN_FRIEND_DECLARES(Item);
+ private:
+ std::shared_ptr<itemImpl> data;
};
}
diff --git a/src/settings.cc b/src/settings.cc
index ba15945..f8bc8b5 100644
--- a/src/settings.cc
+++ b/src/settings.cc
@@ -5,6 +5,7 @@
#include <stdexcept>
#include <confuse.h>
#include <errno.h>
+#include <cstring>
namespace settings {
std::string getString(const std::string& key) {
diff --git a/src/utils.h b/src/utils.h
index 2998704..9d22a66 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -67,7 +67,7 @@ namespace utils {
ent = entry::Entry::create(loadJson(path));
} catch(std::exception& e) {
if(std::filesystem::directory_entry(path).exists()) {
- throw std::runtime_error("Invalid json: " + path.string());
+ throw std::runtime_error("Invalid json: " + path.string() + ": " + e.what());
} else {
throw std::runtime_error("No such file nor directory: " + path.string());
}
diff --git a/src/weapon.cc b/src/weapon.cc
index e5f04f2..4c47d8b 100644
--- a/src/weapon.cc
+++ b/src/weapon.cc
@@ -16,14 +16,12 @@ namespace entry {
std::string weapon_type;
std::pair<int, int> range;
int reach;
- int cost;
- double weight;
std::optional<int> toHitOverride;
std::optional<int> dmgBonusOverride;
std::optional<rules::Ability> abilityOverride;
};
- NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(weaponImpl, damage, properties, weapon_type, range, reach, cost, weight, toHitOverride, dmgBonusOverride, abilityOverride);
+ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(weaponImpl, damage, properties, weapon_type, range, reach, toHitOverride, dmgBonusOverride, abilityOverride);
NLOHMANN_FRIEND_DEFS(Item, Weapon, data);
@@ -34,8 +32,6 @@ namespace entry {
std::string Weapon::getWeaponType(void) const {return data->weapon_type;}
std::pair<int, int> Weapon::getRange(void) const {return data->range;}
int Weapon::getReach(void) const {return data->reach;}
- int Weapon::getCost(void) const {return data->cost;}
- double Weapon::getWeight(void) const {return data->weight;}
string getTextHelper(const Weapon& w, string toHitBonus, string damageBonus) {
stringstream text;
@@ -99,8 +95,8 @@ namespace entry {
if(! props.empty()) {
text << " Additional properties: " << utils::join(props, ", ") << ".";
}
- if(! w.Substantial::getText().empty()) {
- text << " " << w.Substantial::getText();
+ if(! w.getCostWeightText().empty()) {
+ text << " " << w.getCostWeightText();
}
return text.str();
}
diff --git a/src/weapon.h b/src/weapon.h
index 49de396..dc81689 100644
--- a/src/weapon.h
+++ b/src/weapon.h
@@ -29,7 +29,7 @@ namespace entry {
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Damage, dmg_die_count, dmg_die_sides, dmg_type, is_or);
- class Weapon : public Item, public Substantial {
+ class Weapon : public Item {
public:
Weapon();
@@ -38,8 +38,6 @@ namespace entry {
std::string getWeaponType(void) const;
std::pair<int, int> getRange(void) const;
int getReach(void) const;
- int getCost(void) const;
- double getWeight(void) const;
int getToHitBonus(const creature::Creature& c) const;
int getDamageBonus(const creature::Creature& c) const;