AbstractGoal.h 4.3 KB

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