CGameInterface.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include "BattleAction.h"
  3. #include "IGameEventsReceiver.h"
  4. /*
  5. * CGameInterface.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. using namespace boost::logic;
  14. class CCallback;
  15. class CBattleCallback;
  16. class ICallback;
  17. class CGlobalAI;
  18. struct Component;
  19. class CSelectableComponent;
  20. struct TryMoveHero;
  21. class CGHeroInstance;
  22. class CGTownInstance;
  23. class CGObjectInstance;
  24. class CGBlackMarket;
  25. class CGDwelling;
  26. class CCreatureSet;
  27. class CArmedInstance;
  28. class IShipyard;
  29. class IMarket;
  30. struct BattleResult;
  31. struct BattleAttack;
  32. struct BattleStackAttacked;
  33. struct BattleSpellCast;
  34. struct SetStackEffect;
  35. struct Bonus;
  36. struct PackageApplied;
  37. struct SetObjectProperty;
  38. struct CatapultAttack;
  39. struct BattleStacksRemoved;
  40. struct StackLocation;
  41. class CStackInstance;
  42. class CCommanderInstance;
  43. class CStack;
  44. class CCreature;
  45. class CLoadFile;
  46. class CSaveFile;
  47. template <typename Serializer> class CISer;
  48. template <typename Serializer> class COSer;
  49. struct ArtifactLocation;
  50. class CScriptingModule;
  51. class CAutomationModule;
  52. class CClient;
  53. class CAutomationCallback;
  54. struct ObjectsCeded
  55. {
  56. std::vector<const CGObjectInstance *> objects;
  57. bool hasObject(const CGObjectInstance *obj) const
  58. {
  59. return vstd::contains(objects, obj);
  60. }
  61. void remove(const CGObjectInstance *obj)
  62. {
  63. auto it = range::find(objects, obj);
  64. if(it != objects.end())
  65. objects.erase(it);
  66. }
  67. void add(const CGObjectInstance *obj)
  68. {
  69. objects.push_back(obj);
  70. }
  71. };
  72. class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
  73. {
  74. public:
  75. bool human;
  76. PlayerColor playerID;
  77. std::string dllName;
  78. virtual ~CBattleGameInterface() {};
  79. virtual void init(shared_ptr<CBattleCallback> CB){};
  80. //battle call-ins
  81. virtual BattleAction activeStack(const CStack * stack)=0; //called when it's turn of that stack
  82. virtual void yourTacticPhase(int distance){}; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
  83. virtual void saveGame(COSer<CSaveFile> &h, const int version);
  84. virtual void loadGame(CISer<CLoadFile> &h, const int version);
  85. };
  86. /// Central class for managing human player / AI interface logic
  87. class CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
  88. {
  89. public:
  90. virtual void init(shared_ptr<CCallback> CB){};
  91. virtual void yourTurn(){}; //called AFTER playerStartsTurn(player)
  92. //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  93. virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
  94. virtual void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID)=0;
  95. // Show a dialog, player must take decision. If selection then he has to choose between one of given components,
  96. // if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called
  97. // with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  98. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, const int soundID, bool selection, bool cancel) = 0;
  99. // all stacks operations between these objects become allowed, interface has to call onEnd when done
  100. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) = 0;
  101. virtual void finish(){}; //if for some reason we want to end
  102. };
  103. class DLL_LINKAGE CDynLibHandler
  104. {
  105. public:
  106. static shared_ptr<CGlobalAI> getNewAI(std::string dllname);
  107. static shared_ptr<CBattleGameInterface> getNewBattleAI(std::string dllname);
  108. static shared_ptr<CScriptingModule> getNewScriptingModule(std::string dllname);
  109. static shared_ptr<CAutomationModule> getNewAutomationModule(std::string dllname);
  110. };
  111. class DLL_LINKAGE CGlobalAI : public CGameInterface // AI class (to derivate)
  112. {
  113. public:
  114. CGlobalAI();
  115. virtual BattleAction activeStack(const CStack * stack) OVERRIDE;
  116. };
  117. //class to be inherited by adventure-only AIs, it cedes battle actions to given battle-AI
  118. class DLL_LINKAGE CAdventureAI : public CGlobalAI
  119. {
  120. public:
  121. CAdventureAI() {};
  122. shared_ptr<CBattleGameInterface> battleAI;
  123. shared_ptr<CBattleCallback> cbc;
  124. virtual std::string getBattleAIName() const = 0; //has to return name of the battle AI to be used
  125. //battle interface
  126. virtual BattleAction activeStack(const CStack * stack);
  127. virtual void yourTacticPhase(int distance);
  128. virtual void battleNewRound(int round);
  129. virtual void battleCatapultAttacked(const CatapultAttack & ca);
  130. virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side);
  131. virtual void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa);
  132. virtual void actionStarted(const BattleAction &action);
  133. virtual void battleNewRoundFirst(int round);
  134. virtual void actionFinished(const BattleAction &action);
  135. virtual void battleStacksEffectsSet(const SetStackEffect & sse);
  136. //virtual void battleTriggerEffect(const BattleTriggerEffect & bte);
  137. virtual void battleStacksRemoved(const BattleStacksRemoved & bsr);
  138. virtual void battleObstaclesRemoved(const std::set<si32> & removedObstacles);
  139. virtual void battleNewStackAppeared(const CStack * stack);
  140. virtual void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance);
  141. virtual void battleAttack(const BattleAttack *ba);
  142. virtual void battleSpellCast(const BattleSpellCast *sc);
  143. virtual void battleEnd(const BattleResult *br);
  144. virtual void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom);
  145. virtual void saveGame(COSer<CSaveFile> &h, const int version); //saving
  146. virtual void loadGame(CISer<CLoadFile> &h, const int version); //loading
  147. };
  148. class DLL_LINKAGE CAutomationModule : public IGameEventsReceiver
  149. {
  150. ObjectsCeded receivedObjects;
  151. protected:
  152. const std::vector<const CGObjectInstance*> &getMyObjects() const;
  153. std::vector<const CGHeroInstance*> getMyHeroes() const;
  154. shared_ptr<CAutomationCallback> cb;
  155. public:
  156. void automationInit(shared_ptr<CAutomationCallback> cb, const ObjectsCeded &objs);
  157. virtual bool needsWaitingForRealize() const {return true;}
  158. virtual bool needsUnlockingGs() const {return false;}
  159. virtual void newObjectReceived(const CGObjectInstance *obj);
  160. virtual void objectTakenAway(const CGObjectInstance *obj);
  161. virtual void modulePrepared(){};
  162. virtual void receivedMessage(const boost::any &msg);
  163. virtual void executeInternal() = 0;
  164. void execute();
  165. //friend class CAutomationCallback;
  166. friend class CCallback;
  167. //friend class CClient;
  168. };