AbstractGoal.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. EXECUTE_HERO_CHAIN,
  62. EXCHANGE_SWAP_TOWN_HEROES,
  63. DISMISS_HERO
  64. };
  65. class DLL_EXPORT TSubgoal : public std::shared_ptr<AbstractGoal>
  66. {
  67. public:
  68. bool operator==(const TSubgoal & rhs) const;
  69. bool operator<(const TSubgoal & rhs) const;
  70. //TODO: serialize?
  71. };
  72. typedef std::vector<TSubgoal> TGoalVec;
  73. //method chaining + clone pattern
  74. #define VSETTER(type, field) virtual AbstractGoal & set ## field(const type &rhs) {field = rhs; return *this;};
  75. #define OSETTER(type, field) CGoal<T> & set ## field(const type &rhs) override { field = rhs; return *this; };
  76. #if 0
  77. #define SETTER
  78. #endif // _DEBUG
  79. enum { LOW_PR = -1 };
  80. DLL_EXPORT TSubgoal sptr(const AbstractGoal & tmp);
  81. struct DLL_EXPORT EvaluationContext
  82. {
  83. float movementCost;
  84. std::map<HeroRole, float> movementCostByRole;
  85. int manaCost;
  86. uint64_t danger;
  87. float closestWayRatio;
  88. uint64_t armyLoss;
  89. uint64_t heroStrength;
  90. float armyLossPersentage;
  91. float armyReward;
  92. int32_t goldReward;
  93. float skillReward;
  94. float strategicalValue;
  95. HeroRole heroRole;
  96. EvaluationContext()
  97. : movementCost(0.0),
  98. manaCost(0),
  99. danger(0),
  100. closestWayRatio(1),
  101. armyLoss(0),
  102. heroStrength(0),
  103. movementCostByRole(),
  104. skillReward(0),
  105. goldReward(0),
  106. armyReward(0),
  107. armyLossPersentage(0),
  108. heroRole(HeroRole::SCOUT)
  109. {
  110. }
  111. };
  112. class DLL_EXPORT AbstractGoal
  113. {
  114. public:
  115. bool isElementar; VSETTER(bool, isElementar)
  116. bool isAbstract; VSETTER(bool, isAbstract)
  117. float priority; VSETTER(float, priority)
  118. int value; VSETTER(int, value)
  119. int resID; VSETTER(int, resID)
  120. int objid; VSETTER(int, objid)
  121. int aid; VSETTER(int, aid)
  122. int3 tile; VSETTER(int3, tile)
  123. HeroPtr hero; VSETTER(HeroPtr, hero)
  124. const CGTownInstance *town; VSETTER(CGTownInstance *, town)
  125. int bid; VSETTER(int, bid)
  126. TSubgoal parent; VSETTER(TSubgoal, parent)
  127. EvaluationContext evaluationContext; VSETTER(EvaluationContext, evaluationContext)
  128. AbstractGoal(EGoals goal = EGoals::INVALID)
  129. : goalType(goal), evaluationContext()
  130. {
  131. priority = 0;
  132. isElementar = false;
  133. isAbstract = false;
  134. value = 0;
  135. aid = -1;
  136. resID = -1;
  137. objid = -1;
  138. tile = int3(-1, -1, -1);
  139. town = nullptr;
  140. bid = -1;
  141. }
  142. virtual ~AbstractGoal() {}
  143. //FIXME: abstract goal should be abstract, but serializer fails to instantiate subgoals in such case
  144. virtual AbstractGoal * clone() const
  145. {
  146. return const_cast<AbstractGoal *>(this);
  147. }
  148. virtual TGoalVec getAllPossibleSubgoals()
  149. {
  150. return TGoalVec();
  151. }
  152. virtual TSubgoal whatToDoToAchieve()
  153. {
  154. return sptr(AbstractGoal());
  155. }
  156. EGoals goalType;
  157. virtual std::string name() const;
  158. virtual std::string completeMessage() const
  159. {
  160. return "This goal is unspecified!";
  161. }
  162. bool invalid() const;
  163. ///Visitor pattern
  164. //TODO: make accept work for std::shared_ptr... somehow
  165. virtual void accept(VCAI * ai); //unhandled goal will report standard error
  166. virtual float accept(FuzzyHelper * f);
  167. virtual bool operator==(const AbstractGoal & g) const;
  168. bool operator<(AbstractGoal & g); //final
  169. virtual bool fulfillsMe(Goals::TSubgoal goal) //TODO: multimethod instead of type check
  170. {
  171. return false; //use this method to check if goal is fulfilled by another (not equal) goal, operator == is handled spearately
  172. }
  173. bool operator!=(const AbstractGoal & g) const
  174. {
  175. return !(*this == g);
  176. }
  177. template<typename Handler> void serialize(Handler & h, const int version)
  178. {
  179. h & goalType;
  180. h & isElementar;
  181. h & isAbstract;
  182. h & priority;
  183. h & value;
  184. h & resID;
  185. h & objid;
  186. h & aid;
  187. h & tile;
  188. h & hero;
  189. h & town;
  190. h & bid;
  191. }
  192. };
  193. }