2
0

AIUtility.h 4.9 KB

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