AIUtility.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. const CGHeroInstance * get(bool doWeExpectNull = false) const;
  51. bool validAndSet() const;
  52. template<typename Handler> void serialize(Handler & h, const int version)
  53. {
  54. h & this->h;
  55. h & hid;
  56. h & name;
  57. }
  58. };
  59. enum BattleState
  60. {
  61. NO_BATTLE,
  62. UPCOMING_BATTLE,
  63. ONGOING_BATTLE,
  64. ENDING_BATTLE
  65. };
  66. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  67. // This class stores object id, so we can detect when we lose access to the underlying object.
  68. struct ObjectIdRef
  69. {
  70. ObjectInstanceID id;
  71. const CGObjectInstance * operator->() const;
  72. operator const CGObjectInstance *() const;
  73. ObjectIdRef(ObjectInstanceID _id);
  74. ObjectIdRef(const CGObjectInstance * obj);
  75. bool operator<(const ObjectIdRef & rhs) const;
  76. template<typename Handler> void serialize(Handler & h, const int version)
  77. {
  78. h & id;
  79. }
  80. };
  81. struct TimeCheck
  82. {
  83. CStopWatch time;
  84. std::string txt;
  85. TimeCheck(crstring TXT)
  86. : txt(TXT)
  87. {
  88. }
  89. ~TimeCheck()
  90. {
  91. logAi->trace("Time of %s was %d ms.", txt, time.getDiff());
  92. }
  93. };
  94. //TODO: replace with vstd::
  95. struct AtScopeExit
  96. {
  97. std::function<void()> foo;
  98. AtScopeExit(const std::function<void()> & FOO)
  99. : foo(FOO)
  100. {}
  101. ~AtScopeExit()
  102. {
  103. foo();
  104. }
  105. };
  106. class ObjsVector : public std::vector<ObjectIdRef>
  107. {
  108. };
  109. template<int id>
  110. bool objWithID(const CGObjectInstance * obj)
  111. {
  112. return obj->ID == id;
  113. }
  114. struct creInfo
  115. {
  116. int count;
  117. CreatureID creID;
  118. CCreature * cre;
  119. int level;
  120. };
  121. creInfo infoFromDC(const dwellingContent & dc);
  122. void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
  123. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  124. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
  125. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  126. int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp, HeroPtr hero);
  127. void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out);
  128. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
  129. bool isBlockedBorderGate(int3 tileToHit);
  130. bool isBlockVisitObj(const int3 & pos);
  131. bool isWeeklyRevisitable(const CGObjectInstance * obj);
  132. bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
  133. ui64 evaluateDanger(const CGObjectInstance * obj);
  134. ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor);
  135. bool isSafeToVisit(HeroPtr h, crint3 tile);
  136. bool isObjectRemovable(const CGObjectInstance * obj); //FIXME FIXME: move logic to object property!
  137. bool compareMovement(HeroPtr lhs, HeroPtr rhs);
  138. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  139. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
  140. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
  141. ui64 howManyReinforcementsCanBuy(HeroPtr h, const CGTownInstance * t);
  142. ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance * t);
  143. int3 whereToExplore(HeroPtr h);
  144. class CDistanceSorter
  145. {
  146. const CGHeroInstance * hero;
  147. public:
  148. CDistanceSorter(const CGHeroInstance * hero)
  149. : hero(hero)
  150. {
  151. }
  152. bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs);
  153. };