AIUtility.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 FuzzyHelper * fh;
  23. const CGObjectInstance * ObjectIdRef::operator->() const
  24. {
  25. return cb->getObj(id, false);
  26. }
  27. ObjectIdRef::operator const CGObjectInstance *() const
  28. {
  29. return cb->getObj(id, false);
  30. }
  31. ObjectIdRef::operator bool() const
  32. {
  33. return cb->getObj(id, false);
  34. }
  35. ObjectIdRef::ObjectIdRef(ObjectInstanceID _id)
  36. : id(_id)
  37. {
  38. }
  39. ObjectIdRef::ObjectIdRef(const CGObjectInstance * obj)
  40. : id(obj->id)
  41. {
  42. }
  43. bool ObjectIdRef::operator<(const ObjectIdRef & rhs) const
  44. {
  45. return id < rhs.id;
  46. }
  47. HeroPtr::HeroPtr(const CGHeroInstance * H)
  48. {
  49. if(!H)
  50. {
  51. //init from nullptr should equal to default init
  52. *this = HeroPtr();
  53. return;
  54. }
  55. h = H;
  56. name = h->getNameTranslated();
  57. hid = H->id;
  58. // infosCount[ai->playerID][hid]++;
  59. }
  60. HeroPtr::HeroPtr()
  61. {
  62. h = nullptr;
  63. hid = ObjectInstanceID();
  64. }
  65. HeroPtr::~HeroPtr()
  66. {
  67. // if(hid >= 0)
  68. // infosCount[ai->playerID][hid]--;
  69. }
  70. bool HeroPtr::operator<(const HeroPtr & rhs) const
  71. {
  72. return hid < rhs.hid;
  73. }
  74. const CGHeroInstance * HeroPtr::get(bool doWeExpectNull) const
  75. {
  76. //TODO? check if these all assertions every time we get info about hero affect efficiency
  77. //
  78. //behave terribly when attempting unauthorized access to hero that is not ours (or was lost)
  79. assert(doWeExpectNull || h);
  80. if(h)
  81. {
  82. auto obj = cb->getObj(hid);
  83. const bool owned = obj && obj->tempOwner == ai->playerID;
  84. if(doWeExpectNull && !owned)
  85. {
  86. return nullptr;
  87. }
  88. else
  89. {
  90. assert(obj);
  91. assert(owned);
  92. }
  93. }
  94. return h;
  95. }
  96. const CGHeroInstance * HeroPtr::operator->() const
  97. {
  98. return get();
  99. }
  100. bool HeroPtr::validAndSet() const
  101. {
  102. return get(true);
  103. }
  104. const CGHeroInstance * HeroPtr::operator*() const
  105. {
  106. return get();
  107. }
  108. bool HeroPtr::operator==(const HeroPtr & rhs) const
  109. {
  110. return h == rhs.get(true);
  111. }
  112. bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const
  113. {
  114. const CGPathNode * ln = ai->myCb->getPathsInfo(hero)->getPathInfo(lhs->visitablePos());
  115. const CGPathNode * rn = ai->myCb->getPathsInfo(hero)->getPathInfo(rhs->visitablePos());
  116. return ln->getCost() < rn->getCost();
  117. }
  118. bool isSafeToVisit(HeroPtr h, crint3 tile)
  119. {
  120. return isSafeToVisit(h, fh->evaluateDanger(tile, h.get()));
  121. }
  122. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength)
  123. {
  124. const ui64 heroStrength = h->getTotalStrength();
  125. if(dangerStrength)
  126. {
  127. return heroStrength / SAFE_ATTACK_CONSTANT > dangerStrength;
  128. }
  129. return true; //there's no danger
  130. }
  131. bool isObjectRemovable(const CGObjectInstance * obj)
  132. {
  133. //FIXME: move logic to object property!
  134. switch (obj->ID)
  135. {
  136. case Obj::MONSTER:
  137. case Obj::RESOURCE:
  138. case Obj::CAMPFIRE:
  139. case Obj::TREASURE_CHEST:
  140. case Obj::ARTIFACT:
  141. case Obj::BORDERGUARD:
  142. case Obj::FLOTSAM:
  143. case Obj::PANDORAS_BOX:
  144. case Obj::OCEAN_BOTTLE:
  145. case Obj::SEA_CHEST:
  146. case Obj::SHIPWRECK_SURVIVOR:
  147. case Obj::SPELL_SCROLL:
  148. return true;
  149. break;
  150. default:
  151. return false;
  152. break;
  153. }
  154. }
  155. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater)
  156. {
  157. // TODO: Such information should be provided by pathfinder
  158. // Tile must be free or with unoccupied boat
  159. if(!t->blocked)
  160. {
  161. return true;
  162. }
  163. else if(!fromWater) // do not try to board when in water sector
  164. {
  165. if(t->visitableObjects.size() == 1 && t->topVisitableId() == Obj::BOAT)
  166. return true;
  167. }
  168. return false;
  169. }
  170. bool isBlockedBorderGate(int3 tileToHit) //TODO: is that function needed? should be handled by pathfinder
  171. {
  172. if(cb->getTile(tileToHit)->topVisitableId() != Obj::BORDER_GATE)
  173. return false;
  174. auto gate = dynamic_cast<const CGKeys *>(cb->getTile(tileToHit)->topVisitableObj());
  175. return !gate->passableFor(ai->playerID);
  176. }
  177. bool isBlockVisitObj(const int3 & pos)
  178. {
  179. if(auto obj = cb->getTopObj(pos))
  180. {
  181. if(obj->isBlockedVisitable()) //we can't stand on that object
  182. return true;
  183. }
  184. return false;
  185. }
  186. creInfo infoFromDC(const dwellingContent & dc)
  187. {
  188. creInfo ci;
  189. ci.count = dc.first;
  190. ci.creID = dc.second.size() ? dc.second.back() : CreatureID(-1); //should never be accessed
  191. if (ci.creID != CreatureID::NONE)
  192. {
  193. ci.cre = VLC->creatures()->getById(ci.creID);
  194. ci.level = ci.cre->getLevel(); //this is creature tier, while tryRealize expects dwelling level. Ignore.
  195. }
  196. else
  197. {
  198. ci.cre = nullptr;
  199. ci.level = 0;
  200. }
  201. return ci;
  202. }
  203. bool compareHeroStrength(HeroPtr h1, HeroPtr h2)
  204. {
  205. return h1->getTotalStrength() < h2->getTotalStrength();
  206. }
  207. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2)
  208. {
  209. return a1->getArmyStrength() < a2->getArmyStrength();
  210. }
  211. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2)
  212. {
  213. auto art1 = a1->artType;
  214. auto art2 = a2->artType;
  215. if(art1->getPrice() == art2->getPrice())
  216. return art1->valOfBonuses(BonusType::PRIMARY_SKILL) > art2->valOfBonuses(BonusType::PRIMARY_SKILL);
  217. else
  218. return art1->getPrice() > art2->getPrice();
  219. }