AIUtility.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "FuzzyHelper.h"
  14. #include "Goals/Goals.h"
  15. #include "../../lib/UnlockGuard.h"
  16. #include "../../lib/CConfigHandler.h"
  17. #include "../../lib/CHeroHandler.h"
  18. #include "../../lib/mapObjects/CBank.h"
  19. #include "../../lib/mapObjects/CGTownInstance.h"
  20. #include "../../lib/mapObjects/CQuest.h"
  21. #include "../../lib/mapping/CMapDefines.h"
  22. extern boost::thread_specific_ptr<CCallback> cb;
  23. extern boost::thread_specific_ptr<VCAI> ai;
  24. extern FuzzyHelper * fh;
  25. //extern static const int3 dirs[8];
  26. const CGObjectInstance * ObjectIdRef::operator->() const
  27. {
  28. return cb->getObj(id, false);
  29. }
  30. ObjectIdRef::operator const CGObjectInstance *() const
  31. {
  32. return cb->getObj(id, false);
  33. }
  34. ObjectIdRef::operator bool() const
  35. {
  36. return cb->getObj(id, false);
  37. }
  38. ObjectIdRef::ObjectIdRef(ObjectInstanceID _id)
  39. : id(_id)
  40. {
  41. }
  42. ObjectIdRef::ObjectIdRef(const CGObjectInstance * obj)
  43. : id(obj->id)
  44. {
  45. }
  46. bool ObjectIdRef::operator<(const ObjectIdRef & rhs) const
  47. {
  48. return id < rhs.id;
  49. }
  50. HeroPtr::HeroPtr(const CGHeroInstance * H)
  51. {
  52. if(!H)
  53. {
  54. //init from nullptr should equal to default init
  55. *this = HeroPtr();
  56. return;
  57. }
  58. h = H;
  59. name = h->name;
  60. hid = H->id;
  61. // infosCount[ai->playerID][hid]++;
  62. }
  63. HeroPtr::HeroPtr()
  64. {
  65. h = nullptr;
  66. hid = ObjectInstanceID();
  67. }
  68. HeroPtr::~HeroPtr()
  69. {
  70. // if(hid >= 0)
  71. // infosCount[ai->playerID][hid]--;
  72. }
  73. bool HeroPtr::operator<(const HeroPtr & rhs) const
  74. {
  75. return hid < rhs.hid;
  76. }
  77. const CGHeroInstance * HeroPtr::get(bool doWeExpectNull) const
  78. {
  79. //TODO? check if these all assertions every time we get info about hero affect efficiency
  80. //
  81. //behave terribly when attempting unauthorized access to hero that is not ours (or was lost)
  82. assert(doWeExpectNull || h);
  83. if(h)
  84. {
  85. auto obj = cb->getObj(hid);
  86. const bool owned = obj && obj->tempOwner == ai->playerID;
  87. if(doWeExpectNull && !owned)
  88. {
  89. return nullptr;
  90. }
  91. else
  92. {
  93. assert(obj);
  94. assert(owned);
  95. }
  96. }
  97. return h;
  98. }
  99. const CGHeroInstance * HeroPtr::operator->() const
  100. {
  101. return get();
  102. }
  103. bool HeroPtr::validAndSet() const
  104. {
  105. return get(true);
  106. }
  107. const CGHeroInstance * HeroPtr::operator*() const
  108. {
  109. return get();
  110. }
  111. bool HeroPtr::operator==(const HeroPtr & rhs) const
  112. {
  113. return h == rhs.get(true);
  114. }
  115. void foreach_tile_pos(std::function<void(const int3 & pos)> foo)
  116. {
  117. // some micro-optimizations since this function gets called a LOT
  118. // callback pointer is thread-specific and slow to retrieve -> read map size only once
  119. int3 mapSize = cb->getMapSize();
  120. for(int i = 0; i < mapSize.x; i++)
  121. {
  122. for(int j = 0; j < mapSize.y; j++)
  123. {
  124. for(int k = 0; k < mapSize.z; k++)
  125. foo(int3(i, j, k));
  126. }
  127. }
  128. }
  129. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo)
  130. {
  131. int3 mapSize = cbp->getMapSize();
  132. for(int i = 0; i < mapSize.x; i++)
  133. {
  134. for(int j = 0; j < mapSize.y; j++)
  135. {
  136. for(int k = 0; k < mapSize.z; k++)
  137. foo(cbp, int3(i, j, k));
  138. }
  139. }
  140. }
  141. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo)
  142. {
  143. CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
  144. for(const int3 & dir : int3::getDirs())
  145. {
  146. const int3 n = pos + dir;
  147. if(cbp->isInTheMap(n))
  148. foo(pos + dir);
  149. }
  150. }
  151. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo)
  152. {
  153. for(const int3 & dir : int3::getDirs())
  154. {
  155. const int3 n = pos + dir;
  156. if(cbp->isInTheMap(n))
  157. foo(cbp, pos + dir);
  158. }
  159. }
  160. bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
  161. {
  162. const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());
  163. const CGPathNode * rn = ai->myCb->getPathsInfo(hero)->getPathInfo(rhs->visitablePos());
  164. return ln->getCost() < rn->getCost();
  165. }
  166. bool isSafeToVisit(HeroPtr h, crint3 tile)
  167. {
  168. return isSafeToVisit(h, fh->evaluateDanger(tile, h.get()));
  169. }
  170. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength)
  171. {
  172. const ui64 heroStrength = h->getTotalStrength();
  173. if(dangerStrength)
  174. {
  175. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  176. }
  177. return true; //there's no danger
  178. }
  179. bool isObjectRemovable(const CGObjectInstance * obj)
  180. {
  181. //FIXME: move logic to object property!
  182. switch (obj->ID)
  183. {
  184. case Obj::MONSTER:
  185. case Obj::RESOURCE:
  186. case Obj::CAMPFIRE:
  187. case Obj::TREASURE_CHEST:
  188. case Obj::ARTIFACT:
  189. case Obj::BORDERGUARD:
  190. case Obj::FLOTSAM:
  191. case Obj::PANDORAS_BOX:
  192. case Obj::OCEAN_BOTTLE:
  193. case Obj::SEA_CHEST:
  194. case Obj::SHIPWRECK_SURVIVOR:
  195. case Obj::SPELL_SCROLL:
  196. return true;
  197. break;
  198. default:
  199. return false;
  200. break;
  201. }
  202. }
  203. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  204. {
  205. // TODO: Such information should be provided by pathfinder
  206. // Tile must be free or with unoccupied boat
  207. if(!t->blocked)
  208. {
  209. return true;
  210. }
  211. else if(!fromWater) // do not try to board when in water sector
  212. {
  213. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  214. return true;
  215. }
  216. return false;
  217. }
  218. bool isBlockedBorderGate(int3 tileToHit) //TODO: is that function needed? should be handled by pathfinder
  219. {
  220. if(cb->getTile(tileToHit)->topVisitableId() != Obj::BORDER_GATE)
  221. return false;
  222. auto gate = dynamic_cast<const CGKeys *>(cb->getTile(tileToHit)->topVisitableObj());
  223. return !gate->passableFor(ai->playerID);
  224. }
  225. bool isBlockVisitObj(const int3 & pos)
  226. {
  227. if(auto obj = cb->getTopObj(pos))
  228. {
  229. if(obj->blockVisit) //we can't stand on that object
  230. return true;
  231. }
  232. return false;
  233. }
  234. creInfo infoFromDC(const dwellingContent & dc)
  235. {
  236. creInfo ci;
  237. ci.count = dc.first;
  238. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  239. if (ci.creID != -1)
  240. {
  241. ci.cre = VLC->creh->objects[ci.creID];
  242. ci.level = ci.cre->level; //this is cretaure tier, while tryRealize expects dwelling level. Ignore.
  243. }
  244. else
  245. {
  246. ci.cre = nullptr;
  247. ci.level = 0;
  248. }
  249. return ci;
  250. }
  251. bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
  252. {
  253. return h1->getTotalStrength() < h2->getTotalStrength();
  254. }
  255. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  256. {
  257. return a1->getArmyStrength() < a2->getArmyStrength();
  258. }
  259. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  260. {
  261. auto art1 = a1->artType;
  262. auto art2 = a2->artType;
  263. if(art1->price == art2->price)
  264. return art1->valOfBonuses(Bonus::PRIMARY_SKILL) > art2->valOfBonuses(Bonus::PRIMARY_SKILL);
  265. else
  266. return art1->price > art2->price;
  267. }