AIUtility.h 5.3 KB

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