AIUtility.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. class CCallback;
  20. typedef const int3& crint3;
  21. typedef const std::string& crstring;
  22. const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
  23. const int ACTUAL_RESOURCE_COUNT = 7;
  24. const int ALLOWED_ROAMING_HEROES = 8;
  25. //implementation-dependent
  26. extern const double SAFE_ATTACK_CONSTANT;
  27. extern const int GOLD_RESERVE;
  28. //provisional class for AI to store a reference to an owned hero object
  29. //checks if it's valid on access, should be used in place of const CGHeroInstance*
  30. struct HeroPtr
  31. {
  32. const CGHeroInstance *h;
  33. ObjectInstanceID hid;
  34. public:
  35. std::string name;
  36. HeroPtr();
  37. HeroPtr(const CGHeroInstance *H);
  38. ~HeroPtr();
  39. operator bool() const
  40. {
  41. return validAndSet();
  42. }
  43. bool operator<(const HeroPtr &rhs) const;
  44. const CGHeroInstance *operator->() const;
  45. const CGHeroInstance *operator*() const; //not that consistent with -> but all interfaces use CGHeroInstance*, so it's convenient
  46. const CGHeroInstance *get(bool doWeExpectNull = false) const;
  47. bool validAndSet() const;
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & this->h & hid & name;
  51. }
  52. };
  53. enum BattleState
  54. {
  55. NO_BATTLE,
  56. UPCOMING_BATTLE,
  57. ONGOING_BATTLE,
  58. ENDING_BATTLE
  59. };
  60. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  61. // This class stores object id, so we can detect when we lose access to the underlying object.
  62. struct ObjectIdRef
  63. {
  64. ObjectInstanceID id;
  65. const CGObjectInstance *operator->() const;
  66. operator const CGObjectInstance *() const;
  67. ObjectIdRef(ObjectInstanceID _id);
  68. ObjectIdRef(const CGObjectInstance *obj);
  69. bool operator<(const ObjectIdRef &rhs) const;
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & id;
  73. }
  74. };
  75. struct TimeCheck
  76. {
  77. CStopWatch time;
  78. std::string txt;
  79. TimeCheck(crstring TXT) : txt(TXT)
  80. {
  81. }
  82. ~TimeCheck()
  83. {
  84. logAi->trace("Time of %s was %d ms.",txt,time.getDiff());
  85. }
  86. };
  87. //TODO: replace with vstd::
  88. struct AtScopeExit
  89. {
  90. std::function<void()> foo;
  91. AtScopeExit(const std::function<void()> &FOO) : foo(FOO)
  92. {}
  93. ~AtScopeExit()
  94. {
  95. foo();
  96. }
  97. };
  98. class ObjsVector : public std::vector<ObjectIdRef>
  99. {
  100. private:
  101. };
  102. template<int id>
  103. bool objWithID(const CGObjectInstance *obj)
  104. {
  105. return obj->ID == id;
  106. }
  107. std::string strFromInt3(int3 pos);//todo: remove
  108. void foreach_tile_pos(std::function<void(const int3& pos)> foo);
  109. void foreach_tile_pos(CCallback * cbp, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
  110. void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
  111. void foreach_neighbour(CCallback * cbp, const int3 &pos, std::function<void(CCallback * cbp, const int3& pos)> foo); // avoid costly retrieval of thread-specific pointer
  112. int howManyTilesWillBeDiscovered(const int3 &pos, int radious, CCallback * cbp);
  113. int howManyTilesWillBeDiscovered(int radious, int3 pos, crint3 dir);
  114. void getVisibleNeighbours(const std::vector<int3> &tiles, std::vector<int3> &out);
  115. bool canBeEmbarkmentPoint(const TerrainTile *t, bool fromWater);
  116. bool isBlockedBorderGate(int3 tileToHit);
  117. bool isBlockVisitObj(const int3 &pos);
  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. bool compareArtifacts(const CArtifactInstance *a1, const CArtifactInstance *a2);
  128. ui64 howManyReinforcementsCanGet(HeroPtr h, const CGTownInstance *t);
  129. int3 whereToExplore(HeroPtr h);
  130. class CDistanceSorter
  131. {
  132. const CGHeroInstance * hero;
  133. public:
  134. CDistanceSorter(const CGHeroInstance * hero): hero(hero) {}
  135. bool operator ()(const CGObjectInstance *lhs, const CGObjectInstance *rhs);
  136. };