AbstractGoal.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * AbstractGoal.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 AIGateway;
  18. class FuzzyHelper;
  19. namespace Goals
  20. {
  21. class AbstractGoal;
  22. class ITask;
  23. class RecruitHero;
  24. class BuildThis;
  25. class DigAtTile;
  26. class CollectRes;
  27. class BuyArmy;
  28. class BuildBoat;
  29. class Invalid;
  30. class Trade;
  31. class AdventureSpellCast;
  32. enum EGoals
  33. {
  34. INVALID = -1,
  35. WIN, CONQUER,
  36. BUILD,
  37. EXPLORE, GATHER_ARMY,
  38. BOOST_HERO,
  39. RECRUIT_HERO,
  40. RECRUIT_HERO_BEHAVIOR,
  41. BUILD_STRUCTURE, //if hero set, then in visited town
  42. COLLECT_RES,
  43. GATHER_TROOPS, // val of creatures with objid
  44. CAPTURE_OBJECTS,
  45. GET_ART_TYPE,
  46. DEFENCE,
  47. STARTUP,
  48. DIG_AT_TILE,//elementar with hero on tile
  49. BUY_ARMY, //at specific town
  50. TRADE, //val resID at object objid
  51. BUILD_BOAT,
  52. COMPLETE_QUEST,
  53. ADVENTURE_SPELL_CAST,
  54. EXECUTE_HERO_CHAIN,
  55. EXCHANGE_SWAP_TOWN_HEROES,
  56. DISMISS_HERO,
  57. COMPOSITION,
  58. CLUSTER_BEHAVIOR,
  59. UNLOCK_CLUSTER,
  60. HERO_EXCHANGE,
  61. ARMY_UPGRADE,
  62. DEFEND_TOWN,
  63. CAPTURE_OBJECT,
  64. SAVE_RESOURCES
  65. };
  66. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  67. {
  68. public:
  69. bool operator==(const TSubgoal & rhs) const;
  70. bool operator<(const TSubgoal & rhs) const;
  71. };
  72. typedef std::shared_ptr<ITask> TTask;
  73. typedef std::vector<TTask> TTaskVec;
  74. typedef std::vector<TSubgoal> TGoalVec;
  75. //method chaining + clone pattern
  76. #define SETTER(type, field) AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  77. enum { LOW_PR = -1 };
  78. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  79. DLL_EXPORT TTask taskptr(const AbstractGoal & tmp);
  80. class DLL_EXPORT AbstractGoal
  81. {
  82. public:
  83. bool isAbstract; SETTER(bool, isAbstract)
  84. int value; SETTER(int, value)
  85. ui64 goldCost; SETTER(ui64, goldCost)
  86. int resID; SETTER(int, resID)
  87. int objid; SETTER(int, objid)
  88. int aid; SETTER(int, aid)
  89. int3 tile; SETTER(int3, tile)
  90. HeroPtr hero; SETTER(HeroPtr, hero)
  91. const CGTownInstance *town; SETTER(CGTownInstance *, town)
  92. int bid; SETTER(int, bid)
  93. AbstractGoal(EGoals goal = EGoals::INVALID)
  94. : goalType(goal), hero()
  95. {
  96. isAbstract = false;
  97. value = 0;
  98. aid = -1;
  99. resID = -1;
  100. objid = -1;
  101. tile = int3(-1, -1, -1);
  102. town = nullptr;
  103. bid = -1;
  104. goldCost = 0;
  105. }
  106. virtual ~AbstractGoal() {}
  107. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  108. virtual AbstractGoal * clone() const
  109. {
  110. return const_cast<AbstractGoal *>(this);
  111. }
  112. virtual TGoalVec decompose() const
  113. {
  114. return TGoalVec();
  115. }
  116. EGoals goalType;
  117. virtual std::string toString() const;
  118. bool invalid() const;
  119. virtual bool operator==(const AbstractGoal & g) const;
  120. virtual bool isElementar() const { return false; }
  121. virtual bool hasHash() const { return false; }
  122. virtual uint64_t getHash() const { return 0; }
  123. bool operator!=(const AbstractGoal & g) const
  124. {
  125. return !(*this == g);
  126. }
  127. };
  128. class DLL_EXPORT ITask
  129. {
  130. public:
  131. float priority;
  132. ITask() : priority(0) {}
  133. ///Visitor pattern
  134. //TODO: make accept work for std::shared_ptr... somehow
  135. virtual void accept(AIGateway * ai) = 0; //unhandled goal will report standard error
  136. virtual std::string toString() const = 0;
  137. virtual HeroPtr getHero() const = 0;
  138. virtual ~ITask() {}
  139. virtual int getHeroExchangeCount() const = 0;
  140. };
  141. }
  142. class cannotFulfillGoalException : public std::exception
  143. {
  144. std::string msg;
  145. public:
  146. explicit cannotFulfillGoalException(crstring _Message)
  147. : msg(_Message)
  148. {
  149. }
  150. virtual ~cannotFulfillGoalException() throw ()
  151. {
  152. };
  153. const char * what() const throw () override
  154. {
  155. return msg.c_str();
  156. }
  157. };
  158. class goalFulfilledException : public std::exception
  159. {
  160. std::string msg;
  161. public:
  162. Goals::TSubgoal goal;
  163. explicit goalFulfilledException(Goals::TSubgoal Goal)
  164. : goal(Goal)
  165. {
  166. msg = goal->toString();
  167. }
  168. virtual ~goalFulfilledException() throw ()
  169. {
  170. };
  171. const char * what() const throw () override
  172. {
  173. return msg.c_str();
  174. }
  175. };