CArmedInstance.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. void CArmedInstance::randomizeArmy(int type)
  18. {
  19. for (auto & elem : stacks)
  20. {
  21. int & randID = elem.second->idRand;
  22. if(randID >= 0)
  23. {
  24. int level = randID / 2;
  25. bool upgrade = randID % 2;
  26. elem.second->setType((*VLC->townh)[type]->town->creatures[level][upgrade]);
  27. randID = -1;
  28. }
  29. assert(elem.second->valid(false));
  30. assert(elem.second->armyObj == this);
  31. }
  32. return;
  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), nonEvilAlignmentMix(this, nonEvilAlignmentMixSelector)
  42. {
  43. battle = nullptr;
  44. }
  45. void CArmedInstance::updateMoraleBonusFromArmy()
  46. {
  47. if(!validTypes(false)) //object not randomized, don't bother
  48. return;
  49. auto b = getExportedBonusList().getFirst(Selector::sourceType()(Bonus::ARMY).And(Selector::type()(Bonus::MORALE)));
  50. if(!b)
  51. {
  52. b = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  53. addNewBonus(b);
  54. }
  55. //number of alignments and presence of undead
  56. std::set<TFaction> factions;
  57. bool hasUndead = false;
  58. const std::string undeadCacheKey = "type_UNDEAD";
  59. static const CSelector undeadSelector = Selector::type()(Bonus::UNDEAD);
  60. for(auto slot : Slots())
  61. {
  62. const CStackInstance * inst = slot.second;
  63. const CCreature * creature = VLC->creh->objects[inst->getCreatureID()];
  64. factions.insert(creature->faction);
  65. // Check for undead flag instead of faction (undead mummies are neutral)
  66. if (!hasUndead)
  67. {
  68. //this is costly check, let's skip it at first undead
  69. hasUndead |= inst->hasBonus(undeadSelector, undeadCacheKey);
  70. }
  71. }
  72. size_t factionsInArmy = factions.size(); //town garrison seems to take both sets into account
  73. if (nonEvilAlignmentMix.getHasBonus())
  74. {
  75. size_t mixableFactions = 0;
  76. for(TFaction f : factions)
  77. {
  78. if ((*VLC->townh)[f]->alignment != EAlignment::EVIL)
  79. mixableFactions++;
  80. }
  81. if (mixableFactions > 0)
  82. factionsInArmy -= mixableFactions - 1;
  83. }
  84. std::string description;
  85. if(factionsInArmy == 1)
  86. {
  87. b->val = +1;
  88. description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
  89. description = description.substr(0, description.size()-3);//trim "+1"
  90. }
  91. else if (!factions.empty()) // no bonus from empty garrison
  92. {
  93. b->val = 2 - (si32)factionsInArmy;
  94. description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factionsInArmy % b->val); //Troops of %d alignments %d
  95. description = b->description.substr(0, description.size()-2);//trim value
  96. }
  97. boost::algorithm::trim(description);
  98. b->description = description;
  99. CBonusSystemNode::treeHasChanged();
  100. //-1 modifier for any Undead unit in army
  101. const ui8 UNDEAD_MODIFIER_ID = -2;
  102. auto undeadModifier = getExportedBonusList().getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
  103. if(hasUndead)
  104. {
  105. if(!undeadModifier)
  106. {
  107. undeadModifier = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]);
  108. undeadModifier->description = undeadModifier->description.substr(0, undeadModifier->description.size()-2);//trim value
  109. addNewBonus(undeadModifier);
  110. }
  111. }
  112. else if(undeadModifier)
  113. removeBonus(undeadModifier);
  114. }
  115. void CArmedInstance::armyChanged()
  116. {
  117. updateMoraleBonusFromArmy();
  118. }
  119. CBonusSystemNode * CArmedInstance::whereShouldBeAttached(CGameState *gs)
  120. {
  121. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  122. return gs->getPlayerState(tempOwner);
  123. else
  124. return &gs->globalEffects;
  125. }
  126. CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
  127. {
  128. return this;
  129. }