CGoal.h 2.7 KB

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