BattleInterface.h 8.5 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 ClickableHex;
  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. bool eventState;
  85. };
  86. /// Conditional variables that are set depending on ongoing animations on the battlefield
  87. std::array< CondSh<bool>, static_cast<size_t>(EAnimationEvents::COUNT)> animationEvents;
  88. /// List of events that are waiting to be triggered
  89. std::vector<AwaitingAnimationEvents> awaitingEvents;
  90. /// 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
  91. std::shared_ptr<CPlayerInterface> tacticianInterface;
  92. /// attacker interface, not null if attacker is human in our vcmiclient
  93. std::shared_ptr<CPlayerInterface> attackerInt;
  94. /// defender interface, not null if attacker is human in our vcmiclient
  95. std::shared_ptr<CPlayerInterface> defenderInt;
  96. void onIntroSoundPlayed();
  97. public:
  98. /// copy of initial armies (for result window)
  99. const CCreatureSet *army1;
  100. const CCreatureSet *army2;
  101. /// ID of channel on which battle opening sound is playing, or -1 if none
  102. int battleIntroSoundChannel;
  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 myTurn; //if true, interface is active (commands can be ordered)
  121. int moveSoundHander; // sound handler used when moving a unit
  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 setPrintCellBorders(bool set); //if true, cell borders will be printed
  135. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  136. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  137. void setAnimSpeed(int set); //speed of animation; range 1..100
  138. int getAnimSpeed() const; //speed of animation; range 1..100
  139. CPlayerInterface *getCurrentPlayerInterface() const;
  140. void tacticNextStack(const CStack *current);
  141. void tacticPhaseEnd();
  142. /// sets condition to targeted state and executes any awaiting actions
  143. void setAnimationCondition( EAnimationEvents event, bool state);
  144. /// returns current state of condition
  145. bool getAnimationCondition( EAnimationEvents event);
  146. /// locks execution until selected condition reached targeted state
  147. void waitForAnimationCondition( EAnimationEvents event, bool state);
  148. /// adds action that will be executed one selected condition reached targeted state
  149. void executeOnAnimationCondition( EAnimationEvents event, bool state, const AwaitingAnimationAction & action);
  150. //call-ins
  151. void startAction(const BattleAction* action);
  152. void stackReset(const CStack * stack);
  153. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  154. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  155. void stackActivated(const CStack *stack); //active stack has been changed
  156. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport); //stack with id number moved to destHex
  157. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  158. void stackAttacking(const StackAttackInfo & attackInfo); //called when stack with id ID is attacking something on hex dest
  159. void newRoundFirst( int round );
  160. void newRound(int number); //caled when round is ended; number is the number of round
  161. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  162. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  163. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  164. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  165. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  166. void displayBattleLog(const std::vector<MetaString> & battleLog);
  167. void displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit);
  168. void displaySpellCast(const CSpell * spell, BattleHex destinationTile); //displays spell`s cast animation
  169. void displaySpellEffect(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  170. void displaySpellHit(const CSpell * spell, BattleHex destinationTile); //displays spell`s affected animation
  171. void endAction(const BattleAction* action);
  172. void obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi);
  173. void gateStateChanged(const EGateState state);
  174. const CGHeroInstance *currentHero() const;
  175. InfoAboutHero enemyHero() const;
  176. };