InfoAboutArmy.cpp 4.2 KB

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