InfoAboutArmy.cpp 4.2 KB

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