AIUtility.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "VCAI.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. extern boost::thread_specific_ptr<CCallback> cb;
  20. extern boost::thread_specific_ptr<VCAI> 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::operator->() const
  96. {
  97. return get();
  98. }
  99. bool HeroPtr::validAndSet() const
  100. {
  101. return get(true);
  102. }
  103. const CGHeroInstance * HeroPtr::operator*() const
  104. {
  105. return get();
  106. }
  107. bool HeroPtr::operator==(const HeroPtr & rhs) const
  108. {
  109. return h == rhs.get(true);
  110. }
  111. void foreach_tile_pos(std::function<void(const int3 & pos)> foo)
  112. {
  113. // some micro-optimizations since this function gets called a LOT
  114. // callback pointer is thread-specific and slow to retrieve -> read map size only once
  115. int3 mapSize = cb->getMapSize();
  116. for(int i = 0; i < mapSize.x; i++)
  117. {
  118. for(int j = 0; j < mapSize.y; j++)
  119. {
  120. for(int k = 0; k < mapSize.z; k++)
  121. foo(int3(i, j, k));
  122. }
  123. }
  124. }
  125. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo)
  126. {
  127. int3 mapSize = cbp->getMapSize();
  128. for(int i = 0; i < mapSize.x; i++)
  129. {
  130. for(int j = 0; j < mapSize.y; j++)
  131. {
  132. for(int k = 0; k < mapSize.z; k++)
  133. foo(cbp, int3(i, j, k));
  134. }
  135. }
  136. }
  137. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo)
  138. {
  139. CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
  140. for(const int3 & dir : int3::getDirs())
  141. {
  142. const int3 n = pos + dir;
  143. if(cbp->isInTheMap(n))
  144. foo(pos + dir);
  145. }
  146. }
  147. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo)
  148. {
  149. for(const int3 & dir : int3::getDirs())
  150. {
  151. const int3 n = pos + dir;
  152. if(cbp->isInTheMap(n))
  153. foo(cbp, pos + dir);
  154. }
  155. }
  156. bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
  157. {
  158. const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());
  159. const CGPathNode * rn = ai->myCb->getPathsInfo(hero)->getPathInfo(rhs->visitablePos());
  160. return ln->cost < rn->cost;
  161. }
  162. bool isSafeToVisit(HeroPtr h, const CCreatureSet * heroArmy, uint64_t dangerStrength)
  163. {
  164. const ui64 heroStrength = h->getFightingStrength() * heroArmy->getArmyStrength();
  165. if(dangerStrength)
  166. {
  167. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  168. }
  169. return true; //there's no danger
  170. }
  171. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength)
  172. {
  173. return isSafeToVisit(h, h.get(), dangerStrength);
  174. }
  175. bool isObjectRemovable(const CGObjectInstance * obj)
  176. {
  177. //FIXME: move logic to object property!
  178. switch (obj->ID)
  179. {
  180. case Obj::MONSTER:
  181. case Obj::RESOURCE:
  182. case Obj::CAMPFIRE:
  183. case Obj::TREASURE_CHEST:
  184. case Obj::ARTIFACT:
  185. case Obj::BORDERGUARD:
  186. case Obj::FLOTSAM:
  187. case Obj::PANDORAS_BOX:
  188. case Obj::OCEAN_BOTTLE:
  189. case Obj::SEA_CHEST:
  190. case Obj::SHIPWRECK_SURVIVOR:
  191. case Obj::SPELL_SCROLL:
  192. return true;
  193. break;
  194. default:
  195. return false;
  196. break;
  197. }
  198. }
  199. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  200. {
  201. // TODO: Such information should be provided by pathfinder
  202. // Tile must be free or with unoccupied boat
  203. if(!t->blocked)
  204. {
  205. return true;
  206. }
  207. else if(!fromWater) // do not try to board when in water sector
  208. {
  209. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  210. return true;
  211. }
  212. return false;
  213. }
  214. bool isObjectPassable(const CGObjectInstance * obj)
  215. {
  216. return isObjectPassable(obj, ai->playerID, cb->getPlayerRelations(obj->tempOwner, ai->playerID));
  217. }
  218. // Pathfinder internal helper
  219. bool isObjectPassable(const CGObjectInstance * obj, PlayerColor playerColor, PlayerRelations::PlayerRelations objectRelations)
  220. {
  221. if((obj->ID == Obj::GARRISON || obj->ID == Obj::GARRISON2)
  222. && objectRelations != PlayerRelations::ENEMIES)
  223. return true;
  224. if(obj->ID == Obj::BORDER_GATE)
  225. {
  226. auto quest = dynamic_cast<const CGKeys *>(obj);
  227. if(quest->passableFor(playerColor))
  228. return true;
  229. }
  230. return false;
  231. }
  232. bool isBlockedBorderGate(int3 tileToHit) //TODO: is that function needed? should be handled by pathfinder
  233. {
  234. if(cb->getTile(tileToHit)->topVisitableId() != Obj::BORDER_GATE)
  235. return false;
  236. auto gate = dynamic_cast<const CGKeys *>(cb->getTile(tileToHit)->topVisitableObj());
  237. return !gate->passableFor(ai->playerID);
  238. }
  239. bool isBlockVisitObj(const int3 & pos)
  240. {
  241. if(auto obj = cb->getTopObj(pos))
  242. {
  243. if(obj->blockVisit) //we can't stand on that object
  244. return true;
  245. }
  246. return false;
  247. }
  248. creInfo infoFromDC(const dwellingContent & dc)
  249. {
  250. creInfo ci;
  251. ci.count = dc.first;
  252. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  253. if (ci.creID != -1)
  254. {
  255. ci.cre = VLC->creh->creatures[ci.creID];
  256. ci.level = ci.cre->level; //this is cretaure tier, while tryRealize expects dwelling level. Ignore.
  257. }
  258. else
  259. {
  260. ci.cre = nullptr;
  261. ci.level = 0;
  262. }
  263. return ci;
  264. }
  265. bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
  266. {
  267. return h1->getTotalStrength() < h2->getTotalStrength();
  268. }
  269. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  270. {
  271. return a1->getArmyStrength() < a2->getArmyStrength();
  272. }
  273. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  274. {
  275. auto art1 = a1->artType;
  276. auto art2 = a2->artType;
  277. if(art1->price == art2->price)
  278. return art1->valOfBonuses(Bonus::PRIMARY_SKILL) > art2->valOfBonuses(Bonus::PRIMARY_SKILL);
  279. else
  280. return art1->price > art2->price;
  281. }
  282. bool isWeeklyRevisitable(const CGObjectInstance * obj)
  283. {
  284. //TODO: allow polling of remaining creatures in dwelling
  285. if(dynamic_cast<const CGVisitableOPW *>(obj)) // ensures future compatibility, unlike IDs
  286. return true;
  287. if(dynamic_cast<const CGDwelling *>(obj))
  288. return true;
  289. if(dynamic_cast<const CBank *>(obj)) //banks tend to respawn often in mods
  290. return true;
  291. switch(obj->ID)
  292. {
  293. case Obj::STABLES:
  294. case Obj::MAGIC_WELL:
  295. case Obj::HILL_FORT:
  296. return true;
  297. case Obj::BORDER_GATE:
  298. case Obj::BORDERGUARD:
  299. return (dynamic_cast<const CGKeys *>(obj))->wasMyColorVisited(ai->playerID); //FIXME: they could be revisited sooner than in a week
  300. }
  301. return false;
  302. }
  303. uint64_t timeElapsed(boost::chrono::time_point<boost::chrono::steady_clock> start)
  304. {
  305. auto end = boost::chrono::high_resolution_clock::now();
  306. return boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start).count();
  307. }