]> Johnzone git - splittermond-assistant.git/commitdiff
use private implementation for character and add some more stuff
authorJohn Janus <j.janus@lighthouse-it.de>
Tue, 13 Aug 2019 13:40:35 +0000 (15:40 +0200)
committerJohn Janus <j.janus@lighthouse-it.de>
Tue, 13 Aug 2019 13:40:35 +0000 (15:40 +0200)
src/character.cpp
src/character.h

index 04907ee493dffb6827b1d8d8117515136520fbce..3e63414631ec2c7746f458ed8a3e4c6271d80801 100644 (file)
@@ -1,7 +1,67 @@
 #include "character.h"
-namespace sm::data {
-  Character::Character(const QFile& file)
-  {
+#include <random>
+#include <QSharedData>
+#include <vector>
+
+namespace sm::data
+{
+
+class Character::CharacterData : public QSharedData
+{
+public:
+  CharacterData() {}
+  CharacterData (const CharacterData& other) : QSharedData (other) {}
+
+  ~CharacterData() {}
+  unsigned int life = 0;
+  const std::vector<Damage> damage;
+  const std::unordered_map<Skill, unsigned int> skills;
+  const std::unordered_map<Attribute, unsigned int> attributes;
+};
+
+Character::Character () : d{new Character::CharacterData}
+{
+
+}
+
+Character::~Character()
+{
+
+}
+
+unsigned int Character::skill (Skill _skill) const
+{
+  return d->skills.at (_skill);
+}
+
+unsigned int Character::attribute (Attribute _attribute) const
+{
+  return d->attributes.at (_attribute);
+}
+
+unsigned int Character::life() const
+{
+  return d->life;
+}
+
+Character::SkillTest Character::testSkill (Skill _skill, unsigned int against) const
+{
+  std::uniform_int_distribution<int> d10 (1, 10);
+  std::random_device gen;
+  unsigned int s = d->skills.at (_skill);
+  Attribute attr1 = skillAttributes.at (_skill).first;
+  Attribute attr2 = skillAttributes.at (_skill).second;
+  unsigned int a1 = d->attributes.at (attr1);
+  unsigned int a2 = d->attributes.at (attr2);
+
+  unsigned int d1 = unsigned (d10 (gen) );
+  unsigned int d2 = unsigned (d10 (gen) );
+  unsigned int accumulated = d1 + d2 + s + a1 + a2;
+  int delta = signed (accumulated) - signed (against);
+  bool passed = delta >= 0;
+  unsigned int grade = unsigned (std::abs (delta) / 3);
+
+  return {passed, grade, s, a1, a2, d1, d2};
+}
 
-  }
 }
index 18df734ee41c808c7841054a01b66f4206368826..2b013a680e55c66b3eef35c231a437f5f4220ab2 100644 (file)
 #define CHARACTER_H
 
 #include <QObject>
-#include <QFile>
-#include <QXmlSimpleReader>
-#include <unordered_map>
-#include "definitions.h"
+#include <QSharedDataPointer>
 
 namespace sm::data
 {
-  enum Skill {
-    acrobatics,
-    alchemy,
-    leadership,
-    arcanelore,
-    athletics,
-    performance,
-    diplomacy,
-    clscraft,
-    empathy,
-    determination,
-    dexterity,
-    history,
-    craftmanship,
-    heal,
-    stealth,
-    hunting,
-    countrylore,
-    nature,
-    eloquence,
-    locksntraps,
-    swim,
-    seafaring,
-    streetlore,
-    animals,
-    survival,
-    perception,
-    endurance,
-    antimagic,
-    controlmagic,
-    motionmagic,
-    insightmagic,
-    stonemagic,
-    firemagic,
-    healmagic,
-    illusionmagic,
-    combatmagic,
-    lightmagic,
-    naturemagic,
-    shadowmagic,
-    fatemagic,
-    protectionmagic,
-    enhancemagic,
-    deathmagic,
-    transformationmagic,
-    watermagic,
-    windmagic
-  };
+enum Skill {
+  acrobatics,
+  alchemy,
+  leadership,
+  arcanelore,
+  athletics,
+  performance,
+  diplomacy,
+  clscraft,
+  empathy,
+  determination,
+  dexterity,
+  history,
+  craftmanship,
+  heal,
+  stealth,
+  hunting,
+  countrylore,
+  nature,
+  eloquence,
+  locksntraps,
+  swim,
+  seafaring,
+  streetlore,
+  animals,
+  survival,
+  perception,
+  endurance,
+  antimagic,
+  controlmagic,
+  motionmagic,
+  insightmagic,
+  stonemagic,
+  firemagic,
+  healmagic,
+  illusionmagic,
+  combatmagic,
+  lightmagic,
+  naturemagic,
+  shadowmagic,
+  fatemagic,
+  protectionmagic,
+  enhancemagic,
+  deathmagic,
+  transformationmagic,
+  watermagic,
+  windmagic
+};
 
-  enum Attribute {
-    charisma,
-    agility,
-    intuition,
-    constitution,
-    mystic,
-    strength,
-    mind,
-    willpower
-  };
+enum Attribute {
+  charisma,
+  agility,
+  intuition,
+  constitution,
+  mystic,
+  strength,
+  mind,
+  willpower
+};
 
-  static const std::unordered_map<Skill, std::pair<Attribute, Attribute>> skillAttributes{
-    {acrobatics, {agility, strength}},
-    {alchemy, {mystic, mind}},
-    {leadership, {charisma, willpower}},
-    {arcanelore, {mystic, mind}},
-    {athletics, {agility, strength}},
-    {performance, {charisma, willpower}},
-    {diplomacy, {charisma, mind}},
-    {clscraft, {intuition, mind}},
-    {empathy, {intuition, mind}},
-    {determination, {charisma, willpower}},
-    {dexterity, {charisma, agility}},
-    {history, {mystic, mind}},
-    {craftmanship, {constitution, mind}},
-    {heal, {intuition, mind}},
-    {stealth, {agility, intuition}},
-    {hunting, {constitution, mind}},
-    {countrylore, {intuition, mind}},
-    {nature, {intuition, mind}},
-    {eloquence, {charisma, willpower}},
-    {locksntraps, {intuition, agility}},
-    {swim, {strength, constitution}},
-    {seafaring, {agility, constitution}},
-    {streetlore, {charisma, intuition}},
-    {animals, {charisma, agility}},
-    {survival, {intuition, constitution}},
-    {perception, {intuition, willpower}},
-    {endurance, {constitution, willpower}},
-    {antimagic, {mystic, willpower}},
-    {controlmagic, {mystic, willpower}},
-    {motionmagic, {mystic, agility}},
-    {insightmagic, {mystic, mind}},
-    {stonemagic, {mystic, constitution}},
-    {firemagic, {mystic, charisma}},
-    {healmagic, {mystic, charisma}},
-    {illusionmagic, {mystic, charisma}},
-    {combatmagic, {mystic, strength}},
-    {lightmagic, {mystic, charisma}},
-    {naturemagic, {mystic, charisma}},
-    {shadowmagic, {mystic, intuition}},
-    {fatemagic, {mystic, charisma}},
-    {protectionmagic, {mystic, charisma}},
-    {enhancemagic, {mystic, strength}},
-    {deathmagic, {mystic, mind}},
-    {transformationmagic, {mystic, constitution}},
-    {watermagic, {mystic, intuition}},
-    {windmagic, {mystic, mind}}
-  };
+enum Damage {
+  canalised,
+  exhausted,
+  used
+};
 
-  class Character
-  {
-  public:
-    explicit Character(const QFile& file);
-    virtual ~Character() {}
-    unsigned int skill(Skill _skill) const {return skills.at(_skill);}
-    unsigned int attribute(Attribute _attribute) const {return attributes.at(_attribute);}
+static const std::unordered_map<Skill, std::pair<Attribute, Attribute>> skillAttributes{
+  {acrobatics, {agility, strength}},
+  {alchemy, {mystic, mind}},
+  {leadership, {charisma, willpower}},
+  {arcanelore, {mystic, mind}},
+  {athletics, {agility, strength}},
+  {performance, {charisma, willpower}},
+  {diplomacy, {charisma, mind}},
+  {clscraft, {intuition, mind}},
+  {empathy, {intuition, mind}},
+  {determination, {charisma, willpower}},
+  {dexterity, {charisma, agility}},
+  {history, {mystic, mind}},
+  {craftmanship, {constitution, mind}},
+  {heal, {intuition, mind}},
+  {stealth, {agility, intuition}},
+  {hunting, {constitution, mind}},
+  {countrylore, {intuition, mind}},
+  {nature, {intuition, mind}},
+  {eloquence, {charisma, willpower}},
+  {locksntraps, {intuition, agility}},
+  {swim, {strength, constitution}},
+  {seafaring, {agility, constitution}},
+  {streetlore, {charisma, intuition}},
+  {animals, {charisma, agility}},
+  {survival, {intuition, constitution}},
+  {perception, {intuition, willpower}},
+  {endurance, {constitution, willpower}},
+  {antimagic, {mystic, willpower}},
+  {controlmagic, {mystic, willpower}},
+  {motionmagic, {mystic, agility}},
+  {insightmagic, {mystic, mind}},
+  {stonemagic, {mystic, constitution}},
+  {firemagic, {mystic, charisma}},
+  {healmagic, {mystic, charisma}},
+  {illusionmagic, {mystic, charisma}},
+  {combatmagic, {mystic, strength}},
+  {lightmagic, {mystic, charisma}},
+  {naturemagic, {mystic, charisma}},
+  {shadowmagic, {mystic, intuition}},
+  {fatemagic, {mystic, charisma}},
+  {protectionmagic, {mystic, charisma}},
+  {enhancemagic, {mystic, strength}},
+  {deathmagic, {mystic, mind}},
+  {transformationmagic, {mystic, constitution}},
+  {watermagic, {mystic, intuition}},
+  {windmagic, {mystic, mind}}
+};
 
-  private:
-    const std::unordered_map<Skill, unsigned int> skills;
-    const std::unordered_map<Attribute, unsigned int> attributes;
+class Character
+{
+public:
+  explicit Character ();
+  virtual ~Character();
+  unsigned int skill (Skill skill) const;
+  unsigned int attribute (Attribute attribute) const;
+  unsigned int life () const;
+  struct SkillTest {
+    SkillTest (bool _passed, unsigned int _grades, unsigned int _sv, unsigned int _a1v, unsigned int _a2v, unsigned int _d1,
+               unsigned int _d2)
+      : passed (_passed)
+      , grades (_grades)
+      , skillValue (_sv)
+      , attribute1Value (_a1v)
+      , attribute2Value (_a2v)
+      , d1 (_d1)
+      , d2 (_d2)
+    {}
 
+    const bool passed;
+    const unsigned int grades;
+    const unsigned int skillValue;
+    const unsigned int attribute1Value;
+    const unsigned int attribute2Value;
+    const unsigned int d1;
+    const unsigned int d2;
+  };
+  SkillTest testSkill (Skill skill, unsigned int against) const;
 
 
-    class SkillTest
-    {
-    public:
-      SkillTest(const Character& hero, const Skill skill, const unsigned int against)
-      {
-        std::uniform_int_distribution<int> d10(1,10);
-        std::random_device generator;
-        this->d1 = d10(generator);
-        this->d2 = d10(generator);
-        this->accumulated = d1 + d2
-            + hero.skill(skill)
-            + hero.attribute(skillAttributes.at(skill).first)
-            + hero.attribute(skillAttributes.at(skill).second);
-        this->delta = accumulated - against;
-        passed = delta >= 0;
-        grade = std::abs(delta) / 3;
-      }
+private:
+  class CharacterData;
+  QSharedDataPointer<CharacterData> d;
 
-    private:
-      unsigned int d1;
-      unsigned int d2;
-      unsigned int accumulated;
-      int delta;
-      bool passed;
-      unsigned int grade;
-    };
-  };
+};
 }
 #endif // CHARACTER_H