AIUtility.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * AIUtility.h, 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. #pragma once
  11. #include "../../lib/VCMI_Lib.h"
  12. #include "../../lib/CCreatureHandler.h"
  13. #include "../../lib/spells/CSpellHandler.h"
  14. #include "../../lib/CStopWatch.h"
  15. #include "../../lib/mapObjects/CGHeroInstance.h"
  16. #include "../../CCallback.h"
  17. class VCAI;
  18. class CCallback;
  19. struct creInfo;
  20. using crint3 = const int3 &;
  21. using crstring = const std::string &;
  22. using dwellingContent = std::pair<ui32, std::vector<CreatureID>>;
  23. const int ACTUAL_RESOURCE_COUNT = 7;
  24. const int ALLOWED_ROAMING_HEROES = 8;
  25. //implementation-dependent
  26. extern const double SAFE_ATTACK_CONSTANT;
  27. extern const int GOLD_RESERVE;
  28. extern thread_local CCallback * cb;
  29. extern thread_local VCAI * ai;
  30. //provisional class for AI to store a reference to an owned hero object
  31. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  32. struct DLL_EXPORT HeroPtr
  33. {
  34. const CGHeroInstance * h;
  35. ObjectInstanceID hid;
  36. public:
  37. std::string name;
  38. HeroPtr();
  39. HeroPtr(const CGHeroInstance * H);
  40. ~HeroPtr();
  41. operator bool() const
  42. {
  43. return validAndSet();
  44. }
  45. bool operator<(const HeroPtr & rhs) const;
  46. const CGHeroInstance * operator->() const;
  47. const CGHeroInstance * operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
  48. bool operator==(const HeroPtr & rhs) const;
  49. bool operator!=(const HeroPtr & rhs) const
  50. {
  51. return !(*this == rhs);
  52. }
  53. const CGHeroInstance * get(bool doWeExpectNull = false) const;
  54. bool validAndSet() const;
  55. };
  56. enum BattleState
  57. {
  58. NO_BATTLE,
  59. UPCOMING_BATTLE,
  60. ONGOING_BATTLE,
  61. ENDING_BATTLE
  62. };
  63. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  64. // This class stores object id, so we can detect when we lose access to the underlying object.
  65. struct ObjectIdRef
  66. {
  67. ObjectInstanceID id;
  68. const CGObjectInstance * operator->() const;
  69. operator const CGObjectInstance *() const;
  70. operator bool() const;
  71. ObjectIdRef(ObjectInstanceID _id);
  72. ObjectIdRef(const CGObjectInstance * obj);
  73. bool operator<(const ObjectIdRef & rhs) const;
  74. };
  75. struct TimeCheck
  76. {
  77. CStopWatch time;
  78. std::string txt;
  79. TimeCheck(crstring TXT)
  80. : txt(TXT)
  81. {
  82. }
  83. ~TimeCheck()
  84. {
  85. logAi->trace("Time of %s was %d ms.", txt, time.getDiff());
  86. }
  87. };
  88. //TODO: replace with vstd::
  89. struct AtScopeExit
  90. {
  91. std::function<void()> foo;
  92. AtScopeExit(const std::function<void()> & FOO)
  93. : foo(FOO)
  94. {}
  95. ~AtScopeExit()
  96. {
  97. foo();
  98. }
  99. };
  100. class ObjsVector : public std::vector<ObjectIdRef>
  101. {
  102. };
  103. template<Obj::Type id>
  104. bool objWithID(const CGObjectInstance * obj)
  105. {
  106. return obj->ID == id;
  107. }
  108. struct creInfo
  109. {
  110. int count;
  111. CreatureID creID;
  112. const Creature * cre;
  113. int level;
  114. };
  115. creInfo infoFromDC(const dwellingContent & dc);
  116. template<class Func>
  117. void foreach_tile_pos(const Func & foo)
  118. {
  119. // some micro-optimizations since this function gets called a LOT
  120. // callback pointer is thread-specific and slow to retrieve -> read map size only once
  121. int3 mapSize = cb->getMapSize();
  122. for(int z = 0; z < mapSize.z; z++)
  123. {
  124. for(int x = 0; x < mapSize.x; x++)
  125. {
  126. for(int y = 0; y < mapSize.y; y++)
  127. {
  128. foo(int3(x, y, z));
  129. }
  130. }
  131. }
  132. }
  133. template<class Func>
  134. void foreach_tile_pos(CCallback * cbp, const Func & foo) // avoid costly retrieval of thread-specific pointer
  135. {
  136. int3 mapSize = cbp->getMapSize();
  137. for(int z = 0; z < mapSize.z; z++)
  138. {
  139. for(int x = 0; x < mapSize.x; x++)
  140. {
  141. for(int y = 0; y < mapSize.y; y++)
  142. {
  143. foo(cbp, int3(x, y, z));
  144. }
  145. }
  146. }
  147. }
  148. template<class Func>
  149. void foreach_neighbour(const int3 & pos, const Func & foo)
  150. {
  151. CCallback * cbp = cb; // avoid costly retrieval of thread-specific pointer
  152. for(const int3 & dir : int3::getDirs())
  153. {
  154. const int3 n = pos + dir;
  155. if(cbp->isInTheMap(n))
  156. foo(pos + dir);
  157. }
  158. }
  159. template<class Func>
  160. void foreach_neighbour(CCallback * cbp, const int3 & pos, const Func & foo) // avoid costly retrieval of thread-specific pointer
  161. {
  162. for(const int3 & dir : int3::getDirs())
  163. {
  164. const int3 n = pos + dir;
  165. if(cbp->isInTheMap(n))
  166. foo(cbp, pos + dir);
  167. }
  168. }
  169. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
  170. bool isBlockedBorderGate(int3 tileToHit);
  171. bool isBlockVisitObj(const int3 & pos);
  172. bool isWeeklyRevisitable(const CGObjectInstance * obj);
  173. bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
  174. bool isObjectRemovable(const CGObjectInstance * obj); //FIXME FIXME: move logic to object property!
  175. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength);
  176. bool isSafeToVisit(HeroPtr h, crint3 tile);
  177. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  178. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
  179. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
  180. class CDistanceSorter
  181. {
  182. const CGHeroInstance * hero;
  183. public:
  184. CDistanceSorter(const CGHeroInstance * hero)
  185. : hero(hero)
  186. {
  187. }
  188. bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const;
  189. };