AbstractGoal.h 4.3 KB

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