ObjectManager.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * ObjectManager.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 "../Zone.h"
  12. #include "../RmgObject.h"
  13. #include <boost/heap/priority_queue.hpp> //A*
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGObjectInstance;
  16. class ObjectTemplate;
  17. class CGCreature;
  18. using TDistance = std::pair<int3, float>;
  19. struct DistanceMaximizeFunctor
  20. {
  21. bool operator()(const TDistance & lhs, const TDistance & rhs) const
  22. {
  23. return (rhs.second > lhs.second);
  24. }
  25. };
  26. struct RequiredObjectInfo
  27. {
  28. RequiredObjectInfo();
  29. RequiredObjectInfo(CGObjectInstance* obj, ui32 guardStrength = 0, bool allowRoad = true, CGObjectInstance* nearbyTarget = nullptr);
  30. CGObjectInstance* obj;
  31. CGObjectInstance* nearbyTarget;
  32. int3 pos;
  33. ui32 guardStrength;
  34. bool allowRoad;
  35. };
  36. class ObjectManager: public Modificator
  37. {
  38. public:
  39. enum OptimizeType
  40. {
  41. NONE = 0x00000000,
  42. WEIGHT = 0x00000001,
  43. DISTANCE = 0x00000010
  44. };
  45. public:
  46. MODIFICATOR(ObjectManager);
  47. void process() override;
  48. void init() override;
  49. void addRequiredObject(const RequiredObjectInfo & info);
  50. void addCloseObject(const RequiredObjectInfo & info);
  51. void addNearbyObject(const RequiredObjectInfo & info);
  52. bool createRequiredObjects();
  53. int3 findPlaceForObject(const rmg::Area & searchArea, rmg::Object & obj, si32 min_dist, OptimizeType optimizer) const;
  54. int3 findPlaceForObject(const rmg::Area & searchArea, rmg::Object & obj, const std::function<float(const int3)> & weightFunction, OptimizeType optimizer) const;
  55. rmg::Path placeAndConnectObject(const rmg::Area & searchArea, rmg::Object & obj, si32 min_dist, bool isGuarded, bool onlyStraight, OptimizeType optimizer) const;
  56. rmg::Path placeAndConnectObject(const rmg::Area & searchArea, rmg::Object & obj, const std::function<float(const int3)> & weightFunction, bool isGuarded, bool onlyStraight, OptimizeType optimizer) const;
  57. CGCreature * chooseGuard(si32 strength, bool zoneGuard = false);
  58. bool addGuard(rmg::Object & object, si32 strength, bool zoneGuard = false);
  59. void placeObject(rmg::Object & object, bool guarded, bool updateDistance, bool allowRoad = true);
  60. void updateDistances(const rmg::Object & obj);
  61. void updateDistances(const int3& pos);
  62. void updateDistances(std::function<ui32(const int3 & tile)> distanceFunction);
  63. void createDistancesPriorityQueue();
  64. const rmg::Area & getVisitableArea() const;
  65. std::vector<CGObjectInstance*> getMines() const;
  66. protected:
  67. //content info
  68. std::vector<RequiredObjectInfo> requiredObjects;
  69. std::vector<RequiredObjectInfo> closeObjects;
  70. std::vector<RequiredObjectInfo> instantObjects;
  71. std::vector<RequiredObjectInfo> nearbyObjects;
  72. std::vector<CGObjectInstance*> objects;
  73. rmg::Area objectsVisitableArea;
  74. boost::heap::priority_queue<TDistance, boost::heap::compare<DistanceMaximizeFunctor>> tilesByDistance;
  75. };
  76. VCMI_LIB_NAMESPACE_END