AIUtility.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/spells/CSpellHandler.h"
  7. #include "../../lib/Connection.h"
  8. #include "../../lib/CStopWatch.h"
  9. /*
  10. * AIUtility.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. class CCallback;
  19. typedef const int3& crint3;
  20. typedef const std::string& crstring;
  21. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  22. const int ACTUAL_RESOURCE_COUNT = 7;
  23. const int ALLOWED_ROAMING_HEROES = 8;
  24. //implementation-dependent
  25. extern const double SAFE_ATTACK_CONSTANT;
  26. extern const int GOLD_RESERVE;
  27. //provisional class for AI to store a reference to an owned hero object
  28. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  29. struct HeroPtr
  30. {
  31. const CGHeroInstance *h;
  32. ObjectInstanceID hid;
  33. public:
  34. std::string name;
  35. HeroPtr();
  36. HeroPtr(const CGHeroInstance *H);
  37. ~HeroPtr();
  38. operator bool() const
  39. {
  40. return validAndSet();
  41. }
  42. bool operator<(const HeroPtr &rhs) const;
  43. const CGHeroInstance *operator->() const;
  44. const CGHeroInstance *operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
  45. const CGHeroInstance *get(bool doWeExpectNull = false) const;
  46. bool validAndSet() const;
  47. template <typename Handler> void serialize(Handler &h, const int version)
  48. {
  49. h & this->h & hid & name;
  50. }
  51. };
  52. enum BattleState
  53. {
  54. NO_BATTLE,
  55. UPCOMING_BATTLE,
  56. ONGOING_BATTLE,
  57. ENDING_BATTLE
  58. };
  59. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  60. // This class stores object id, so we can detect when we lose access to the underlying object.
  61. struct ObjectIdRef
  62. {
  63. ObjectInstanceID id;
  64. const CGObjectInstance *operator->() const;
  65. operator const CGObjectInstance *() const;
  66. ObjectIdRef(ObjectInstanceID _id);
  67. ObjectIdRef(const CGObjectInstance *obj);
  68. bool operator<(const ObjectIdRef &rhs) const;
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & id;
  72. }
  73. };
  74. struct TimeCheck
  75. {
  76. CStopWatch time;
  77. std::string txt;
  78. TimeCheck(crstring TXT) : txt(TXT)
  79. {
  80. }
  81. ~TimeCheck()
  82. {
  83. logAi->traceStream() << boost::format("Time of %s was %d ms.") % txt % time.getDiff();
  84. }
  85. };
  86. struct AtScopeExit
  87. {
  88. std::function<void()> foo;
  89. AtScopeExit(const std::function<void()> &FOO) : foo(FOO)
  90. {}
  91. ~AtScopeExit()
  92. {
  93. foo();
  94. }
  95. };
  96. class ObjsVector : public std::vector<ObjectIdRef>
  97. {
  98. private:
  99. };
  100. template<int id>
  101. bool objWithID(const CGObjectInstance *obj)
  102. {
  103. return obj->ID == id;
  104. }
  105. std::string strFromInt3(int3 pos);
  106. void foreach_tile_pos(std::function<void(const int3& pos)> foo);
  107. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
  108. void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
  109. void foreach_neighbour(CCallback * cbp, const int3 &pos, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
  110. int howManyTilesWillBeDiscovered(const int3 &pos, int radious, CCallback * cbp);
  111. int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
  112. void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out);
  113. bool canBeEmbarkmentPoint(const TerrainTile *t, bool fromWater);
  114. bool isBlockedBorderGate(int3 tileToHit);
  115. bool isWeeklyRevisitable (const CGObjectInstance * obj);
  116. bool shouldVisit (HeroPtr h, const CGObjectInstance * obj);
  117. ui64 evaluateDanger(const CGObjectInstance *obj);
  118. ui64 evaluateDanger(crint3 tile, const CGHeroInstance *visitor);
  119. bool isSafeToVisit(HeroPtr h, crint3 tile);
  120. bool boundaryBetweenTwoPoints (int3 pos1, int3 pos2, CCallback * cbp);
  121. bool compareMovement(HeroPtr lhs, HeroPtr rhs);
  122. bool compareHeroStrength(HeroPtr h1, HeroPtr h2);
  123. bool compareArmyStrength(const CArmedInstance *a1, const CArmedInstance *a2);
  124. bool compareArtifacts(const CArtifactInstance *a1, const CArtifactInstance *a2);
  125. ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t);
  126. int3 whereToExplore(HeroPtr h);
  127. class CDistanceSorter
  128. {
  129. const CGHeroInstance * hero;
  130. public:
  131. CDistanceSorter(const CGHeroInstance * hero): hero(hero) {}
  132. bool operator ()(const CGObjectInstance *lhs, const CGObjectInstance *rhs);
  133. };