Goals.h 9.2 KB

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