AIUtility.h 5.4 KB

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