Goals.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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;
  104. h & isElementar;
  105. h & isAbstract;
  106. h & priority;
  107. h & value;
  108. h & resID;
  109. h & objid;
  110. h & aid;
  111. h & tile;
  112. h & hero;
  113. h & town;
  114. h & bid;
  115. }
  116. };
  117. template <typename T> class CGoal : public AbstractGoal
  118. {
  119. public:
  120. CGoal<T> (EGoals goal = INVALID) : AbstractGoal (goal)
  121. {
  122. priority = 0;
  123. isElementar = false;
  124. isAbstract = false;
  125. value = 0;
  126. aid = -1;
  127. objid = -1;
  128. resID = -1;
  129. tile = int3(-1, -1, -1);
  130. town = nullptr;
  131. }
  132. OSETTER(bool, isElementar)
  133. OSETTER(bool, isAbstract)
  134. OSETTER(float, priority)
  135. OSETTER(int, value)
  136. OSETTER(int, resID)
  137. OSETTER(int, objid)
  138. OSETTER(int, aid)
  139. OSETTER(int3, tile)
  140. OSETTER(HeroPtr, hero)
  141. OSETTER(CGTownInstance *, town)
  142. OSETTER(int, bid)
  143. void accept (VCAI * ai) override;
  144. float accept (FuzzyHelper * f) override;
  145. CGoal<T> * clone() const override
  146. {
  147. return new T(static_cast<T const&>(*this)); //casting enforces template instantiation
  148. }
  149. TSubgoal iAmElementar()
  150. {
  151. setisElementar(true);
  152. std::shared_ptr<AbstractGoal> ptr;
  153. ptr.reset(clone());
  154. return ptr;
  155. }
  156. template <typename Handler> void serialize(Handler &h, const int version)
  157. {
  158. h & static_cast<AbstractGoal&> (*this);
  159. //h & goalType & isElementar & isAbstract & priority;
  160. //h & value & resID & objid & aid & tile & hero & town & bid;
  161. }
  162. };
  163. class Invalid : public CGoal<Invalid>
  164. {
  165. public:
  166. Invalid() : CGoal (Goals::INVALID) {priority = -1e10;};
  167. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  168. TSubgoal whatToDoToAchieve() override;
  169. };
  170. class Win : public CGoal<Win>
  171. {
  172. public:
  173. Win() : CGoal (Goals::WIN) {priority = 100;};
  174. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  175. TSubgoal whatToDoToAchieve() override;
  176. };
  177. class NotLose : public CGoal<NotLose>
  178. {
  179. public:
  180. NotLose() : CGoal (Goals::DO_NOT_LOSE) {priority = 100;};
  181. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  182. //TSubgoal whatToDoToAchieve() override;
  183. };
  184. class Conquer : public CGoal<Conquer>
  185. {
  186. public:
  187. Conquer() : CGoal (Goals::CONQUER) {priority = 10;};
  188. TGoalVec getAllPossibleSubgoals() override;
  189. TSubgoal whatToDoToAchieve() override;
  190. };
  191. class Build : public CGoal<Build>
  192. {
  193. public:
  194. Build() : CGoal (Goals::BUILD) {priority = 1;};
  195. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  196. TSubgoal whatToDoToAchieve() override;
  197. };
  198. class Explore : public CGoal<Explore>
  199. {
  200. public:
  201. Explore() : CGoal (Goals::EXPLORE){priority = 1;};
  202. Explore(HeroPtr h) : CGoal (Goals::EXPLORE){hero = h; priority = 1;};
  203. TGoalVec getAllPossibleSubgoals() override;
  204. TSubgoal whatToDoToAchieve() override;
  205. std::string completeMessage() const override;
  206. bool fulfillsMe (TSubgoal goal) override;
  207. };
  208. class GatherArmy : public CGoal<GatherArmy>
  209. {
  210. public:
  211. GatherArmy() : CGoal (Goals::GATHER_ARMY){};
  212. GatherArmy(int val) : CGoal (Goals::GATHER_ARMY){value = val; priority = 2.5;};
  213. TGoalVec getAllPossibleSubgoals() override;
  214. TSubgoal whatToDoToAchieve() override;
  215. std::string completeMessage() const override;
  216. };
  217. class BoostHero : public CGoal<BoostHero>
  218. {
  219. public:
  220. BoostHero() : CGoal (Goals::INVALID){priority = -1e10;}; //TODO
  221. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  222. //TSubgoal whatToDoToAchieve() override {return sptr(Invalid());};
  223. };
  224. class RecruitHero : public CGoal<RecruitHero>
  225. {
  226. public:
  227. RecruitHero() : CGoal (Goals::RECRUIT_HERO){priority = 1;};
  228. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  229. TSubgoal whatToDoToAchieve() override;
  230. };
  231. class BuildThis : public CGoal<BuildThis>
  232. {
  233. public:
  234. BuildThis() : CGoal (Goals::BUILD_STRUCTURE){}; //FIXME: should be not allowed (private)
  235. BuildThis(BuildingID Bid, const CGTownInstance *tid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; town = tid; priority = 5;};
  236. BuildThis(BuildingID Bid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; priority = 5;};
  237. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  238. TSubgoal whatToDoToAchieve() override;
  239. };
  240. class CollectRes : public CGoal<CollectRes>
  241. {
  242. public:
  243. CollectRes() : CGoal (Goals::COLLECT_RES){};
  244. CollectRes(int rid, int val) : CGoal (Goals::COLLECT_RES) {resID = rid; value = val; priority = 2;};
  245. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  246. TSubgoal whatToDoToAchieve() override;
  247. };
  248. class GatherTroops : public CGoal<GatherTroops>
  249. {
  250. public:
  251. GatherTroops() : CGoal (Goals::GATHER_TROOPS){priority = 2;};
  252. GatherTroops(int type, int val) : CGoal (Goals::GATHER_TROOPS){objid = type; value = val; priority = 2;};
  253. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  254. TSubgoal whatToDoToAchieve() override;
  255. };
  256. class GetObj : public CGoal<GetObj>
  257. {
  258. public:
  259. GetObj() {}; // empty constructor not allowed
  260. GetObj(int Objid) : CGoal(Goals::GET_OBJ) {objid = Objid; priority = 3;};
  261. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  262. TSubgoal whatToDoToAchieve() override;
  263. bool operator== (GetObj &g) {return g.objid == objid;}
  264. bool fulfillsMe (TSubgoal goal) override;
  265. std::string completeMessage() const override;
  266. };
  267. class FindObj : public CGoal<FindObj>
  268. {
  269. public:
  270. FindObj() {}; // empty constructor not allowed
  271. FindObj(int ID) : CGoal(Goals::FIND_OBJ) {objid = ID; priority = 1;};
  272. FindObj(int ID, int subID) : CGoal(Goals::FIND_OBJ) {objid = ID; resID = subID; priority = 1;};
  273. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  274. TSubgoal whatToDoToAchieve() override;
  275. };
  276. class VisitHero : public CGoal<VisitHero>
  277. {
  278. public:
  279. VisitHero() : CGoal (Goals::VISIT_HERO){};
  280. VisitHero(int hid) : CGoal (Goals::VISIT_HERO){objid = hid; priority = 4;};
  281. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  282. TSubgoal whatToDoToAchieve() override;
  283. bool operator== (VisitHero &g) { return g.goalType == goalType && g.objid == objid; }
  284. bool fulfillsMe (TSubgoal goal) override;
  285. std::string completeMessage() const override;
  286. };
  287. class GetArtOfType : public CGoal<GetArtOfType>
  288. {
  289. public:
  290. GetArtOfType() : CGoal (Goals::GET_ART_TYPE){};
  291. GetArtOfType(int type) : CGoal (Goals::GET_ART_TYPE){aid = type; priority = 2;};
  292. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  293. TSubgoal whatToDoToAchieve() override;
  294. };
  295. class VisitTile : public CGoal<VisitTile>
  296. //tile, in conjunction with hero elementar; assumes tile is reachable
  297. {
  298. public:
  299. VisitTile() {}; // empty constructor not allowed
  300. VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {tile = Tile; priority = 5;};
  301. TGoalVec getAllPossibleSubgoals() override;
  302. TSubgoal whatToDoToAchieve() override;
  303. bool operator== (VisitTile &g) { return g.goalType == goalType && g.tile == tile; }
  304. std::string completeMessage() const override;
  305. };
  306. class ClearWayTo : public CGoal<ClearWayTo>
  307. {
  308. public:
  309. ClearWayTo() : CGoal (Goals::CLEAR_WAY_TO){};
  310. ClearWayTo(int3 Tile) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile; priority = 5;};
  311. ClearWayTo(int3 Tile, HeroPtr h) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile; hero = h; priority = 5;};
  312. TGoalVec getAllPossibleSubgoals() override;
  313. TSubgoal whatToDoToAchieve() override;
  314. bool operator== (ClearWayTo &g) { return g.goalType == goalType && g.tile == tile; }
  315. };
  316. class DigAtTile : public CGoal<DigAtTile>
  317. //elementar with hero on tile
  318. {
  319. public:
  320. DigAtTile() : CGoal (Goals::DIG_AT_TILE){};
  321. DigAtTile(int3 Tile) : CGoal (Goals::DIG_AT_TILE) {tile = Tile; priority = 20;};
  322. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  323. TSubgoal whatToDoToAchieve() override;
  324. bool operator== (DigAtTile &g) { return g.goalType == goalType && g.tile == tile; }
  325. };
  326. class CIssueCommand : public CGoal<CIssueCommand>
  327. {
  328. std::function<bool()> command;
  329. public:
  330. CIssueCommand(): CGoal(ISSUE_COMMAND){};
  331. CIssueCommand(std::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {priority = 1e10;};
  332. TGoalVec getAllPossibleSubgoals() override {return TGoalVec();};
  333. //TSubgoal whatToDoToAchieve() override {return sptr(Invalid());};
  334. };
  335. }