CGoal.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace NKAI
  13. {
  14. class AIGateway;
  15. namespace Goals
  16. {
  17. template<typename T>
  18. class DLL_EXPORT CGoal : public AbstractGoal
  19. {
  20. public:
  21. CGoal(EGoals goal = INVALID) : AbstractGoal(goal)
  22. {
  23. isAbstract = true;
  24. value = 0;
  25. aid = -1;
  26. objid = -1;
  27. resID = -1;
  28. tile = int3(-1, -1, -1);
  29. town = nullptr;
  30. }
  31. CGoal * clone() const override
  32. {
  33. return new T(static_cast<T const &>(*this)); //casting enforces template instantiation
  34. }
  35. template<typename Handler> void serialize(Handler & h)
  36. {
  37. h & static_cast<AbstractGoal &>(*this);
  38. //h & goalType & isElementar & isAbstract & priority;
  39. //h & value & resID & objid & aid & tile & hero & town & bid;
  40. }
  41. bool operator==(const AbstractGoal & g) const override
  42. {
  43. if(goalType != g.goalType)
  44. return false;
  45. return (*this) == (static_cast<const T &>(g));
  46. }
  47. virtual bool operator==(const T & other) const = 0;
  48. TGoalVec decompose(const Nullkiller * ai) const override
  49. {
  50. TSubgoal single = decomposeSingle(ai);
  51. if(!single || single->invalid())
  52. return {};
  53. return {single};
  54. }
  55. protected:
  56. virtual TSubgoal decomposeSingle(const Nullkiller * ai) const
  57. {
  58. return TSubgoal();
  59. }
  60. };
  61. template<typename T>
  62. class DLL_EXPORT ElementarGoal : public CGoal<T>, public ITask
  63. {
  64. public:
  65. ElementarGoal(EGoals goal = INVALID) : CGoal<T>(goal), ITask()
  66. {
  67. AbstractGoal::isAbstract = false;
  68. }
  69. ElementarGoal(const ElementarGoal<T> & other) : CGoal<T>(other), ITask(other)
  70. {
  71. }
  72. T & setpriority(float p)
  73. {
  74. ITask::priority = p;
  75. return *((T *)this);
  76. }
  77. bool isElementar() const override { return true; }
  78. const CGHeroInstance * getHero() const override { return AbstractGoal::hero; }
  79. int getHeroExchangeCount() const override { return 0; }
  80. bool isObjectAffected(ObjectInstanceID id) const override
  81. {
  82. return (AbstractGoal::hero && AbstractGoal::hero->id == id)
  83. || AbstractGoal::objid == id
  84. || (AbstractGoal::town && AbstractGoal::town->id == id);
  85. }
  86. std::vector<ObjectInstanceID> getAffectedObjects() const override
  87. {
  88. auto result = std::vector<ObjectInstanceID>();
  89. if(AbstractGoal::hero)
  90. result.push_back(AbstractGoal::hero->id);
  91. if(AbstractGoal::objid != -1)
  92. result.push_back(ObjectInstanceID(AbstractGoal::objid));
  93. if(AbstractGoal::town)
  94. result.push_back(AbstractGoal::town->id);
  95. return result;
  96. }
  97. ITask * asTask() override
  98. {
  99. return this;
  100. }
  101. };
  102. }
  103. }