Nullkiller.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. bool openMap;
  65. bool useObjectGraph;
  66. public:
  67. static std::unique_ptr<ObjectGraph> baseGraph;
  68. std::unique_ptr<DangerHitMapAnalyzer> dangerHitMap;
  69. std::unique_ptr<BuildAnalyzer> buildAnalyzer;
  70. std::unique_ptr<ObjectClusterizer> objectClusterizer;
  71. std::unique_ptr<PriorityEvaluator> priorityEvaluator;
  72. std::unique_ptr<SharedPool<PriorityEvaluator>> priorityEvaluators;
  73. std::unique_ptr<AIPathfinder> pathfinder;
  74. std::unique_ptr<HeroManager> heroManager;
  75. std::unique_ptr<ArmyManager> armyManager;
  76. std::unique_ptr<AIMemory> memory;
  77. std::unique_ptr<FuzzyHelper> dangerEvaluator;
  78. std::unique_ptr<DeepDecomposer> decomposer;
  79. std::unique_ptr<ArmyFormation> armyFormation;
  80. std::unique_ptr<Settings> settings;
  81. PlayerColor playerID;
  82. std::shared_ptr<CCallback> cb;
  83. std::mutex aiStateMutex;
  84. Nullkiller();
  85. void init(std::shared_ptr<CCallback> cb, AIGateway * gateway);
  86. void makeTurn();
  87. bool isActive(const CGHeroInstance * hero) const { return activeHero == hero; }
  88. bool isHeroLocked(const CGHeroInstance * hero) const;
  89. HeroPtr getActiveHero() { return activeHero; }
  90. HeroLockedReason getHeroLockedReason(const CGHeroInstance * hero) const;
  91. int3 getTargetTile() const { return targetTile; }
  92. ObjectInstanceID getTargetObject() const { return targetObject; }
  93. void setTargetObject(int objid) { targetObject = ObjectInstanceID(objid); }
  94. void setActive(const CGHeroInstance * hero, int3 tile) { activeHero = hero; targetTile = tile; }
  95. void lockHero(const CGHeroInstance * hero, HeroLockedReason lockReason) { lockedHeroes[hero] = lockReason; }
  96. void unlockHero(const CGHeroInstance * hero) { lockedHeroes.erase(hero); }
  97. bool arePathHeroesLocked(const AIPath & path) const;
  98. TResources getFreeResources() const;
  99. int32_t getFreeGold() const { return getFreeResources()[EGameResID::GOLD]; }
  100. void lockResources(const TResources & res);
  101. const TResources & getLockedResources() const { return lockedResources; }
  102. ScanDepth getScanDepth() const { return scanDepth; }
  103. bool isOpenMap() const { return openMap; }
  104. bool isObjectGraphAllowed() const { return useObjectGraph; }
  105. bool handleTrading();
  106. private:
  107. void resetAiState();
  108. void updateAiState(int pass, bool fast = false);
  109. void decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int decompositionMaxDepth) const;
  110. Goals::TTask choseBestTask(Goals::TGoalVec & tasks) const;
  111. Goals::TTaskVec buildPlan(Goals::TGoalVec & tasks, int priorityTier) const;
  112. bool executeTask(Goals::TTask task);
  113. bool areAffectedObjectsPresent(Goals::TTask task) const;
  114. HeroRole getTaskRole(Goals::TTask task) const;
  115. };
  116. }