2
0

AIUtility.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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->getNameTranslated();
  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. bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
  116. {
  117. const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());
  118. const CGPathNode * rn = ai->myCb->getPathsInfo(hero)->getPathInfo(rhs->visitablePos());
  119. return ln->getCost() < rn->getCost();
  120. }
  121. bool isSafeToVisit(HeroPtr h, crint3 tile)
  122. {
  123. return isSafeToVisit(h, fh->evaluateDanger(tile, h.get()));
  124. }
  125. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength)
  126. {
  127. const ui64 heroStrength = h->getTotalStrength();
  128. if(dangerStrength)
  129. {
  130. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  131. }
  132. return true; //there's no danger
  133. }
  134. bool isObjectRemovable(const CGObjectInstance * obj)
  135. {
  136. //FIXME: move logic to object property!
  137. switch (obj->ID)
  138. {
  139. case Obj::MONSTER:
  140. case Obj::RESOURCE:
  141. case Obj::CAMPFIRE:
  142. case Obj::TREASURE_CHEST:
  143. case Obj::ARTIFACT:
  144. case Obj::BORDERGUARD:
  145. case Obj::FLOTSAM:
  146. case Obj::PANDORAS_BOX:
  147. case Obj::OCEAN_BOTTLE:
  148. case Obj::SEA_CHEST:
  149. case Obj::SHIPWRECK_SURVIVOR:
  150. case Obj::SPELL_SCROLL:
  151. return true;
  152. break;
  153. default:
  154. return false;
  155. break;
  156. }
  157. }
  158. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  159. {
  160. // TODO: Such information should be provided by pathfinder
  161. // Tile must be free or with unoccupied boat
  162. if(!t->blocked)
  163. {
  164. return true;
  165. }
  166. else if(!fromWater) // do not try to board when in water sector
  167. {
  168. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  169. return true;
  170. }
  171. return false;
  172. }
  173. bool isBlockedBorderGate(int3 tileToHit) //TODO: is that function needed? should be handled by pathfinder
  174. {
  175. if(cb->getTile(tileToHit)->topVisitableId() != Obj::BORDER_GATE)
  176. return false;
  177. auto gate = dynamic_cast<const CGKeys *>(cb->getTile(tileToHit)->topVisitableObj());
  178. return !gate->passableFor(ai->playerID);
  179. }
  180. bool isBlockVisitObj(const int3 & pos)
  181. {
  182. if(auto obj = cb->getTopObj(pos))
  183. {
  184. if(obj->blockVisit) //we can't stand on that object
  185. return true;
  186. }
  187. return false;
  188. }
  189. creInfo infoFromDC(const dwellingContent & dc)
  190. {
  191. creInfo ci;
  192. ci.count = dc.first;
  193. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  194. if (ci.creID != -1)
  195. {
  196. ci.cre = VLC->creatures()->getById(ci.creID);
  197. ci.level = ci.cre->getLevel(); //this is creature tier, while tryRealize expects dwelling level. Ignore.
  198. }
  199. else
  200. {
  201. ci.cre = nullptr;
  202. ci.level = 0;
  203. }
  204. return ci;
  205. }
  206. bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
  207. {
  208. return h1->getTotalStrength() < h2->getTotalStrength();
  209. }
  210. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  211. {
  212. return a1->getArmyStrength() < a2->getArmyStrength();
  213. }
  214. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  215. {
  216. auto art1 = a1->artType;
  217. auto art2 = a2->artType;
  218. if(art1->price == art2->price)
  219. return art1->valOfBonuses(Bonus::PRIMARY_SKILL) > art2->valOfBonuses(Bonus::PRIMARY_SKILL);
  220. else
  221. return art1->price > art2->price;
  222. }