ArmyManager.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * BuildingManager.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 "ArmyManager.h"
  12. #include "../../CCallback.h"
  13. #include "../../lib/mapObjects/MapObjects.h"
  14. void ArmyManager::init(CPlayerSpecificInfoCallback * CB)
  15. {
  16. cb = CB;
  17. }
  18. void ArmyManager::setAI(VCAI * AI)
  19. {
  20. ai = AI;
  21. }
  22. std::vector<SlotInfo> ArmyManager::getSortedSlots(const CCreatureSet * target, const CCreatureSet * source) const
  23. {
  24. const CCreatureSet * armies[] = { target, source };
  25. //we calculate total strength for each creature type available in armies
  26. std::map<const CCreature *, SlotInfo> creToPower;
  27. std::vector<SlotInfo> resultingArmy;
  28. for(auto armyPtr : armies)
  29. {
  30. for(auto & i : armyPtr->Slots())
  31. {
  32. auto & slotInfp = creToPower[i.second->type];
  33. slotInfp.creature = i.second->type;
  34. slotInfp.power += i.second->getPower();
  35. slotInfp.count += i.second->count;
  36. }
  37. }
  38. for(auto pair : creToPower)
  39. resultingArmy.push_back(pair.second);
  40. boost::sort(resultingArmy, [](const SlotInfo & left, const SlotInfo & right) -> bool
  41. {
  42. return left.power > right.power;
  43. });
  44. return resultingArmy;
  45. }
  46. std::vector<SlotInfo>::iterator ArmyManager::getWeakestCreature(std::vector<SlotInfo> & army) const
  47. {
  48. auto weakest = boost::min_element(army, [](const SlotInfo & left, const SlotInfo & right) -> bool
  49. {
  50. if(left.creature->level != right.creature->level)
  51. return left.creature->level < right.creature->level;
  52. return left.creature->Speed() > right.creature->Speed();
  53. });
  54. return weakest;
  55. }
  56. std::vector<SlotInfo> ArmyManager::getBestArmy(const CCreatureSet * target, const CCreatureSet * source) const
  57. {
  58. auto resultingArmy = getSortedSlots(target, source);
  59. if(resultingArmy.size() > GameConstants::ARMY_SIZE)
  60. {
  61. resultingArmy.resize(GameConstants::ARMY_SIZE);
  62. }
  63. else if(source->needsLastStack())
  64. {
  65. auto weakest = getWeakestCreature(resultingArmy);
  66. if(weakest->count == 1)
  67. {
  68. resultingArmy.erase(weakest);
  69. }
  70. else
  71. {
  72. weakest->power -= weakest->power / weakest->count;
  73. weakest->count--;
  74. }
  75. }
  76. return resultingArmy;
  77. }
  78. bool ArmyManager::canGetArmy(const CArmedInstance * target, const CArmedInstance * source) const
  79. {
  80. //TODO: merge with pickBestCreatures
  81. //if (ai->primaryHero().h == source)
  82. if(target->tempOwner != source->tempOwner)
  83. {
  84. logAi->error("Why are we even considering exchange between heroes from different players?");
  85. return false;
  86. }
  87. return 0 < howManyReinforcementsCanGet(target, source);
  88. }
  89. ui64 ArmyManager::howManyReinforcementsCanBuy(const CCreatureSet * h, const CGDwelling * t) const
  90. {
  91. ui64 aivalue = 0;
  92. auto army = getArmyAvailableToBuy(h, t);
  93. for(const creInfo & ci : army)
  94. {
  95. aivalue += ci.count * ci.cre->AIValue;
  96. }
  97. return aivalue;
  98. }
  99. std::vector<creInfo> ArmyManager::getArmyAvailableToBuy(const CCreatureSet * hero, const CGDwelling * dwelling) const
  100. {
  101. auto availableRes = cb->getResourceAmount();
  102. std::vector<creInfo> creaturesInDwellings;
  103. int freeHeroSlots = GameConstants::ARMY_SIZE - hero->stacksCount();
  104. for(int i = dwelling->creatures.size() - 1; i >= 0; i--)
  105. {
  106. auto ci = infoFromDC(dwelling->creatures[i]);
  107. if(!ci.count || ci.creID == -1)
  108. continue;
  109. SlotID dst = hero->getSlotFor(ci.creID);
  110. if(!hero->hasStackAtSlot(dst)) //need another new slot for this stack
  111. {
  112. if(!freeHeroSlots) //no more place for stacks
  113. continue;
  114. else
  115. freeHeroSlots--; //new slot will be occupied
  116. }
  117. vstd::amin(ci.count, availableRes / ci.cre->cost); //max count we can afford
  118. if(!ci.count)
  119. continue;
  120. ci.level = i; //this is important for Dungeon Summoning Portal
  121. creaturesInDwellings.push_back(ci);
  122. availableRes -= ci.cre->cost * ci.count;
  123. }
  124. return creaturesInDwellings;
  125. }
  126. ui64 ArmyManager::howManyReinforcementsCanGet(const CCreatureSet * target, const CCreatureSet * source) const
  127. {
  128. auto bestArmy = getBestArmy(target, source);
  129. uint64_t newArmy = 0;
  130. uint64_t oldArmy = target->getArmyStrength();
  131. for(auto & slot : bestArmy)
  132. {
  133. newArmy += slot.power;
  134. }
  135. return newArmy > oldArmy ? newArmy - oldArmy : 0;
  136. }