AIUtility.h 5.1 KB

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