BattleInterface.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. struct CustomEffectInfo;
  30. VCMI_LIB_NAMESPACE_END
  31. class BattleHero;
  32. class Canvas;
  33. class BattleResultWindow;
  34. class StackQueue;
  35. class CPlayerInterface;
  36. class ClickableHex;
  37. class CAnimation;
  38. struct BattleEffect;
  39. class IImage;
  40. class StackQueue;
  41. class BattleProjectileController;
  42. class BattleSiegeController;
  43. class BattleObstacleController;
  44. class BattleFieldController;
  45. class BattleRenderer;
  46. class BattleControlPanel;
  47. class BattleStacksController;
  48. class BattleActionsController;
  49. class BattleEffectsController;
  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. EBattleEffect::EBattleEffect battleEffect;
  58. SpellID spellEffect;
  59. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  60. bool killed; //if true, stack has been killed
  61. bool rebirth; //if true, play rebirth animation after all
  62. bool cloneKilled;
  63. };
  64. struct StackAttackInfo
  65. {
  66. const CStack *attacker;
  67. const CStack *defender;
  68. std::vector< const CStack *> secondaryDefender;
  69. //EBattleEffect::EBattleEffect battleEffect;
  70. SpellID spellEffect;
  71. BattleHex tile;
  72. bool indirectAttack;
  73. bool lucky;
  74. bool unlucky;
  75. bool deathBlow;
  76. bool lifeDrain;
  77. };
  78. /// Big class which handles the overall battle interface actions and it is also responsible for
  79. /// drawing everything correctly.
  80. class BattleInterface : public WindowBase
  81. {
  82. private:
  83. std::shared_ptr<BattleHero> attackingHero;
  84. std::shared_ptr<BattleHero> defendingHero;
  85. std::shared_ptr<StackQueue> queue;
  86. std::shared_ptr<BattleControlPanel> controlPanel;
  87. std::shared_ptr<CPlayerInterface> tacticianInterface; //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
  88. std::shared_ptr<CPlayerInterface> attackerInt, defenderInt; //because LOCPLINT is not enough in hotSeat
  89. std::shared_ptr<CPlayerInterface> curInt; //current player interface
  90. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  91. const CGHeroInstance *attackingHeroInstance, *defendingHeroInstance;
  92. ui8 animCount;
  93. bool tacticsMode;
  94. int battleIntroSoundChannel; //required as variable for disabling it via ESC key
  95. using AwaitingAnimationAction = std::function<void()>;
  96. struct AwaitingAnimationEvents {
  97. AwaitingAnimationAction action;
  98. EAnimationEvents event;
  99. bool eventState;
  100. };
  101. /// Conditional variables that are set depending on ongoing animations on the battlefield
  102. std::array< CondSh<bool>, static_cast<size_t>(EAnimationEvents::COUNT)> animationEvents;
  103. /// List of events that are waiting to be triggered
  104. std::vector<AwaitingAnimationEvents> awaitingEvents;
  105. void trySetActivePlayer( PlayerColor player ); // if in hotseat, will activate interface of chosen player
  106. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  107. void requestAutofightingAIToTakeAction();
  108. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  109. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  110. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  111. void showInterface(SDL_Surface * to);
  112. void setHeroAnimation(ui8 side, EHeroAnimType phase);
  113. void executeSpellCast(); //called when a hero casts a spell
  114. public:
  115. std::unique_ptr<BattleProjectileController> projectilesController;
  116. std::unique_ptr<BattleSiegeController> siegeController;
  117. std::unique_ptr<BattleObstacleController> obstacleController;
  118. std::unique_ptr<BattleFieldController> fieldController;
  119. std::unique_ptr<BattleStacksController> stacksController;
  120. std::unique_ptr<BattleActionsController> actionsController;
  121. std::unique_ptr<BattleEffectsController> effectsController;
  122. static CondSh<BattleAction *> givenCommand; //data != nullptr if we have i.e. moved current unit
  123. bool myTurn; //if true, interface is active (commands can be ordered)
  124. int moveSoundHander; // sound handler used when moving a unit
  125. const BattleResult *bresult; //result of a battle; if non-zero then display when all animations end
  126. BattleInterface(const CCreatureSet *army1, const CCreatureSet *army2, const CGHeroInstance *hero1, const CGHeroInstance *hero2, const SDL_Rect & myRect, std::shared_ptr<CPlayerInterface> att, std::shared_ptr<CPlayerInterface> defen, std::shared_ptr<CPlayerInterface> spectatorInt = nullptr);
  127. virtual ~BattleInterface();
  128. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  129. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  130. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  131. void setAnimSpeed(int set); //speed of animation; range 1..100
  132. int getAnimSpeed() const; //speed of animation; range 1..100
  133. CPlayerInterface *getCurrentPlayerInterface() const;
  134. void tacticNextStack(const CStack *current);
  135. void tacticPhaseEnd();
  136. /// sets condition to targeted state and executes any awaiting actions
  137. void setAnimationCondition( EAnimationEvents event, bool state);
  138. /// returns current state of condition
  139. bool getAnimationCondition( EAnimationEvents event);
  140. /// locks execution until selected condition reached targeted state
  141. void waitForAnimationCondition( EAnimationEvents event, bool state);
  142. /// adds action that will be executed one selected condition reached targeted state
  143. void executeOnAnimationCondition( EAnimationEvents event, bool state, const AwaitingAnimationAction & action);
  144. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  145. void activate() override;
  146. void deactivate() override;
  147. void keyPressed(const SDL_KeyboardEvent & key) override;
  148. void mouseMoved(const SDL_MouseMotionEvent &sEvent) override;
  149. void clickRight(tribool down, bool previousState) override;
  150. void show(SDL_Surface *to) override;
  151. void showAll(SDL_Surface *to) override;
  152. void collectRenderableObjects(BattleRenderer & renderer);
  153. //call-ins
  154. void startAction(const BattleAction* action);
  155. void stackReset(const CStack * stack);
  156. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  157. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  158. void stackActivated(const CStack *stack); //active stack has been changed
  159. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  160. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  161. void stackAttacking(const StackAttackInfo & attackInfo); //called when stack with id ID is attacking something on hex dest
  162. void newRoundFirst( int round );
  163. void newRound(int number); //caled when round is ended; number is the number of round
  164. void hexLclicked(int whichOne); //hex only call-in
  165. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  166. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  167. void displayBattleFinished(); //displays battle result
  168. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  169. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  170. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  171. void displayBattleLog(const std::vector<MetaString> & battleLog);
  172. void displaySpellAnimationQueue(const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit);
  173. void displaySpellCast(SpellID spellID, BattleHex destinationTile); //displays spell`s cast animation
  174. void displaySpellEffect(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  175. void displaySpellHit(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  176. void endAction(const BattleAction* action);
  177. void hideQueue();
  178. void showQueue();
  179. void obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi);
  180. void gateStateChanged(const EGateState state);
  181. const CGHeroInstance *currentHero() const;
  182. InfoAboutHero enemyHero() const;
  183. // TODO: cleanup this list
  184. friend class CPlayerInterface;
  185. friend class CInGameConsole;
  186. friend class StackQueue;
  187. friend class BattleResultWindow;
  188. friend class BattleHero;
  189. friend class CBattleStackAnimation;
  190. friend class CReverseAnimation;
  191. friend class CDefenceAnimation;
  192. friend class CMovementAnimation;
  193. friend class CMovementStartAnimation;
  194. friend class CAttackAnimation;
  195. friend class CMeleeAttackAnimation;
  196. friend class CShootingAnimation;
  197. friend class CCastAnimation;
  198. friend class ClickableHex;
  199. friend class BattleProjectileController;
  200. friend class BattleSiegeController;
  201. friend class BattleObstacleController;
  202. friend class BattleFieldController;
  203. friend class BattleControlPanel;
  204. friend class BattleStacksController;
  205. friend class BattleActionsController;
  206. friend class BattleEffectsController;
  207. };