DangerHitMapAnalyzer.h 2.3 KB

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