CGoal.h 2.4 KB

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