AbstractGoal.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 VCAI;
  18. class FuzzyHelper;
  19. class CCallback;
  20. namespace Goals
  21. {
  22. class AbstractGoal;
  23. class Explore;
  24. class RecruitHero;
  25. class VisitTile;
  26. class VisitObj;
  27. class VisitHero;
  28. class BuildThis;
  29. class DigAtTile;
  30. class CollectRes;
  31. class Build;
  32. class BuyArmy;
  33. class BuildBoat;
  34. class GatherArmy;
  35. class ClearWayTo;
  36. class Invalid;
  37. class Trade;
  38. class CompleteQuest;
  39. class AdventureSpellCast;
  40. enum EGoals
  41. {
  42. INVALID = -1,
  43. WIN, CONQUER, BUILD, //build needs to get a real reasoning
  44. EXPLORE, GATHER_ARMY,
  45. BOOST_HERO,
  46. RECRUIT_HERO,
  47. BUILD_STRUCTURE, //if hero set, then in visited town
  48. COLLECT_RES,
  49. GATHER_TROOPS, // val of creatures with objid
  50. VISIT_OBJ, //visit or defeat or collect the object
  51. FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
  52. VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
  53. GET_ART_TYPE,
  54. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  55. CLEAR_WAY_TO,
  56. DIG_AT_TILE,//elementar with hero on tile
  57. BUY_ARMY, //at specific town
  58. TRADE, //val resID at object objid
  59. BUILD_BOAT,
  60. COMPLETE_QUEST,
  61. ADVENTURE_SPELL_CAST
  62. };
  63. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  64. {
  65. public:
  66. bool operator==(const TSubgoal & rhs) const;
  67. bool operator<(const TSubgoal & rhs) const;
  68. //TODO: serialize?
  69. };
  70. using TGoalVec = std::vector<TSubgoal>;
  71. //method chaining + clone pattern
  72. #define VSETTER(type, field) virtual AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  73. #define OSETTER(type, field) CGoal<T> & set ## field(const type &rhs) override { field = rhs; return *this; };
  74. #if 0
  75. #define SETTER
  76. #endif // _DEBUG
  77. enum { LOW_PR = -1 };
  78. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  79. struct DLL_EXPORT EvaluationContext
  80. {
  81. float movementCost;
  82. int manaCost;
  83. uint64_t danger;
  84. EvaluationContext()
  85. : movementCost(0.0),
  86. manaCost(0),
  87. danger(0)
  88. {
  89. }
  90. };
  91. class DLL_EXPORT AbstractGoal
  92. {
  93. public:
  94. bool isElementar; VSETTER(bool, isElementar)
  95. bool isAbstract; VSETTER(bool, isAbstract)
  96. float priority; VSETTER(float, priority)
  97. int value; VSETTER(int, value)
  98. int resID; VSETTER(int, resID)
  99. int objid; VSETTER(int, objid)
  100. int aid; VSETTER(int, aid)
  101. int3 tile; VSETTER(int3, tile)
  102. HeroPtr hero; VSETTER(HeroPtr, hero)
  103. const CGTownInstance *town; VSETTER(CGTownInstance *, town)
  104. int bid; VSETTER(int, bid)
  105. TSubgoal parent; VSETTER(TSubgoal, parent)
  106. EvaluationContext evaluationContext; VSETTER(EvaluationContext, evaluationContext)
  107. AbstractGoal(EGoals goal = EGoals::INVALID): goalType(goal)
  108. {
  109. priority = 0;
  110. isElementar = false;
  111. isAbstract = false;
  112. value = 0;
  113. aid = -1;
  114. resID = -1;
  115. objid = -1;
  116. tile = int3(-1, -1, -1);
  117. town = nullptr;
  118. bid = -1;
  119. }
  120. virtual ~AbstractGoal() {}
  121. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  122. virtual AbstractGoal * clone() const
  123. {
  124. return const_cast<AbstractGoal *>(this);
  125. }
  126. virtual TGoalVec getAllPossibleSubgoals()
  127. {
  128. return TGoalVec();
  129. }
  130. virtual TSubgoal whatToDoToAchieve()
  131. {
  132. return sptr(AbstractGoal());
  133. }
  134. EGoals goalType;
  135. virtual std::string name() const;
  136. virtual std::string completeMessage() const
  137. {
  138. return "This goal is unspecified!";
  139. }
  140. bool invalid() const;
  141. ///Visitor pattern
  142. //TODO: make accept work for std::shared_ptr... somehow
  143. virtual void accept(VCAI * ai); //unhandled goal will report standard error
  144. virtual float accept(FuzzyHelper * f);
  145. virtual bool operator==(const AbstractGoal & g) const;
  146. bool operator<(AbstractGoal & g); //final
  147. virtual bool fulfillsMe(Goals::TSubgoal goal) //TODO: multimethod instead of type check
  148. {
  149. return false; //use this method to check if goal is fulfilled by another (not equal) goal, operator == is handled spearately
  150. }
  151. bool operator!=(const AbstractGoal & g) const
  152. {
  153. return !(*this == g);
  154. }
  155. template<typename Handler> void serialize(Handler & h, const int version)
  156. {
  157. h & goalType;
  158. h & isElementar;
  159. h & isAbstract;
  160. h & priority;
  161. h & value;
  162. h & resID;
  163. h & objid;
  164. h & aid;
  165. h & tile;
  166. h & hero;
  167. h & town;
  168. h & bid;
  169. }
  170. };
  171. }