AIUtility.cpp 5.2 KB

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