AIUtility.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * AIUtility.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 "AIUtility.h"
  12. #include "AIGateway.h"
  13. #include "Goals/Goals.h"
  14. #include "../../lib/UnlockGuard.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/mapObjects/MapObjects.h"
  17. #include "../../lib/mapping/CMapDefines.h"
  18. #include "../../lib/gameState/QuestInfo.h"
  19. #include "../../lib/IGameSettings.h"
  20. #include <vcmi/CreatureService.h>
  21. namespace NKAI
  22. {
  23. const CGObjectInstance * ObjectIdRef::operator->() const
  24. {
  25. return cb->getObj(id, false);
  26. }
  27. ObjectIdRef::operator const CGObjectInstance *() const
  28. {
  29. return cb->getObj(id, false);
  30. }
  31. ObjectIdRef::operator bool() const
  32. {
  33. return cb->getObj(id, false);
  34. }
  35. ObjectIdRef::ObjectIdRef(ObjectInstanceID _id)
  36. : id(_id)
  37. {
  38. }
  39. ObjectIdRef::ObjectIdRef(const CGObjectInstance * obj)
  40. : id(obj->id)
  41. {
  42. }
  43. bool ObjectIdRef::operator<(const ObjectIdRef & rhs) const
  44. {
  45. return id < rhs.id;
  46. }
  47. HeroPtr::HeroPtr(const CGHeroInstance * H)
  48. {
  49. if(!H)
  50. {
  51. //init from nullptr should equal to default init
  52. *this = HeroPtr();
  53. return;
  54. }
  55. h = H;
  56. hid = H->id;
  57. // infosCount[ai->playerID][hid]++;
  58. }
  59. HeroPtr::HeroPtr()
  60. {
  61. h = nullptr;
  62. hid = ObjectInstanceID();
  63. }
  64. HeroPtr::~HeroPtr()
  65. {
  66. // if(hid >= 0)
  67. // infosCount[ai->playerID][hid]--;
  68. }
  69. bool HeroPtr::operator<(const HeroPtr & rhs) const
  70. {
  71. return hid < rhs.hid;
  72. }
  73. std::string HeroPtr::name() const
  74. {
  75. if (h)
  76. return h->getNameTextID();
  77. else
  78. return "<NO HERO>";
  79. }
  80. const CGHeroInstance * HeroPtr::get(bool doWeExpectNull) const
  81. {
  82. return get(cb, doWeExpectNull);
  83. }
  84. const CGHeroInstance * HeroPtr::get(const CPlayerSpecificInfoCallback * cb, bool doWeExpectNull) const
  85. {
  86. //TODO? check if these all assertions every time we get info about hero affect efficiency
  87. //
  88. //behave terribly when attempting unauthorized access to hero that is not ours (or was lost)
  89. assert(doWeExpectNull || h);
  90. if(h)
  91. {
  92. auto obj = cb->getObj(hid);
  93. //const bool owned = obj && obj->tempOwner == ai->playerID;
  94. if(doWeExpectNull && !obj)
  95. {
  96. return nullptr;
  97. }
  98. else
  99. {
  100. assert(obj);
  101. //assert(owned);
  102. }
  103. }
  104. return h;
  105. }
  106. const CGHeroInstance * HeroPtr::operator->() const
  107. {
  108. return get();
  109. }
  110. bool HeroPtr::validAndSet() const
  111. {
  112. return get(true);
  113. }
  114. const CGHeroInstance * HeroPtr::operator*() const
  115. {
  116. return get();
  117. }
  118. bool HeroPtr::operator==(const HeroPtr & rhs) const
  119. {
  120. return h == rhs.get(true);
  121. }
  122. bool isSafeToVisit(const CGHeroInstance * h, const CCreatureSet * heroArmy, uint64_t dangerStrength)
  123. {
  124. const ui64 heroStrength = h->getFightingStrength() * heroArmy->getArmyStrength();
  125. if(dangerStrength)
  126. {
  127. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  128. }
  129. return true; //there's no danger
  130. }
  131. bool isSafeToVisit(const CGHeroInstance * h, uint64_t dangerStrength)
  132. {
  133. return isSafeToVisit(h, h, dangerStrength);
  134. }
  135. bool isObjectRemovable(const CGObjectInstance * obj)
  136. {
  137. //FIXME: move logic to object property!
  138. switch (obj->ID)
  139. {
  140. case Obj::MONSTER:
  141. case Obj::RESOURCE:
  142. case Obj::CAMPFIRE:
  143. case Obj::TREASURE_CHEST:
  144. case Obj::ARTIFACT:
  145. case Obj::BORDERGUARD:
  146. case Obj::FLOTSAM:
  147. case Obj::PANDORAS_BOX:
  148. case Obj::OCEAN_BOTTLE:
  149. case Obj::SEA_CHEST:
  150. case Obj::SHIPWRECK_SURVIVOR:
  151. case Obj::SPELL_SCROLL:
  152. return true;
  153. break;
  154. default:
  155. return false;
  156. break;
  157. }
  158. }
  159. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  160. {
  161. // TODO: Such information should be provided by pathfinder
  162. // Tile must be free or with unoccupied boat
  163. if(!t->blocked())
  164. {
  165. return true;
  166. }
  167. else if(!fromWater) // do not try to board when in water sector
  168. {
  169. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  170. return true;
  171. }
  172. return false;
  173. }
  174. bool isObjectPassable(const Nullkiller * ai, const CGObjectInstance * obj)
  175. {
  176. return isObjectPassable(obj, ai->playerID, ai->cb->getPlayerRelations(obj->tempOwner, ai->playerID));
  177. }
  178. // Pathfinder internal helper
  179. bool isObjectPassable(const CGObjectInstance * obj, PlayerColor playerColor, PlayerRelations objectRelations)
  180. {
  181. if((obj->ID == Obj::GARRISON || obj->ID == Obj::GARRISON2)
  182. && objectRelations != PlayerRelations::ENEMIES)
  183. return true;
  184. if(obj->ID == Obj::BORDER_GATE)
  185. {
  186. auto quest = dynamic_cast<const CGKeys *>(obj);
  187. if(quest->wasMyColorVisited(playerColor))
  188. return true;
  189. }
  190. return false;
  191. }
  192. bool isBlockVisitObj(const int3 & pos)
  193. {
  194. if(auto obj = cb->getTopObj(pos))
  195. {
  196. if(obj->isBlockedVisitable()) //we can't stand on that object
  197. return true;
  198. }
  199. return false;
  200. }
  201. creInfo infoFromDC(const dwellingContent & dc)
  202. {
  203. creInfo ci;
  204. ci.count = dc.first;
  205. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  206. if (ci.creID != CreatureID::NONE)
  207. {
  208. ci.level = ci.creID.toCreature()->getLevel(); //this is creature tier, while tryRealize expects dwelling level. Ignore.
  209. }
  210. else
  211. {
  212. ci.level = 0;
  213. }
  214. return ci;
  215. }
  216. bool compareHeroStrength(const CGHeroInstance * h1, const CGHeroInstance * h2)
  217. {
  218. return h1->getTotalStrength() < h2->getTotalStrength();
  219. }
  220. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  221. {
  222. return a1->getArmyStrength() < a2->getArmyStrength();
  223. }
  224. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  225. {
  226. auto art1 = a1->getType();
  227. auto art2 = a2->getType();
  228. if(art1->getPrice() == art2->getPrice())
  229. return art1->valOfBonuses(BonusType::PRIMARY_SKILL) > art2->valOfBonuses(BonusType::PRIMARY_SKILL);
  230. else
  231. return art1->getPrice() > art2->getPrice();
  232. }
  233. bool isWeeklyRevisitable(const Nullkiller * ai, const CGObjectInstance * obj)
  234. {
  235. if(!obj)
  236. return false;
  237. //TODO: allow polling of remaining creatures in dwelling
  238. if(const auto * rewardable = dynamic_cast<const CRewardableObject *>(obj))
  239. return rewardable->configuration.getResetDuration() == 7;
  240. if(dynamic_cast<const CGDwelling *>(obj))
  241. return true;
  242. switch(obj->ID)
  243. {
  244. case Obj::HILL_FORT:
  245. return true;
  246. case Obj::BORDER_GATE:
  247. case Obj::BORDERGUARD:
  248. return (dynamic_cast<const CGKeys *>(obj))->wasMyColorVisited(ai->playerID); //FIXME: they could be revisited sooner than in a week
  249. }
  250. return false;
  251. }
  252. uint64_t timeElapsed(std::chrono::time_point<std::chrono::high_resolution_clock> start)
  253. {
  254. auto end = std::chrono::high_resolution_clock::now();
  255. return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  256. }
  257. int getDuplicatingSlots(const CArmedInstance * army)
  258. {
  259. int duplicatingSlots = 0;
  260. for(auto stack : army->Slots())
  261. {
  262. if(stack.second->getCreature() && army->getSlotFor(stack.second->getCreature()) != stack.first)
  263. duplicatingSlots++;
  264. }
  265. return duplicatingSlots;
  266. }
  267. // todo: move to obj manager
  268. bool shouldVisit(const Nullkiller * ai, const CGHeroInstance * h, const CGObjectInstance * obj)
  269. {
  270. auto relations = ai->cb->getPlayerRelations(obj->tempOwner, h->tempOwner);
  271. switch(obj->ID)
  272. {
  273. case Obj::TOWN:
  274. case Obj::HERO: //never visit our heroes at random
  275. return relations == PlayerRelations::ENEMIES; //do not visit our towns at random
  276. case Obj::BORDER_GATE:
  277. {
  278. for(auto q : ai->cb->getMyQuests())
  279. {
  280. if(q.obj == obj)
  281. {
  282. return false; // do not visit guards or gates when wandering
  283. }
  284. }
  285. return true; //we don't have this quest yet
  286. }
  287. case Obj::BORDERGUARD: //open borderguard if possible
  288. return (dynamic_cast<const CGKeys *>(obj))->wasMyColorVisited(ai->playerID);
  289. case Obj::SEER_HUT:
  290. {
  291. for(auto q : ai->cb->getMyQuests())
  292. {
  293. if(q.obj == obj)
  294. {
  295. if(q.quest->checkQuest(h))
  296. return true; //we completed the quest
  297. else
  298. return false; //we can't complete this quest
  299. }
  300. }
  301. return true; //we don't have this quest yet
  302. }
  303. case Obj::CREATURE_GENERATOR1:
  304. {
  305. if(relations == PlayerRelations::ENEMIES)
  306. return true; //flag just in case
  307. if(relations == PlayerRelations::ALLIES)
  308. return false;
  309. const CGDwelling * d = dynamic_cast<const CGDwelling *>(obj);
  310. auto duplicatingSlotsCount = getDuplicatingSlots(h);
  311. for(auto level : d->creatures)
  312. {
  313. for(auto c : level.second)
  314. {
  315. if(level.first
  316. && (h->getSlotFor(CreatureID(c)) != SlotID() || duplicatingSlotsCount > 0)
  317. && ai->cb->getResourceAmount().canAfford(c.toCreature()->getFullRecruitCost()))
  318. {
  319. return true;
  320. }
  321. }
  322. }
  323. return false;
  324. }
  325. case Obj::HILL_FORT:
  326. {
  327. for(auto slot : h->Slots())
  328. {
  329. if(slot.second->getType()->hasUpgrades())
  330. return true; //TODO: check price?
  331. }
  332. return false;
  333. }
  334. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  335. case Obj::MONOLITH_ONE_WAY_EXIT:
  336. case Obj::MONOLITH_TWO_WAY:
  337. case Obj::WHIRLPOOL:
  338. return false;
  339. case Obj::SCHOOL_OF_MAGIC:
  340. case Obj::SCHOOL_OF_WAR:
  341. {
  342. if(ai->getFreeGold() < 1000)
  343. return false;
  344. break;
  345. }
  346. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  347. if(h->level < 10)
  348. return false;
  349. break;
  350. case Obj::TREE_OF_KNOWLEDGE:
  351. {
  352. if(ai->heroManager->getHeroRole(h) == HeroRole::SCOUT)
  353. return false;
  354. TResources myRes = ai->getFreeResources();
  355. if(myRes[EGameResID::GOLD] < 2000 || myRes[EGameResID::GEMS] < 10)
  356. return false;
  357. break;
  358. }
  359. case Obj::MAGIC_WELL:
  360. return h->mana < h->manaLimit();
  361. case Obj::PRISON:
  362. return !ai->heroManager->heroCapReached();
  363. case Obj::TAVERN:
  364. case Obj::EYE_OF_MAGI:
  365. case Obj::BOAT:
  366. case Obj::SIGN:
  367. return false;
  368. }
  369. if(obj->wasVisited(h))
  370. return false;
  371. auto rewardable = dynamic_cast<const Rewardable::Interface *>(obj);
  372. if(rewardable && rewardable->getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
  373. {
  374. return false;
  375. }
  376. return true;
  377. }
  378. bool townHasFreeTavern(const CGTownInstance * town)
  379. {
  380. if(!town->hasBuilt(BuildingID::TAVERN)) return false;
  381. if(!town->visitingHero) return true;
  382. bool canMoveVisitingHeroToGarrison = !town->getUpperArmy()->stacksCount();
  383. return canMoveVisitingHeroToGarrison;
  384. }
  385. uint64_t getHeroArmyStrengthWithCommander(const CGHeroInstance * hero, const CCreatureSet * heroArmy)
  386. {
  387. auto armyStrength = heroArmy->getArmyStrength();
  388. if(hero && hero->commander && hero->commander->alive)
  389. {
  390. armyStrength += 100 * hero->commander->level;
  391. }
  392. return armyStrength;
  393. }
  394. }