Goals.h 12 KB

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