AIGateway.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * AIGateway.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 "AIUtility.h"
  12. #include "Goals/AbstractGoal.h"
  13. #include "../../lib/AI_Base.h"
  14. #include "../../CCallback.h"
  15. #include "../../lib/CThreadHelper.h"
  16. #include "../../lib/GameConstants.h"
  17. #include "../../lib/VCMI_Lib.h"
  18. #include "../../lib/CCreatureHandler.h"
  19. #include "../../lib/mapObjects/MiscObjects.h"
  20. #include "../../lib/spells/CSpellHandler.h"
  21. #include "Pathfinding/AIPathfinder.h"
  22. #include "Engine/Nullkiller.h"
  23. namespace NKAI
  24. {
  25. class AIStatus
  26. {
  27. boost::mutex mx;
  28. boost::condition_variable cv;
  29. BattleState battle;
  30. std::map<QueryID, std::string> remainingQueries;
  31. std::map<int, QueryID> requestToQueryID; //IDs of answer-requests sent to server => query ids (so we can match answer confirmation from server to the query)
  32. std::vector<const CGObjectInstance *> objectsBeingVisited;
  33. bool ongoingHeroMovement;
  34. bool ongoingChannelProbing; // true if AI currently explore bidirectional teleport channel exits
  35. bool havingTurn;
  36. public:
  37. AIStatus();
  38. ~AIStatus();
  39. void setBattle(BattleState BS);
  40. void setMove(bool ongoing);
  41. void setChannelProbing(bool ongoing);
  42. bool channelProbing();
  43. BattleState getBattle();
  44. void addQuery(QueryID ID, std::string description);
  45. void removeQuery(QueryID ID);
  46. int getQueriesCount();
  47. void startedTurn();
  48. void madeTurn();
  49. void waitTillFree();
  50. bool haveTurn();
  51. void attemptedAnsweringQuery(QueryID queryID, int answerRequestID);
  52. void receivedAnswerConfirmation(int answerRequestID, int result);
  53. void heroVisit(const CGObjectInstance * obj, bool started);
  54. };
  55. // The gateway is responsible for AI events handling. Copied from VCAI.h and refined a bit
  56. // Probably we can use concept of hooks to handle event in their related goals
  57. class DLL_EXPORT AIGateway : public CAdventureAI
  58. {
  59. public:
  60. ObjectInstanceID destinationTeleport;
  61. int3 destinationTeleportPos;
  62. std::vector<ObjectInstanceID> teleportChannelProbingList; //list of teleport channel exits that not visible and need to be (re-)explored
  63. //std::vector<const CGObjectInstance *> visitedThisWeek; //only OPWs
  64. //std::set<HeroPtr> invalidPathHeroes; //FIXME, just a workaround
  65. //std::map<HeroPtr, Goals::TSubgoal> lockedHeroes; //TODO: allow non-elementar objectives
  66. //std::map<HeroPtr, std::set<const CGObjectInstance *>> reservedHeroesMap; //objects reserved by specific heroes
  67. //std::set<HeroPtr> heroesUnableToExplore; //these heroes will not be polled for exploration in current state of game
  68. //sets are faster to search, also do not contain duplicates
  69. //std::set<const CGObjectInstance *> reservedObjs; //to be visited by specific hero
  70. //std::map<HeroPtr, std::set<HeroPtr>> visitedHeroes; //visited this turn //FIXME: this is just bug workaround
  71. AIStatus status;
  72. std::string battlename;
  73. std::shared_ptr<CCallback> myCb;
  74. std::unique_ptr<boost::thread> makingTurn;
  75. private:
  76. boost::mutex turnInterruptionMutex;
  77. public:
  78. ObjectInstanceID selectedObject;
  79. std::unique_ptr<Nullkiller> nullkiller;
  80. AIGateway();
  81. virtual ~AIGateway();
  82. //TODO: extract to appropriate goals
  83. void tryRealize(Goals::DigAtTile & g);
  84. void tryRealize(Goals::Trade & g);
  85. std::string getBattleAIName() const override;
  86. void initGameInterface(std::shared_ptr<Environment> env, std::shared_ptr<CCallback> CB) override;
  87. void yourTurn(QueryID queryID) override;
  88. void heroGotLevel(const CGHeroInstance * hero, PrimarySkill pskill, std::vector<SecondarySkill> & skills, QueryID queryID) override; //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  89. void commanderGotLevel(const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override; //TODO
  90. void showBlockingDialog(const std::string & text, const std::vector<Component> & components, QueryID askID, const int soundID, bool selection, bool cancel, bool safeToAutoaccept) 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.
  91. void showGarrisonDialog(const CArmedInstance * up, const CGHeroInstance * down, bool removableUnits, QueryID queryID) override; //all stacks operations between these objects become allowed, interface has to call onEnd when done
  92. void showTeleportDialog(const CGHeroInstance * hero, TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) override;
  93. void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) override;
  94. void finish() override;
  95. void availableCreaturesChanged(const CGDwelling * town) override;
  96. void heroMoved(const TryMoveHero & details, bool verbose = true) override;
  97. void heroInGarrisonChange(const CGTownInstance * town) override;
  98. void centerView(int3 pos, int focusTime) override;
  99. void tileHidden(const std::unordered_set<int3> & pos) override;
  100. void artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst) override;
  101. void artifactAssembled(const ArtifactLocation & al) override;
  102. void showTavernWindow(const CGObjectInstance * object, const CGHeroInstance * visitor, QueryID queryID) override;
  103. void showThievesGuildWindow(const CGObjectInstance * obj) override;
  104. void playerBlocked(int reason, bool start) override;
  105. void showPuzzleMap() override;
  106. void showShipyardDialog(const IShipyard * obj) override;
  107. void gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult) override;
  108. void artifactPut(const ArtifactLocation & al) override;
  109. void artifactRemoved(const ArtifactLocation & al) override;
  110. void artifactDisassembled(const ArtifactLocation & al) override;
  111. void heroVisit(const CGHeroInstance * visitor, const CGObjectInstance * visitedObj, bool start) override;
  112. void availableArtifactsChanged(const CGBlackMarket * bm = nullptr) override;
  113. void heroVisitsTown(const CGHeroInstance * hero, const CGTownInstance * town) override;
  114. void tileRevealed(const std::unordered_set<int3> & pos) override;
  115. void heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query) override;
  116. void heroPrimarySkillChanged(const CGHeroInstance * hero, PrimarySkill which, si64 val) override;
  117. void showRecruitmentDialog(const CGDwelling * dwelling, const CArmedInstance * dst, int level, QueryID queryID) override;
  118. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  119. void garrisonsChanged(ObjectInstanceID id1, ObjectInstanceID id2) override;
  120. void newObject(const CGObjectInstance * obj) override;
  121. void showHillFortWindow(const CGObjectInstance * object, const CGHeroInstance * visitor) override;
  122. void playerBonusChanged(const Bonus & bonus, bool gain) override;
  123. void heroCreated(const CGHeroInstance *) override;
  124. void advmapSpellCast(const CGHeroInstance * caster, SpellID spellID) override;
  125. void showInfoDialog(EInfoWindowMode type, const std::string & text, const std::vector<Component> & components, int soundID) override;
  126. void requestRealized(PackageApplied * pa) override;
  127. void receivedResource() override;
  128. void objectRemoved(const CGObjectInstance * obj, const PlayerColor & initiator) override;
  129. void showUniversityWindow(const IMarket * market, const CGHeroInstance * visitor, QueryID queryID) override;
  130. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  131. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  132. void battleResultsApplied() override;
  133. void beforeObjectPropertyChanged(const SetObjectProperty * sop) override;
  134. void objectPropertyChanged(const SetObjectProperty * sop) override;
  135. void buildChanged(const CGTownInstance * town, BuildingID buildingID, int what) override;
  136. void heroBonusChanged(const CGHeroInstance * hero, const Bonus & bonus, bool gain) override;
  137. void showMarketWindow(const IMarket * market, const CGHeroInstance * visitor, QueryID queryID) override;
  138. void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain) override;
  139. std::optional<BattleAction> makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState) override;
  140. void battleStart(const BattleID & battleID, const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2, BattleSide side, bool replayAllowed) override;
  141. void battleEnd(const BattleID & battleID, const BattleResult * br, QueryID queryID) override;
  142. void makeTurn();
  143. void buildArmyIn(const CGTownInstance * t);
  144. void endTurn();
  145. // TODO: all the routines like recruiting hero or building army should be removed from here and extracted to elementar goals or whatever
  146. void recruitCreatures(const CGDwelling * d, const CArmedInstance * recruiter);
  147. void pickBestCreatures(const CArmedInstance * army, const CArmedInstance * source); //called when we can't find a slot for new stack
  148. void pickBestArtifacts(const CGHeroInstance * h, const CGHeroInstance * other = nullptr);
  149. void moveCreaturesToHero(const CGTownInstance * t);
  150. void performObjectInteraction(const CGObjectInstance * obj, HeroPtr h);
  151. bool makePossibleUpgrades(const CArmedInstance * obj);
  152. bool moveHeroToTile(int3 dst, HeroPtr h);
  153. void buildStructure(const CGTownInstance * t, BuildingID building);
  154. void lostHero(HeroPtr h); //should remove all references to hero (assigned tasks and so on)
  155. void waitTillFree();
  156. void addVisitableObj(const CGObjectInstance * obj);
  157. void validateObject(const CGObjectInstance * obj); //checks if object is still visible and if not, removes references to it
  158. void validateObject(ObjectIdRef obj); //checks if object is still visible and if not, removes references to it
  159. void retrieveVisitableObjs();
  160. virtual std::vector<const CGObjectInstance *> getFlaggedObjects() const;
  161. void requestSent(const CPackForServer * pack, int requestID) override;
  162. void answerQuery(QueryID queryID, int selection);
  163. //special function that can be called ONLY from game events handling thread and will send request ASAP
  164. void requestActionASAP(std::function<void()> whatToDo);
  165. };
  166. }