ResourceManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * ResourceManager.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 "../../lib/GameConstants.h"
  13. #include "../../lib/VCMI_Lib.h"
  14. #include "VCAI.h"
  15. #include <boost/heap/binomial_heap.hpp>
  16. class AIhelper;
  17. class IResourceManager;
  18. struct DLL_EXPORT ResourceObjective
  19. {
  20. ResourceObjective() = default;
  21. ResourceObjective(const TResources &res, Goals::TSubgoal goal);
  22. bool operator < (const ResourceObjective &ro) const;
  23. TResources resources; //how many resoures do we need
  24. Goals::TSubgoal goal; //what for (build, gather army etc...)
  25. //TODO: register?
  26. template<typename Handler> void serializeInternal(Handler & h, const int version)
  27. {
  28. h & resources;
  29. //h & goal; //FIXME: goal serialization is broken
  30. }
  31. };
  32. class DLL_EXPORT IResourceManager //: public: IAbstractManager
  33. {
  34. public:
  35. virtual ~IResourceManager() = default;
  36. virtual void init(CPlayerSpecificInfoCallback * CB) = 0;
  37. virtual void setAI(VCAI * AI) = 0;
  38. virtual TResources reservedResources() const = 0;
  39. virtual TResources freeResources() const = 0;
  40. virtual TResource freeGold() const = 0;
  41. virtual TResources allResources() const = 0;
  42. virtual TResource allGold() const = 0;
  43. virtual Goals::TSubgoal whatToDo() const = 0;//get highest-priority goal
  44. virtual Goals::TSubgoal whatToDo(TResources &res, Goals::TSubgoal goal) = 0;
  45. virtual bool containsObjective(Goals::TSubgoal goal) const = 0;
  46. virtual bool hasTasksLeft() const = 0;
  47. virtual bool removeOutdatedObjectives(std::function<bool(const Goals::TSubgoal &)> predicate) = 0; //remove ResourceObjectives from queue if ResourceObjective->goal meets specific criteria
  48. virtual bool notifyGoalCompleted(Goals::TSubgoal goal) = 0;
  49. };
  50. class DLL_EXPORT ResourceManager : public IResourceManager
  51. {
  52. /*Resource Manager is a smart shopping list for AI to help
  53. Uses priority queue based on CGoal.priority */
  54. friend class VCAI;
  55. friend class AIhelper;
  56. friend struct SetGlobalState;
  57. CPlayerSpecificInfoCallback * cb; //this is enough, but we downcast from CCallback
  58. VCAI * ai;
  59. public:
  60. ResourceManager() = default;
  61. ResourceManager(CPlayerSpecificInfoCallback * CB, VCAI * AI = nullptr); //for tests only
  62. bool canAfford(const TResources & cost) const;
  63. TResources reservedResources() const override;
  64. TResources freeResources() const override;
  65. TResource freeGold() const override;
  66. TResources allResources() const override;
  67. TResource allGold() const override;
  68. Goals::TSubgoal whatToDo() const override; //peek highest-priority goal
  69. Goals::TSubgoal whatToDo(TResources & res, Goals::TSubgoal goal) override; //can we afford this goal or need to CollectRes?
  70. bool containsObjective(Goals::TSubgoal goal) const override;
  71. bool hasTasksLeft() const override;
  72. bool removeOutdatedObjectives(std::function<bool(const Goals::TSubgoal &)> predicate) override;
  73. bool notifyGoalCompleted(Goals::TSubgoal goal) override;
  74. protected: //not-const actions only for AI
  75. virtual void reserveResoures(const TResources & res, Goals::TSubgoal goal = Goals::TSubgoal());
  76. virtual bool updateGoal(Goals::TSubgoal goal); //new goal must have same properties but different priority
  77. virtual bool tryPush(const ResourceObjective &o);
  78. //inner processing
  79. virtual TResources estimateIncome() const;
  80. virtual Goals::TSubgoal collectResourcesForOurGoal(ResourceObjective &o) const;
  81. void init(CPlayerSpecificInfoCallback * CB) override;
  82. void setAI(VCAI * AI) override;
  83. private:
  84. TResources saving;
  85. boost::heap::binomial_heap<ResourceObjective> queue;
  86. void dumpToLog() const;
  87. //TODO: register?
  88. template<typename Handler> void serializeInternal(Handler & h, const int version)
  89. {
  90. h & saving;
  91. h & queue;
  92. }
  93. };