ArmyManager.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "../../lib/mapObjects/MapObjects.h"
  13. void ArmyManager::init(CPlayerSpecificInfoCallback * CB)
  14. {
  15. cb = CB;
  16. }
  17. void ArmyManager::setAI(VCAI * AI)
  18. {
  19. ai = AI;
  20. }
  21. std::vector<SlotInfo> ArmyManager::getSortedSlots(const CCreatureSet * target, const CCreatureSet * source) const
  22. {
  23. const CCreatureSet * armies[] = { target, source };
  24. //we calculate total strength for each creature type available in armies
  25. std::map<const CCreature *, SlotInfo> creToPower;
  26. std::vector<SlotInfo> resultingArmy;
  27. for(auto armyPtr : armies)
  28. {
  29. for(auto & i : armyPtr->Slots())
  30. {
  31. auto cre = dynamic_cast<const CCreature*>(i.second->getType());
  32. auto & slotInfp = creToPower[cre];
  33. slotInfp.creature = cre;
  34. slotInfp.power += i.second->getPower();
  35. slotInfp.count += i.second->getCount();
  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->getLevel() != right.creature->getLevel())
  51. return left.creature->getLevel() < right.creature->getLevel();
  52. return left.creature->getMovementRange() > right.creature->getMovementRange();
  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. TResources availableRes = cb->getResourceAmount();
  93. int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount();
  94. for(auto const & dc : t->creatures)
  95. {
  96. creInfo ci = infoFromDC(dc);
  97. if(!ci.count || ci.creID == CreatureID::NONE)
  98. continue;
  99. vstd::amin(ci.count, availableRes / ci.cre->getFullRecruitCost()); //max count we can afford
  100. if(ci.count && ci.creID != CreatureID::NONE) //valid creature at this level
  101. {
  102. //can be merged with another stack?
  103. SlotID dst = h->getSlotFor(ci.creID);
  104. if(!h->hasStackAtSlot(dst)) //need another new slot for this stack
  105. {
  106. if(!freeHeroSlots) //no more place for stacks
  107. continue;
  108. else
  109. freeHeroSlots--; //new slot will be occupied
  110. }
  111. //we found matching occupied or free slot
  112. aivalue += ci.count * ci.cre->getAIValue();
  113. availableRes -= ci.cre->getFullRecruitCost() * ci.count;
  114. }
  115. }
  116. return aivalue;
  117. }
  118. ui64 ArmyManager::howManyReinforcementsCanGet(const CCreatureSet * target, const CCreatureSet * source) const
  119. {
  120. auto bestArmy = getBestArmy(target, source);
  121. uint64_t newArmy = 0;
  122. uint64_t oldArmy = target->getArmyStrength();
  123. for(auto & slot : bestArmy)
  124. {
  125. newArmy += slot.power;
  126. }
  127. return newArmy > oldArmy ? newArmy - oldArmy : 0;
  128. }