AIUtility.cpp 9.6 KB

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