CArmedInstance.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "../CTownHandler.h"
  13. #include "../CCreatureHandler.h"
  14. #include "../CGeneralTextHandler.h"
  15. #include "../gameState/CGameState.h"
  16. #include "../CPlayerState.h"
  17. #include "../MetaString.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. void CArmedInstance::randomizeArmy(FactionID type)
  20. {
  21. for (auto & elem : stacks)
  22. {
  23. if(elem.second->randomStack)
  24. {
  25. int level = elem.second->randomStack->level;
  26. int upgrade = elem.second->randomStack->upgrade;
  27. elem.second->setType((*VLC->townh)[type]->town->creatures[level][upgrade]);
  28. elem.second->randomStack = std::nullopt;
  29. }
  30. assert(elem.second->valid(false));
  31. assert(elem.second->armyObj == this);
  32. }
  33. }
  34. // Take Angelic Alliance troop-mixing freedom of non-evil units into account.
  35. CSelector CArmedInstance::nonEvilAlignmentMixSelector = Selector::type()(BonusType::NONEVIL_ALIGNMENT_MIX);
  36. CArmedInstance::CArmedInstance(IGameCallback *cb)
  37. :CArmedInstance(cb, false)
  38. {
  39. }
  40. CArmedInstance::CArmedInstance(IGameCallback *cb, bool isHypothetic):
  41. CGObjectInstance(cb),
  42. CBonusSystemNode(isHypothetic),
  43. nonEvilAlignmentMix(this, nonEvilAlignmentMixSelector),
  44. battle(nullptr)
  45. {
  46. }
  47. void CArmedInstance::updateMoraleBonusFromArmy()
  48. {
  49. if(!validTypes(false)) //object not randomized, don't bother
  50. return;
  51. auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
  52. if(!b)
  53. {
  54. b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
  55. addNewBonus(b);
  56. }
  57. //number of alignments and presence of undead
  58. std::set<FactionID> factions;
  59. bool hasUndead = false;
  60. const std::string undeadCacheKey = "type_UNDEAD";
  61. static const CSelector undeadSelector = Selector::type()(BonusType::UNDEAD);
  62. for(const auto & slot : Slots())
  63. {
  64. const CStackInstance * inst = slot.second;
  65. const auto * creature = inst->getCreatureID().toEntity(VLC);
  66. factions.insert(creature->getFaction());
  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 |= inst->hasBonus(undeadSelector, undeadCacheKey);
  72. }
  73. }
  74. size_t factionsInArmy = factions.size(); //town garrison seems to take both sets into account
  75. if (nonEvilAlignmentMix.getHasBonus())
  76. {
  77. size_t mixableFactions = 0;
  78. for(auto f : factions)
  79. {
  80. if (VLC->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. CBonusSystemNode::treeHasChanged();
  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. const IBonusBearer* CArmedInstance::getBonusBearer() const
  130. {
  131. return this;
  132. }
  133. void CArmedInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  134. {
  135. CGObjectInstance::serializeJsonOptions(handler);
  136. CCreatureSet::serializeJson(handler, "army", 7);
  137. }
  138. VCMI_LIB_NAMESPACE_END