Actors.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * AINodeStorage.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 "Actors.h"
  12. #include "../VCAI.h"
  13. #include "../Engine/Nullkiller.h"
  14. #include "../../../CCallback.h"
  15. #include "../../../lib/mapping/CMap.h"
  16. #include "../../../lib/mapObjects/MapObjects.h"
  17. #include "Actions/BuyArmyAction.h"
  18. CCreatureSet emptyArmy;
  19. bool HeroExchangeArmy::needsLastStack() const
  20. {
  21. return true;
  22. }
  23. std::shared_ptr<SpecialAction> HeroExchangeArmy::getActorAction() const
  24. {
  25. std::shared_ptr<SpecialAction> result;
  26. if(requireBuyArmy)
  27. {
  28. result.reset(new AIPathfinding::BuyArmyAction());
  29. }
  30. return result;
  31. }
  32. ChainActor::ChainActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask)
  33. :hero(hero), heroRole(heroRole), isMovable(true), chainMask(chainMask), creatureSet(hero),
  34. baseActor(this), carrierParent(nullptr), otherParent(nullptr), actorExchangeCount(1), armyCost(), actorAction()
  35. {
  36. initialPosition = hero->visitablePos();
  37. layer = hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND;
  38. initialMovement = hero->movement;
  39. initialTurn = 0;
  40. armyValue = hero->getArmyStrength();
  41. heroFightingStrength = hero->getFightingStrength();
  42. }
  43. ChainActor::ChainActor(const ChainActor * carrier, const ChainActor * other, const CCreatureSet * heroArmy)
  44. :hero(carrier->hero), heroRole(carrier->heroRole), isMovable(true), creatureSet(heroArmy), chainMask(carrier->chainMask | other->chainMask),
  45. baseActor(this), carrierParent(carrier), otherParent(other), heroFightingStrength(carrier->heroFightingStrength),
  46. actorExchangeCount(carrier->actorExchangeCount + other->actorExchangeCount), armyCost(carrier->armyCost + other->armyCost), actorAction()
  47. {
  48. armyValue = heroArmy->getArmyStrength();
  49. }
  50. ChainActor::ChainActor(const CGObjectInstance * obj, const CCreatureSet * creatureSet, uint64_t chainMask, int initialTurn)
  51. :hero(nullptr), heroRole(HeroRole::MAIN), isMovable(false), creatureSet(creatureSet), chainMask(chainMask),
  52. baseActor(this), carrierParent(nullptr), otherParent(nullptr), initialTurn(initialTurn), initialMovement(0),
  53. heroFightingStrength(0), actorExchangeCount(1), armyCost(), actorAction()
  54. {
  55. initialPosition = obj->visitablePos();
  56. layer = EPathfindingLayer::LAND;
  57. armyValue = creatureSet->getArmyStrength();
  58. }
  59. std::string ChainActor::toString() const
  60. {
  61. return hero->name;
  62. }
  63. ObjectActor::ObjectActor(const CGObjectInstance * obj, const CCreatureSet * army, uint64_t chainMask, int initialTurn)
  64. :ChainActor(obj, army, chainMask, initialTurn), object(obj)
  65. {
  66. }
  67. const CGObjectInstance * ObjectActor::getActorObject() const
  68. {
  69. return object;
  70. }
  71. std::string ObjectActor::toString() const
  72. {
  73. return object->getObjectName() + " at " + object->visitablePos().toString();
  74. }
  75. HeroActor::HeroActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t chainMask, const Nullkiller * ai)
  76. :ChainActor(hero, heroRole, chainMask)
  77. {
  78. exchangeMap.reset(new HeroExchangeMap(this, ai));
  79. setupSpecialActors();
  80. }
  81. HeroActor::HeroActor(
  82. const ChainActor * carrier,
  83. const ChainActor * other,
  84. const HeroExchangeArmy * army,
  85. const Nullkiller * ai)
  86. :ChainActor(carrier, other, army)
  87. {
  88. exchangeMap.reset(new HeroExchangeMap(this, ai));
  89. armyCost += army->armyCost;
  90. actorAction = army->getActorAction();
  91. setupSpecialActors();
  92. }
  93. void ChainActor::setBaseActor(HeroActor * base)
  94. {
  95. baseActor = base;
  96. hero = base->hero;
  97. heroRole = base->heroRole;
  98. layer = base->layer;
  99. initialMovement = base->initialMovement;
  100. initialTurn = base->initialTurn;
  101. armyValue = base->armyValue;
  102. chainMask = base->chainMask;
  103. creatureSet = base->creatureSet;
  104. isMovable = base->isMovable;
  105. heroFightingStrength = base->heroFightingStrength;
  106. armyCost = base->armyCost;
  107. actorAction = base->actorAction;
  108. }
  109. void HeroActor::setupSpecialActors()
  110. {
  111. auto allActors = std::vector<ChainActor *>{ this };
  112. for(ChainActor & specialActor : specialActors)
  113. {
  114. specialActor.setBaseActor(this);
  115. allActors.push_back(&specialActor);
  116. }
  117. for(int i = 0; i <= SPECIAL_ACTORS_COUNT; i++)
  118. {
  119. ChainActor * actor = allActors[i];
  120. actor->allowBattle = (i & 1) > 0;
  121. actor->allowSpellCast = (i & 2) > 0;
  122. actor->allowUseResources = (i & 4) > 0;
  123. actor->battleActor = allActors[i | 1];
  124. actor->castActor = allActors[i | 2];
  125. actor->resourceActor = allActors[i | 4];
  126. }
  127. }
  128. ChainActor * ChainActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  129. {
  130. return baseActor->exchange(specialActor, other);
  131. }
  132. bool ChainActor::canExchange(const ChainActor * other) const
  133. {
  134. return isMovable && baseActor->canExchange(other);
  135. }
  136. namespace vstd
  137. {
  138. template <class M, class Key, class F>
  139. typename M::mapped_type & getOrCompute(M &m, Key const& k, F f)
  140. {
  141. typedef typename M::mapped_type V;
  142. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  143. V &v = r.first->second;
  144. if(r.second)
  145. f(v);
  146. return v;
  147. }
  148. }
  149. bool HeroActor::canExchange(const ChainActor * other) const
  150. {
  151. return exchangeMap->canExchange(other);
  152. }
  153. bool HeroExchangeMap::canExchange(const ChainActor * other)
  154. {
  155. return vstd::getOrCompute(canExchangeCache, other, [&](bool & result) {
  156. result = (actor->chainMask & other->chainMask) == 0;
  157. if(result)
  158. {
  159. TResources resources = ai->cb->getResourceAmount();
  160. if(!resources.canAfford(actor->armyCost + other->armyCost))
  161. {
  162. result = false;
  163. #if PATHFINDER_TRACE_LEVEL >= 2
  164. logAi->trace(
  165. "Can not afford exchange because of total cost %s but we have %s",
  166. (actor->armyCost + other->armyCost).toString(),
  167. resources.toString());
  168. #endif
  169. return;
  170. }
  171. auto upgradeInfo = ai->armyManager->calculateCreateresUpgrade(
  172. actor->creatureSet,
  173. other->getActorObject(),
  174. resources - actor->armyCost - other->armyCost);
  175. uint64_t reinforcment = upgradeInfo.upgradeValue;
  176. if(other->creatureSet->Slots().size())
  177. reinforcment += ai->armyManager->howManyReinforcementsCanGet(actor->hero, actor->creatureSet, other->creatureSet);
  178. auto obj = other->getActorObject();
  179. if(obj && obj->ID == Obj::TOWN)
  180. {
  181. reinforcment += ai->armyManager->howManyReinforcementsCanBuy(actor->creatureSet, ai->cb->getTown(obj->id));
  182. }
  183. #if PATHFINDER_TRACE_LEVEL >= 2
  184. logAi->trace(
  185. "Exchange %s->%s reinforcement: %d, %f%%",
  186. actor->toString(),
  187. other->toString(),
  188. reinforcment,
  189. 100.0f * reinforcment / actor->armyValue);
  190. #endif
  191. result = reinforcment > actor->armyValue / 10 || reinforcment > 1000;
  192. }
  193. });
  194. }
  195. ChainActor * HeroActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  196. {
  197. const ChainActor * otherBase = other->baseActor;
  198. HeroActor * result = exchangeMap->exchange(otherBase);
  199. if(specialActor == this)
  200. return result;
  201. int index = vstd::find_pos_if(specialActors, [specialActor](const ChainActor & actor) -> bool
  202. {
  203. return &actor == specialActor;
  204. });
  205. return &result->specialActors[index];
  206. }
  207. HeroExchangeMap::HeroExchangeMap(const HeroActor * actor, const Nullkiller * ai)
  208. :actor(actor), ai(ai)
  209. {
  210. }
  211. HeroExchangeMap::~HeroExchangeMap()
  212. {
  213. for(auto & exchange : exchangeMap)
  214. {
  215. delete exchange.second->creatureSet;
  216. delete exchange.second;
  217. }
  218. exchangeMap.clear();
  219. }
  220. HeroActor * HeroExchangeMap::exchange(const ChainActor * other)
  221. {
  222. HeroActor * result;
  223. if(vstd::contains(exchangeMap, other))
  224. result = exchangeMap.at(other);
  225. else
  226. {
  227. TResources availableResources = ai->cb->getResourceAmount() - actor->armyCost - other->armyCost;
  228. HeroExchangeArmy * upgradedInitialArmy = tryUpgrade(actor->creatureSet, other->getActorObject(), availableResources);
  229. HeroExchangeArmy * newArmy;
  230. if(other->creatureSet->Slots().size())
  231. {
  232. if(upgradedInitialArmy)
  233. {
  234. newArmy = pickBestCreatures(upgradedInitialArmy, other->creatureSet);
  235. newArmy->armyCost = upgradedInitialArmy->armyCost;
  236. delete upgradedInitialArmy;
  237. }
  238. else
  239. {
  240. newArmy = pickBestCreatures(actor->creatureSet, other->creatureSet);
  241. }
  242. }
  243. else
  244. {
  245. newArmy = upgradedInitialArmy;
  246. }
  247. result = new HeroActor(actor, other, newArmy, ai);
  248. result->armyCost += newArmy->armyCost;
  249. exchangeMap[other] = result;
  250. }
  251. return result;
  252. }
  253. HeroExchangeArmy * HeroExchangeMap::tryUpgrade(
  254. const CCreatureSet * army,
  255. const CGObjectInstance * upgrader,
  256. TResources resources) const
  257. {
  258. HeroExchangeArmy * target = new HeroExchangeArmy();
  259. auto upgradeInfo = ai->armyManager->calculateCreateresUpgrade(army, upgrader, resources);
  260. if(upgradeInfo.upgradeValue)
  261. {
  262. for(auto & slotInfo : upgradeInfo.resultingArmy)
  263. {
  264. auto targetSlot = target->getFreeSlot();
  265. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  266. }
  267. resources -= upgradeInfo.upgradeCost;
  268. target->armyCost += upgradeInfo.upgradeCost;
  269. }
  270. else
  271. {
  272. for(auto slot : army->Slots())
  273. {
  274. auto targetSlot = target->getSlotFor(slot.second->getCreatureID());
  275. target->addToSlot(targetSlot, slot.second->getCreatureID(), slot.second->count);
  276. }
  277. }
  278. if(upgrader->ID == Obj::TOWN)
  279. {
  280. auto buyArmy = ai->armyManager->getArmyAvailableToBuy(target, ai->cb->getTown(upgrader->id), resources);
  281. for(auto creatureToBuy : buyArmy)
  282. {
  283. auto targetSlot = target->getSlotFor(creatureToBuy.cre);
  284. target->addToSlot(targetSlot, creatureToBuy.creID, creatureToBuy.count);
  285. target->armyCost += creatureToBuy.cre->cost * creatureToBuy.count;
  286. target->requireBuyArmy = true;
  287. }
  288. }
  289. if(target->getArmyStrength() <= army->getArmyStrength())
  290. {
  291. delete target;
  292. return nullptr;
  293. }
  294. return target;
  295. }
  296. HeroExchangeArmy * HeroExchangeMap::pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const
  297. {
  298. HeroExchangeArmy * target = new HeroExchangeArmy();
  299. auto bestArmy = ai->armyManager->getBestArmy(actor->hero, army1, army2);
  300. for(auto & slotInfo : bestArmy)
  301. {
  302. auto targetSlot = target->getFreeSlot();
  303. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  304. }
  305. return target;
  306. }
  307. HillFortActor::HillFortActor(const CGObjectInstance * hillFort, uint64_t chainMask)
  308. :ObjectActor(hillFort, &emptyArmy, chainMask, 0)
  309. {
  310. }
  311. DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek)
  312. :ObjectActor(
  313. dwelling,
  314. getDwellingCreatures(dwelling, waitForGrowth),
  315. chainMask,
  316. getInitialTurn(waitForGrowth, dayOfWeek)),
  317. dwelling(dwelling)
  318. {
  319. for(auto & slot : creatureSet->Slots())
  320. {
  321. armyCost += slot.second->getCreatureID().toCreature()->cost * slot.second->count;
  322. }
  323. }
  324. DwellingActor::~DwellingActor()
  325. {
  326. delete creatureSet;
  327. }
  328. int DwellingActor::getInitialTurn(bool waitForGrowth, int dayOfWeek)
  329. {
  330. if(!waitForGrowth)
  331. return 0;
  332. return 8 - dayOfWeek;
  333. }
  334. std::string DwellingActor::toString() const
  335. {
  336. return dwelling->typeName + dwelling->visitablePos().toString();
  337. }
  338. CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth)
  339. {
  340. CCreatureSet * dwellingCreatures = new CCreatureSet();
  341. for(auto & creatureInfo : dwelling->creatures)
  342. {
  343. if(!creatureInfo.second.size())
  344. continue;
  345. auto creature = creatureInfo.second.back().toCreature();
  346. auto count = creatureInfo.first;
  347. if(waitForGrowth)
  348. {
  349. const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(dwelling);
  350. count += town ? town->creatureGrowth(creature->level) : creature->growth;
  351. }
  352. dwellingCreatures->addToSlot(
  353. dwellingCreatures->getSlotFor(creature),
  354. creature->idNumber,
  355. TQuantity(creatureInfo.first));
  356. }
  357. return dwellingCreatures;
  358. }
  359. TownGarrisonActor::TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask)
  360. :ObjectActor(town, town->getUpperArmy(), chainMask, 0), town(town)
  361. {
  362. }
  363. std::string TownGarrisonActor::toString() const
  364. {
  365. return town->name;
  366. }