CGoal.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * CGoal.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 "AbstractGoal.h"
  12. struct HeroPtr;
  13. class VCAI;
  14. namespace Goals
  15. {
  16. template<typename T> class DLL_EXPORT CGoal : public AbstractGoal
  17. {
  18. public:
  19. CGoal<T>(EGoals goal = INVALID) : AbstractGoal(goal)
  20. {
  21. isAbstract = true;
  22. value = 0;
  23. aid = -1;
  24. objid = -1;
  25. resID = -1;
  26. tile = int3(-1, -1, -1);
  27. town = nullptr;
  28. }
  29. OSETTER(bool, isAbstract)
  30. OSETTER(int, value)
  31. OSETTER(int, resID)
  32. OSETTER(int, objid)
  33. OSETTER(int, aid)
  34. OSETTER(int3, tile)
  35. OSETTER(HeroPtr, hero)
  36. OSETTER(CGTownInstance *, town)
  37. OSETTER(int, bid)
  38. CGoal<T> * clone() const override
  39. {
  40. return new T(static_cast<T const &>(*this)); //casting enforces template instantiation
  41. }
  42. template<typename Handler> void serialize(Handler & h, const int version)
  43. {
  44. h & static_cast<AbstractGoal &>(*this);
  45. //h & goalType & isElementar & isAbstract & priority;
  46. //h & value & resID & objid & aid & tile & hero & town & bid;
  47. }
  48. virtual bool operator==(const AbstractGoal & g) const override
  49. {
  50. if(goalType != g.goalType)
  51. return false;
  52. return (*this) == (static_cast<const T &>(g));
  53. }
  54. virtual bool operator==(const T & other) const = 0;
  55. virtual TGoalVec decompose() const override
  56. {
  57. TSubgoal single = decomposeSingle();
  58. if(!single || single->invalid())
  59. return {};
  60. return {single};
  61. }
  62. protected:
  63. virtual TSubgoal decomposeSingle() const
  64. {
  65. return TSubgoal();
  66. }
  67. };
  68. template<typename T> class DLL_EXPORT ElementarGoal : public CGoal<T>, public ITask
  69. {
  70. public:
  71. ElementarGoal<T>(EGoals goal = INVALID) : CGoal<T>(goal), ITask()
  72. {
  73. AbstractGoal::isAbstract = false;
  74. }
  75. ElementarGoal<T>(const ElementarGoal<T> & other) : CGoal<T>(other), ITask(other)
  76. {
  77. }
  78. T & setpriority(float p)
  79. {
  80. ITask::priority = p;
  81. return *((T *)this);
  82. }
  83. virtual bool isElementar() const override
  84. {
  85. return true;
  86. }
  87. };
  88. class DLL_EXPORT Invalid : public ElementarGoal<Invalid>
  89. {
  90. public:
  91. Invalid()
  92. : ElementarGoal(Goals::INVALID)
  93. {
  94. priority = -1;
  95. }
  96. TGoalVec decompose() const override
  97. {
  98. return TGoalVec();
  99. }
  100. virtual bool operator==(const Invalid & other) const override
  101. {
  102. return true;
  103. }
  104. virtual std::string toString() const override
  105. {
  106. return "Invalid";
  107. }
  108. virtual void accept(VCAI * ai) override
  109. {
  110. throw cannotFulfillGoalException("Can not fulfill Invalid goal!");
  111. }
  112. };
  113. }