CArmedInstance.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // 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->creatures[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->factions[f]->alignment != EAlignment::EVIL)
  71. mixableFactions++;
  72. }
  73. if (mixableFactions > 0)
  74. factionsInArmy -= mixableFactions - 1;
  75. }
  76. if(factionsInArmy == 1)
  77. {
  78. b->val = +1;
  79. b->description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
  80. b->description = b->description.substr(0, b->description.size()-3);//trim "+1"
  81. }
  82. else if (!factions.empty()) // no bonus from empty garrison
  83. {
  84. b->val = 2 - (si32)factionsInArmy;
  85. b->description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factionsInArmy % b->val); //Troops of %d alignments %d
  86. b->description = b->description.substr(0, b->description.size()-2);//trim value
  87. }
  88. boost::algorithm::trim(b->description);
  89. CBonusSystemNode::treeHasChanged();
  90. //-1 modifier for any Undead unit in army
  91. const ui8 UNDEAD_MODIFIER_ID = -2;
  92. auto undeadModifier = getExportedBonusList().getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
  93. if(hasUndead)
  94. {
  95. if(!undeadModifier)
  96. {
  97. undeadModifier = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]);
  98. undeadModifier->description = undeadModifier->description.substr(0, undeadModifier->description.size()-2);//trim value
  99. addNewBonus(undeadModifier);
  100. }
  101. }
  102. else if(undeadModifier)
  103. removeBonus(undeadModifier);
  104. }
  105. void CArmedInstance::armyChanged()
  106. {
  107. updateMoraleBonusFromArmy();
  108. }
  109. CBonusSystemNode * CArmedInstance::whereShouldBeAttached(CGameState *gs)
  110. {
  111. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  112. return gs->getPlayer(tempOwner);
  113. else
  114. return &gs->globalEffects;
  115. }
  116. CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
  117. {
  118. return this;
  119. }