AIUtility.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include "../../lib/VCMI_Lib.h"
  3. #include "../../lib/CBuildingHandler.h"
  4. #include "../../lib/CCreatureHandler.h"
  5. #include "../../lib/CTownHandler.h"
  6. #include "../../lib/CSpellHandler.h"
  7. #include "../../lib/CObjectHandler.h"
  8. #include "../../lib/Connection.h"
  9. #include "../../lib/CGameState.h"
  10. #include "../../lib/mapping/CMap.h"
  11. #include "../../lib/NetPacks.h"
  12. #include "../../lib/CStopWatch.h"
  13. /*
  14. * AIUtility.h, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. //provisional class for AI to store a reference to an owned hero object
  23. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  24. typedef const int3& crint3;
  25. typedef const std::string& crstring;
  26. const int HERO_GOLD_COST = 2500;
  27. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  28. const int ACTUAL_RESOURCE_COUNT = 7;
  29. const int ALLOWED_ROAMING_HEROES = 8;
  30. //implementation-dependent
  31. extern const double SAFE_ATTACK_CONSTANT;
  32. extern const int GOLD_RESERVE;
  33. struct 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. const CGHeroInstance *get(bool doWeExpectNull = false) const;
  50. bool validAndSet() const;
  51. template <typename Handler> void serialize(Handler &h, const int version)
  52. {
  53. h & this->h & hid & name;
  54. }
  55. };
  56. enum BattleState
  57. {
  58. NO_BATTLE,
  59. UPCOMING_BATTLE,
  60. ONGOING_BATTLE,
  61. ENDING_BATTLE
  62. };
  63. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  64. // This class stores object id, so we can detect when we lose access to the underlying object.
  65. struct ObjectIdRef
  66. {
  67. ObjectInstanceID id;
  68. const CGObjectInstance *operator->() const;
  69. operator const CGObjectInstance *() const;
  70. ObjectIdRef(ObjectInstanceID _id);
  71. ObjectIdRef(const CGObjectInstance *obj);
  72. bool operator<(const ObjectIdRef &rhs) const;
  73. template <typename Handler> void serialize(Handler &h, const int version)
  74. {
  75. h & id;
  76. }
  77. };
  78. struct TimeCheck
  79. {
  80. CStopWatch time;
  81. std::string txt;
  82. TimeCheck(crstring TXT) : txt(TXT)
  83. {
  84. }
  85. ~TimeCheck()
  86. {
  87. logAi->debugStream() << boost::format("Time of %s was %d ms.") % txt % time.getDiff();
  88. }
  89. };
  90. struct AtScopeExit
  91. {
  92. std::function<void()> foo;
  93. AtScopeExit(const std::function<void()> &FOO) : foo(FOO)
  94. {}
  95. ~AtScopeExit()
  96. {
  97. foo();
  98. }
  99. };
  100. class ObjsVector : public std::vector<ObjectIdRef>
  101. {
  102. private:
  103. };
  104. template<int id>
  105. bool objWithID(const CGObjectInstance *obj)
  106. {
  107. return obj->ID == id;
  108. }
  109. template <typename Container, typename Item>
  110. bool erase_if_present(Container &c, const Item &item)
  111. {
  112. auto i = std::find(c.begin(), c.end(), item);
  113. if (i != c.end())
  114. {
  115. c.erase(i);
  116. return true;
  117. }
  118. return false;
  119. }
  120. template <typename V, typename Item, typename Item2>
  121. bool erase_if_present(std::map<Item,V> & c, const Item2 &item)
  122. {
  123. auto i = c.find(item);
  124. if (i != c.end())
  125. {
  126. c.erase(i);
  127. return true;
  128. }
  129. return false;
  130. }
  131. template <typename Container, typename Pred>
  132. void erase(Container &c, Pred pred)
  133. {
  134. c.erase(boost::remove_if(c, pred), c.end());
  135. }
  136. template<typename T>
  137. void removeDuplicates(std::vector<T> &vec)
  138. {
  139. boost::sort(vec);
  140. vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  141. }
  142. std::string strFromInt3(int3 pos);
  143. void foreach_tile_pos(std::function<void(const int3& pos)> foo);
  144. void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
  145. int howManyTilesWillBeDiscovered(const int3 &pos, int radious);
  146. int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
  147. void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out);
  148. bool canBeEmbarkmentPoint(const TerrainTile *t);
  149. bool isBlockedBorderGate(int3 tileToHit);
  150. bool isReachable(const CGObjectInstance *obj);
  151. bool isCloser(const CGObjectInstance *lhs, const CGObjectInstance *rhs);
  152. bool isWeeklyRevisitable (const CGObjectInstance * obj);
  153. bool shouldVisit (HeroPtr h, const CGObjectInstance * obj);
  154. ui64 evaluateDanger(const CGObjectInstance *obj);
  155. ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor);
  156. bool isSafeToVisit(HeroPtr h, crint3 tile);
  157. bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2);
  158. bool compareMovement(HeroPtr lhs, HeroPtr rhs);
  159. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  160. bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2);
  161. ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t);
  162. int3 whereToExplore(HeroPtr h);