2
0

ResourceManager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/GameLibrary.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 resources do we need
  24. Goals::TSubgoal goal; //what for (build, gather army etc...)
  25. };
  26. class DLL_EXPORT IResourceManager //: public: IAbstractManager
  27. {
  28. public:
  29. virtual ~IResourceManager() = default;
  30. virtual void init(CPlayerSpecificInfoCallback * CB) = 0;
  31. virtual void setAI(VCAI * AI) = 0;
  32. virtual TResources reservedResources() const = 0;
  33. virtual TResources freeResources() const = 0;
  34. virtual TResource freeGold() const = 0;
  35. virtual TResources allResources() const = 0;
  36. virtual TResource allGold() const = 0;
  37. virtual Goals::TSubgoal whatToDo() const = 0;//get highest-priority goal
  38. virtual Goals::TSubgoal whatToDo(TResources &res, Goals::TSubgoal goal) = 0;
  39. virtual bool containsObjective(Goals::TSubgoal goal) const = 0;
  40. virtual bool hasTasksLeft() const = 0;
  41. virtual bool removeOutdatedObjectives(std::function<bool(const Goals::TSubgoal &)> predicate) = 0; //remove ResourceObjectives from queue if ResourceObjective->goal meets specific criteria
  42. virtual bool notifyGoalCompleted(Goals::TSubgoal goal) = 0;
  43. };
  44. class DLL_EXPORT ResourceManager : public IResourceManager
  45. {
  46. /*Resource Manager is a smart shopping list for AI to help
  47. Uses priority queue based on CGoal.priority */
  48. friend class VCAI;
  49. friend class AIhelper;
  50. friend struct SetGlobalState;
  51. CPlayerSpecificInfoCallback * cb; //this is enough, but we downcast from CCallback
  52. VCAI * ai;
  53. public:
  54. ResourceManager() = default;
  55. ResourceManager(CPlayerSpecificInfoCallback * CB, VCAI * AI = nullptr); //for tests only
  56. bool canAfford(const TResources & cost) const;
  57. TResources reservedResources() const override;
  58. TResources freeResources() const override;
  59. TResource freeGold() const override;
  60. TResources allResources() const override;
  61. TResource allGold() const override;
  62. Goals::TSubgoal whatToDo() const override; //peek highest-priority goal
  63. Goals::TSubgoal whatToDo(TResources & res, Goals::TSubgoal goal) override; //can we afford this goal or need to CollectRes?
  64. bool containsObjective(Goals::TSubgoal goal) const override;
  65. bool hasTasksLeft() const override;
  66. bool removeOutdatedObjectives(std::function<bool(const Goals::TSubgoal &)> predicate) override;
  67. bool notifyGoalCompleted(Goals::TSubgoal goal) override;
  68. protected: //not-const actions only for AI
  69. virtual void reserveResources(const TResources & res, Goals::TSubgoal goal = Goals::TSubgoal());
  70. virtual bool updateGoal(Goals::TSubgoal goal); //new goal must have same properties but different priority
  71. virtual bool tryPush(const ResourceObjective &o);
  72. //inner processing
  73. virtual TResources estimateIncome() const;
  74. virtual Goals::TSubgoal collectResourcesForOurGoal(ResourceObjective &o) const;
  75. void init(CPlayerSpecificInfoCallback * CB) override;
  76. void setAI(VCAI * AI) override;
  77. private:
  78. TResources saving;
  79. boost::heap::binomial_heap<ResourceObjective> queue;
  80. void dumpToLog() const;
  81. };