BattleInterface.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. };
  84. /// Conditional variables that are set depending on ongoing animations on the battlefield
  85. CondSh<bool> ongoingAnimationsState;
  86. /// List of events that are waiting to be triggered
  87. std::vector<AwaitingAnimationEvents> awaitingEvents;
  88. /// 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
  89. std::shared_ptr<CPlayerInterface> tacticianInterface;
  90. /// attacker interface, not null if attacker is human in our vcmiclient
  91. std::shared_ptr<CPlayerInterface> attackerInt;
  92. /// defender interface, not null if attacker is human in our vcmiclient
  93. std::shared_ptr<CPlayerInterface> defenderInt;
  94. /// if set to true, battle is still starting and waiting for intro sound to end / key press from player
  95. bool battleOpeningDelayActive;
  96. void playIntroSoundAndUnlockInterface();
  97. void onIntroSoundPlayed();
  98. public:
  99. /// copy of initial armies (for result window)
  100. const CCreatureSet *army1;
  101. const CCreatureSet *army2;
  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 openingPlaying();
  120. void openingEnd();
  121. bool makingTurn() const;
  122. 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);
  123. ~BattleInterface();
  124. void trySetActivePlayer( PlayerColor player ); // if in hotseat, will activate interface of chosen player
  125. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  126. void requestAutofightingAIToTakeAction();
  127. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  128. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  129. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  130. void showInterface(SDL_Surface * to);
  131. void setHeroAnimation(ui8 side, EHeroAnimType phase);
  132. void executeSpellCast(); //called when a hero casts a spell
  133. void appendBattleLog(const std::string & newEntry);
  134. void redrawBattlefield(); //refresh GUI after changing stack range / grid settings
  135. CPlayerInterface *getCurrentPlayerInterface() const;
  136. void tacticNextStack(const CStack *current);
  137. void tacticPhaseEnd();
  138. void setBattleQueueVisibility(bool visible);
  139. void executeStagedAnimations();
  140. void executeAnimationStage( EAnimationEvents event);
  141. void onAnimationsStarted();
  142. void onAnimationsFinished();
  143. void waitForAnimations();
  144. bool hasAnimations();
  145. void checkForAnimations();
  146. void addToAnimationStage( EAnimationEvents event, const AwaitingAnimationAction & action);
  147. //call-ins
  148. void startAction(const BattleAction* action);
  149. void stackReset(const CStack * stack);
  150. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  151. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  152. void stackActivated(const CStack *stack); //active stack has been changed
  153. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport); //stack with id number moved to destHex
  154. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  155. void stackAttacking(const StackAttackInfo & attackInfo); //called when stack with id ID is attacking something on hex dest
  156. void newRoundFirst( int round );
  157. void newRound(int number); //caled when round is ended; number is the number of round
  158. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  159. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  160. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  161. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  162. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  163. void displayBattleLog(const std::vector<MetaString> & battleLog);
  164. void displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit);
  165. void displaySpellCast(const CSpell * spell, BattleHex destinationTile); //displays spell`s cast animation
  166. void displaySpellEffect(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  167. void displaySpellHit(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  168. void endAction(const BattleAction* action);
  169. void obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi);
  170. void gateStateChanged(const EGateState state);
  171. const CGHeroInstance *currentHero() const;
  172. InfoAboutHero enemyHero() const;
  173. };