InfoAboutArmy.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.getCount();
  40. }
  41. else
  42. {
  43. for(const auto & elem : *this)
  44. ret += elem.second.getType()->getAIValue() * CCreature::estimateCreatureCount(elem.second.getCount());
  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. details.reset();;
  64. InfoAboutArmy::operator = (iah);
  65. details = iah.details;
  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::operator=(const InfoAboutHero & iah)
  80. {
  81. assign(iah);
  82. return *this;
  83. }
  84. int32_t InfoAboutHero::getIconIndex() const
  85. {
  86. return LIBRARY->heroTypes()->getById(portraitSource)->getIconIndex();
  87. }
  88. void InfoAboutHero::initFromHero(const CGHeroInstance *h, InfoAboutHero::EInfoLevel infoLevel)
  89. {
  90. details.reset();
  91. if(!h)
  92. return;
  93. bool detailed = ( (infoLevel == EInfoLevel::DETAILED) || (infoLevel == EInfoLevel::INBATTLE) );
  94. initFromArmy(h, detailed);
  95. hclass = h->getHeroClass();
  96. name = h->getNameTranslated();
  97. portraitSource = h->getPortraitSource();
  98. if(detailed)
  99. {
  100. //include details about hero
  101. details = Details();
  102. details->luck = h->luckVal();
  103. details->morale = h->moraleVal();
  104. details->mana = h->mana;
  105. details->primskills.resize(GameConstants::PRIMARY_SKILLS);
  106. for (int i = 0; i < GameConstants::PRIMARY_SKILLS ; i++)
  107. {
  108. details->primskills[i] = h->getPrimSkillLevel(static_cast<PrimarySkill>(i));
  109. }
  110. if (infoLevel == EInfoLevel::INBATTLE)
  111. details->manaLimit = h->manaLimit();
  112. else
  113. details->manaLimit = -1; //we do not want to leak max mana info outside battle so set to meaningless value
  114. }
  115. }
  116. InfoAboutTown::InfoAboutTown():
  117. tType(nullptr),
  118. built(0),
  119. fortLevel(0)
  120. {
  121. }
  122. InfoAboutTown::InfoAboutTown(const CGTownInstance *t, bool detailed):
  123. tType(nullptr),
  124. built(0),
  125. fortLevel(0)
  126. {
  127. initFromTown(t, detailed);
  128. }
  129. void InfoAboutTown::initFromTown(const CGTownInstance *t, bool detailed)
  130. {
  131. initFromArmy(t, detailed);
  132. army = ArmyDescriptor(t->getUpperArmy(), detailed);
  133. built = t->built;
  134. fortLevel = t->fortLevel();
  135. name = t->getNameTranslated();
  136. tType = t->getTown();
  137. details.reset();
  138. if(detailed)
  139. {
  140. //include details about hero
  141. details = Details();
  142. TResources income = t->dailyIncome();
  143. details->goldIncome = income[EGameResID::GOLD];
  144. details->customRes = t->hasBuilt(BuildingID::RESOURCE_SILO);
  145. details->hallLevel = t->hallLevel();
  146. details->garrisonedHero = t->getGarrisonHero();
  147. }
  148. }
  149. VCMI_LIB_NAMESPACE_END