AIUtility.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. class CCallback;
  21. struct creInfo;
  22. typedef const int3 & crint3;
  23. typedef const std::string & crstring;
  24. typedef std::pair<ui32, std::vector<CreatureID>> dwellingContent;
  25. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  26. const int ACTUAL_RESOURCE_COUNT = 7;
  27. const int ALLOWED_ROAMING_HEROES = 8;
  28. //implementation-dependent
  29. extern const float SAFE_ATTACK_CONSTANT;
  30. extern const int GOLD_RESERVE;
  31. enum HeroRole
  32. {
  33. MAIN,
  34. SCOUT
  35. };
  36. //provisional class for AI to store a reference to an owned hero object
  37. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  38. struct DLL_EXPORT HeroPtr
  39. {
  40. const CGHeroInstance * h;
  41. ObjectInstanceID hid;
  42. public:
  43. std::string name;
  44. HeroPtr();
  45. HeroPtr(const CGHeroInstance * H);
  46. ~HeroPtr();
  47. operator bool() const
  48. {
  49. return validAndSet();
  50. }
  51. bool operator<(const HeroPtr & rhs) const;
  52. const CGHeroInstance * operator->() const;
  53. const CGHeroInstance * operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
  54. bool operator==(const HeroPtr & rhs) const;
  55. bool operator!=(const HeroPtr & rhs) const
  56. {
  57. return !(*this == rhs);
  58. }
  59. const CGHeroInstance * get(bool doWeExpectNull = false) const;
  60. bool validAndSet() const;
  61. template<typename Handler> void serialize(Handler & h, const int version)
  62. {
  63. h & this->h;
  64. h & hid;
  65. h & name;
  66. }
  67. };
  68. enum BattleState
  69. {
  70. NO_BATTLE,
  71. UPCOMING_BATTLE,
  72. ONGOING_BATTLE,
  73. ENDING_BATTLE
  74. };
  75. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  76. // This class stores object id, so we can detect when we lose access to the underlying object.
  77. struct ObjectIdRef
  78. {
  79. ObjectInstanceID id;
  80. const CGObjectInstance * operator->() const;
  81. operator const CGObjectInstance *() const;
  82. operator bool() const;
  83. ObjectIdRef(ObjectInstanceID _id);
  84. ObjectIdRef(const CGObjectInstance * obj);
  85. bool operator<(const ObjectIdRef & rhs) const;
  86. template<typename Handler> void serialize(Handler & h, const int version)
  87. {
  88. h & id;
  89. }
  90. };
  91. struct TimeCheck
  92. {
  93. CStopWatch time;
  94. std::string txt;
  95. TimeCheck(crstring TXT)
  96. : txt(TXT)
  97. {
  98. }
  99. ~TimeCheck()
  100. {
  101. logAi->trace("Time of %s was %d ms.", txt, time.getDiff());
  102. }
  103. };
  104. //TODO: replace with vstd::
  105. struct AtScopeExit
  106. {
  107. std::function<void()> foo;
  108. AtScopeExit(const std::function<void()> & FOO)
  109. : foo(FOO)
  110. {}
  111. ~AtScopeExit()
  112. {
  113. foo();
  114. }
  115. };
  116. class ObjsVector : public std::vector<ObjectIdRef>
  117. {
  118. };
  119. template<int id>
  120. bool objWithID(const CGObjectInstance * obj)
  121. {
  122. return obj->ID == id;
  123. }
  124. struct creInfo
  125. {
  126. int count;
  127. CreatureID creID;
  128. CCreature * cre;
  129. int level;
  130. };
  131. creInfo infoFromDC(const dwellingContent & dc);
  132. void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
  133. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  134. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
  135. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  136. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
  137. bool isBlockedBorderGate(int3 tileToHit);
  138. bool isBlockVisitObj(const int3 & pos);
  139. bool isWeeklyRevisitable(const CGObjectInstance * obj);
  140. bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
  141. bool isObjectRemovable(const CGObjectInstance * obj); //FIXME FIXME: move logic to object property!
  142. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength);
  143. bool isSafeToVisit(HeroPtr h, const CCreatureSet *, uint64_t dangerStrength);
  144. bool isSafeToVisit(HeroPtr h, crint3 tile);
  145. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  146. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
  147. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
  148. class CDistanceSorter
  149. {
  150. const CGHeroInstance * hero;
  151. public:
  152. CDistanceSorter(const CGHeroInstance * hero)
  153. : hero(hero)
  154. {
  155. }
  156. bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const;
  157. };