Nullkiller.h 4.2 KB

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