AIGateway.h 9.7 KB

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