CArmedInstance.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * CArmedInstance.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 "CArmedInstance.h"
  12. #include "../CCreatureHandler.h"
  13. #include "../CPlayerState.h"
  14. #include "../entities/faction/CFaction.h"
  15. #include "../entities/faction/CTown.h"
  16. #include "../entities/faction/CTownHandler.h"
  17. #include "../GameLibrary.h"
  18. #include "../gameState/CGameState.h"
  19. #include "../mapping/CMapDefines.h"
  20. #include "../texts/CGeneralTextHandler.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. void CArmedInstance::randomizeArmy(FactionID type)
  23. {
  24. for (auto & elem : stacks)
  25. {
  26. if(elem.second->randomStack)
  27. {
  28. int level = elem.second->randomStack->level;
  29. int upgrade = elem.second->randomStack->upgrade;
  30. elem.second->setType((*LIBRARY->townh)[type]->town->creatures[level][upgrade]);
  31. elem.second->randomStack = std::nullopt;
  32. }
  33. assert(elem.second->valid(false));
  34. assert(elem.second->getArmy() == this);
  35. }
  36. }
  37. CArmedInstance::CArmedInstance(IGameCallback *cb)
  38. :CArmedInstance(cb, false)
  39. {
  40. }
  41. CArmedInstance::CArmedInstance(IGameCallback *cb, bool isHypothetic):
  42. CGObjectInstance(cb),
  43. CBonusSystemNode(isHypothetic),
  44. nonEvilAlignmentMix(this, Selector::type()(BonusType::NONEVIL_ALIGNMENT_MIX)), // Take Angelic Alliance troop-mixing freedom of non-evil units into account.
  45. battle(nullptr)
  46. {
  47. }
  48. void CArmedInstance::updateMoraleBonusFromArmy()
  49. {
  50. if(!validTypes(false)) //object not randomized, don't bother
  51. return;
  52. auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
  53. if(!b)
  54. {
  55. b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
  56. addNewBonus(b);
  57. }
  58. //number of alignments and presence of undead
  59. std::set<FactionID> factions;
  60. bool hasUndead = false;
  61. const std::string undeadCacheKey = "type_UNDEAD";
  62. static const CSelector undeadSelector = Selector::type()(BonusType::UNDEAD);
  63. for(const auto & slot : Slots())
  64. {
  65. const auto * creature = slot.second->getCreatureID().toEntity(LIBRARY);
  66. factions.insert(creature->getFactionID());
  67. // Check for undead flag instead of faction (undead mummies are neutral)
  68. if (!hasUndead)
  69. {
  70. //this is costly check, let's skip it at first undead
  71. hasUndead |= slot.second->hasBonus(undeadSelector, undeadCacheKey);
  72. }
  73. }
  74. size_t factionsInArmy = factions.size(); //town garrison seems to take both sets into account
  75. if (nonEvilAlignmentMix.hasBonus())
  76. {
  77. size_t mixableFactions = 0;
  78. for(auto f : factions)
  79. {
  80. if (LIBRARY->factions()->getById(f)->getAlignment() != EAlignment::EVIL)
  81. mixableFactions++;
  82. }
  83. if (mixableFactions > 0)
  84. factionsInArmy -= mixableFactions - 1;
  85. }
  86. MetaString bonusDescription;
  87. if(factionsInArmy == 1)
  88. {
  89. b->val = +1;
  90. bonusDescription.appendTextID("core.arraytxt.115"); //All troops of one alignment +1
  91. }
  92. else if (!factions.empty()) // no bonus from empty garrison
  93. {
  94. b->val = 2 - static_cast<si32>(factionsInArmy);
  95. bonusDescription.appendTextID("core.arraytxt.114"); //Troops of %d alignments %d
  96. bonusDescription.replaceNumber(factionsInArmy);
  97. }
  98. b->description = bonusDescription;
  99. nodeHasChanged();
  100. //-1 modifier for any Undead unit in army
  101. auto undeadModifier = getExportedBonusList().getFirst(Selector::source(BonusSource::ARMY, BonusCustomSource::undeadMoraleDebuff));
  102. if(hasUndead)
  103. {
  104. if(!undeadModifier)
  105. {
  106. undeadModifier = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, -1, BonusCustomSource::undeadMoraleDebuff);
  107. undeadModifier->description.appendTextID("core.arraytxt.116");
  108. addNewBonus(undeadModifier);
  109. }
  110. }
  111. else if(undeadModifier)
  112. removeBonus(undeadModifier);
  113. }
  114. void CArmedInstance::armyChanged()
  115. {
  116. updateMoraleBonusFromArmy();
  117. }
  118. CBonusSystemNode & CArmedInstance::whereShouldBeAttached(CGameState * gs)
  119. {
  120. if(tempOwner.isValidPlayer())
  121. if(auto * where = gs->getPlayerState(tempOwner))
  122. return *where;
  123. return gs->globalEffects;
  124. }
  125. CBonusSystemNode & CArmedInstance::whatShouldBeAttached()
  126. {
  127. return *this;
  128. }
  129. void CArmedInstance::attachToBonusSystem(CGameState * gs)
  130. {
  131. CArmedInstance::restoreBonusSystem(gs);
  132. }
  133. void CArmedInstance::restoreBonusSystem(CGameState * gs)
  134. {
  135. whatShouldBeAttached().attachTo(whereShouldBeAttached(gs));
  136. for(const auto & elem : stacks)
  137. elem.second->artDeserializationFix(gs, elem.second.get());
  138. }
  139. void CArmedInstance::detachFromBonusSystem(CGameState * gs)
  140. {
  141. whatShouldBeAttached().detachFrom(whereShouldBeAttached(gs));
  142. // TODO: the opposite
  143. // for(const auto & elem : stacks)
  144. // elem.second->artDeserializationFix(elem.second.get());
  145. }
  146. void CArmedInstance::attachUnitsToArmy()
  147. {
  148. for(const auto & elem : stacks)
  149. elem.second->attachTo(*getArmy());
  150. }
  151. const IBonusBearer* CArmedInstance::getBonusBearer() const
  152. {
  153. return this;
  154. }
  155. void CArmedInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  156. {
  157. CGObjectInstance::serializeJsonOptions(handler);
  158. CCreatureSet::serializeJson(handler, "army", 7);
  159. }
  160. TerrainId CArmedInstance::getCurrentTerrain() const
  161. {
  162. if (anchorPos().isValid())
  163. return cb->getTile(visitablePos())->getTerrainID();
  164. else
  165. return TerrainId::NONE;
  166. }
  167. VCMI_LIB_NAMESPACE_END