InfoAboutArmy.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * InfoAboutArmy.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 "InfoAboutArmy.h"
  12. #include "../mapObjects/CGHeroInstance.h"
  13. #include "../mapObjects/CGTownInstance.h"
  14. #include "../CHeroHandler.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. ArmyDescriptor::ArmyDescriptor(const CArmedInstance *army, bool detailed)
  17. : isDetailed(detailed)
  18. {
  19. for(const auto & elem : army->Slots())
  20. {
  21. if(detailed)
  22. (*this)[elem.first] = *elem.second;
  23. else
  24. (*this)[elem.first] = CStackBasicDescriptor(elem.second->type, (int)elem.second->getQuantityID());
  25. }
  26. }
  27. ArmyDescriptor::ArmyDescriptor()
  28. : isDetailed(false)
  29. {
  30. }
  31. int ArmyDescriptor::getStrength() const
  32. {
  33. ui64 ret = 0;
  34. if(isDetailed)
  35. {
  36. for(const auto & elem : *this)
  37. ret += elem.second.type->getAIValue() * elem.second.count;
  38. }
  39. else
  40. {
  41. for(const auto & elem : *this)
  42. ret += elem.second.type->getAIValue() * CCreature::estimateCreatureCount(elem.second.count);
  43. }
  44. return static_cast<int>(ret);
  45. }
  46. InfoAboutArmy::InfoAboutArmy():
  47. owner(PlayerColor::NEUTRAL)
  48. {}
  49. InfoAboutArmy::InfoAboutArmy(const CArmedInstance *Army, bool detailed)
  50. {
  51. initFromArmy(Army, detailed);
  52. }
  53. void InfoAboutArmy::initFromArmy(const CArmedInstance *Army, bool detailed)
  54. {
  55. army = ArmyDescriptor(Army, detailed);
  56. owner = Army->tempOwner;
  57. name = Army->getObjectName();
  58. }
  59. void InfoAboutHero::assign(const InfoAboutHero & iah)
  60. {
  61. vstd::clear_pointer(details);
  62. InfoAboutArmy::operator = (iah);
  63. details = (iah.details ? new Details(*iah.details) : nullptr);
  64. hclass = iah.hclass;
  65. portraitSource = iah.portraitSource;
  66. }
  67. InfoAboutHero::InfoAboutHero()
  68. {}
  69. InfoAboutHero::InfoAboutHero(const InfoAboutHero & iah): InfoAboutArmy(iah)
  70. {
  71. assign(iah);
  72. }
  73. InfoAboutHero::InfoAboutHero(const CGHeroInstance * h, InfoAboutHero::EInfoLevel infoLevel)
  74. {
  75. initFromHero(h, infoLevel);
  76. }
  77. InfoAboutHero::~InfoAboutHero()
  78. {
  79. vstd::clear_pointer(details);
  80. }
  81. InfoAboutHero & InfoAboutHero::operator=(const InfoAboutHero & iah)
  82. {
  83. assign(iah);
  84. return *this;
  85. }
  86. int32_t InfoAboutHero::getIconIndex() const
  87. {
  88. return VLC->heroTypes()->getById(portraitSource)->getIconIndex();
  89. }
  90. void InfoAboutHero::initFromHero(const CGHeroInstance *h, InfoAboutHero::EInfoLevel infoLevel)
  91. {
  92. vstd::clear_pointer(details);
  93. if(!h)
  94. return;
  95. bool detailed = ( (infoLevel == EInfoLevel::DETAILED) || (infoLevel == EInfoLevel::INBATTLE) );
  96. initFromArmy(h, detailed);
  97. hclass = h->type->heroClass;
  98. name = h->getNameTranslated();
  99. portraitSource = h->getPortraitSource();
  100. if(detailed)
  101. {
  102. //include details about hero
  103. details = new Details();
  104. details->luck = h->luckVal();
  105. details->morale = h->moraleVal();
  106. details->mana = h->mana;
  107. details->primskills.resize(GameConstants::PRIMARY_SKILLS);
  108. for (int i = 0; i < GameConstants::PRIMARY_SKILLS ; i++)
  109. {
  110. details->primskills[i] = h->getPrimSkillLevel(static_cast<PrimarySkill>(i));
  111. }
  112. if (infoLevel == EInfoLevel::INBATTLE)
  113. details->manaLimit = h->manaLimit();
  114. else
  115. details->manaLimit = -1; //we do not want to leak max mana info outside battle so set to meaningless value
  116. }
  117. }
  118. InfoAboutTown::InfoAboutTown():
  119. details(nullptr),
  120. tType(nullptr),
  121. built(0),
  122. fortLevel(0)
  123. {
  124. }
  125. InfoAboutTown::InfoAboutTown(const CGTownInstance *t, bool detailed):
  126. details(nullptr),
  127. tType(nullptr),
  128. built(0),
  129. fortLevel(0)
  130. {
  131. initFromTown(t, detailed);
  132. }
  133. InfoAboutTown::~InfoAboutTown()
  134. {
  135. vstd::clear_pointer(details);
  136. }
  137. void InfoAboutTown::initFromTown(const CGTownInstance *t, bool detailed)
  138. {
  139. initFromArmy(t, detailed);
  140. army = ArmyDescriptor(t->getUpperArmy(), detailed);
  141. built = t->builded;
  142. fortLevel = t->fortLevel();
  143. name = t->getNameTranslated();
  144. tType = t->getTown();
  145. vstd::clear_pointer(details);
  146. if(detailed)
  147. {
  148. //include details about hero
  149. details = new Details();
  150. TResources income = t->dailyIncome();
  151. details->goldIncome = income[EGameResID::GOLD];
  152. details->customRes = t->hasBuilt(BuildingID::RESOURCE_SILO);
  153. details->hallLevel = t->hallLevel();
  154. details->garrisonedHero = t->garrisonHero;
  155. }
  156. }
  157. VCMI_LIB_NAMESPACE_END