Nullkiller.h 3.7 KB

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