AbstractGoal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. };
  69. using TGoalVec = std::vector<TSubgoal>;
  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): goalType(goal)
  107. {
  108. priority = 0;
  109. isElementar = false;
  110. isAbstract = false;
  111. value = 0;
  112. aid = -1;
  113. resID = -1;
  114. objid = -1;
  115. tile = int3(-1, -1, -1);
  116. town = nullptr;
  117. bid = -1;
  118. }
  119. virtual ~AbstractGoal() {}
  120. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  121. virtual AbstractGoal * clone() const
  122. {
  123. return const_cast<AbstractGoal *>(this);
  124. }
  125. virtual TGoalVec getAllPossibleSubgoals()
  126. {
  127. return TGoalVec();
  128. }
  129. virtual TSubgoal whatToDoToAchieve()
  130. {
  131. return sptr(AbstractGoal());
  132. }
  133. EGoals goalType;
  134. virtual std::string name() const;
  135. virtual std::string completeMessage() const
  136. {
  137. return "This goal is unspecified!";
  138. }
  139. bool invalid() const;
  140. ///Visitor pattern
  141. //TODO: make accept work for std::shared_ptr... somehow
  142. virtual void accept(VCAI * ai); //unhandled goal will report standard error
  143. virtual float accept(FuzzyHelper * f);
  144. virtual bool operator==(const AbstractGoal & g) const;
  145. // bool operator<(AbstractGoal & g); //final
  146. virtual bool fulfillsMe(Goals::TSubgoal goal) //TODO: multimethod instead of type check
  147. {
  148. return false; //use this method to check if goal is fulfilled by another (not equal) goal, operator == is handled spearately
  149. }
  150. // bool operator!=(const AbstractGoal & g) const
  151. // {
  152. // return !(*this == g);
  153. // }
  154. };
  155. }