CArmedInstance.cpp 3.9 KB

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