Nullkiller.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Nullkiller.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 "PriorityEvaluator.h"
  12. #include "FuzzyHelper.h"
  13. #include "Settings.h"
  14. #include "AIMemory.h"
  15. #include "DeepDecomposer.h"
  16. #include "../Analyzers/DangerHitMapAnalyzer.h"
  17. #include "../Analyzers/BuildAnalyzer.h"
  18. #include "../Analyzers/ArmyManager.h"
  19. #include "../Analyzers/HeroManager.h"
  20. #include "../Analyzers/ObjectClusterizer.h"
  21. #include "../Helpers/ArmyFormation.h"
  22. #include "../../../lib/ConditionalWait.h"
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. class PathfinderCache;
  25. VCMI_LIB_NAMESPACE_END
  26. namespace NKAI
  27. {
  28. const float MIN_PRIORITY = 0.01f;
  29. const float SMALL_SCAN_MIN_PRIORITY = 0.4f;
  30. enum class HeroLockedReason
  31. {
  32. NOT_LOCKED = 0,
  33. STARTUP = 1,
  34. DEFENCE = 2,
  35. HERO_CHAIN = 3
  36. };
  37. enum class ScanDepth
  38. {
  39. MAIN_FULL = 0,
  40. SMALL = 1,
  41. ALL_FULL = 2
  42. };
  43. struct TaskPlanItem
  44. {
  45. std::vector<ObjectInstanceID> affectedObjects;
  46. Goals::TSubgoal task;
  47. TaskPlanItem(Goals::TSubgoal goal);
  48. };
  49. class TaskPlan
  50. {
  51. private:
  52. std::vector<TaskPlanItem> tasks;
  53. public:
  54. Goals::TTaskVec getTasks() const;
  55. void merge(Goals::TSubgoal task);
  56. };
  57. class Nullkiller
  58. {
  59. private:
  60. const CGHeroInstance * activeHero;
  61. int3 targetTile;
  62. ObjectInstanceID targetObject;
  63. std::map<const CGHeroInstance *, HeroLockedReason> lockedHeroes;
  64. std::unique_ptr<PathfinderCache> pathfinderCache;
  65. ScanDepth scanDepth;
  66. TResources lockedResources;
  67. bool useHeroChain;
  68. AIGateway * gateway;
  69. bool openMap;
  70. bool useObjectGraph;
  71. bool pathfinderInvalidated;
  72. public:
  73. static std::unique_ptr<ObjectGraph> baseGraph;
  74. std::unique_ptr<DangerHitMapAnalyzer> dangerHitMap;
  75. std::unique_ptr<BuildAnalyzer> buildAnalyzer;
  76. std::unique_ptr<ObjectClusterizer> objectClusterizer;
  77. std::unique_ptr<PriorityEvaluator> priorityEvaluator;
  78. std::unique_ptr<SharedPool<PriorityEvaluator>> priorityEvaluators;
  79. std::unique_ptr<AIPathfinder> pathfinder;
  80. std::unique_ptr<HeroManager> heroManager;
  81. std::unique_ptr<ArmyManager> armyManager;
  82. std::unique_ptr<AIMemory> memory;
  83. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  84. std::unique_ptr<DeepDecomposer> decomposer;
  85. std::unique_ptr<ArmyFormation> armyFormation;
  86. std::unique_ptr<Settings> settings;
  87. PlayerColor playerID;
  88. std::shared_ptr<CCallback> cb;
  89. std::mutex aiStateMutex;
  90. mutable ThreadInterruption makingTurnInterrupption;
  91. Nullkiller();
  92. ~Nullkiller();
  93. void init(std::shared_ptr<CCallback> cb, AIGateway * gateway);
  94. void makeTurn();
  95. bool isActive(const CGHeroInstance * hero) const { return activeHero == hero; }
  96. bool isHeroLocked(const CGHeroInstance * hero) const;
  97. HeroPtr getActiveHero() { return activeHero; }
  98. HeroLockedReason getHeroLockedReason(const CGHeroInstance * hero) const;
  99. int3 getTargetTile() const { return targetTile; }
  100. ObjectInstanceID getTargetObject() const { return targetObject; }
  101. void setTargetObject(int objid) { targetObject = ObjectInstanceID(objid); }
  102. void setActive(const CGHeroInstance * hero, int3 tile) { activeHero = hero; targetTile = tile; }
  103. void lockHero(const CGHeroInstance * hero, HeroLockedReason lockReason) { lockedHeroes[hero] = lockReason; }
  104. void unlockHero(const CGHeroInstance * hero) { lockedHeroes.erase(hero); }
  105. bool arePathHeroesLocked(const AIPath & path) const;
  106. TResources getFreeResources() const;
  107. int32_t getFreeGold() const { return getFreeResources()[EGameResID::GOLD]; }
  108. void lockResources(const TResources & res);
  109. const TResources & getLockedResources() const { return lockedResources; }
  110. ScanDepth getScanDepth() const { return scanDepth; }
  111. bool isOpenMap() const { return openMap; }
  112. bool isObjectGraphAllowed() const { return useObjectGraph; }
  113. bool handleTrading();
  114. void invalidatePathfinderData();
  115. std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h) const;
  116. void invalidatePaths();
  117. private:
  118. void resetAiState();
  119. void updateAiState(int pass, bool fast = false);
  120. void decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int decompositionMaxDepth) const;
  121. Goals::TTask choseBestTask(Goals::TGoalVec & tasks) const;
  122. Goals::TTaskVec buildPlan(Goals::TGoalVec & tasks, int priorityTier) const;
  123. bool executeTask(Goals::TTask task);
  124. bool areAffectedObjectsPresent(Goals::TTask task) const;
  125. HeroRole getTaskRole(Goals::TTask task) const;
  126. };
  127. }