AIUtility.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. typedef const int3 & crint3;
  22. typedef const std::string & crstring;
  23. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  24. const int ACTUAL_RESOURCE_COUNT = 7;
  25. const int ALLOWED_ROAMING_HEROES = 8;
  26. //implementation-dependent
  27. extern const double SAFE_ATTACK_CONSTANT;
  28. extern const int GOLD_RESERVE;
  29. //provisional class for AI to store a reference to an owned hero object
  30. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  31. struct HeroPtr
  32. {
  33. const CGHeroInstance * h;
  34. ObjectInstanceID hid;
  35. public:
  36. std::string name;
  37. HeroPtr();
  38. HeroPtr(const CGHeroInstance * H);
  39. ~HeroPtr();
  40. operator bool() const
  41. {
  42. return validAndSet();
  43. }
  44. bool operator<(const HeroPtr & rhs) const;
  45. const CGHeroInstance * operator->() const;
  46. const CGHeroInstance * operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
  47. const CGHeroInstance * get(bool doWeExpectNull = false) const;
  48. bool validAndSet() const;
  49. template<typename Handler> void serialize(Handler & h, const int version)
  50. {
  51. h & this->h;
  52. h & hid;
  53. h & 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)
  83. : txt(TXT)
  84. {
  85. }
  86. ~TimeCheck()
  87. {
  88. logAi->trace("Time of %s was %d ms.", txt, time.getDiff());
  89. }
  90. };
  91. //TODO: replace with vstd::
  92. struct AtScopeExit
  93. {
  94. std::function<void()> foo;
  95. AtScopeExit(const std::function<void()> & FOO)
  96. : foo(FOO)
  97. {}
  98. ~AtScopeExit()
  99. {
  100. foo();
  101. }
  102. };
  103. class ObjsVector : public std::vector<ObjectIdRef>
  104. {
  105. };
  106. template<int id>
  107. bool objWithID(const CGObjectInstance * obj)
  108. {
  109. return obj->ID == id;
  110. }
  111. void foreach_tile_pos(std::function<void(const int3 & pos)> foo);
  112. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  113. void foreach_neighbour(const int3 & pos, std::function<void(const int3 & pos)> foo);
  114. void foreach_neighbour(CCallback * cbp, const int3 & pos, std::function<void(CCallback * cbp, const int3 & pos)> foo); // avoid costly retrieval of thread-specific pointer
  115. int howManyTilesWillBeDiscovered(const int3 & pos, int radious, CCallback * cbp, HeroPtr hero);
  116. void getVisibleNeighbours(const std::vector<int3> & tiles, std::vector<int3> & out);
  117. bool canBeEmbarkmentPoint(const TerrainTile * t, bool fromWater);
  118. bool isBlockedBorderGate(int3 tileToHit);
  119. bool isBlockVisitObj(const int3 & pos);
  120. bool isWeeklyRevisitable(const CGObjectInstance * obj);
  121. bool shouldVisit(HeroPtr h, const CGObjectInstance * obj);
  122. ui64 evaluateDanger(const CGObjectInstance * obj);
  123. ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor);
  124. bool isSafeToVisit(HeroPtr h, crint3 tile);
  125. bool compareMovement(HeroPtr lhs, HeroPtr rhs);
  126. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  127. bool compareArmyStrength(const CArmedInstance * a1, const CArmedInstance * a2);
  128. bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2);
  129. ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance * t);
  130. int3 whereToExplore(HeroPtr h);
  131. class CDistanceSorter
  132. {
  133. const CGHeroInstance * hero;
  134. public:
  135. CDistanceSorter(const CGHeroInstance * hero)
  136. : hero(hero)
  137. {
  138. }
  139. bool operator()(const CGObjectInstance * lhs, const CGObjectInstance * rhs);
  140. };