CArmedInstance.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "../CGameState.h"
  16. #include "../CPlayerState.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. void CArmedInstance::randomizeArmy(int type)
  19. {
  20. for (auto & elem : stacks)
  21. {
  22. int & randID = elem.second->idRand;
  23. if(randID >= 0)
  24. {
  25. int level = randID / 2;
  26. bool upgrade = randID % 2;
  27. elem.second->setType((*VLC->townh)[type]->town->creatures[level][upgrade]);
  28. randID = -1;
  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()(Bonus::NONEVIL_ALIGNMENT_MIX);
  36. CArmedInstance::CArmedInstance()
  37. :CArmedInstance(false)
  38. {
  39. }
  40. CArmedInstance::CArmedInstance(bool isHypotetic):
  41. CBonusSystemNode(isHypotetic),
  42. nonEvilAlignmentMix(this, nonEvilAlignmentMixSelector),
  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()(Bonus::ARMY).And(Selector::type()(Bonus::MORALE)));
  51. if(!b)
  52. {
  53. b = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  54. addNewBonus(b);
  55. }
  56. //number of alignments and presence of undead
  57. std::set<TFaction> factions;
  58. bool hasUndead = false;
  59. const std::string undeadCacheKey = "type_UNDEAD";
  60. static const CSelector undeadSelector = Selector::type()(Bonus::UNDEAD);
  61. for(const auto & slot : Slots())
  62. {
  63. const CStackInstance * inst = slot.second;
  64. const CCreature * creature = VLC->creh->objects[inst->getCreatureID()];
  65. factions.insert(creature->getFactionIndex());
  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(TFaction f : factions)
  78. {
  79. if (VLC->factions()->getByIndex(f)->getAlignment() != EAlignment::EVIL)
  80. mixableFactions++;
  81. }
  82. if (mixableFactions > 0)
  83. factionsInArmy -= mixableFactions - 1;
  84. }
  85. std::string description;
  86. if(factionsInArmy == 1)
  87. {
  88. b->val = +1;
  89. description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
  90. description = description.substr(0, description.size()-3);//trim "+1"
  91. }
  92. else if (!factions.empty()) // no bonus from empty garrison
  93. {
  94. b->val = 2 - static_cast<si32>(factionsInArmy);
  95. description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factionsInArmy % b->val); //Troops of %d alignments %d
  96. description = b->description.substr(0, description.size()-2);//trim value
  97. }
  98. boost::algorithm::trim(description);
  99. b->description = description;
  100. CBonusSystemNode::treeHasChanged();
  101. //-1 modifier for any Undead unit in army
  102. const ui8 UNDEAD_MODIFIER_ID = -2;
  103. auto undeadModifier = getExportedBonusList().getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
  104. if(hasUndead)
  105. {
  106. if(!undeadModifier)
  107. {
  108. undeadModifier = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]);
  109. undeadModifier->description = undeadModifier->description.substr(0, undeadModifier->description.size()-2);//trim value
  110. addNewBonus(undeadModifier);
  111. }
  112. }
  113. else if(undeadModifier)
  114. removeBonus(undeadModifier);
  115. }
  116. void CArmedInstance::armyChanged()
  117. {
  118. updateMoraleBonusFromArmy();
  119. }
  120. CBonusSystemNode & CArmedInstance::whereShouldBeAttached(CGameState * gs)
  121. {
  122. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  123. if(auto * where = gs->getPlayerState(tempOwner))
  124. return *where;
  125. return gs->globalEffects;
  126. }
  127. CBonusSystemNode & CArmedInstance::whatShouldBeAttached()
  128. {
  129. return *this;
  130. }
  131. VCMI_LIB_NAMESPACE_END