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