2
0

AbstractGoal.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/GameLibrary.h"
  12. #include "../../../lib/mapObjects/CGTownInstance.h"
  13. #include "../../../lib/mapObjects/CGHeroInstance.h"
  14. #include "../AIUtility.h"
  15. namespace NKAI
  16. {
  17. struct HeroPtr;
  18. class AIGateway;
  19. class FuzzyHelper;
  20. class Nullkiller;
  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. STAY_AT_TOWN_BEHAVIOR,
  68. STAY_AT_TOWN,
  69. EXPLORATION_BEHAVIOR,
  70. EXPLORATION_POINT,
  71. EXPLORE_NEIGHBOUR_TILE
  72. };
  73. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  74. {
  75. public:
  76. bool operator==(const TSubgoal & rhs) const;
  77. bool operator<(const TSubgoal & rhs) const;
  78. };
  79. using TTask = std::shared_ptr<ITask>;
  80. using TTaskVec = std::vector<TTask>;
  81. using TGoalVec = std::vector<TSubgoal>;
  82. //method chaining + clone pattern
  83. #define SETTER(type, field) AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  84. enum { LOW_PR = -1 };
  85. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  86. DLL_EXPORT TTask taskptr(const AbstractGoal & tmp);
  87. class DLL_EXPORT AbstractGoal
  88. {
  89. public:
  90. bool isAbstract; SETTER(bool, isAbstract)
  91. int value; SETTER(int, value)
  92. ui64 goldCost; SETTER(ui64, goldCost)
  93. TResources buildingCost; SETTER(TResources, buildingCost)
  94. int resID; SETTER(int, resID)
  95. int objid; SETTER(int, objid)
  96. int aid; SETTER(int, aid)
  97. int3 tile; SETTER(int3, tile)
  98. const CGHeroInstance * hero; SETTER(CGHeroInstance *, hero)
  99. const CGTownInstance *town; SETTER(CGTownInstance *, town)
  100. int bid; SETTER(int, bid)
  101. AbstractGoal(EGoals goal = EGoals::INVALID): goalType(goal)
  102. {
  103. isAbstract = false;
  104. value = 0;
  105. aid = -1;
  106. resID = -1;
  107. objid = -1;
  108. tile = int3(-1, -1, -1);
  109. town = nullptr;
  110. hero = nullptr;
  111. bid = -1;
  112. goldCost = 0;
  113. }
  114. virtual ~AbstractGoal() {}
  115. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  116. virtual AbstractGoal * clone() const
  117. {
  118. return const_cast<AbstractGoal *>(this);
  119. }
  120. virtual TGoalVec decompose(const Nullkiller * ai) const
  121. {
  122. return TGoalVec();
  123. }
  124. EGoals goalType;
  125. virtual std::string toString() const;
  126. bool invalid() const;
  127. virtual bool operator==(const AbstractGoal & g) const;
  128. virtual bool isElementar() const { return false; }
  129. virtual bool hasHash() const { return false; }
  130. virtual uint64_t getHash() const { return 0; }
  131. virtual ITask * asTask()
  132. {
  133. throw std::runtime_error("Abstract goal is not a task");
  134. }
  135. bool operator!=(const AbstractGoal & g) const
  136. {
  137. return !(*this == g);
  138. }
  139. };
  140. class DLL_EXPORT ITask
  141. {
  142. public:
  143. float priority;
  144. ITask() : priority(0) {}
  145. ///Visitor pattern
  146. //TODO: make accept work for std::shared_ptr... somehow
  147. virtual void accept(AIGateway * ai) = 0; //unhandled goal will report standard error
  148. virtual std::string toString() const = 0;
  149. virtual const CGHeroInstance * getHero() const = 0;
  150. virtual ~ITask() {}
  151. virtual int getHeroExchangeCount() const = 0;
  152. virtual bool isObjectAffected(ObjectInstanceID h) const = 0;
  153. virtual std::vector<ObjectInstanceID> getAffectedObjects() const = 0;
  154. };
  155. }
  156. class cannotFulfillGoalException : public std::exception
  157. {
  158. std::string msg;
  159. public:
  160. explicit cannotFulfillGoalException(const std::string & message)
  161. : msg(message)
  162. {
  163. }
  164. const char * what() const noexcept override
  165. {
  166. return msg.c_str();
  167. }
  168. };
  169. class goalFulfilledException : public std::exception
  170. {
  171. std::string msg;
  172. public:
  173. Goals::TSubgoal goal;
  174. explicit goalFulfilledException(Goals::TSubgoal Goal)
  175. : goal(Goal)
  176. {
  177. msg = goal->toString();
  178. }
  179. const char * what() const noexcept override
  180. {
  181. return msg.c_str();
  182. }
  183. };
  184. }