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