Goals.h 11 KB

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