Actors.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "../Goals/VisitHero.h"
  13. #include "../VCAI.h"
  14. #include "../AIhelper.h"
  15. #include "../../../CCallback.h"
  16. #include "../../../lib/mapping/CMap.h"
  17. #include "../../../lib/mapObjects/MapObjects.h"
  18. bool HeroExchangeArmy::needsLastStack() const
  19. {
  20. return true;
  21. }
  22. ChainActor::ChainActor(const CGHeroInstance * hero, uint64_t chainMask)
  23. :hero(hero), isMovable(true), chainMask(chainMask), creatureSet(hero),
  24. baseActor(this), carrierParent(nullptr), otherParent(nullptr), actorExchangeCount(1), armyCost()
  25. {
  26. initialPosition = hero->visitablePos();
  27. layer = hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND;
  28. initialMovement = hero->movement;
  29. initialTurn = 0;
  30. armyValue = hero->getArmyStrength();
  31. heroFightingStrength = hero->getFightingStrength();
  32. }
  33. ChainActor::ChainActor(const ChainActor * carrier, const ChainActor * other, const CCreatureSet * heroArmy)
  34. :hero(carrier->hero), isMovable(true), creatureSet(heroArmy), chainMask(carrier->chainMask | other->chainMask),
  35. baseActor(this), carrierParent(carrier), otherParent(other), heroFightingStrength(carrier->heroFightingStrength),
  36. actorExchangeCount(carrier->actorExchangeCount + other->actorExchangeCount), armyCost(carrier->armyCost + other->armyCost)
  37. {
  38. armyValue = heroArmy->getArmyStrength();
  39. }
  40. ChainActor::ChainActor(const CGObjectInstance * obj, const CCreatureSet * creatureSet, uint64_t chainMask, int initialTurn)
  41. :hero(nullptr), isMovable(false), creatureSet(creatureSet), chainMask(chainMask),
  42. baseActor(this), carrierParent(nullptr), otherParent(nullptr), initialTurn(initialTurn), initialMovement(0),
  43. heroFightingStrength(0), actorExchangeCount(1), armyCost()
  44. {
  45. initialPosition = obj->visitablePos();
  46. layer = EPathfindingLayer::LAND;
  47. armyValue = creatureSet->getArmyStrength();
  48. }
  49. std::string ChainActor::toString() const
  50. {
  51. return hero->name;
  52. }
  53. HeroActor::HeroActor(const CGHeroInstance * hero, uint64_t chainMask, const VCAI * ai)
  54. :ChainActor(hero, chainMask)
  55. {
  56. exchangeMap = new HeroExchangeMap(this, ai);
  57. setupSpecialActors();
  58. }
  59. HeroActor::HeroActor(
  60. const ChainActor * carrier,
  61. const ChainActor * other,
  62. const CCreatureSet * army,
  63. const VCAI * ai)
  64. :ChainActor(carrier, other, army)
  65. {
  66. exchangeMap = new HeroExchangeMap(this, ai);
  67. setupSpecialActors();
  68. }
  69. void ChainActor::setBaseActor(HeroActor * base)
  70. {
  71. baseActor = base;
  72. hero = base->hero;
  73. layer = base->layer;
  74. initialMovement = base->initialMovement;
  75. initialTurn = base->initialTurn;
  76. armyValue = base->armyValue;
  77. chainMask = base->chainMask;
  78. creatureSet = base->creatureSet;
  79. isMovable = base->isMovable;
  80. heroFightingStrength = base->heroFightingStrength;
  81. armyCost = base->armyCost;
  82. }
  83. void HeroActor::setupSpecialActors()
  84. {
  85. auto allActors = std::vector<ChainActor *>{ this };
  86. for(ChainActor & specialActor : specialActors)
  87. {
  88. specialActor.setBaseActor(this);
  89. allActors.push_back(&specialActor);
  90. }
  91. for(int i = 0; i <= SPECIAL_ACTORS_COUNT; i++)
  92. {
  93. ChainActor * actor = allActors[i];
  94. actor->allowBattle = (i & 1) > 0;
  95. actor->allowSpellCast = (i & 2) > 0;
  96. actor->allowUseResources = (i & 4) > 0;
  97. actor->battleActor = allActors[i | 1];
  98. actor->castActor = allActors[i | 2];
  99. actor->resourceActor = allActors[i | 4];
  100. }
  101. }
  102. ChainActor * ChainActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  103. {
  104. return baseActor->exchange(specialActor, other);
  105. }
  106. bool ChainActor::canExchange(const ChainActor * other) const
  107. {
  108. return isMovable && baseActor->canExchange(other);
  109. }
  110. namespace vstd
  111. {
  112. template <class M, class Key, class F>
  113. typename M::mapped_type & getOrCompute(M &m, Key const& k, F f)
  114. {
  115. typedef typename M::mapped_type V;
  116. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  117. V &v = r.first->second;
  118. if(r.second)
  119. f(v);
  120. return v;
  121. }
  122. }
  123. bool HeroActor::canExchange(const ChainActor * other) const
  124. {
  125. return exchangeMap->canExchange(other);
  126. }
  127. bool HeroExchangeMap::canExchange(const ChainActor * other)
  128. {
  129. return vstd::getOrCompute(canExchangeCache, other, [&](bool & result) {
  130. result = (actor->chainMask & other->chainMask) == 0;
  131. if(result)
  132. {
  133. if(other->armyCost.nonZero())
  134. {
  135. TResources resources = ai->myCb->getResourceAmount();
  136. if(!resources.canAfford(actor->armyCost + other->armyCost))
  137. {
  138. result = false;
  139. return;
  140. }
  141. }
  142. uint64_t reinforcment = ai->ah->howManyReinforcementsCanGet(actor->creatureSet, other->creatureSet);
  143. result = reinforcment > actor->armyValue / 10 || reinforcment > 1000;
  144. }
  145. });
  146. }
  147. ChainActor * HeroActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  148. {
  149. const ChainActor * otherBase = other->baseActor;
  150. HeroActor * result = exchangeMap->exchange(otherBase);
  151. if(specialActor == this)
  152. return result;
  153. int index = vstd::find_pos_if(specialActors, [specialActor](const ChainActor & actor) -> bool
  154. {
  155. return &actor == specialActor;
  156. });
  157. return &result->specialActors[index];
  158. }
  159. HeroExchangeMap::~HeroExchangeMap()
  160. {
  161. for(auto & exchange : exchangeMap)
  162. {
  163. delete exchange.second->creatureSet;
  164. delete exchange.second;
  165. }
  166. exchangeMap.clear();
  167. }
  168. HeroActor * HeroExchangeMap::exchange(const ChainActor * other)
  169. {
  170. HeroActor * result;
  171. if(vstd::contains(exchangeMap, other))
  172. result = exchangeMap.at(other);
  173. else
  174. {
  175. CCreatureSet * newArmy = pickBestCreatures(actor->creatureSet, other->creatureSet);
  176. result = new HeroActor(actor, other, newArmy, ai);
  177. exchangeMap[other] = result;
  178. }
  179. return result;
  180. }
  181. CCreatureSet * HeroExchangeMap::pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const
  182. {
  183. CCreatureSet * target = new HeroExchangeArmy();
  184. auto bestArmy = ai->ah->getBestArmy(army1, army2);
  185. for(auto & slotInfo : bestArmy)
  186. {
  187. auto targetSlot = target->getFreeSlot();
  188. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  189. }
  190. return target;
  191. }
  192. DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek)
  193. :ChainActor(
  194. dwelling,
  195. getDwellingCreatures(dwelling, waitForGrowth),
  196. chainMask,
  197. getInitialTurn(waitForGrowth, dayOfWeek)),
  198. dwelling(dwelling)
  199. {
  200. for(auto & slot : creatureSet->Slots())
  201. {
  202. armyCost += slot.second->getCreatureID().toCreature()->cost * slot.second->count;
  203. }
  204. }
  205. DwellingActor::~DwellingActor()
  206. {
  207. delete creatureSet;
  208. }
  209. int DwellingActor::getInitialTurn(bool waitForGrowth, int dayOfWeek)
  210. {
  211. if(!waitForGrowth)
  212. return 0;
  213. return 8 - dayOfWeek;
  214. }
  215. std::string DwellingActor::toString() const
  216. {
  217. return dwelling->typeName + dwelling->visitablePos().toString();
  218. }
  219. CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth)
  220. {
  221. CCreatureSet * dwellingCreatures = new CCreatureSet();
  222. for(auto & creatureInfo : dwelling->creatures)
  223. {
  224. if(!creatureInfo.second.size())
  225. continue;
  226. auto creature = creatureInfo.second.back().toCreature();
  227. auto count = creatureInfo.first;
  228. if(waitForGrowth)
  229. {
  230. const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(dwelling);
  231. count += town ? town->creatureGrowth(creature->level) : creature->growth;
  232. }
  233. dwellingCreatures->addToSlot(
  234. dwellingCreatures->getSlotFor(creature),
  235. creature->idNumber,
  236. TQuantity(creatureInfo.first));
  237. }
  238. return dwellingCreatures;
  239. }
  240. TownGarrisonActor::TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask)
  241. :ChainActor(town, town->getUpperArmy(), chainMask, 0), town(town)
  242. {
  243. }
  244. std::string TownGarrisonActor::toString() const
  245. {
  246. return town->name;
  247. }