AIUtility.h 4.9 KB

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