ObjectClusterizer.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "../Pathfinding/AINodeStorage.h"
  12. namespace NKAI
  13. {
  14. struct ClusterObjectInfo
  15. {
  16. float priority;
  17. float movementCost;
  18. uint64_t danger;
  19. uint8_t turn;
  20. };
  21. using ClusterObjects = tbb::concurrent_hash_map<const CGObjectInstance *, ClusterObjectInfo>;
  22. struct ObjectCluster
  23. {
  24. public:
  25. ClusterObjects objects;
  26. const CGObjectInstance * blocker;
  27. void reset()
  28. {
  29. objects.clear();
  30. }
  31. void addObject(const CGObjectInstance * object, const AIPath & path, float priority);
  32. ObjectCluster(const CGObjectInstance * blocker): blocker(blocker) {}
  33. ObjectCluster() : ObjectCluster(nullptr)
  34. {
  35. }
  36. std::vector<const CGObjectInstance *> getObjects() const;
  37. const CGObjectInstance * calculateCenter() const;
  38. };
  39. using ClusterMap = tbb::concurrent_hash_map<const CGObjectInstance *, std::shared_ptr<ObjectCluster>>;
  40. class ObjectClusterizer
  41. {
  42. private:
  43. ObjectCluster nearObjects;
  44. ObjectCluster farObjects;
  45. ClusterMap blockedObjects;
  46. const Nullkiller * ai;
  47. public:
  48. void clusterize();
  49. std::vector<const CGObjectInstance *> getNearbyObjects() const;
  50. std::vector<const CGObjectInstance *> getFarObjects() const;
  51. std::vector<std::shared_ptr<ObjectCluster>> getLockedClusters() const;
  52. const CGObjectInstance * getBlocker(const AIPath & path) const;
  53. ObjectClusterizer(const Nullkiller * ai): ai(ai) {}
  54. private:
  55. bool shouldVisitObject(const CGObjectInstance * obj) const;
  56. };
  57. }