BattleInterface.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * BattleInterface.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 "BattleConstants.h"
  12. #include "../gui/CIntObject.h"
  13. #include "../../lib/spells/CSpellHandler.h" //CSpell::TAnimation
  14. #include "../../lib/CondSh.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CCreatureSet;
  17. class CGHeroInstance;
  18. class CStack;
  19. struct BattleResult;
  20. struct BattleSpellCast;
  21. struct CObstacleInstance;
  22. struct SetStackEffect;
  23. class BattleAction;
  24. class CGTownInstance;
  25. struct CatapultAttack;
  26. struct BattleTriggerEffect;
  27. struct BattleHex;
  28. struct InfoAboutHero;
  29. VCMI_LIB_NAMESPACE_END
  30. class BattleHero;
  31. class Canvas;
  32. class BattleResultWindow;
  33. class StackQueue;
  34. class CPlayerInterface;
  35. class CAnimation;
  36. struct BattleEffect;
  37. class IImage;
  38. class StackQueue;
  39. class BattleProjectileController;
  40. class BattleSiegeController;
  41. class BattleObstacleController;
  42. class BattleFieldController;
  43. class BattleRenderer;
  44. class BattleWindow;
  45. class BattleStacksController;
  46. class BattleActionsController;
  47. class BattleEffectsController;
  48. class BattleConsole;
  49. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  50. struct StackAttackedInfo
  51. {
  52. const CStack *defender;
  53. const CStack *attacker;
  54. int64_t damageDealt;
  55. uint32_t amountKilled;
  56. SpellID spellEffect;
  57. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  58. bool killed; //if true, stack has been killed
  59. bool rebirth; //if true, play rebirth animation after all
  60. bool cloneKilled;
  61. bool fireShield;
  62. };
  63. struct StackAttackInfo
  64. {
  65. const CStack *attacker;
  66. const CStack *defender;
  67. std::vector< const CStack *> secondaryDefender;
  68. SpellID spellEffect;
  69. BattleHex tile;
  70. bool indirectAttack;
  71. bool lucky;
  72. bool unlucky;
  73. bool deathBlow;
  74. bool lifeDrain;
  75. };
  76. /// Main class for battles, responsible for relaying information from server to various battle entities
  77. class BattleInterface
  78. {
  79. using AwaitingAnimationAction = std::function<void()>;
  80. struct AwaitingAnimationEvents {
  81. AwaitingAnimationAction action;
  82. EAnimationEvents event;
  83. bool eventState;
  84. };
  85. /// Conditional variables that are set depending on ongoing animations on the battlefield
  86. std::array< CondSh<bool>, static_cast<size_t>(EAnimationEvents::COUNT)> animationEvents;
  87. /// List of events that are waiting to be triggered
  88. std::vector<AwaitingAnimationEvents> awaitingEvents;
  89. /// used during tactics mode, points to the interface of player with higher tactics (can be either attacker or defender in hot-seat), valid onloy for human players
  90. std::shared_ptr<CPlayerInterface> tacticianInterface;
  91. /// attacker interface, not null if attacker is human in our vcmiclient
  92. std::shared_ptr<CPlayerInterface> attackerInt;
  93. /// defender interface, not null if attacker is human in our vcmiclient
  94. std::shared_ptr<CPlayerInterface> defenderInt;
  95. void onIntroSoundPlayed();
  96. public:
  97. /// copy of initial armies (for result window)
  98. const CCreatureSet *army1;
  99. const CCreatureSet *army2;
  100. /// ID of channel on which battle opening sound is playing, or -1 if none
  101. int battleIntroSoundChannel;
  102. std::shared_ptr<BattleWindow> windowObject;
  103. std::shared_ptr<BattleConsole> console;
  104. /// currently active player interface
  105. std::shared_ptr<CPlayerInterface> curInt;
  106. const CGHeroInstance *attackingHeroInstance;
  107. const CGHeroInstance *defendingHeroInstance;
  108. bool tacticsMode;
  109. std::unique_ptr<BattleProjectileController> projectilesController;
  110. std::unique_ptr<BattleSiegeController> siegeController;
  111. std::unique_ptr<BattleObstacleController> obstacleController;
  112. std::unique_ptr<BattleFieldController> fieldController;
  113. std::unique_ptr<BattleStacksController> stacksController;
  114. std::unique_ptr<BattleActionsController> actionsController;
  115. std::unique_ptr<BattleEffectsController> effectsController;
  116. std::shared_ptr<BattleHero> attackingHero;
  117. std::shared_ptr<BattleHero> defendingHero;
  118. static CondSh<BattleAction *> givenCommand; //data != nullptr if we have i.e. moved current unit
  119. bool makingTurn() const;
  120. int moveSoundHander; // sound handler used when moving a unit
  121. BattleInterface(const CCreatureSet *army1, const CCreatureSet *army2, const CGHeroInstance *hero1, const CGHeroInstance *hero2, std::shared_ptr<CPlayerInterface> att, std::shared_ptr<CPlayerInterface> defen, std::shared_ptr<CPlayerInterface> spectatorInt = nullptr);
  122. ~BattleInterface();
  123. void trySetActivePlayer( PlayerColor player ); // if in hotseat, will activate interface of chosen player
  124. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  125. void requestAutofightingAIToTakeAction();
  126. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  127. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  128. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  129. void showInterface(SDL_Surface * to);
  130. void setHeroAnimation(ui8 side, EHeroAnimType phase);
  131. void executeSpellCast(); //called when a hero casts a spell
  132. void appendBattleLog(const std::string & newEntry);
  133. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  134. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  135. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  136. void setAnimSpeed(int set); //speed of animation; range 1..100
  137. int getAnimSpeed() const; //speed of animation; range 1..100
  138. CPlayerInterface *getCurrentPlayerInterface() const;
  139. void tacticNextStack(const CStack *current);
  140. void tacticPhaseEnd();
  141. /// sets condition to targeted state and executes any awaiting actions
  142. void setAnimationCondition( EAnimationEvents event, bool state);
  143. /// returns current state of condition
  144. bool getAnimationCondition( EAnimationEvents event);
  145. /// locks execution until selected condition reached targeted state
  146. void waitForAnimationCondition( EAnimationEvents event, bool state);
  147. /// adds action that will be executed one selected condition reached targeted state
  148. void executeOnAnimationCondition( EAnimationEvents event, bool state, const AwaitingAnimationAction & action);
  149. //call-ins
  150. void startAction(const BattleAction* action);
  151. void stackReset(const CStack * stack);
  152. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  153. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  154. void stackActivated(const CStack *stack); //active stack has been changed
  155. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport); //stack with id number moved to destHex
  156. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  157. void stackAttacking(const StackAttackInfo & attackInfo); //called when stack with id ID is attacking something on hex dest
  158. void newRoundFirst( int round );
  159. void newRound(int number); //caled when round is ended; number is the number of round
  160. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  161. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  162. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  163. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  164. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  165. void displayBattleLog(const std::vector<MetaString> & battleLog);
  166. void displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit);
  167. void displaySpellCast(const CSpell * spell, BattleHex destinationTile); //displays spell`s cast animation
  168. void displaySpellEffect(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  169. void displaySpellHit(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  170. void endAction(const BattleAction* action);
  171. void obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi);
  172. void gateStateChanged(const EGateState state);
  173. const CGHeroInstance *currentHero() const;
  174. InfoAboutHero enemyHero() const;
  175. };