Goals.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #pragma once
  2. #include "../../lib/VCMI_Lib.h"
  3. #include "../../lib/CObjectHandler.h"
  4. #include "../../lib/CBuildingHandler.h"
  5. #include "../../lib/CCreatureHandler.h"
  6. #include "../../lib/CTownHandler.h"
  7. #include "AIUtility.h"
  8. /*
  9. * Goals.h, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. struct HeroPtr;
  18. class VCAI;
  19. namespace Goals
  20. {
  21. struct AbstractGoal;
  22. class VisitTile;
  23. typedef std::shared_ptr<Goals::AbstractGoal> TSubgoal;
  24. typedef std::vector<TSubgoal> TGoalVec;
  25. enum EGoals
  26. {
  27. INVALID = -1,
  28. WIN, DO_NOT_LOSE, CONQUER, BUILD, //build needs to get a real reasoning
  29. EXPLORE, GATHER_ARMY, BOOST_HERO,
  30. RECRUIT_HERO,
  31. BUILD_STRUCTURE, //if hero set, then in visited town
  32. COLLECT_RES,
  33. GATHER_TROOPS, // val of creatures with objid
  34. OBJECT_GOALS_BEGIN,
  35. GET_OBJ, //visit or defeat or collect the object
  36. FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
  37. VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
  38. GET_ART_TYPE,
  39. //BUILD_STRUCTURE,
  40. ISSUE_COMMAND,
  41. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  42. CLEAR_WAY_TO,
  43. DIG_AT_TILE //elementar with hero on tile
  44. };
  45. //method chaining + clone pattern
  46. #define VSETTER(type, field) virtual AbstractGoal & set ## field(const type &rhs) = 0;
  47. #define OSETTER(type, field) CGoal<T> & set ## field(const type &rhs) override { field = rhs; return *this; };
  48. #if 0
  49. #define SETTER
  50. #endif // _DEBUG
  51. enum {LOW_PR = -1};
  52. class AbstractGoal
  53. {
  54. public:
  55. bool isElementar; VSETTER(bool, isElementar)
  56. bool isAbstract; VSETTER(bool, isAbstract)
  57. int priority; VSETTER(bool, priority)
  58. int value; VSETTER(int, value)
  59. int resID; VSETTER(int, resID)
  60. int objid; VSETTER(int, objid)
  61. int aid; VSETTER(int, aid)
  62. int3 tile; VSETTER(int3, tile)
  63. HeroPtr hero; VSETTER(HeroPtr, hero)
  64. const CGTownInstance *town; VSETTER(CGTownInstance *, town)
  65. int bid; VSETTER(int, bid)
  66. AbstractGoal (EGoals goal = INVALID) : goalType (goal)
  67. {
  68. priority = 0;
  69. isElementar = false;
  70. isAbstract = false;
  71. value = 0;
  72. aid = -1;
  73. resID = -1;
  74. tile = int3(-1, -1, -1);
  75. town = nullptr;
  76. }
  77. virtual ~AbstractGoal(){};
  78. virtual AbstractGoal * clone() const = 0;
  79. EGoals goalType;
  80. std::string name() const;
  81. virtual std::string completeMessage() const {return "This goal is unspecified!";};
  82. bool invalid() const;
  83. static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is nullptr, then we'll explore
  84. static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
  85. static TSubgoal tryRecruitHero();
  86. virtual TSubgoal whatToDoToAchieve() = 0;
  87. ///Visitor pattern
  88. //TODO: make accept work for shared_ptr... somehow
  89. virtual void accept (VCAI * ai); //unhandled goal will report standard error
  90. virtual bool operator== (AbstractGoal &g)
  91. {
  92. return false;
  93. }
  94. virtual bool fulfillsMe (Goals::TSubgoal goal)
  95. {
  96. return false;
  97. }
  98. virtual bool fulfillsMe (shared_ptr<VisitTile> goal)
  99. {
  100. return false;
  101. }
  102. //template <typename Handler> void serialize(Handler &h, const int version)
  103. //{
  104. // h & goalType & isElementar & isAbstract & priority;
  105. // h & value & resID & objid & aid & tile & hero & town & bid;
  106. //}
  107. };
  108. template <typename T> class CGoal : public AbstractGoal
  109. {
  110. public:
  111. CGoal<T> (EGoals goal = INVALID) : AbstractGoal (goal)
  112. {
  113. priority = 0;
  114. isElementar = false;
  115. isAbstract = false;
  116. value = 0;
  117. aid = -1;
  118. resID = -1;
  119. tile = int3(-1, -1, -1);
  120. town = nullptr;
  121. }
  122. //virtual TSubgoal whatToDoToAchieve() override; //can't have virtual and template class at once
  123. OSETTER(bool, isElementar)
  124. OSETTER(bool, isAbstract)
  125. OSETTER(bool, priority)
  126. OSETTER(int, value)
  127. OSETTER(int, resID)
  128. OSETTER(int, objid)
  129. OSETTER(int, aid)
  130. OSETTER(int3, tile)
  131. OSETTER(HeroPtr, hero)
  132. OSETTER(CGTownInstance *, town)
  133. OSETTER(int, bid)
  134. void accept (VCAI * ai) override;
  135. CGoal<T> * clone() const override
  136. {
  137. return new T(static_cast<T const&>(*this)); //casting enforces template instantiation
  138. }
  139. TSubgoal iAmElementar()
  140. {
  141. setisElementar(true);
  142. shared_ptr<AbstractGoal> ptr;
  143. ptr.reset(clone());
  144. return ptr;
  145. }
  146. template <typename Handler> void serialize(Handler &h, const int version)
  147. {
  148. h & goalType & isElementar & isAbstract & priority;
  149. h & value & resID & objid & aid & tile & hero & town & bid;
  150. }
  151. };
  152. TSubgoal sptr(const AbstractGoal & tmp);
  153. class Invalid : public CGoal<Invalid>
  154. {
  155. public:
  156. Invalid() : CGoal (Goals::INVALID){};
  157. TSubgoal whatToDoToAchieve() override;
  158. };
  159. class Win : public CGoal<Win>
  160. {
  161. public:
  162. Win() : CGoal (Goals::WIN){};
  163. TSubgoal whatToDoToAchieve() override;
  164. };
  165. class NotLose : public CGoal<NotLose>
  166. {
  167. public:
  168. NotLose() : CGoal (Goals::DO_NOT_LOSE){};
  169. TSubgoal whatToDoToAchieve() override;
  170. };
  171. class Conquer : public CGoal<Conquer>
  172. {
  173. public:
  174. Conquer() : CGoal (Goals::CONQUER){};
  175. TSubgoal whatToDoToAchieve() override;
  176. };
  177. class Build : public CGoal<Build>
  178. {
  179. public:
  180. Build() : CGoal (Goals::BUILD){};
  181. TSubgoal whatToDoToAchieve() override;
  182. };
  183. class Explore : public CGoal<Explore>
  184. {
  185. public:
  186. Explore() : CGoal (Goals::EXPLORE){};
  187. Explore(HeroPtr h) : CGoal (Goals::EXPLORE){hero = h;};
  188. TSubgoal whatToDoToAchieve() override;
  189. std::string completeMessage() const override;
  190. };
  191. class GatherArmy : public CGoal<GatherArmy>
  192. {
  193. private:
  194. GatherArmy() : CGoal (Goals::GATHER_ARMY){};
  195. public:
  196. GatherArmy(int val) : CGoal (Goals::GATHER_ARMY){value = val;};
  197. TSubgoal whatToDoToAchieve() override;
  198. std::string completeMessage() const override;
  199. };
  200. class BoostHero : public CGoal<BoostHero>
  201. {
  202. public:
  203. BoostHero() : CGoal (Goals::INVALID){}; //TODO
  204. TSubgoal whatToDoToAchieve() override;
  205. };
  206. class RecruitHero : public CGoal<RecruitHero>
  207. {
  208. public:
  209. RecruitHero() : CGoal (Goals::RECRUIT_HERO){};
  210. TSubgoal whatToDoToAchieve() override;
  211. };
  212. class BuildThis : public CGoal<BuildThis>
  213. {
  214. private:
  215. BuildThis() : CGoal (Goals::BUILD_STRUCTURE){};
  216. public:
  217. BuildThis(BuildingID Bid, const CGTownInstance *tid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; town = tid;};
  218. BuildThis(BuildingID Bid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid;};
  219. TSubgoal whatToDoToAchieve() override;
  220. };
  221. class CollectRes : public CGoal<CollectRes>
  222. {
  223. private:
  224. CollectRes() : CGoal (Goals::COLLECT_RES){};
  225. public:
  226. CollectRes(int rid, int val) : CGoal (Goals::COLLECT_RES) {resID = rid; value = val;};
  227. TSubgoal whatToDoToAchieve() override;
  228. };
  229. class GatherTroops : public CGoal<GatherTroops>
  230. {
  231. private:
  232. GatherTroops() : CGoal (Goals::GATHER_TROOPS){};
  233. public:
  234. GatherTroops(int type, int val) : CGoal (Goals::GATHER_TROOPS){objid = type; value = val;};
  235. TSubgoal whatToDoToAchieve() override;
  236. };
  237. class GetObj : public CGoal<GetObj>
  238. {
  239. private:
  240. GetObj() {}; // empty constructor not allowed
  241. public:
  242. GetObj(int Objid) : CGoal(Goals::GET_OBJ) {objid = Objid;};
  243. TSubgoal whatToDoToAchieve() override;
  244. bool operator== (GetObj &g) {return g.objid == objid;}
  245. bool fulfillsMe (shared_ptr<VisitTile> goal) override;
  246. std::string completeMessage() const override;
  247. };
  248. class FindObj : public CGoal<FindObj>
  249. {
  250. private:
  251. FindObj() {}; // empty constructor not allowed
  252. public:
  253. FindObj(int ID) : CGoal(Goals::FIND_OBJ) {objid = ID;};
  254. FindObj(int ID, int subID) : CGoal(Goals::FIND_OBJ) {objid = ID; resID = subID;};
  255. TSubgoal whatToDoToAchieve() override;
  256. };
  257. class VisitHero : public CGoal<VisitHero>
  258. {
  259. private:
  260. VisitHero() : CGoal (Goals::VISIT_HERO){};
  261. public:
  262. VisitHero(int hid) : CGoal (Goals::VISIT_HERO){objid = hid;};
  263. TSubgoal whatToDoToAchieve() override;
  264. bool operator== (VisitHero &g) {return g.objid == objid;}
  265. bool fulfillsMe (shared_ptr<VisitTile> goal) override;
  266. std::string completeMessage() const override;
  267. };
  268. class GetArtOfType : public CGoal<GetArtOfType>
  269. {
  270. private:
  271. GetArtOfType() : CGoal (Goals::GET_ART_TYPE){};
  272. public:
  273. GetArtOfType(int type) : CGoal (Goals::GET_ART_TYPE){aid = type;};
  274. TSubgoal whatToDoToAchieve() override;
  275. };
  276. class VisitTile : public CGoal<VisitTile>
  277. //tile, in conjunction with hero elementar; assumes tile is reachable
  278. {
  279. private:
  280. VisitTile() {}; // empty constructor not allowed
  281. public:
  282. VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {tile = Tile;};
  283. TSubgoal whatToDoToAchieve() override;
  284. bool operator== (VisitTile &g) {return g.tile == tile;}
  285. std::string completeMessage() const override;
  286. };
  287. class ClearWayTo : public CGoal<ClearWayTo>
  288. {
  289. public:
  290. ClearWayTo(int3 Tile) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile;};
  291. TSubgoal whatToDoToAchieve() override;
  292. bool operator== (ClearWayTo &g) {return g.tile == tile;}
  293. };
  294. class DigAtTile : public CGoal<DigAtTile>
  295. //elementar with hero on tile
  296. {
  297. private:
  298. DigAtTile() : CGoal (Goals::DIG_AT_TILE){};
  299. public:
  300. DigAtTile(int3 Tile) : CGoal (Goals::DIG_AT_TILE) {tile = Tile;};
  301. TSubgoal whatToDoToAchieve() override;
  302. bool operator== (DigAtTile &g) {return g.tile == tile;}
  303. };
  304. class CIssueCommand : public CGoal<CIssueCommand>
  305. {
  306. std::function<bool()> command;
  307. public:
  308. CIssueCommand(std::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
  309. TSubgoal whatToDoToAchieve() override;
  310. };
  311. }