ArmyManager.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "../Engine/Nullkiller.h"
  13. #include "../../../CCallback.h"
  14. #include "../../../lib/mapObjects/MapObjects.h"
  15. class StackUpgradeInfo
  16. {
  17. public:
  18. CreatureID initialCreature;
  19. CreatureID upgradedCreature;
  20. TResources cost;
  21. int count;
  22. uint64_t upgradeValue;
  23. StackUpgradeInfo(CreatureID initial, CreatureID upgraded, int count)
  24. :initialCreature(initial), upgradedCreature(upgraded), count(count)
  25. {
  26. cost = (upgradedCreature.toCreature()->cost - initialCreature.toCreature()->cost) * count;
  27. upgradeValue = (upgradedCreature.toCreature()->AIValue - initialCreature.toCreature()->AIValue) * count;
  28. }
  29. };
  30. std::vector<SlotInfo> ArmyManager::getSortedSlots(const CCreatureSet * target, const CCreatureSet * source) const
  31. {
  32. const CCreatureSet * armies[] = { target, source };
  33. //we calculate total strength for each creature type available in armies
  34. std::map<const CCreature *, SlotInfo> creToPower;
  35. std::vector<SlotInfo> resultingArmy;
  36. for(auto armyPtr : armies)
  37. {
  38. for(auto & i : armyPtr->Slots())
  39. {
  40. auto & slotInfp = creToPower[i.second->type];
  41. slotInfp.creature = i.second->type;
  42. slotInfp.power += i.second->getPower();
  43. slotInfp.count += i.second->count;
  44. }
  45. }
  46. for(auto pair : creToPower)
  47. resultingArmy.push_back(pair.second);
  48. boost::sort(resultingArmy, [](const SlotInfo & left, const SlotInfo & right) -> bool
  49. {
  50. return left.power > right.power;
  51. });
  52. return resultingArmy;
  53. }
  54. std::vector<SlotInfo>::iterator ArmyManager::getWeakestCreature(std::vector<SlotInfo> & army) const
  55. {
  56. auto weakest = boost::min_element(army, [](const SlotInfo & left, const SlotInfo & right) -> bool
  57. {
  58. if(left.creature->level != right.creature->level)
  59. return left.creature->level < right.creature->level;
  60. return left.creature->Speed() > right.creature->Speed();
  61. });
  62. return weakest;
  63. }
  64. std::vector<SlotInfo> ArmyManager::getBestArmy(const CCreatureSet * target, const CCreatureSet * source) const
  65. {
  66. auto resultingArmy = getSortedSlots(target, source);
  67. if(resultingArmy.size() > GameConstants::ARMY_SIZE)
  68. {
  69. resultingArmy.resize(GameConstants::ARMY_SIZE);
  70. }
  71. else if(source->needsLastStack())
  72. {
  73. auto weakest = getWeakestCreature(resultingArmy);
  74. if(weakest->count == 1)
  75. {
  76. resultingArmy.erase(weakest);
  77. }
  78. else
  79. {
  80. weakest->power -= weakest->power / weakest->count;
  81. weakest->count--;
  82. }
  83. }
  84. return resultingArmy;
  85. }
  86. bool ArmyManager::canGetArmy(const CArmedInstance * target, const CArmedInstance * source) const
  87. {
  88. //TODO: merge with pickBestCreatures
  89. //if (ai->primaryHero().h == source)
  90. if(target->tempOwner != source->tempOwner)
  91. {
  92. logAi->error("Why are we even considering exchange between heroes from different players?");
  93. return false;
  94. }
  95. return 0 < howManyReinforcementsCanGet(target, source);
  96. }
  97. ui64 ArmyManager::howManyReinforcementsCanBuy(const CCreatureSet * h, const CGDwelling * t) const
  98. {
  99. ui64 aivalue = 0;
  100. auto army = getArmyAvailableToBuy(h, t);
  101. for(const creInfo & ci : army)
  102. {
  103. aivalue += ci.count * ci.cre->AIValue;
  104. }
  105. return aivalue;
  106. }
  107. std::vector<creInfo> ArmyManager::getArmyAvailableToBuy(const CCreatureSet * hero, const CGDwelling * dwelling) const
  108. {
  109. auto availableRes = cb->getResourceAmount();
  110. std::vector<creInfo> creaturesInDwellings;
  111. int freeHeroSlots = GameConstants::ARMY_SIZE - hero->stacksCount();
  112. for(int i = dwelling->creatures.size() - 1; i >= 0; i--)
  113. {
  114. auto ci = infoFromDC(dwelling->creatures[i]);
  115. if(!ci.count || ci.creID == -1)
  116. continue;
  117. SlotID dst = hero->getSlotFor(ci.creID);
  118. if(!hero->hasStackAtSlot(dst)) //need another new slot for this stack
  119. {
  120. if(!freeHeroSlots) //no more place for stacks
  121. continue;
  122. else
  123. freeHeroSlots--; //new slot will be occupied
  124. }
  125. vstd::amin(ci.count, availableRes / ci.cre->cost); //max count we can afford
  126. if(!ci.count)
  127. continue;
  128. ci.level = i; //this is important for Dungeon Summoning Portal
  129. creaturesInDwellings.push_back(ci);
  130. availableRes -= ci.cre->cost * ci.count;
  131. }
  132. return creaturesInDwellings;
  133. }
  134. ui64 ArmyManager::howManyReinforcementsCanGet(const CCreatureSet * target, const CCreatureSet * source) const
  135. {
  136. auto bestArmy = getBestArmy(target, source);
  137. uint64_t newArmy = 0;
  138. uint64_t oldArmy = target->getArmyStrength();
  139. for(auto & slot : bestArmy)
  140. {
  141. newArmy += slot.power;
  142. }
  143. return newArmy > oldArmy ? newArmy - oldArmy : 0;
  144. }
  145. uint64_t ArmyManager::evaluateStackPower(const CCreature * creature, int count) const
  146. {
  147. return creature->AIValue * count;
  148. }
  149. SlotInfo ArmyManager::getTotalCreaturesAvailable(CreatureID creatureID) const
  150. {
  151. auto creatureInfo = totalArmy.find(creatureID);
  152. return creatureInfo == totalArmy.end() ? SlotInfo() : creatureInfo->second;
  153. }
  154. void ArmyManager::update()
  155. {
  156. logAi->trace("Start analysing army");
  157. std::vector<const CCreatureSet *> total;
  158. auto heroes = cb->getHeroesInfo();
  159. auto towns = cb->getTownsInfo();
  160. std::copy(heroes.begin(), heroes.end(), std::back_inserter(total));
  161. std::copy(towns.begin(), towns.end(), std::back_inserter(total));
  162. totalArmy.clear();
  163. for(auto army : total)
  164. {
  165. for(auto slot : army->Slots())
  166. {
  167. totalArmy[slot.second->getCreatureID()].count += slot.second->count;
  168. }
  169. }
  170. for(auto army : totalArmy)
  171. {
  172. army.second.creature = army.first.toCreature();
  173. army.second.power = evaluateStackPower(army.second.creature, army.second.count);
  174. }
  175. }
  176. std::vector<SlotInfo> ArmyManager::convertToSlots(const CCreatureSet * army) const
  177. {
  178. std::vector<SlotInfo> result;
  179. for(auto slot : army->Slots())
  180. {
  181. SlotInfo slotInfo;
  182. slotInfo.creature = slot.second->getCreatureID().toCreature();
  183. slotInfo.count = slot.second->count;
  184. slotInfo.power = evaluateStackPower(slotInfo.creature, slotInfo.count);
  185. result.push_back(slotInfo);
  186. }
  187. return result;
  188. }
  189. std::vector<StackUpgradeInfo> ArmyManager::getHillFortUpgrades(const CCreatureSet * army) const
  190. {
  191. std::vector<StackUpgradeInfo> upgrades;
  192. for(auto creature : army->Slots())
  193. {
  194. CreatureID initial = creature.second->getCreatureID();
  195. auto possibleUpgrades = initial.toCreature()->upgrades;
  196. if(possibleUpgrades.empty())
  197. continue;
  198. CreatureID strongestUpgrade = *vstd::minElementByFun(possibleUpgrades, [](CreatureID cre) -> uint64_t
  199. {
  200. return cre.toCreature()->AIValue;
  201. });
  202. StackUpgradeInfo upgrade = StackUpgradeInfo(initial, strongestUpgrade, creature.second->count);
  203. if(initial.toCreature()->level == 1)
  204. upgrade.cost = TResources();
  205. upgrades.push_back(upgrade);
  206. }
  207. return upgrades;
  208. }
  209. std::vector<StackUpgradeInfo> ArmyManager::getDwellingUpgrades(const CCreatureSet * army, const CGDwelling * dwelling) const
  210. {
  211. std::vector<StackUpgradeInfo> upgrades;
  212. for(auto creature : army->Slots())
  213. {
  214. CreatureID initial = creature.second->getCreatureID();
  215. auto possibleUpgrades = initial.toCreature()->upgrades;
  216. vstd::erase_if(possibleUpgrades, [&](CreatureID creID) -> bool
  217. {
  218. for(auto pair : dwelling->creatures)
  219. {
  220. if(vstd::contains(pair.second, creID))
  221. return false;
  222. }
  223. return true;
  224. });
  225. if(possibleUpgrades.empty())
  226. continue;
  227. CreatureID strongestUpgrade = *vstd::minElementByFun(possibleUpgrades, [](CreatureID cre) -> uint64_t
  228. {
  229. return cre.toCreature()->AIValue;
  230. });
  231. StackUpgradeInfo upgrade = StackUpgradeInfo(initial, strongestUpgrade, creature.second->count);
  232. upgrades.push_back(upgrade);
  233. }
  234. return upgrades;
  235. }
  236. std::vector<StackUpgradeInfo> ArmyManager::getPossibleUpgrades(const CCreatureSet * army, const CGObjectInstance * upgrader) const
  237. {
  238. std::vector<StackUpgradeInfo> upgrades;
  239. if(upgrader->ID == Obj::HILL_FORT)
  240. {
  241. upgrades = getHillFortUpgrades(army);
  242. }
  243. else
  244. {
  245. auto dwelling = dynamic_cast<const CGDwelling *>(upgrader);
  246. if(dwelling)
  247. {
  248. upgrades = getDwellingUpgrades(army, dwelling);
  249. }
  250. }
  251. return upgrades;
  252. }
  253. ArmyUpgradeInfo ArmyManager::calculateCreateresUpgrade(
  254. const CCreatureSet * army,
  255. const CGObjectInstance * upgrader,
  256. const TResources & availableResources) const
  257. {
  258. if(!upgrader)
  259. return ArmyUpgradeInfo();
  260. std::vector<StackUpgradeInfo> upgrades = getPossibleUpgrades(army, upgrader);
  261. vstd::erase_if(upgrades, [&](const StackUpgradeInfo & u) -> bool
  262. {
  263. return !availableResources.canAfford(u.cost);
  264. });
  265. if(upgrades.empty())
  266. return ArmyUpgradeInfo();
  267. std::sort(upgrades.begin(), upgrades.end(), [](const StackUpgradeInfo & u1, const StackUpgradeInfo & u2) -> bool
  268. {
  269. return u1.upgradeValue > u2.upgradeValue;
  270. });
  271. TResources resourcesLeft = availableResources;
  272. ArmyUpgradeInfo result;
  273. result.resultingArmy = convertToSlots(army);
  274. for(auto upgrade : upgrades)
  275. {
  276. if(resourcesLeft.canAfford(upgrade.cost))
  277. {
  278. SlotInfo upgradedArmy;
  279. upgradedArmy.creature = upgrade.upgradedCreature.toCreature();
  280. upgradedArmy.count = upgrade.count;
  281. upgradedArmy.power = evaluateStackPower(upgradedArmy.creature, upgradedArmy.count);
  282. auto slotToReplace = std::find_if(result.resultingArmy.begin(), result.resultingArmy.end(), [&](const SlotInfo & slot) -> bool {
  283. return slot.count == upgradedArmy.count && slot.creature->idNumber == upgrade.initialCreature;
  284. });
  285. resourcesLeft -= upgrade.cost;
  286. result.upgradeCost += upgrade.cost;
  287. result.upgradeValue += upgrade.upgradeValue;
  288. *slotToReplace = upgradedArmy;
  289. }
  290. }
  291. return result;
  292. }