VCAI.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, //build needs to get a real reasoning
  36. EXPLORE, GATHER_ARMY, BOOST_HERO,
  37. RECRUIT_HERO,
  38. BUILD_STRUCTURE, //if hero set, then in visited town
  39. COLLECT_RES,
  40. OBJECT_GOALS_BEGIN,
  41. GET_OBJ, //visit or defeat or collect the object
  42. GET_ART_TYPE,
  43. //BUILD_STRUCTURE,
  44. ISSUE_COMMAND,
  45. //hero
  46. //VISIT_OBJ, //hero + tile
  47. VISIT_TILE, //tile, in conjunction with hero elementar; assumes tile is reachable
  48. CLEAR_WAY_TO,
  49. DIG_AT_TILE //elementar with hero on tile
  50. };
  51. struct CGoal;
  52. typedef CGoal TSubgoal;
  53. #define SETTER(type, field) CGoal &set ## field(const type &rhs) { field = rhs; return *this; }
  54. #if 0
  55. #define SETTER
  56. #endif // _DEBUG
  57. enum {LOW_PR = -1};
  58. struct CGoal
  59. {
  60. EGoals goalType;
  61. bool isElementar; SETTER(bool, isElementar)
  62. int priority; SETTER(bool, priority)
  63. virtual TSubgoal whatToDoToAchieve();
  64. bool isBlockedBorderGate(int3 tileToHit);
  65. CGoal(EGoals goal = INVALID) : goalType(goal)
  66. {
  67. priority = 0;
  68. isElementar = false;
  69. objid = -1;
  70. aid = -1;
  71. tile = int3(-1, -1, -1);
  72. hero = NULL;
  73. town = NULL;
  74. }
  75. bool invalid() const;
  76. static TSubgoal goVisitOrLookFor(const CGObjectInstance *obj); //if obj is NULL, then we'll explore
  77. static TSubgoal lookForArtSmart(int aid); //checks non-standard ways of obtaining art (merchants, quests, etc.)
  78. int value; SETTER(int, value)
  79. int resID; SETTER(int, resID)
  80. int objid; SETTER(int, objid)
  81. int aid; SETTER(int, aid)
  82. int3 tile; SETTER(int3, tile)
  83. const CGHeroInstance *hero; SETTER(CGHeroInstance *, hero)
  84. const CGTownInstance *town; SETTER(CGTownInstance *, town)
  85. int bid; SETTER(int, bid)
  86. };
  87. enum {NOT_VISIBLE = 0, NOT_CHECKED = 1, NOT_AVAILABLE};
  88. struct SectorMap
  89. {
  90. //a sector is set of tiles that would be mutually reachable if all visitable objs would be passable (incl monsters)
  91. struct Sector
  92. {
  93. int id;
  94. std::vector<int3> tiles;
  95. std::vector<int3> embarkmentPoints; //tiles of other sectors onto which we can (dis)embark
  96. bool water; //all tiles of sector are land or water
  97. Sector()
  98. {
  99. id = -1;
  100. }
  101. };
  102. bool valid; //some kind of lazy eval
  103. std::map<int3, int3> parent;
  104. std::vector<std::vector<std::vector<unsigned char>>> sector;
  105. //std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
  106. std::map<int, Sector> infoOnSectors;
  107. SectorMap();
  108. void update();
  109. void clear();
  110. void exploreNewSector(crint3 pos, int num);
  111. void write(crstring fname);
  112. unsigned char &retreiveTile(crint3 pos);
  113. void makeParentBFS(crint3 source);
  114. int3 firstTileToGet(const CGHeroInstance *h, crint3 dst); //if h wants to reach tile dst, which tile he should visit to clear the way?
  115. };
  116. struct CIssueCommand : CGoal
  117. {
  118. boost::function<bool()> command;
  119. CIssueCommand(boost::function<bool()> _command): CGoal(ISSUE_COMMAND), command(_command) {}
  120. };
  121. // AI lives in a dangerous world. CGObjectInstances under pointer may got deleted/hidden.
  122. // This class stores object id, so we can detect when we lose access to the underlying object.
  123. struct ObjectIdRef
  124. {
  125. int id;
  126. const CGObjectInstance *operator->() const;
  127. operator const CGObjectInstance *() const;
  128. ObjectIdRef(int _id);
  129. ObjectIdRef(const CGObjectInstance *obj);
  130. bool operator<(const ObjectIdRef &rhs) const;
  131. };
  132. class ObjsVector : public std::vector<ObjectIdRef>
  133. {
  134. private:
  135. };
  136. class VCAI : public CAdventureAI
  137. {
  138. public:
  139. friend class FuzzyHelper;
  140. std::map<const CGObjectInstance *, const CGObjectInstance *> knownSubterraneanGates;
  141. std::vector<const CGObjectInstance *> visitedThisWeek; //only OPWs
  142. std::map<const CGHeroInstance *, std::vector<const CGTownInstance *> > townVisitsThisWeek;
  143. std::map<const CGHeroInstance *, CGoal> lockedHeroes; //TODO: allow non-elementar objectives
  144. std::map<const CGHeroInstance *, std::set<const CGObjectInstance *> > reservedHeroesMap; //objects reserved by specific heroes
  145. std::vector<const CGObjectInstance *> visitableObjs;
  146. std::vector<const CGObjectInstance *> alreadyVisited;
  147. std::vector<const CGObjectInstance *> reservedObjs; //to be visited by specific hero
  148. TResources saving;
  149. AIStatus status;
  150. std::string battlename;
  151. CCallback *myCb;
  152. VCAI(void);
  153. ~VCAI(void);
  154. CGObjectInstance * visitedObject; //remember currently viisted object
  155. boost::thread *makingTurn;
  156. void tryRealize(CGoal g);
  157. int3 explorationBestNeighbour(int3 hpos, int radius, const CGHeroInstance * h);
  158. int3 explorationNewPoint(int radius, const CGHeroInstance * h, std::vector<std::vector<int3> > &tiles);
  159. void recruitHero();
  160. virtual void init(CCallback * CB);
  161. virtual void yourTurn();
  162. 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
  163. 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.
  164. 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
  165. virtual void serialize(COSer<CSaveFile> &h, const int version) OVERRIDE; //saving
  166. virtual void serialize(CISer<CLoadFile> &h, const int version) OVERRIDE; //loading
  167. virtual void finish() OVERRIDE;
  168. virtual void availableCreaturesChanged(const CGDwelling *town) OVERRIDE;
  169. virtual void heroMoved(const TryMoveHero & details) OVERRIDE;
  170. virtual void stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute) OVERRIDE;
  171. virtual void heroInGarrisonChange(const CGTownInstance *town) OVERRIDE;
  172. virtual void centerView(int3 pos, int focusTime) OVERRIDE;
  173. virtual void tileHidden(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE;
  174. virtual void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) OVERRIDE;
  175. virtual void artifactAssembled(const ArtifactLocation &al) OVERRIDE;
  176. virtual void showTavernWindow(const CGObjectInstance *townOrTavern) OVERRIDE;
  177. virtual void showThievesGuildWindow (const CGObjectInstance * obj) OVERRIDE;
  178. virtual void playerBlocked(int reason) OVERRIDE;
  179. virtual void showPuzzleMap() OVERRIDE;
  180. virtual void showShipyardDialog(const IShipyard *obj) OVERRIDE;
  181. virtual void gameOver(ui8 player, bool victory) OVERRIDE;
  182. virtual void artifactPut(const ArtifactLocation &al) OVERRIDE;
  183. virtual void artifactRemoved(const ArtifactLocation &al) OVERRIDE;
  184. virtual void stacksErased(const StackLocation &location) OVERRIDE;
  185. virtual void artifactDisassembled(const ArtifactLocation &al) OVERRIDE;
  186. virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start) OVERRIDE;
  187. virtual void availableArtifactsChanged(const CGBlackMarket *bm = NULL) OVERRIDE;
  188. virtual void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) OVERRIDE;
  189. virtual void tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE;
  190. virtual void heroExchangeStarted(si32 hero1, si32 hero2) OVERRIDE;
  191. virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) OVERRIDE;
  192. virtual void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) OVERRIDE;
  193. virtual void heroMovePointsChanged(const CGHeroInstance * hero) OVERRIDE;
  194. virtual void stackChangedType(const StackLocation &location, const CCreature &newType) OVERRIDE;
  195. virtual void stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count) OVERRIDE;
  196. virtual void newObject(const CGObjectInstance * obj) OVERRIDE;
  197. virtual void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) OVERRIDE;
  198. virtual void playerBonusChanged(const Bonus &bonus, bool gain) OVERRIDE;
  199. virtual void newStackInserted(const StackLocation &location, const CStackInstance &stack) OVERRIDE;
  200. virtual void heroCreated(const CGHeroInstance*) OVERRIDE;
  201. virtual void advmapSpellCast(const CGHeroInstance * caster, int spellID) OVERRIDE;
  202. virtual void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) OVERRIDE;
  203. virtual void requestRealized(PackageApplied *pa) OVERRIDE;
  204. virtual void receivedResource(int type, int val) OVERRIDE;
  205. virtual void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) OVERRIDE;
  206. virtual void objectRemoved(const CGObjectInstance *obj) OVERRIDE;
  207. virtual void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  208. virtual void heroManaPointsChanged(const CGHeroInstance * hero) OVERRIDE;
  209. virtual void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) OVERRIDE;
  210. virtual void battleResultsApplied() OVERRIDE;
  211. virtual void objectPropertyChanged(const SetObjectProperty * sop) OVERRIDE;
  212. virtual void buildChanged(const CGTownInstance *town, int buildingID, int what) OVERRIDE;
  213. virtual void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) OVERRIDE;
  214. virtual void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  215. virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) OVERRIDE;
  216. virtual void battleEnd(const BattleResult *br) OVERRIDE;
  217. void makeTurn();
  218. void makeTurnInternal();
  219. void performTypicalActions();
  220. void buildArmyIn(const CGTownInstance * t);
  221. void striveToGoal(const CGoal &ultimateGoal);
  222. void endTurn();
  223. void wander(const CGHeroInstance * h);
  224. void setGoal (const CGHeroInstance *h, const CGoal goal);
  225. void setGoal (const CGHeroInstance *h, EGoals goalType = INVALID);
  226. void recruitHero(const CGTownInstance * t);
  227. std::vector<const CGObjectInstance *> getPossibleDestinations(const CGHeroInstance *h);
  228. void buildStructure(const CGTownInstance * t);
  229. //void recruitCreatures(const CGTownInstance * t);
  230. void recruitCreatures(const CGDwelling * d);
  231. void pickBestCreatures(const CArmedInstance * army, const CArmedInstance * source); //called when we can't find a slot for new stack
  232. void moveCreaturesToHero(const CGTownInstance * t);
  233. bool goVisitObj(const CGObjectInstance * obj, const CGHeroInstance * h);
  234. void performObjectInteraction(const CGObjectInstance * obj, const CGHeroInstance * h);
  235. bool moveHeroToTile(int3 dst, const CGHeroInstance * h);
  236. void waitTillFree();
  237. void addVisitableObj(const CGObjectInstance *obj);
  238. void markObjectVisited (const CGObjectInstance *obj);
  239. void reserveObject (const CGHeroInstance * h, const CGObjectInstance *obj);
  240. //void removeVisitableObj(const CGObjectInstance *obj);
  241. void validateVisitableObjs();
  242. void retreiveVisitableObjs(std::vector<const CGObjectInstance *> &out, bool includeOwned = false) const;
  243. std::vector<const CGObjectInstance *> getFlaggedObjects() const;
  244. const CGObjectInstance *lookForArt(int aid) const;
  245. bool isAccessible(const int3 &pos);
  246. const CGHeroInstance *getHeroWithGrail() const;
  247. const CGObjectInstance *getUnvisitedObj(const boost::function<bool(const CGObjectInstance *)> &predicate);
  248. bool isAccessibleForHero(const int3 & pos, const CGHeroInstance * h) const;
  249. const CGTownInstance *findTownWithTavern() const;
  250. std::vector<const CGHeroInstance *> getUnblockedHeroes() const;
  251. const CGHeroInstance *primaryHero() const;
  252. TResources estimateIncome() const;
  253. bool containsSavedRes(const TResources &cost) const;
  254. //special function that can be called ONLY from game events handling thread and will send request ASAP
  255. void requestActionASAP(boost::function<void()> whatToDo);
  256. };
  257. template<int id>
  258. bool objWithID(const CGObjectInstance *obj)
  259. {
  260. return obj->ID == id;
  261. }
  262. bool isWeeklyRevisitable (const CGObjectInstance * obj);
  263. bool shouldVisit (const CGHeroInstance * h, const CGObjectInstance * obj);