CArmedInstance.cpp 4.4 KB

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