ArmyManager.cpp 9.4 KB

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