CGoal.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. bool operator==(const AbstractGoal & g) const override
  35. {
  36. if(goalType != g.goalType)
  37. return false;
  38. return (*this) == (static_cast<const T &>(g));
  39. }
  40. virtual bool operator==(const T & other) const = 0;
  41. TGoalVec decompose(const Nullkiller * ai) const override
  42. {
  43. TSubgoal single = decomposeSingle(ai);
  44. if(!single || single->invalid())
  45. return {};
  46. return {single};
  47. }
  48. protected:
  49. virtual TSubgoal decomposeSingle(const Nullkiller * ai) const
  50. {
  51. return TSubgoal();
  52. }
  53. };
  54. template<typename T> class DLL_EXPORT ElementarGoal : public CGoal<T>, public ITask
  55. {
  56. public:
  57. ElementarGoal<T>(EGoals goal = INVALID) : CGoal<T>(goal), ITask()
  58. {
  59. AbstractGoal::isAbstract = false;
  60. }
  61. ElementarGoal<T>(const ElementarGoal<T> & other) : CGoal<T>(other), ITask(other)
  62. {
  63. }
  64. T & setpriority(float p)
  65. {
  66. ITask::priority = p;
  67. return *((T *)this);
  68. }
  69. bool isElementar() const override { return true; }
  70. const CGHeroInstance * getHero() const override { return AbstractGoal::hero; }
  71. int getHeroExchangeCount() const override { return 0; }
  72. bool isObjectAffected(ObjectInstanceID id) const override
  73. {
  74. return (AbstractGoal::hero && AbstractGoal::hero->id == id)
  75. || AbstractGoal::objid == id
  76. || (AbstractGoal::town && AbstractGoal::town->id == id);
  77. }
  78. std::vector<ObjectInstanceID> getAffectedObjects() const override
  79. {
  80. auto result = std::vector<ObjectInstanceID>();
  81. if(AbstractGoal::hero)
  82. result.push_back(AbstractGoal::hero->id);
  83. if(AbstractGoal::objid != -1)
  84. result.push_back(ObjectInstanceID(AbstractGoal::objid));
  85. if(AbstractGoal::town)
  86. result.push_back(AbstractGoal::town->id);
  87. return result;
  88. }
  89. ITask * asTask() override
  90. {
  91. return this;
  92. }
  93. };
  94. }
  95. }