AIUtility.h 5.4 KB

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