AbstractGoal.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * AbstractGoal.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 "../../../lib/VCMI_Lib.h"
  12. #include "../../../lib/CBuildingHandler.h"
  13. #include "../../../lib/CCreatureHandler.h"
  14. #include "../../../lib/CTownHandler.h"
  15. #include "../AIUtility.h"
  16. struct HeroPtr;
  17. class VCAI;
  18. class FuzzyHelper;
  19. namespace Goals
  20. {
  21. class AbstractGoal;
  22. class ITask;
  23. class RecruitHero;
  24. class BuildThis;
  25. class DigAtTile;
  26. class CollectRes;
  27. class BuyArmy;
  28. class BuildBoat;
  29. class Invalid;
  30. class Trade;
  31. class AdventureSpellCast;
  32. enum EGoals
  33. {
  34. INVALID = -1,
  35. WIN, CONQUER,
  36. BUILD,
  37. EXPLORE, GATHER_ARMY,
  38. BOOST_HERO,
  39. RECRUIT_HERO,
  40. RECRUIT_HERO_BEHAVIOR,
  41. BUILD_STRUCTURE, //if hero set, then in visited town
  42. COLLECT_RES,
  43. GATHER_TROOPS, // val of creatures with objid
  44. CAPTURE_OBJECTS,
  45. GET_ART_TYPE,
  46. DEFENCE,
  47. STARTUP,
  48. DIG_AT_TILE,//elementar with hero on tile
  49. BUY_ARMY, //at specific town
  50. TRADE, //val resID at object objid
  51. BUILD_BOAT,
  52. COMPLETE_QUEST,
  53. ADVENTURE_SPELL_CAST,
  54. EXECUTE_HERO_CHAIN,
  55. EXCHANGE_SWAP_TOWN_HEROES,
  56. DISMISS_HERO,
  57. COMPOSITION,
  58. CLUSTER_BEHAVIOR,
  59. UNLOCK_CLUSTER,
  60. HERO_EXCHANGE,
  61. ARMY_UPGRADE
  62. };
  63. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  64. {
  65. public:
  66. bool operator==(const TSubgoal & rhs) const;
  67. bool operator<(const TSubgoal & rhs) const;
  68. //TODO: serialize?
  69. };
  70. typedef std::shared_ptr<ITask> TTask;
  71. typedef std::vector<TTask> TTaskVec;
  72. typedef std::vector<TSubgoal> TGoalVec;
  73. //method chaining + clone pattern
  74. #define VSETTER(type, field) virtual AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  75. #define OSETTER(type, field) CGoal<T> & set ## field(const type &rhs) override { field = rhs; return *this; };
  76. #if 0
  77. #define SETTER
  78. #endif // _DEBUG
  79. enum { LOW_PR = -1 };
  80. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  81. DLL_EXPORT TTask taskptr(const AbstractGoal & tmp);
  82. class DLL_EXPORT AbstractGoal
  83. {
  84. public:
  85. bool isAbstract; VSETTER(bool, isAbstract)
  86. int value; VSETTER(int, value)
  87. ui64 goldCost; VSETTER(ui64, goldCost)
  88. int resID; VSETTER(int, resID)
  89. int objid; VSETTER(int, objid)
  90. int aid; VSETTER(int, aid)
  91. int3 tile; VSETTER(int3, tile)
  92. HeroPtr hero; VSETTER(HeroPtr, hero)
  93. const CGTownInstance *town; VSETTER(CGTownInstance *, town)
  94. int bid; VSETTER(int, bid)
  95. TSubgoal parent; VSETTER(TSubgoal, parent)
  96. //EvaluationContext evaluationContext; VSETTER(EvaluationContext, evaluationContext)
  97. AbstractGoal(EGoals goal = EGoals::INVALID)
  98. : goalType(goal), hero()
  99. {
  100. isAbstract = false;
  101. value = 0;
  102. aid = -1;
  103. resID = -1;
  104. objid = -1;
  105. tile = int3(-1, -1, -1);
  106. town = nullptr;
  107. bid = -1;
  108. goldCost = 0;
  109. }
  110. virtual ~AbstractGoal() {}
  111. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  112. virtual AbstractGoal * clone() const
  113. {
  114. return const_cast<AbstractGoal *>(this);
  115. }
  116. virtual TGoalVec decompose() const
  117. {
  118. return TGoalVec();
  119. }
  120. EGoals goalType;
  121. virtual std::string toString() const;
  122. bool invalid() const;
  123. virtual bool operator==(const AbstractGoal & g) const;
  124. virtual bool isElementar() const { return false; }
  125. virtual bool hasHash() const { return false; }
  126. virtual uint64_t getHash() const { return 0; }
  127. bool operator!=(const AbstractGoal & g) const
  128. {
  129. return !(*this == g);
  130. }
  131. template<typename Handler> void serialize(Handler & h, const int version)
  132. {
  133. float priority;
  134. bool isElementar;
  135. h & goalType;
  136. h & isElementar;
  137. h & isAbstract;
  138. h & priority;
  139. h & value;
  140. h & resID;
  141. h & objid;
  142. h & aid;
  143. h & tile;
  144. h & hero;
  145. h & town;
  146. h & bid;
  147. }
  148. };
  149. class DLL_EXPORT ITask
  150. {
  151. public:
  152. float priority;
  153. ITask() : priority(0) {}
  154. ///Visitor pattern
  155. //TODO: make accept work for std::shared_ptr... somehow
  156. virtual void accept(VCAI * ai) = 0; //unhandled goal will report standard error
  157. virtual std::string toString() const = 0;
  158. virtual ~ITask() {}
  159. };
  160. }
  161. class cannotFulfillGoalException : public std::exception
  162. {
  163. std::string msg;
  164. public:
  165. explicit cannotFulfillGoalException(crstring _Message)
  166. : msg(_Message)
  167. {
  168. }
  169. virtual ~cannotFulfillGoalException() throw ()
  170. {
  171. };
  172. const char * what() const throw () override
  173. {
  174. return msg.c_str();
  175. }
  176. };
  177. class goalFulfilledException : public std::exception
  178. {
  179. std::string msg;
  180. public:
  181. Goals::TSubgoal goal;
  182. explicit goalFulfilledException(Goals::TSubgoal Goal)
  183. : goal(Goal)
  184. {
  185. msg = goal->toString();
  186. }
  187. virtual ~goalFulfilledException() throw ()
  188. {
  189. };
  190. const char * what() const throw () override
  191. {
  192. return msg.c_str();
  193. }
  194. };