CHeroClass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * CHeroClass.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CHeroClass.h"
  12. #include "../faction/CFaction.h"
  13. #include "../../VCMI_Lib.h"
  14. #include "../../texts/CGeneralTextHandler.h"
  15. #include <vstd/RNG.h>
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. SecondarySkill CHeroClass::chooseSecSkill(const std::set<SecondarySkill> & possibles, vstd::RNG & rand) const //picks secondary skill out from given possibilities
  18. {
  19. assert(!possibles.empty());
  20. if (possibles.size() == 1)
  21. return *possibles.begin();
  22. int totalProb = 0;
  23. for(const auto & possible : possibles)
  24. if (secSkillProbability.count(possible) != 0)
  25. totalProb += secSkillProbability.at(possible);
  26. if (totalProb == 0) // may trigger if set contains only banned skills (0 probability)
  27. return *RandomGeneratorUtil::nextItem(possibles, rand);
  28. auto ran = rand.nextInt(totalProb - 1);
  29. for(const auto & possible : possibles)
  30. {
  31. if (secSkillProbability.count(possible) != 0)
  32. ran -= secSkillProbability.at(possible);
  33. if(ran < 0)
  34. return possible;
  35. }
  36. assert(0); // should not be possible
  37. return *possibles.begin();
  38. }
  39. bool CHeroClass::isMagicHero() const
  40. {
  41. return affinity == MAGIC;
  42. }
  43. int CHeroClass::tavernProbability(FactionID targetFaction) const
  44. {
  45. auto it = selectionProbability.find(targetFaction);
  46. if (it != selectionProbability.end())
  47. return it->second;
  48. return 0;
  49. }
  50. EAlignment CHeroClass::getAlignment() const
  51. {
  52. return faction.toEntity(VLC)->getAlignment();
  53. }
  54. int32_t CHeroClass::getIndex() const
  55. {
  56. return id.getNum();
  57. }
  58. int32_t CHeroClass::getIconIndex() const
  59. {
  60. return getIndex();
  61. }
  62. std::string CHeroClass::getJsonKey() const
  63. {
  64. return modScope + ':' + identifier;
  65. }
  66. std::string CHeroClass::getModScope() const
  67. {
  68. return modScope;
  69. }
  70. HeroClassID CHeroClass::getId() const
  71. {
  72. return id;
  73. }
  74. void CHeroClass::registerIcons(const IconRegistar & cb) const
  75. {
  76. }
  77. std::string CHeroClass::getNameTranslated() const
  78. {
  79. return VLC->generaltexth->translate(getNameTextID());
  80. }
  81. std::string CHeroClass::getNameTextID() const
  82. {
  83. return TextIdentifier("heroClass", modScope, identifier, "name").get();
  84. }
  85. void CHeroClass::updateFrom(const JsonNode & data)
  86. {
  87. //TODO: CHeroClass::updateFrom
  88. }
  89. void CHeroClass::serializeJson(JsonSerializeFormat & handler)
  90. {
  91. }
  92. CHeroClass::CHeroClass():
  93. faction(0),
  94. affinity(0),
  95. defaultTavernChance(0)
  96. {
  97. }
  98. VCMI_LIB_NAMESPACE_END