ArmyManager.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 cre = dynamic_cast<const CCreature*>(i.second->getType());
  33. auto & slotInfp = creToPower[cre];
  34. slotInfp.creature = cre;
  35. slotInfp.power += i.second->getPower();
  36. slotInfp.count += i.second->count;
  37. }
  38. }
  39. for(auto pair : creToPower)
  40. resultingArmy.push_back(pair.second);
  41. boost::sort(resultingArmy, [](const SlotInfo & left, const SlotInfo & right) -> bool
  42. {
  43. return left.power > right.power;
  44. });
  45. return resultingArmy;
  46. }
  47. std::vector<SlotInfo>::iterator ArmyManager::getWeakestCreature(std::vector<SlotInfo> & army) const
  48. {
  49. auto weakest = boost::min_element(army, [](const SlotInfo & left, const SlotInfo & right) -> bool
  50. {
  51. if(left.creature->getLevel() != right.creature->getLevel())
  52. return left.creature->getLevel() < right.creature->getLevel();
  53. return left.creature->getMovementRange() > right.creature->getMovementRange();
  54. });
  55. return weakest;
  56. }
  57. std::vector<SlotInfo> ArmyManager::getBestArmy(const CCreatureSet * target, const CCreatureSet * source) const
  58. {
  59. auto resultingArmy = getSortedSlots(target, source);
  60. if(resultingArmy.size() > GameConstants::ARMY_SIZE)
  61. {
  62. resultingArmy.resize(GameConstants::ARMY_SIZE);
  63. }
  64. else if(source->needsLastStack())
  65. {
  66. auto weakest = getWeakestCreature(resultingArmy);
  67. if(weakest->count == 1)
  68. {
  69. resultingArmy.erase(weakest);
  70. }
  71. else
  72. {
  73. weakest->power -= weakest->power / weakest->count;
  74. weakest->count--;
  75. }
  76. }
  77. return resultingArmy;
  78. }
  79. bool ArmyManager::canGetArmy(const CArmedInstance * target, const CArmedInstance * source) const
  80. {
  81. //TODO: merge with pickBestCreatures
  82. //if (ai->primaryHero().h == source)
  83. if(target->tempOwner != source->tempOwner)
  84. {
  85. logAi->error("Why are we even considering exchange between heroes from different players?");
  86. return false;
  87. }
  88. return 0 < howManyReinforcementsCanGet(target, source);
  89. }
  90. ui64 ArmyManager::howManyReinforcementsCanBuy(const CCreatureSet * h, const CGDwelling * t) const
  91. {
  92. ui64 aivalue = 0;
  93. TResources availableRes = cb->getResourceAmount();
  94. int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount();
  95. for(auto const & dc : t->creatures)
  96. {
  97. creInfo ci = infoFromDC(dc);
  98. if(!ci.count || ci.creID == CreatureID::NONE)
  99. continue;
  100. vstd::amin(ci.count, availableRes / ci.cre->getFullRecruitCost()); //max count we can afford
  101. if(ci.count && ci.creID != CreatureID::NONE) //valid creature at this level
  102. {
  103. //can be merged with another stack?
  104. SlotID dst = h->getSlotFor(ci.creID);
  105. if(!h->hasStackAtSlot(dst)) //need another new slot for this stack
  106. {
  107. if(!freeHeroSlots) //no more place for stacks
  108. continue;
  109. else
  110. freeHeroSlots--; //new slot will be occupied
  111. }
  112. //we found matching occupied or free slot
  113. aivalue += ci.count * ci.cre->getAIValue();
  114. availableRes -= ci.cre->getFullRecruitCost() * ci.count;
  115. }
  116. }
  117. return aivalue;
  118. }
  119. ui64 ArmyManager::howManyReinforcementsCanGet(const CCreatureSet * target, const CCreatureSet * source) const
  120. {
  121. auto bestArmy = getBestArmy(target, source);
  122. uint64_t newArmy = 0;
  123. uint64_t oldArmy = target->getArmyStrength();
  124. for(auto & slot : bestArmy)
  125. {
  126. newArmy += slot.power;
  127. }
  128. return newArmy > oldArmy ? newArmy - oldArmy : 0;
  129. }