Goals.h 8.6 KB

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