ArmyManager.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. }
  137. uint64_t ArmyManager::evaluateStackPower(const CCreature * creature, int count) const
  138. {
  139. return creature->AIValue * count;
  140. }
  141. SlotInfo ArmyManager::getTotalCreaturesAvailable(CreatureID creatureID) const
  142. {
  143. auto creatureInfo = totalArmy.find(creatureID);
  144. return creatureInfo == totalArmy.end() ? SlotInfo() : creatureInfo->second;
  145. }
  146. void ArmyManager::update()
  147. {
  148. logAi->trace("Start analysing army");
  149. std::vector<const CCreatureSet *> total;
  150. auto heroes = cb->getHeroesInfo();
  151. auto towns = cb->getTownsInfo();
  152. std::copy(heroes.begin(), heroes.end(), std::back_inserter(total));
  153. std::copy(towns.begin(), towns.end(), std::back_inserter(total));
  154. totalArmy.clear();
  155. for(auto army : total)
  156. {
  157. for(auto slot : army->Slots())
  158. {
  159. totalArmy[slot.second->getCreatureID()].count += slot.second->count;
  160. }
  161. }
  162. for(auto army : totalArmy)
  163. {
  164. army.second.creature = army.first.toCreature();
  165. army.second.power = evaluateStackPower(army.second.creature, army.second.count);
  166. }
  167. }
  168. struct UpgradeInfo
  169. {
  170. const CCreature * initialCreature;
  171. const CCreature * upgradedCreature;
  172. TResources cost;
  173. int count;
  174. uint64_t upgradeValue;
  175. UpgradeInfo(CreatureID initial, CreatureID upgraded, int count)
  176. :initialCreature(initial.toCreature()), upgradedCreature(upgraded.toCreature()), count(count)
  177. {
  178. cost = (upgradedCreature->cost - initialCreature->cost) * count;
  179. upgradeValue = (upgradedCreature->AIValue - initialCreature->AIValue) * count;
  180. }
  181. };
  182. std::vector<SlotInfo> ArmyManager::convertToSlots(const CCreatureSet * army) const
  183. {
  184. std::vector<SlotInfo> result;
  185. for(auto slot : army->Slots())
  186. {
  187. SlotInfo slotInfo;
  188. slotInfo.creature = slot.second->getCreatureID().toCreature();
  189. slotInfo.count = slot.second->count;
  190. slotInfo.power = evaluateStackPower(slotInfo.creature, slotInfo.count);
  191. result.push_back(slotInfo);
  192. }
  193. return result;
  194. }
  195. std::vector<UpgradeInfo> ArmyManager::getHillFortUpgrades(const CCreatureSet * army) const
  196. {
  197. std::vector<UpgradeInfo> upgrades;
  198. for(auto creature : army->Slots())
  199. {
  200. CreatureID initial = creature.second->getCreatureID();
  201. auto possibleUpgrades = initial.toCreature()->upgrades;
  202. if(possibleUpgrades.empty())
  203. continue;
  204. CreatureID strongestUpgrade = *vstd::minElementByFun(possibleUpgrades, [](CreatureID cre) -> uint64_t
  205. {
  206. return cre.toCreature()->AIValue;
  207. });
  208. UpgradeInfo upgrade = UpgradeInfo(initial, strongestUpgrade, creature.second->count);
  209. if(initial.toCreature()->level == 1)
  210. upgrade.cost = TResources();
  211. upgrades.push_back(upgrade);
  212. }
  213. return upgrades;
  214. }
  215. std::vector<UpgradeInfo> ArmyManager::getDwellingUpgrades(const CCreatureSet * army, const CGDwelling * dwelling) const
  216. {
  217. std::vector<UpgradeInfo> upgrades;
  218. return upgrades;
  219. }
  220. std::vector<UpgradeInfo> ArmyManager::getPossibleUpgrades(const CCreatureSet * army, const CGObjectInstance * upgrader) const
  221. {
  222. std::vector<UpgradeInfo> upgrades;
  223. if(upgrader->ID == Obj::HILL_FORT)
  224. {
  225. upgrades = getHillFortUpgrades(army);
  226. }
  227. else
  228. {
  229. auto dwelling = dynamic_cast<const CGDwelling *>(upgrader);
  230. if(dwelling)
  231. {
  232. upgrades = getDwellingUpgrades(army, dwelling);
  233. }
  234. }
  235. return upgrades;
  236. }
  237. ArmyUpgradeInfo ArmyManager::calculateCreateresUpgrade(
  238. const CCreatureSet * army,
  239. const CGObjectInstance * upgrader,
  240. const TResources & availableResources) const
  241. {
  242. std::vector<UpgradeInfo> upgrades = getPossibleUpgrades(army, upgrader);
  243. vstd::erase_if(upgrades, [&](const UpgradeInfo & u) -> bool
  244. {
  245. return !availableResources.canAfford(u.cost);
  246. });
  247. if(upgrades.empty())
  248. return ArmyUpgradeInfo();
  249. std::sort(upgrades.begin(), upgrades.end(), [](const UpgradeInfo & u1, const UpgradeInfo & u2) -> bool
  250. {
  251. return u1.upgradeValue > u2.upgradeValue;
  252. });
  253. TResources resourcesLeft = availableResources;
  254. ArmyUpgradeInfo result;
  255. result.resultingArmy = convertToSlots(army);
  256. for(auto upgrade : upgrades)
  257. {
  258. if(resourcesLeft.canAfford(upgrade.cost))
  259. {
  260. SlotInfo upgradedArmy;
  261. upgradedArmy.creature = upgrade.upgradedCreature;
  262. upgradedArmy.count = upgrade.count;
  263. upgradedArmy.power = evaluateStackPower(upgradedArmy.creature, upgradedArmy.count);
  264. auto slotToReplace = std::find_if(result.resultingArmy.begin(), result.resultingArmy.end(), [&](const SlotInfo & slot) -> bool {
  265. return slot.count == upgradedArmy.count && slot.creature == upgrade.initialCreature;
  266. });
  267. resourcesLeft -= upgrade.cost;
  268. result.upgradeCost += upgrade.cost;
  269. result.upgradeValue += upgrade.upgradeValue;
  270. *slotToReplace = upgradedArmy;
  271. }
  272. }
  273. return result;
  274. }