CGoal.h 2.4 KB

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