AIUtility.h 4.7 KB

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