DangerHitMapAnalyzer.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * DangerHitMapAnalyzer.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 "../AIUtility.h"
  12. namespace NKAI
  13. {
  14. struct AIPath;
  15. struct HitMapInfo
  16. {
  17. static const HitMapInfo NoThreat;
  18. uint64_t danger;
  19. uint8_t turn;
  20. float threat;
  21. HeroPtr hero;
  22. HitMapInfo()
  23. {
  24. reset();
  25. }
  26. void reset()
  27. {
  28. danger = 0;
  29. turn = 255;
  30. threat = 0;
  31. hero = HeroPtr();
  32. }
  33. double value() const;
  34. };
  35. struct HitMapNode
  36. {
  37. HitMapInfo maximumDanger;
  38. HitMapInfo fastestDanger;
  39. const CGTownInstance * closestTown = nullptr;
  40. HitMapNode() = default;
  41. void reset()
  42. {
  43. maximumDanger.reset();
  44. fastestDanger.reset();
  45. }
  46. };
  47. struct EnemyHeroAccessibleObject
  48. {
  49. const CGHeroInstance * hero;
  50. const CGObjectInstance * obj;
  51. EnemyHeroAccessibleObject(const CGHeroInstance * hero, const CGObjectInstance * obj)
  52. :hero(hero), obj(obj)
  53. {
  54. }
  55. };
  56. class DangerHitMapAnalyzer
  57. {
  58. private:
  59. boost::multi_array<HitMapNode, 3> hitMap;
  60. tbb::concurrent_vector<EnemyHeroAccessibleObject> enemyHeroAccessibleObjects;
  61. bool hitMapUpToDate = false;
  62. bool tileOwnersUpToDate = false;
  63. const Nullkiller * ai;
  64. std::map<ObjectInstanceID, std::vector<HitMapInfo>> townThreats;
  65. public:
  66. DangerHitMapAnalyzer(const Nullkiller * ai) :ai(ai) {}
  67. void updateHitMap();
  68. void calculateTileOwners();
  69. uint64_t enemyCanKillOurHeroesAlongThePath(const AIPath & path) const;
  70. const HitMapNode & getObjectThreat(const CGObjectInstance * obj) const;
  71. const HitMapNode & getTileThreat(const int3 & tile) const;
  72. std::set<const CGObjectInstance *> getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const;
  73. void reset();
  74. void resetTileOwners() { tileOwnersUpToDate = false; }
  75. PlayerColor getTileOwner(const int3 & tile) const;
  76. const CGTownInstance * getClosestTown(const int3 & tile) const;
  77. const std::vector<HitMapInfo> & getTownThreats(const CGTownInstance * town) const;
  78. };
  79. }