InfoAboutArmy.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. portrait = iah.portrait;
  66. }
  67. InfoAboutHero::InfoAboutHero(): portrait(-1) {}
  68. InfoAboutHero::InfoAboutHero(const InfoAboutHero & iah): InfoAboutArmy(iah)
  69. {
  70. assign(iah);
  71. }
  72. InfoAboutHero::InfoAboutHero(const CGHeroInstance * h, InfoAboutHero::EInfoLevel infoLevel):
  73. portrait(-1)
  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. void InfoAboutHero::initFromHero(const CGHeroInstance *h, InfoAboutHero::EInfoLevel infoLevel)
  87. {
  88. vstd::clear_pointer(details);
  89. if(!h)
  90. return;
  91. bool detailed = ( (infoLevel == EInfoLevel::DETAILED) || (infoLevel == EInfoLevel::INBATTLE) );
  92. initFromArmy(h, detailed);
  93. hclass = h->type->heroClass;
  94. name = h->getNameTranslated();
  95. portrait = h->portrait;
  96. if(detailed)
  97. {
  98. //include details about hero
  99. details = new Details();
  100. details->luck = h->luckVal();
  101. details->morale = h->moraleVal();
  102. details->mana = h->mana;
  103. details->primskills.resize(GameConstants::PRIMARY_SKILLS);
  104. for (int i = 0; i < GameConstants::PRIMARY_SKILLS ; i++)
  105. {
  106. details->primskills[i] = h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i));
  107. }
  108. if (infoLevel == EInfoLevel::INBATTLE)
  109. details->manaLimit = h->manaLimit();
  110. else
  111. details->manaLimit = -1; //we do not want to leak max mana info outside battle so set to meaningless value
  112. }
  113. }
  114. InfoAboutTown::InfoAboutTown():
  115. details(nullptr),
  116. tType(nullptr),
  117. built(0),
  118. fortLevel(0)
  119. {
  120. }
  121. InfoAboutTown::InfoAboutTown(const CGTownInstance *t, bool detailed):
  122. details(nullptr),
  123. tType(nullptr),
  124. built(0),
  125. fortLevel(0)
  126. {
  127. initFromTown(t, detailed);
  128. }
  129. InfoAboutTown::~InfoAboutTown()
  130. {
  131. vstd::clear_pointer(details);
  132. }
  133. void InfoAboutTown::initFromTown(const CGTownInstance *t, bool detailed)
  134. {
  135. initFromArmy(t, detailed);
  136. army = ArmyDescriptor(t->getUpperArmy(), detailed);
  137. built = t->builded;
  138. fortLevel = t->fortLevel();
  139. name = t->getNameTranslated();
  140. tType = t->getTown();
  141. vstd::clear_pointer(details);
  142. if(detailed)
  143. {
  144. //include details about hero
  145. details = new Details();
  146. TResources income = t->dailyIncome();
  147. details->goldIncome = income[EGameResID::GOLD];
  148. details->customRes = t->hasBuilt(BuildingID::RESOURCE_SILO);
  149. details->hallLevel = t->hallLevel();
  150. details->garrisonedHero = t->garrisonHero;
  151. }
  152. }
  153. VCMI_LIB_NAMESPACE_END