AIUtility.cpp 10 KB

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