AbstractGoal.h 4.3 KB

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