Actors.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "../AIhelper.h"
  14. #include "../../../CCallback.h"
  15. #include "../../../lib/mapping/CMap.h"
  16. #include "../../../lib/mapObjects/MapObjects.h"
  17. CCreatureSet emptyArmy;
  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. ObjectActor::ObjectActor(const CGObjectInstance * obj, const CCreatureSet * army, uint64_t chainMask, int initialTurn)
  54. :ChainActor(obj, army, chainMask, initialTurn), object(obj)
  55. {
  56. }
  57. const CGObjectInstance * ObjectActor::getActorObject() const
  58. {
  59. return object;
  60. }
  61. std::string ObjectActor::toString() const
  62. {
  63. return object->getObjectName() + " at " + object->visitablePos().toString();
  64. }
  65. HeroActor::HeroActor(const CGHeroInstance * hero, uint64_t chainMask, const VCAI * ai)
  66. :ChainActor(hero, chainMask)
  67. {
  68. exchangeMap = new HeroExchangeMap(this, ai);
  69. setupSpecialActors();
  70. }
  71. HeroActor::HeroActor(
  72. const ChainActor * carrier,
  73. const ChainActor * other,
  74. const CCreatureSet * army,
  75. const VCAI * ai)
  76. :ChainActor(carrier, other, army)
  77. {
  78. exchangeMap = new HeroExchangeMap(this, ai);
  79. setupSpecialActors();
  80. }
  81. void ChainActor::setBaseActor(HeroActor * base)
  82. {
  83. baseActor = base;
  84. hero = base->hero;
  85. layer = base->layer;
  86. initialMovement = base->initialMovement;
  87. initialTurn = base->initialTurn;
  88. armyValue = base->armyValue;
  89. chainMask = base->chainMask;
  90. creatureSet = base->creatureSet;
  91. isMovable = base->isMovable;
  92. heroFightingStrength = base->heroFightingStrength;
  93. armyCost = base->armyCost;
  94. }
  95. void HeroActor::setupSpecialActors()
  96. {
  97. auto allActors = std::vector<ChainActor *>{ this };
  98. for(ChainActor & specialActor : specialActors)
  99. {
  100. specialActor.setBaseActor(this);
  101. allActors.push_back(&specialActor);
  102. }
  103. for(int i = 0; i <= SPECIAL_ACTORS_COUNT; i++)
  104. {
  105. ChainActor * actor = allActors[i];
  106. actor->allowBattle = (i & 1) > 0;
  107. actor->allowSpellCast = (i & 2) > 0;
  108. actor->allowUseResources = (i & 4) > 0;
  109. actor->battleActor = allActors[i | 1];
  110. actor->castActor = allActors[i | 2];
  111. actor->resourceActor = allActors[i | 4];
  112. }
  113. }
  114. ChainActor * ChainActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  115. {
  116. return baseActor->exchange(specialActor, other);
  117. }
  118. bool ChainActor::canExchange(const ChainActor * other) const
  119. {
  120. return isMovable && baseActor->canExchange(other);
  121. }
  122. namespace vstd
  123. {
  124. template <class M, class Key, class F>
  125. typename M::mapped_type & getOrCompute(M &m, Key const& k, F f)
  126. {
  127. typedef typename M::mapped_type V;
  128. std::pair<typename M::iterator, bool> r = m.insert(typename M::value_type(k, V()));
  129. V &v = r.first->second;
  130. if(r.second)
  131. f(v);
  132. return v;
  133. }
  134. }
  135. bool HeroActor::canExchange(const ChainActor * other) const
  136. {
  137. return exchangeMap->canExchange(other);
  138. }
  139. bool HeroExchangeMap::canExchange(const ChainActor * other)
  140. {
  141. return vstd::getOrCompute(canExchangeCache, other, [&](bool & result) {
  142. result = (actor->chainMask & other->chainMask) == 0;
  143. if(result)
  144. {
  145. TResources resources = ai->myCb->getResourceAmount();
  146. if(!resources.canAfford(actor->armyCost + other->armyCost))
  147. {
  148. result = false;
  149. #if PATHFINDER_TRACE_LEVEL >= 2
  150. logAi->trace(
  151. "Can not afford exchange because of total cost %s but we have %s",
  152. (actor->armyCost + other->armyCost).toString(),
  153. resources.toString());
  154. #endif
  155. return;
  156. }
  157. auto upgradeInfo = ai->ah->calculateCreateresUpgrade(
  158. actor->creatureSet,
  159. other->getActorObject(),
  160. resources - actor->armyCost - other->armyCost);
  161. uint64_t reinforcment = upgradeInfo.upgradeValue;
  162. if(other->creatureSet->Slots().size())
  163. reinforcment += ai->ah->howManyReinforcementsCanGet(actor->creatureSet, other->creatureSet);
  164. #if PATHFINDER_TRACE_LEVEL >= 2
  165. logAi->trace(
  166. "Exchange %s->%s reinforcement: %d, %f%%",
  167. actor->toString(),
  168. other->toString(),
  169. reinforcment,
  170. 100.0f * reinforcment / actor->armyValue);
  171. #endif
  172. result = reinforcment > actor->armyValue / 10 || reinforcment > 1000;
  173. }
  174. });
  175. }
  176. ChainActor * HeroActor::exchange(const ChainActor * specialActor, const ChainActor * other) const
  177. {
  178. const ChainActor * otherBase = other->baseActor;
  179. HeroActor * result = exchangeMap->exchange(otherBase);
  180. if(specialActor == this)
  181. return result;
  182. int index = vstd::find_pos_if(specialActors, [specialActor](const ChainActor & actor) -> bool
  183. {
  184. return &actor == specialActor;
  185. });
  186. return &result->specialActors[index];
  187. }
  188. HeroExchangeMap::~HeroExchangeMap()
  189. {
  190. for(auto & exchange : exchangeMap)
  191. {
  192. delete exchange.second->creatureSet;
  193. delete exchange.second;
  194. }
  195. exchangeMap.clear();
  196. }
  197. HeroActor * HeroExchangeMap::exchange(const ChainActor * other)
  198. {
  199. HeroActor * result;
  200. if(vstd::contains(exchangeMap, other))
  201. result = exchangeMap.at(other);
  202. else
  203. {
  204. TResources availableResources = ai->myCb->getResourceAmount() - actor->armyCost - other->armyCost;
  205. CCreatureSet * upgradedInitialArmy = tryUpgrade(actor->creatureSet, other->getActorObject(), availableResources);
  206. CCreatureSet * newArmy;
  207. if(other->creatureSet->Slots().size())
  208. {
  209. if(upgradedInitialArmy)
  210. {
  211. newArmy = pickBestCreatures(upgradedInitialArmy, other->creatureSet);
  212. delete upgradedInitialArmy;
  213. }
  214. else
  215. {
  216. newArmy = pickBestCreatures(actor->creatureSet, other->creatureSet);
  217. }
  218. }
  219. else
  220. {
  221. newArmy = upgradedInitialArmy;
  222. }
  223. result = new HeroActor(actor, other, newArmy, ai);
  224. exchangeMap[other] = result;
  225. }
  226. return result;
  227. }
  228. CCreatureSet * HeroExchangeMap::tryUpgrade(const CCreatureSet * army, const CGObjectInstance * upgrader, TResources resources) const
  229. {
  230. auto upgradeInfo = ai->ah->calculateCreateresUpgrade(army, upgrader, resources);
  231. if(!upgradeInfo.upgradeValue)
  232. return nullptr;
  233. CCreatureSet * target = new HeroExchangeArmy();
  234. for(auto & slotInfo : upgradeInfo.resultingArmy)
  235. {
  236. auto targetSlot = target->getFreeSlot();
  237. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  238. }
  239. return target;
  240. }
  241. CCreatureSet * HeroExchangeMap::pickBestCreatures(const CCreatureSet * army1, const CCreatureSet * army2) const
  242. {
  243. CCreatureSet * target = new HeroExchangeArmy();
  244. auto bestArmy = ai->ah->getBestArmy(army1, army2);
  245. for(auto & slotInfo : bestArmy)
  246. {
  247. auto targetSlot = target->getFreeSlot();
  248. target->addToSlot(targetSlot, slotInfo.creature->idNumber, TQuantity(slotInfo.count));
  249. }
  250. return target;
  251. }
  252. HillFortActor::HillFortActor(const CGObjectInstance * hillFort, uint64_t chainMask)
  253. :ObjectActor(hillFort, &emptyArmy, chainMask, 0)
  254. {
  255. }
  256. DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek)
  257. :ObjectActor(
  258. dwelling,
  259. getDwellingCreatures(dwelling, waitForGrowth),
  260. chainMask,
  261. getInitialTurn(waitForGrowth, dayOfWeek)),
  262. dwelling(dwelling)
  263. {
  264. for(auto & slot : creatureSet->Slots())
  265. {
  266. armyCost += slot.second->getCreatureID().toCreature()->cost * slot.second->count;
  267. }
  268. }
  269. DwellingActor::~DwellingActor()
  270. {
  271. delete creatureSet;
  272. }
  273. int DwellingActor::getInitialTurn(bool waitForGrowth, int dayOfWeek)
  274. {
  275. if(!waitForGrowth)
  276. return 0;
  277. return 8 - dayOfWeek;
  278. }
  279. std::string DwellingActor::toString() const
  280. {
  281. return dwelling->typeName + dwelling->visitablePos().toString();
  282. }
  283. CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, bool waitForGrowth)
  284. {
  285. CCreatureSet * dwellingCreatures = new CCreatureSet();
  286. for(auto & creatureInfo : dwelling->creatures)
  287. {
  288. if(!creatureInfo.second.size())
  289. continue;
  290. auto creature = creatureInfo.second.back().toCreature();
  291. auto count = creatureInfo.first;
  292. if(waitForGrowth)
  293. {
  294. const CGTownInstance * town = dynamic_cast<const CGTownInstance *>(dwelling);
  295. count += town ? town->creatureGrowth(creature->level) : creature->growth;
  296. }
  297. dwellingCreatures->addToSlot(
  298. dwellingCreatures->getSlotFor(creature),
  299. creature->idNumber,
  300. TQuantity(creatureInfo.first));
  301. }
  302. return dwellingCreatures;
  303. }
  304. TownGarrisonActor::TownGarrisonActor(const CGTownInstance * town, uint64_t chainMask)
  305. :ObjectActor(town, town->getUpperArmy(), chainMask, 0), town(town)
  306. {
  307. }
  308. std::string TownGarrisonActor::toString() const
  309. {
  310. return town->name;
  311. }