VCAI.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #pragma once
  2. typedef const int3& crint3;
  3. typedef const std::string& crstring;
  4. enum BattleState
  5. {
  6. NO_BATTLE,
  7. UPCOMING_BATTLE,
  8. ONGOING_BATTLE,
  9. ENDING_BATTLE
  10. };
  11. class AIStatus
  12. {
  13. boost::mutex mx;
  14. boost::condition_variable cv;
  15. BattleState battle;
  16. int remainingQueries;
  17. bool havingTurn;
  18. public:
  19. AIStatus();
  20. ~AIStatus();
  21. void setBattle(BattleState BS);
  22. BattleState getBattle();
  23. void addQueries(int val);
  24. void addQuery();
  25. void removeQuery();
  26. int getQueriesCount();
  27. void startedTurn();
  28. void madeTurn();
  29. void waitTillFree();
  30. bool haveTurn();
  31. };
  32. enum EGoals
  33. {
  34. INVALID = -1,
  35. WIN, DO_NOT_LOSE, CONQUER, BUILD, EXPLORE, //GATHER_ARMY,// BOOST_HERO,
  36. RECRUIT_HERO,
  37. BUILD_STRUCTURE, //if hero set, then in visited town
  38. COLLECT_RES,
  39. OBJECT_GOALS_BEGIN,
  40. GET_OBJ, //visit or defeat or collect the object
  41. GET_ART_TYPE,
  42. //BUILD_STRUCTURE,
  43. ISSUE_COMMAND,
  44. //hero
  45. //VISIT_OBJ, //hero + tile
  46. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  47. CLEAR_WAY_TO,
  48. DIG_AT_TILE //elementar with hero on tile
  49. };
  50. struct CGoal;
  51. typedef CGoal TSubgoal;
  52. #define SETTER(type, field) CGoal &set ## field(const type &rhs) { field = rhs; return *this; }
  53. #if 0
  54. #define SETTER
  55. #endif // _DEBUG
  56. enum {LOW_PR = -1};
  57. struct CGoal
  58. {
  59. EGoals goalType;
  60. bool isElementar; SETTER(bool, isElementar)
  61. int priority; SETTER(bool, priority)
  62. virtual TSubgoal whatToDoToAchieve();
  63. bool isBlockedBorderGate(int3 tileToHit);
  64. CGoal(EGoals goal = INVALID) : goalType(goal)
  65. {
  66. priority = 0;
  67. isElementar = false;
  68. objid = -1;
  69. aid = -1;
  70. tile = int3(-1, -1, -1);
  71. hero = NULL;
  72. town = NULL;
  73. }
  74. bool invalid() const;
  75. static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is NULL, then we'll explore
  76. static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
  77. int value; SETTER(int, value)
  78. int resID; SETTER(int, resID)
  79. int objid; SETTER(int, objid)
  80. int aid; SETTER(int, aid)
  81. int3 tile; SETTER(int3, tile)
  82. const CGHeroInstance *hero; SETTER(CGHeroInstance *, hero)
  83. const CGTownInstance *town; SETTER(CGTownInstance *, town)
  84. int bid; SETTER(int, bid)
  85. };
  86. enum {NOT_VISIBLE = 0, NOT_CHECKED = 1, NOT_AVAILABLE};
  87. struct SectorMap
  88. {
  89. //a sector is set of tiles that would be mutually reachable if all visitable objs would be passable (incl monsters)
  90. struct Sector
  91. {
  92. int id;
  93. std::vector<int3> tiles;
  94. std::vector<int3> embarkmentPoints; //tiles of other sectors onto which we can (dis)embark
  95. bool water; //all tiles of sector are land or water
  96. Sector()
  97. {
  98. id = -1;
  99. }
  100. };
  101. bool valid; //some kind of lazy eval
  102. std::map<int3, int3> parent;
  103. std::vector<std::vector<std::vector<unsigned char>>> sector;
  104. //std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
  105. std::map<int, Sector> infoOnSectors;
  106. SectorMap();
  107. void update();
  108. void clear();
  109. void exploreNewSector(crint3 pos, int num);
  110. void write(crstring fname);
  111. unsigned char &retreiveTile(crint3 pos);
  112. void makeParentBFS(crint3 source);
  113. int3 firstTileToGet(const CGHeroInstance *h, crint3 dst); //if h wants to reach tile dst, which tile he should visit to clear the way?
  114. };
  115. struct CIssueCommand : CGoal
  116. {
  117. boost::function<bool()> command;
  118. CIssueCommand(boost::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
  119. };
  120. class VCAI : public CAdventureAI
  121. {
  122. public:
  123. friend class FuzzyHelper;
  124. std::map<const CGObjectInstance *, const CGObjectInstance *> knownSubterraneanGates;
  125. std::vector<const CGObjectInstance *> visitedThisWeek; //only OPWs
  126. std::map<const CGHeroInstance *, std::vector<const CGTownInstance *> > townVisitsThisWeek;
  127. std::set<const CGHeroInstance *> blockedHeroes; //they won't get any new action
  128. std::vector<const CGObjectInstance *> visitableObjs;
  129. std::vector<const CGObjectInstance *> alreadyVisited;
  130. TResources saving;
  131. AIStatus status;
  132. std::string battlename;
  133. CCallback *myCb;
  134. VCAI(void);
  135. ~VCAI(void);
  136. CGoal currentGoal;
  137. CGObjectInstance * visitedObject; //remember currently viisted object
  138. boost::thread *makingTurn;
  139. void tryRealize(CGoal g);
  140. int3 explorationBestNeighbour(int3 hpos, int radius, const CGHeroInstance * h);
  141. int3 explorationNewPoint(int radius, const CGHeroInstance * h, std::vector<std::vector<int3> > &tiles);
  142. void recruitHero();
  143. virtual void init(CCallback * CB);
  144. virtual void yourTurn();
  145. virtual void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback) OVERRIDE; //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  146. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, const int soundID, bool selection, bool cancel) OVERRIDE; //Show a dialog, player must take decision. If selection then he has to choose between one of given components, if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  147. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd) OVERRIDE; //all stacks operations between these objects become allowed, interface has to call onEnd when done
  148. virtual void serialize(COSer<CSaveFile> &h, const int version) OVERRIDE; //saving
  149. virtual void serialize(CISer<CLoadFile> &h, const int version) OVERRIDE; //loading
  150. virtual void finish() OVERRIDE;
  151. virtual void availableCreaturesChanged(const CGDwelling *town) OVERRIDE;
  152. virtual void heroMoved(const TryMoveHero & details) OVERRIDE;
  153. virtual void stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute) OVERRIDE;
  154. virtual void heroInGarrisonChange(const CGTownInstance *town) OVERRIDE;
  155. virtual void centerView(int3 pos, int focusTime) OVERRIDE;
  156. virtual void tileHidden(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE;
  157. virtual void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) OVERRIDE;
  158. virtual void artifactAssembled(const ArtifactLocation &al) OVERRIDE;
  159. virtual void showTavernWindow(const CGObjectInstance *townOrTavern) OVERRIDE;
  160. virtual void showThievesGuildWindow (const CGObjectInstance * obj) OVERRIDE;
  161. virtual void playerBlocked(int reason) OVERRIDE;
  162. virtual void showPuzzleMap() OVERRIDE;
  163. virtual void showShipyardDialog(const IShipyard *obj) OVERRIDE;
  164. virtual void gameOver(ui8 player, bool victory) OVERRIDE;
  165. virtual void artifactPut(const ArtifactLocation &al) OVERRIDE;
  166. virtual void artifactRemoved(const ArtifactLocation &al) OVERRIDE;
  167. virtual void stacksErased(const StackLocation &location) OVERRIDE;
  168. virtual void artifactDisassembled(const ArtifactLocation &al) OVERRIDE;
  169. virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start) OVERRIDE;
  170. virtual void availableArtifactsChanged(const CGBlackMarket *bm = NULL) OVERRIDE;
  171. virtual void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) OVERRIDE;
  172. virtual void tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE;
  173. virtual void heroExchangeStarted(si32 hero1, si32 hero2) OVERRIDE;
  174. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) OVERRIDE;
  175. virtual void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) OVERRIDE;
  176. virtual void heroMovePointsChanged(const CGHeroInstance * hero) OVERRIDE;
  177. virtual void stackChangedType(const StackLocation &location, const CCreature &newType) OVERRIDE;
  178. virtual void stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count) OVERRIDE;
  179. virtual void newObject(const CGObjectInstance * obj) OVERRIDE;
  180. virtual void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) OVERRIDE;
  181. virtual void playerBonusChanged(const Bonus &bonus, bool gain) OVERRIDE;
  182. virtual void newStackInserted(const StackLocation &location, const CStackInstance &stack) OVERRIDE;
  183. virtual void heroCreated(const CGHeroInstance*) OVERRIDE;
  184. virtual void advmapSpellCast(const CGHeroInstance * caster, int spellID) OVERRIDE;
  185. virtual void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) OVERRIDE;
  186. virtual void requestRealized(PackageApplied *pa) OVERRIDE;
  187. virtual void receivedResource(int type, int val) OVERRIDE;
  188. virtual void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) OVERRIDE;
  189. virtual void objectRemoved(const CGObjectInstance *obj) OVERRIDE;
  190. virtual void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  191. virtual void heroManaPointsChanged(const CGHeroInstance * hero) OVERRIDE;
  192. virtual void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) OVERRIDE;
  193. virtual void battleResultsApplied() OVERRIDE;
  194. virtual void objectPropertyChanged(const SetObjectProperty * sop) OVERRIDE;
  195. virtual void buildChanged(const CGTownInstance *town, int buildingID, int what) OVERRIDE;
  196. virtual void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) OVERRIDE;
  197. virtual void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  198. virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) OVERRIDE;
  199. virtual void battleEnd(const BattleResult *br) OVERRIDE;
  200. void makeTurn();
  201. void makeTurnInternal();
  202. void performTypicalActions();
  203. void buildArmyIn(const CGTownInstance * t);
  204. void striveToGoal(const CGoal &ultimateGoal);
  205. void endTurn();
  206. void wander(const CGHeroInstance * h);
  207. void recruitHero(const CGTownInstance * t);
  208. std::vector<const CGObjectInstance *> getPossibleDestinations(const CGHeroInstance *h);
  209. void buildStructure(const CGTownInstance * t);
  210. //void recruitCreatures(const CGTownInstance * t);
  211. void recruitCreatures(const CGDwelling * d);
  212. void pickBestCreatures(const CArmedInstance * army, const CArmedInstance * source); //called when we can't find a slot for new stack
  213. void moveCreaturesToHero(const CGTownInstance * t);
  214. bool goVisitObj(const CGObjectInstance * obj, const CGHeroInstance * h);
  215. void performObjectInteraction(const CGObjectInstance * obj, const CGHeroInstance * h);
  216. bool moveHeroToTile(int3 dst, const CGHeroInstance * h);
  217. void waitTillFree();
  218. void addVisitableObj(const CGObjectInstance *obj);
  219. //void removeVisitableObj(const CGObjectInstance *obj);
  220. void validateVisitableObjs();
  221. void retreiveVisitableObjs(std::vector<const CGObjectInstance *> &out, bool includeOwned = false) const;
  222. std::vector<const CGObjectInstance *> getFlaggedObjects() const;
  223. const CGObjectInstance *lookForArt(int aid) const;
  224. bool isAccessible(const int3 &pos);
  225. const CGHeroInstance *getHeroWithGrail() const;
  226. const CGObjectInstance *getUnvisitedObj(const boost::function<bool(const CGObjectInstance *)> &predicate);
  227. bool isAccessibleForHero(const int3 & pos, const CGHeroInstance * h) const;
  228. const CGTownInstance *findTownWithTavern() const;
  229. std::vector<const CGHeroInstance *> getUnblockedHeroes() const;
  230. const CGHeroInstance *primaryHero() const;
  231. TResources estimateIncome() const;
  232. bool containsSavedRes(const TResources &cost) const;
  233. };
  234. template<int id>
  235. bool objWithID(const CGObjectInstance *obj)
  236. {
  237. return obj->ID == id;
  238. }
  239. bool isWeeklyRevisitable (const CGObjectInstance * obj);