ObjectClusterizer.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 PriorityEvaluator;
  41. class ObjectClusterizer
  42. {
  43. private:
  44. static Obj IgnoredObjectTypes[];
  45. ObjectCluster nearObjects;
  46. ObjectCluster farObjects;
  47. ClusterMap blockedObjects;
  48. const Nullkiller * ai;
  49. public:
  50. void clusterize();
  51. std::vector<const CGObjectInstance *> getNearbyObjects() const;
  52. std::vector<const CGObjectInstance *> getFarObjects() const;
  53. std::vector<std::shared_ptr<ObjectCluster>> getLockedClusters() const;
  54. const CGObjectInstance * getBlocker(const AIPath & path) const;
  55. ObjectClusterizer(const Nullkiller * ai): ai(ai) {}
  56. private:
  57. bool shouldVisitObject(const CGObjectInstance * obj) const;
  58. void clusterizeObject(const CGObjectInstance * obj, PriorityEvaluator * priorityEvaluator);
  59. };
  60. }