CHeroClass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. std::vector<int> weights;
  21. std::vector<SecondarySkill> skills;
  22. for(const auto & possible : possibles)
  23. {
  24. skills.push_back(possible);
  25. if (secSkillProbability.count(possible) != 0)
  26. {
  27. int weight = secSkillProbability.at(possible);
  28. weights.push_back(std::max(1, weight));
  29. }
  30. else
  31. weights.push_back(1); // H3 behavior - banned skills have minimal (1) chance to be picked
  32. }
  33. int selectedIndex = RandomGeneratorUtil::nextItemWeighted(weights, rand);
  34. return skills.at(selectedIndex);
  35. }
  36. bool CHeroClass::isMagicHero() const
  37. {
  38. return affinity == MAGIC;
  39. }
  40. int CHeroClass::tavernProbability(FactionID targetFaction) const
  41. {
  42. auto it = selectionProbability.find(targetFaction);
  43. if (it != selectionProbability.end())
  44. return it->second;
  45. return 0;
  46. }
  47. EAlignment CHeroClass::getAlignment() const
  48. {
  49. return faction.toEntity(VLC)->getAlignment();
  50. }
  51. int32_t CHeroClass::getIndex() const
  52. {
  53. return id.getNum();
  54. }
  55. int32_t CHeroClass::getIconIndex() const
  56. {
  57. return getIndex();
  58. }
  59. std::string CHeroClass::getJsonKey() const
  60. {
  61. return modScope + ':' + identifier;
  62. }
  63. std::string CHeroClass::getModScope() const
  64. {
  65. return modScope;
  66. }
  67. HeroClassID CHeroClass::getId() const
  68. {
  69. return id;
  70. }
  71. void CHeroClass::registerIcons(const IconRegistar & cb) const
  72. {
  73. }
  74. std::string CHeroClass::getNameTranslated() const
  75. {
  76. return VLC->generaltexth->translate(getNameTextID());
  77. }
  78. std::string CHeroClass::getNameTextID() const
  79. {
  80. return TextIdentifier("heroClass", modScope, identifier, "name").get();
  81. }
  82. void CHeroClass::updateFrom(const JsonNode & data)
  83. {
  84. //TODO: CHeroClass::updateFrom
  85. }
  86. void CHeroClass::serializeJson(JsonSerializeFormat & handler)
  87. {
  88. }
  89. CHeroClass::CHeroClass():
  90. faction(0),
  91. affinity(0),
  92. defaultTavernChance(0)
  93. {
  94. }
  95. VCMI_LIB_NAMESPACE_END