AIGateway.h 9.6 KB

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