CGoal.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. struct HeroPtr;
  15. class AIGateway;
  16. namespace Goals
  17. {
  18. template<typename T> class DLL_EXPORT CGoal : public AbstractGoal
  19. {
  20. public:
  21. CGoal<T>(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<T> * 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, const int version)
  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. virtual 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. virtual TGoalVec decompose() const override
  49. {
  50. TSubgoal single = decomposeSingle();
  51. if(!single || single->invalid())
  52. return {};
  53. return {single};
  54. }
  55. protected:
  56. virtual TSubgoal decomposeSingle() const
  57. {
  58. return TSubgoal();
  59. }
  60. };
  61. template<typename T> class DLL_EXPORT ElementarGoal : public CGoal<T>, public ITask
  62. {
  63. public:
  64. ElementarGoal<T>(EGoals goal = INVALID) : CGoal<T>(goal), ITask()
  65. {
  66. AbstractGoal::isAbstract = false;
  67. }
  68. ElementarGoal<T>(const ElementarGoal<T> & other) : CGoal<T>(other), ITask(other)
  69. {
  70. }
  71. T & setpriority(float p)
  72. {
  73. ITask::priority = p;
  74. return *((T *)this);
  75. }
  76. virtual bool isElementar() const override { return true; }
  77. virtual HeroPtr getHero() const override { return AbstractGoal::hero; }
  78. virtual int getHeroExchangeCount() const override { return 0; }
  79. };
  80. }
  81. }