Actors.cpp 10.0 KB

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