Goals.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. namespace Goals
  19. {
  20. struct CGoal;
  21. typedef std::shared_ptr<Goals::CGoal> TSubgoal;
  22. enum EGoals
  23. {
  24. INVALID = -1,
  25. WIN, DO_NOT_LOSE, CONQUER, BUILD, //build needs to get a real reasoning
  26. EXPLORE, GATHER_ARMY, BOOST_HERO,
  27. RECRUIT_HERO,
  28. BUILD_STRUCTURE, //if hero set, then in visited town
  29. COLLECT_RES,
  30. GATHER_TROOPS, // val of creatures with objid
  31. OBJECT_GOALS_BEGIN,
  32. GET_OBJ, //visit or defeat or collect the object
  33. FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
  34. VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
  35. GET_ART_TYPE,
  36. //BUILD_STRUCTURE,
  37. ISSUE_COMMAND,
  38. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  39. CLEAR_WAY_TO,
  40. DIG_AT_TILE //elementar with hero on tile
  41. };
  42. #define SETTER(type, field) CGoal &set ## field(const type &rhs) { field = rhs; return *this; }
  43. #if 0
  44. #define SETTER
  45. #endif // _DEBUG
  46. enum {LOW_PR = -1};
  47. struct CGoal
  48. {
  49. EGoals goalType;
  50. bool isElementar; SETTER(bool, isElementar)
  51. bool isAbstract; SETTER(bool, isAbstract) //allows to remember abstract goals
  52. int priority; SETTER(bool, priority)
  53. std::string name() const;
  54. virtual TSubgoal whatToDoToAchieve();
  55. CGoal(EGoals goal = INVALID) : goalType(goal)
  56. {
  57. priority = 0;
  58. isElementar = false;
  59. isAbstract = false;
  60. value = 0;
  61. aid = -1;
  62. resID = -1;
  63. tile = int3(-1, -1, -1);
  64. town = nullptr;
  65. }
  66. bool invalid() const;
  67. static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is nullptr, then we'll explore
  68. static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
  69. static TSubgoal tryRecruitHero();
  70. int value; SETTER(int, value)
  71. int resID; SETTER(int, resID)
  72. int objid; SETTER(int, objid)
  73. int aid; SETTER(int, aid)
  74. int3 tile; SETTER(int3, tile)
  75. HeroPtr hero; SETTER(HeroPtr, hero)
  76. const CGTownInstance *town; SETTER(CGTownInstance *, town)
  77. int bid; SETTER(int, bid)
  78. bool operator== (CGoal &g)
  79. {
  80. switch (goalType)
  81. {
  82. case EGoals::GET_OBJ:
  83. return objid == g.objid;
  84. }
  85. return false;
  86. }
  87. template <typename Handler> void serialize(Handler &h, const int version)
  88. {
  89. h & goalType & isElementar & isAbstract & priority;
  90. h & value & resID & objid & aid & tile & hero & town & bid;
  91. }
  92. };
  93. class Invalid : public CGoal
  94. {
  95. public:
  96. Invalid() : CGoal (Goals::INVALID){};
  97. TSubgoal whatToDoToAchieve() override;
  98. };
  99. class Win : public CGoal
  100. {
  101. public:
  102. Win() : CGoal (Goals::WIN){};
  103. TSubgoal whatToDoToAchieve() override;
  104. };
  105. class NotLose : public CGoal
  106. {
  107. public:
  108. NotLose() : CGoal (Goals::DO_NOT_LOSE){};
  109. TSubgoal whatToDoToAchieve() override;
  110. };
  111. class Conquer : public CGoal
  112. {
  113. public:
  114. Conquer() : CGoal (Goals::CONQUER){};
  115. TSubgoal whatToDoToAchieve() override;
  116. };
  117. class Build : public CGoal
  118. {
  119. public:
  120. Build() : CGoal (Goals::BUILD){};
  121. TSubgoal whatToDoToAchieve() override;
  122. };
  123. class Explore : public CGoal
  124. {
  125. public:
  126. Explore() : CGoal (Goals::EXPLORE){};
  127. TSubgoal whatToDoToAchieve() override;
  128. };
  129. class GatherArmy : public CGoal
  130. {
  131. public:
  132. GatherArmy() : CGoal (Goals::GATHER_ARMY){};
  133. TSubgoal whatToDoToAchieve() override;
  134. };
  135. class BoostHero : public CGoal
  136. {
  137. public:
  138. BoostHero() : CGoal (Goals::INVALID){}; //TODO
  139. TSubgoal whatToDoToAchieve() override;
  140. };
  141. class RecruitHero : public CGoal
  142. {
  143. public:
  144. RecruitHero() : CGoal (Goals::RECRUIT_HERO){};
  145. TSubgoal whatToDoToAchieve() override;
  146. };
  147. class BuildThis : public CGoal
  148. {
  149. public:
  150. BuildThis() : CGoal (Goals::BUILD_STRUCTURE){};
  151. TSubgoal whatToDoToAchieve() override;
  152. };
  153. class CollectRes : public CGoal
  154. {
  155. public:
  156. CollectRes() : CGoal (Goals::COLLECT_RES){};
  157. TSubgoal whatToDoToAchieve() override;
  158. };
  159. class GatherTroops : public CGoal
  160. {
  161. public:
  162. GatherTroops() : CGoal (Goals::GATHER_TROOPS){};
  163. TSubgoal whatToDoToAchieve() override;
  164. };
  165. class GetObj : public CGoal
  166. {
  167. private:
  168. GetObj() {}; // empty constructor not allowed
  169. public:
  170. GetObj(const int Objid) : CGoal(Goals::GET_OBJ) {setobjid(Objid);};
  171. TSubgoal whatToDoToAchieve() override;
  172. };
  173. class FindObj : public CGoal
  174. {
  175. private:
  176. FindObj() {}; // empty constructor not allowed
  177. public:
  178. FindObj(int ID) : CGoal(Goals::FIND_OBJ) {setobjid(ID);};
  179. FindObj(int ID, int subID) : CGoal(Goals::FIND_OBJ) {setobjid(ID).setresID(subID);};
  180. TSubgoal whatToDoToAchieve() override;
  181. };
  182. class VisitHero : public CGoal
  183. {
  184. public:
  185. VisitHero() : CGoal (Goals::VISIT_HERO){};
  186. TSubgoal whatToDoToAchieve() override;
  187. };
  188. class GetArtOfType : public CGoal
  189. {
  190. public:
  191. GetArtOfType() : CGoal (Goals::GET_ART_TYPE){};
  192. TSubgoal whatToDoToAchieve() override;
  193. };
  194. class VisitTile : public CGoal
  195. //tile, in conjunction with hero elementar; assumes tile is reachable
  196. {
  197. private:
  198. VisitTile() {}; // empty constructor not allowed
  199. public:
  200. VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {settile(Tile);};
  201. TSubgoal whatToDoToAchieve() override;
  202. };
  203. class ClearWayTo : public CGoal
  204. {
  205. public:
  206. ClearWayTo(int3 Tile) : CGoal (Goals::CLEAR_WAY_TO) {settile(Tile);};
  207. TSubgoal whatToDoToAchieve() override;
  208. };
  209. class DigAtTile : public CGoal
  210. //elementar with hero on tile
  211. {
  212. public:
  213. DigAtTile() : CGoal (Goals::DIG_AT_TILE){};
  214. TSubgoal whatToDoToAchieve() override;
  215. };
  216. class CIssueCommand : CGoal
  217. {
  218. std::function<bool()> command;
  219. public:
  220. CIssueCommand(std::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
  221. TSubgoal whatToDoToAchieve() override;
  222. };
  223. }