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. namespace Goals
  20. {
  21. class AbstractGoal;
  22. class Explore;
  23. class RecruitHero;
  24. class VisitTile;
  25. class VisitObj;
  26. class VisitHero;
  27. class BuildThis;
  28. class DigAtTile;
  29. class CollectRes;
  30. class Build;
  31. class BuyArmy;
  32. class BuildBoat;
  33. class GatherArmy;
  34. class ClearWayTo;
  35. class Invalid;
  36. class Trade;
  37. class CompleteQuest;
  38. class AdventureSpellCast;
  39. enum EGoals
  40. {
  41. INVALID = -1,
  42. WIN, CONQUER, BUILD, //build needs to get a real reasoning
  43. EXPLORE, GATHER_ARMY,
  44. BOOST_HERO,
  45. RECRUIT_HERO,
  46. BUILD_STRUCTURE, //if hero set, then in visited town
  47. COLLECT_RES,
  48. GATHER_TROOPS, // val of creatures with objid
  49. VISIT_OBJ, //visit or defeat or collect the object
  50. FIND_OBJ, //find and visit any obj with objid + resid //TODO: consider universal subid for various types (aid, bid)
  51. VISIT_HERO, //heroes can move around - set goal abstract and track hero every turn
  52. GET_ART_TYPE,
  53. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  54. CLEAR_WAY_TO,
  55. DIG_AT_TILE,//elementar with hero on tile
  56. BUY_ARMY, //at specific town
  57. TRADE, //val resID at object objid
  58. BUILD_BOAT,
  59. COMPLETE_QUEST,
  60. ADVENTURE_SPELL_CAST
  61. };
  62. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  63. {
  64. public:
  65. bool operator==(const TSubgoal & rhs) const;
  66. bool operator<(const TSubgoal & rhs) const;
  67. //TODO: serialize?
  68. };
  69. typedef std::vector<TSubgoal> TGoalVec;
  70. //method chaining + clone pattern
  71. #define VSETTER(type, field) virtual AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  72. #define OSETTER(type, field) CGoal<T> & set ## field(const type &rhs) override { field = rhs; return *this; };
  73. #if 0
  74. #define SETTER
  75. #endif // _DEBUG
  76. enum { LOW_PR = -1 };
  77. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  78. struct DLL_EXPORT EvaluationContext
  79. {
  80. float movementCost;
  81. int manaCost;
  82. uint64_t danger;
  83. EvaluationContext()
  84. : movementCost(0.0),
  85. manaCost(0),
  86. danger(0)
  87. {
  88. }
  89. };
  90. class DLL_EXPORT AbstractGoal
  91. {
  92. public:
  93. bool isElementar; VSETTER(bool, isElementar)
  94. bool isAbstract; VSETTER(bool, isAbstract)
  95. float priority; VSETTER(float, priority)
  96. int value; VSETTER(int, value)
  97. int resID; VSETTER(int, resID)
  98. int objid; VSETTER(int, objid)
  99. int aid; VSETTER(int, aid)
  100. int3 tile; VSETTER(int3, tile)
  101. HeroPtr hero; VSETTER(HeroPtr, hero)
  102. const CGTownInstance *town; VSETTER(CGTownInstance *, town)
  103. int bid; VSETTER(int, bid)
  104. TSubgoal parent; VSETTER(TSubgoal, parent)
  105. EvaluationContext evaluationContext; VSETTER(EvaluationContext, evaluationContext)
  106. AbstractGoal(EGoals goal = EGoals::INVALID)
  107. : goalType(goal), evaluationContext()
  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. }