AIUtility.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 double SAFE_ATTACK_CONSTANT;
  30. extern const int GOLD_RESERVE;
  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. CCreature * cre;
  124. int level;
  125. };
  126. creInfo infoFromDC(const dwellingContent & dc);
  127. void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
  128. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  129. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
  130. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  131. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
  132. bool isBlockedBorderGate(int3 tileToHit);
  133. bool isBlockVisitObj(const int3 & pos);
  134. bool isWeeklyRevisitable(const CGObjectInstance * obj);
  135. bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
  136. bool isObjectRemovable(const CGObjectInstance * obj); //FIXME FIXME: move logic to object property!
  137. bool isSafeToVisit(HeroPtr h, uint64_t dangerStrength);
  138. bool isSafeToVisit(HeroPtr h, const CCreatureSet *, uint64_t dangerStrength);
  139. bool isSafeToVisit(HeroPtr h, crint3 tile);
  140. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  141. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
  142. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
  143. class CDistanceSorter
  144. {
  145. const CGHeroInstance * hero;
  146. public:
  147. CDistanceSorter(const CGHeroInstance * hero)
  148. : hero(hero)
  149. {
  150. }
  151. bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs) const;
  152. };