BattleInterface.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. class ObstacleChanges;
  30. VCMI_LIB_NAMESPACE_END
  31. class BattleHero;
  32. class Canvas;
  33. class BattleResultWindow;
  34. class StackQueue;
  35. class CPlayerInterface;
  36. class CAnimation;
  37. struct BattleEffect;
  38. class IImage;
  39. class StackQueue;
  40. class BattleProjectileController;
  41. class BattleSiegeController;
  42. class BattleObstacleController;
  43. class BattleFieldController;
  44. class BattleRenderer;
  45. class BattleWindow;
  46. class BattleStacksController;
  47. class BattleActionsController;
  48. class BattleEffectsController;
  49. class BattleConsole;
  50. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  51. struct StackAttackedInfo
  52. {
  53. const CStack *defender;
  54. const CStack *attacker;
  55. int64_t damageDealt;
  56. uint32_t amountKilled;
  57. SpellID spellEffect;
  58. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  59. bool killed; //if true, stack has been killed
  60. bool rebirth; //if true, play rebirth animation after all
  61. bool cloneKilled;
  62. bool fireShield;
  63. };
  64. struct StackAttackInfo
  65. {
  66. const CStack *attacker;
  67. const CStack *defender;
  68. std::vector< const CStack *> secondaryDefender;
  69. SpellID spellEffect;
  70. BattleHex tile;
  71. bool indirectAttack;
  72. bool lucky;
  73. bool unlucky;
  74. bool deathBlow;
  75. bool lifeDrain;
  76. };
  77. /// Main class for battles, responsible for relaying information from server to various battle entities
  78. class BattleInterface
  79. {
  80. using AwaitingAnimationAction = std::function<void()>;
  81. struct AwaitingAnimationEvents {
  82. AwaitingAnimationAction action;
  83. EAnimationEvents event;
  84. };
  85. /// Conditional variables that are set depending on ongoing animations on the battlefield
  86. CondSh<bool> ongoingAnimationsState;
  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. /// if set to true, battle is still starting and waiting for intro sound to end / key press from player
  96. bool battleOpeningDelayActive;
  97. void playIntroSoundAndUnlockInterface();
  98. void onIntroSoundPlayed();
  99. public:
  100. /// copy of initial armies (for result window)
  101. const CCreatureSet *army1;
  102. const CCreatureSet *army2;
  103. std::shared_ptr<BattleWindow> windowObject;
  104. std::shared_ptr<BattleConsole> console;
  105. /// currently active player interface
  106. std::shared_ptr<CPlayerInterface> curInt;
  107. const CGHeroInstance *attackingHeroInstance;
  108. const CGHeroInstance *defendingHeroInstance;
  109. bool tacticsMode;
  110. std::unique_ptr<BattleProjectileController> projectilesController;
  111. std::unique_ptr<BattleSiegeController> siegeController;
  112. std::unique_ptr<BattleObstacleController> obstacleController;
  113. std::unique_ptr<BattleFieldController> fieldController;
  114. std::unique_ptr<BattleStacksController> stacksController;
  115. std::unique_ptr<BattleActionsController> actionsController;
  116. std::unique_ptr<BattleEffectsController> effectsController;
  117. std::shared_ptr<BattleHero> attackingHero;
  118. std::shared_ptr<BattleHero> defendingHero;
  119. static CondSh<BattleAction *> givenCommand; //data != nullptr if we have i.e. moved current unit
  120. bool openingPlaying();
  121. void openingEnd();
  122. bool makingTurn() const;
  123. 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);
  124. ~BattleInterface();
  125. void trySetActivePlayer( PlayerColor player ); // if in hotseat, will activate interface of chosen player
  126. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  127. void requestAutofightingAIToTakeAction();
  128. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  129. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  130. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  131. void showInterface(SDL_Surface * to);
  132. void setHeroAnimation(ui8 side, EHeroAnimType phase);
  133. void executeSpellCast(); //called when a hero casts a spell
  134. void appendBattleLog(const std::string & newEntry);
  135. void redrawBattlefield(); //refresh GUI after changing stack range / grid settings
  136. CPlayerInterface *getCurrentPlayerInterface() const;
  137. void tacticNextStack(const CStack *current);
  138. void tacticPhaseEnd();
  139. void setBattleQueueVisibility(bool visible);
  140. void executeStagedAnimations();
  141. void executeAnimationStage( EAnimationEvents event);
  142. void onAnimationsStarted();
  143. void onAnimationsFinished();
  144. void waitForAnimations();
  145. bool hasAnimations();
  146. void checkForAnimations();
  147. void addToAnimationStage( EAnimationEvents event, const AwaitingAnimationAction & action);
  148. //call-ins
  149. void startAction(const BattleAction* action);
  150. void stackReset(const CStack * stack);
  151. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  152. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  153. void stackActivated(const CStack *stack); //active stack has been changed
  154. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport); //stack with id number moved to destHex
  155. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  156. void stackAttacking(const StackAttackInfo & attackInfo); //called when stack with id ID is attacking something on hex dest
  157. void newRoundFirst( int round );
  158. void newRound(int number); //caled when round is ended; number is the number of round
  159. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  160. void battleFinished(const BattleResult& br, QueryID queryID); //called when battle is finished - battleresult window should be printed
  161. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  162. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  163. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  164. void displayBattleLog(const std::vector<MetaString> & battleLog);
  165. void displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit);
  166. void displaySpellCast(const CSpell * spell, BattleHex destinationTile); //displays spell`s cast animation
  167. void displaySpellEffect(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  168. void displaySpellHit(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  169. void endAction(const BattleAction* action);
  170. void obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi);
  171. void obstacleRemoved(const std::vector<ObstacleChanges> & obstacles);
  172. void gateStateChanged(const EGateState state);
  173. const CGHeroInstance *currentHero() const;
  174. InfoAboutHero enemyHero() const;
  175. };