CGameInterface.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. struct DLL_LINKAGE Priorities
  73. {
  74. double manaValue;
  75. double generalResourceValueModifier;
  76. std::vector<double> resourceTypeBaseValues;
  77. std::function<double(const CStack *)> stackEvaluator;
  78. double heroValue;
  79. Priorities();
  80. };
  81. class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
  82. {
  83. public:
  84. bool human;
  85. PlayerColor playerID;
  86. std::string dllName;
  87. virtual ~CBattleGameInterface() {};
  88. virtual void init(shared_ptr<CBattleCallback> CB){};
  89. //battle call-ins
  90. virtual BattleAction activeStack(const CStack * stack)=0; //called when it's turn of that stack
  91. virtual void yourTacticPhase(int distance){}; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
  92. virtual void setPriorities(const Priorities &priorities){}
  93. virtual void saveGame(COSer<CSaveFile> &h, const int version);
  94. virtual void loadGame(CISer<CLoadFile> &h, const int version);
  95. };
  96. /// Central class for managing human player / AI interface logic
  97. class CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
  98. {
  99. public:
  100. virtual void init(shared_ptr<CCallback> CB){};
  101. virtual void yourTurn(){}; //called AFTER playerStartsTurn(player)
  102. virtual void receiveMessageFromAutomation(const boost::any &msg){}
  103. //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  104. virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
  105. virtual void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID)=0;
  106. // Show a dialog, player must take decision. If selection then he has to choose between one of given components,
  107. // if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called
  108. // with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  109. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, const int soundID, bool selection, bool cancel) = 0;
  110. // all stacks operations between these objects become allowed, interface has to call onEnd when done
  111. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) = 0;
  112. virtual void finish(){}; //if for some reason we want to end
  113. };
  114. class DLL_LINKAGE CDynLibHandler
  115. {
  116. public:
  117. static shared_ptr<CGlobalAI> getNewAI(std::string dllname);
  118. static shared_ptr<CBattleGameInterface> getNewBattleAI(std::string dllname);
  119. static shared_ptr<CScriptingModule> getNewScriptingModule(std::string dllname);
  120. static shared_ptr<CAutomationModule> getNewAutomationModule(std::string dllname);
  121. };
  122. class DLL_LINKAGE CGlobalAI : public CGameInterface // AI class (to derivate)
  123. {
  124. public:
  125. CGlobalAI();
  126. virtual BattleAction activeStack(const CStack * stack) OVERRIDE;
  127. };
  128. //class to be inherited by adventure-only AIs, it cedes battle actions to given battle-AI
  129. class DLL_LINKAGE CAdventureAI : public CGlobalAI
  130. {
  131. public:
  132. CAdventureAI() {};
  133. shared_ptr<CBattleGameInterface> battleAI;
  134. shared_ptr<CBattleCallback> cbc;
  135. virtual std::string getBattleAIName() const = 0; //has to return name of the battle AI to be used
  136. //battle interface
  137. virtual BattleAction activeStack(const CStack * stack);
  138. virtual void yourTacticPhase(int distance);
  139. virtual void battleNewRound(int round);
  140. virtual void battleCatapultAttacked(const CatapultAttack & ca);
  141. virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side);
  142. virtual void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa);
  143. virtual void actionStarted(const BattleAction &action);
  144. virtual void battleNewRoundFirst(int round);
  145. virtual void actionFinished(const BattleAction &action);
  146. virtual void battleStacksEffectsSet(const SetStackEffect & sse);
  147. //virtual void battleTriggerEffect(const BattleTriggerEffect & bte);
  148. virtual void battleStacksRemoved(const BattleStacksRemoved & bsr);
  149. virtual void battleObstaclesRemoved(const std::set<si32> & removedObstacles);
  150. virtual void battleNewStackAppeared(const CStack * stack);
  151. virtual void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance);
  152. virtual void battleAttack(const BattleAttack *ba);
  153. virtual void battleSpellCast(const BattleSpellCast *sc);
  154. virtual void battleEnd(const BattleResult *br);
  155. virtual void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom);
  156. virtual void saveGame(COSer<CSaveFile> &h, const int version); //saving
  157. virtual void loadGame(CISer<CLoadFile> &h, const int version); //loading
  158. };
  159. class DLL_LINKAGE CAutomationModule : public IGameEventsReceiver
  160. {
  161. ObjectsCeded receivedObjects;
  162. protected:
  163. const std::vector<const CGObjectInstance*> &getMyObjects() const;
  164. std::vector<const CGHeroInstance*> getMyHeroes() const;
  165. shared_ptr<CAutomationCallback> cb;
  166. public:
  167. void automationInit(shared_ptr<CAutomationCallback> cb, const ObjectsCeded &objs);
  168. virtual bool needsWaitingForRealize() const {return true;}
  169. virtual bool needsUnlockingGs() const {return false;}
  170. virtual void newObjectReceived(const CGObjectInstance *obj);
  171. virtual void objectTakenAway(const CGObjectInstance *obj);
  172. virtual void modulePrepared(){};
  173. virtual void receivedMessage(const boost::any &msg);
  174. virtual void executeInternal() = 0;
  175. void execute();
  176. friend class CCallback;
  177. };