ResourceManager.h 3.4 KB

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