Actors.cpp 6.8 KB

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