Actors.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. class ExchangeAction : public ISpecialAction
  19. {
  20. private:
  21. const CGHeroInstance * target;
  22. const CGHeroInstance * source;
  23. public:
  24. ExchangeAction(const CGHeroInstance * target, const CGHeroInstance * source)
  25. :target(target), source(source)
  26. { }
  27. virtual Goals::TSubgoal whatToDo(const HeroPtr & hero) const override
  28. {
  29. return Goals::sptr(Goals::VisitHero(target->id.getNum()).sethero(hero));
  30. }
  31. };
  32. ChainActor::ChainActor(const CGHeroInstance * hero, uint64_t chainMask)
  33. :hero(hero), isMovable(true), chainMask(chainMask), creatureSet(hero),
  34. baseActor(this), carrierParent(nullptr), otherParent(nullptr)
  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. }
  42. ChainActor::ChainActor(const ChainActor * carrier, const ChainActor * other, const CCreatureSet * heroArmy)
  43. :hero(carrier->hero), isMovable(true), creatureSet(heroArmy), chainMask(carrier->chainMask | other->chainMask),
  44. baseActor(this), carrierParent(carrier), otherParent(other)
  45. {
  46. armyValue = heroArmy->getArmyStrength();
  47. }
  48. ChainActor::ChainActor(const CGObjectInstance * obj, const CCreatureSet * creatureSet, uint64_t chainMask, int initialTurn)
  49. :hero(nullptr), isMovable(false), creatureSet(creatureSet), chainMask(chainMask),
  50. baseActor(this), carrierParent(nullptr), otherParent(nullptr), initialTurn(initialTurn), initialMovement(0)
  51. {
  52. initialPosition = obj->visitablePos();
  53. layer = EPathfindingLayer::LAND;
  54. armyValue = creatureSet->getArmyStrength();
  55. }
  56. HeroActor::HeroActor(const CGHeroInstance * hero, uint64_t chainMask, const VCAI * ai)
  57. :ChainActor(hero, chainMask)
  58. {
  59. exchangeMap = new HeroExchangeMap(this, ai);
  60. setupSpecialActors();
  61. }
  62. HeroActor::HeroActor(
  63. const ChainActor * carrier,
  64. const ChainActor * other,
  65. const CCreatureSet * army,
  66. const VCAI * ai)
  67. :ChainActor(carrier, other, army)
  68. {
  69. exchangeMap = new HeroExchangeMap(this, ai);
  70. setupSpecialActors();
  71. }
  72. void ChainActor::setBaseActor(HeroActor * base)
  73. {
  74. baseActor = base;
  75. hero = base->hero;
  76. layer = base->layer;
  77. initialMovement = base->initialMovement;
  78. initialTurn = base->initialTurn;
  79. armyValue = base->armyValue;
  80. chainMask = base->chainMask;
  81. creatureSet = base->creatureSet;
  82. isMovable = base->isMovable;
  83. }
  84. void HeroActor::setupSpecialActors()
  85. {
  86. auto allActors = std::vector<ChainActor *>{ this };
  87. for(ChainActor & specialActor : specialActors)
  88. {
  89. specialActor.setBaseActor(this);
  90. allActors.push_back(&specialActor);
  91. }
  92. for(int i = 0; i <= SPECIAL_ACTORS_COUNT; i++)
  93. {
  94. ChainActor * actor = allActors[i];
  95. actor->allowBattle = (i & 1) > 0;
  96. actor->allowSpellCast = (i & 2) > 0;
  97. actor->allowUseResources = (i & 4) > 0;
  98. actor->battleActor = allActors[i | 1];
  99. actor->castActor = allActors[i | 2];
  100. actor->resourceActor = allActors[i | 4];
  101. }
  102. }
  103. ChainActor * ChainActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  104. {
  105. return baseActor->exchange(specialActor, other);
  106. }
  107. bool ChainActor::canExchange(const ChainActor * other) const
  108. {
  109. return isMovable && baseActor->canExchange(other);
  110. }
  111. namespace vstd
  112. {
  113. template <class M, class Key, class F>
  114. typename M::mapped_type & getOrCompute(M &m, Key const& k, F f)
  115. {
  116. typedef typename M::mapped_type V;
  117. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  118. V &v = r.first->second;
  119. if(r.second)
  120. f(v);
  121. return v;
  122. }
  123. }
  124. bool HeroActor::canExchange(const ChainActor * other) const
  125. {
  126. return exchangeMap->canExchange(other);
  127. }
  128. bool HeroExchangeMap::canExchange(const ChainActor * other)
  129. {
  130. return vstd::getOrCompute(canExchangeCache, other, [&](bool & result) {
  131. result = (actor->chainMask & other->chainMask) == 0;
  132. if(result)
  133. {
  134. uint64_t reinforcment = ai->ah->howManyReinforcementsCanGet(actor->creatureSet, other->creatureSet);
  135. result = reinforcment > actor->armyValue / 10 || reinforcment > 1000;
  136. }
  137. });
  138. }
  139. ChainActor * HeroActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  140. {
  141. const ChainActor * otherBase = other->baseActor;
  142. HeroActor * result = exchangeMap->exchange(otherBase);
  143. if(specialActor == this)
  144. return result;
  145. int index = vstd::find_pos_if(specialActors, [specialActor](const ChainActor & actor) -> bool
  146. {
  147. return &actor == specialActor;
  148. });
  149. return &result->specialActors[index];
  150. }
  151. HeroActor * HeroExchangeMap::exchange(const ChainActor * other)
  152. {
  153. HeroActor * result;
  154. if(vstd::contains(exchangeMap, other))
  155. result = exchangeMap.at(other);
  156. else
  157. {
  158. // TODO: decide where to release this CCreatureSet and HeroActor. Probably custom ~ctor?
  159. CCreatureSet * newArmy = pickBestCreatures(actor->creatureSet, other->creatureSet);
  160. result = new HeroActor(actor, other, newArmy, ai);
  161. exchangeMap[other] = result;
  162. }
  163. return result;
  164. }
  165. CCreatureSet * HeroExchangeMap::pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const
  166. {
  167. CCreatureSet * target = new CCreatureSet();
  168. auto bestArmy = ai->ah->getBestArmy(army1, army2);
  169. for(auto & slotInfo : bestArmy)
  170. {
  171. auto targetSlot = target->getFreeSlot();
  172. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  173. }
  174. return target;
  175. }
  176. DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek)
  177. :ChainActor(
  178. dwelling,
  179. getDwellingCreatures(dwelling, waitForGrowth),
  180. chainMask,
  181. getInitialTurn(waitForGrowth, dayOfWeek))
  182. {
  183. }
  184. DwellingActor::~DwellingActor()
  185. {
  186. delete creatureSet;
  187. }
  188. int DwellingActor::getInitialTurn(bool waitForGrowth, int dayOfWeek)
  189. {
  190. if(!waitForGrowth)
  191. return 0;
  192. return 8 - dayOfWeek;
  193. }
  194. CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth)
  195. {
  196. CCreatureSet * dwellingCreatures = new CCreatureSet();
  197. for(auto & creatureInfo : dwelling->creatures)
  198. {
  199. if(!creatureInfo.second.size())
  200. continue;
  201. auto creature = creatureInfo.second.back().toCreature();
  202. auto count = creatureInfo.first;
  203. if(waitForGrowth)
  204. {
  205. const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(dwelling);
  206. count += town ? town->creatureGrowth(creature->level) : creature->growth;
  207. }
  208. dwellingCreatures->addToSlot(
  209. dwellingCreatures->getSlotFor(creature),
  210. creature->idNumber,
  211. TQuantity(creatureInfo.first));
  212. }
  213. return dwellingCreatures;
  214. }
  215. TownGarrisonActor::TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask)
  216. :ChainActor(town, town->getUpperArmy(), chainMask, 0)
  217. {
  218. }