CHeroClass.cpp 2.3 KB

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