2
0

CArmedInstance.cpp 3.6 KB

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