CBattleInterface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * CBattleInterface.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 <vcmi/spells/Magic.h>
  12. #include "../../lib/ConstTransitivePtr.h" //may be redundant
  13. #include "../../lib/GameConstants.h"
  14. #include "CBattleAnimations.h"
  15. #include "../../lib/spells/CSpellHandler.h" //CSpell::TAnimation
  16. #include "../../lib/CCreatureHandler.h"
  17. #include "../../lib/battle/CBattleInfoCallback.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CCreatureSet;
  20. class CGHeroInstance;
  21. class CStack;
  22. struct BattleResult;
  23. struct BattleSpellCast;
  24. struct CObstacleInstance;
  25. template <typename T> struct CondSh;
  26. struct SetStackEffect;
  27. class BattleAction;
  28. class CGTownInstance;
  29. struct CatapultAttack;
  30. struct BattleTriggerEffect;
  31. struct BattleHex;
  32. struct InfoAboutHero;
  33. class CBattleGameInterface;
  34. struct CustomEffectInfo;
  35. class CSpell;
  36. VCMI_LIB_NAMESPACE_END
  37. class CLabel;
  38. class CCallback;
  39. class CButton;
  40. class CToggleButton;
  41. class CToggleGroup;
  42. class CBattleAnimation;
  43. class CBattleHero;
  44. class CBattleConsole;
  45. class CBattleResultWindow;
  46. class CStackQueue;
  47. class CPlayerInterface;
  48. class CCreatureAnimation;
  49. class CClickableHex;
  50. class CAnimation;
  51. class IImage;
  52. class CBattleProjectileController;
  53. class CBattleSiegeController;
  54. class CBattleObstacleController;
  55. class CBattleFieldController;
  56. class CBattleControlPanel;
  57. class CBattleStacksController;
  58. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  59. struct StackAttackedInfo
  60. {
  61. const CStack *defender; //attacked stack
  62. int64_t dmg; //damage dealt
  63. unsigned int amountKilled; //how many creatures in stack has been killed
  64. const CStack *attacker; //attacking stack
  65. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  66. bool killed; //if true, stack has been killed
  67. bool rebirth; //if true, play rebirth animation after all
  68. bool cloneKilled;
  69. };
  70. /// Struct for battle effect animation e.g. morale, prayer, armageddon, bless,...
  71. struct BattleEffect
  72. {
  73. int x, y; //position on the screen
  74. float currentFrame;
  75. std::shared_ptr<CAnimation> animation;
  76. int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim
  77. BattleHex position; //Indicates if effect which hex the effect is drawn on
  78. };
  79. struct BattleObjectsByHex
  80. {
  81. typedef std::vector<int> TWallList;
  82. typedef std::vector<const CStack *> TStackList;
  83. typedef std::vector<const BattleEffect *> TEffectList;
  84. typedef std::vector<std::shared_ptr<const CObstacleInstance>> TObstacleList;
  85. struct HexData
  86. {
  87. TWallList walls;
  88. TStackList dead;
  89. TStackList alive;
  90. TEffectList effects;
  91. TObstacleList obstacles;
  92. };
  93. HexData beforeAll;
  94. HexData afterAll;
  95. std::array<HexData, GameConstants::BFIELD_SIZE> hex;
  96. };
  97. enum class MouseHoveredHexContext
  98. {
  99. UNOCCUPIED_HEX,
  100. OCCUPIED_HEX
  101. };
  102. /// Big class which handles the overall battle interface actions and it is also responsible for
  103. /// drawing everything correctly.
  104. class CBattleInterface : public WindowBase
  105. {
  106. private:
  107. std::shared_ptr<CBattleHero> attackingHero;
  108. std::shared_ptr<CBattleHero> defendingHero;
  109. std::shared_ptr<CStackQueue> queue;
  110. std::shared_ptr<CBattleControlPanel> controlPanel;
  111. 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
  112. std::shared_ptr<CPlayerInterface> attackerInt, defenderInt; //because LOCPLINT is not enough in hotSeat
  113. std::shared_ptr<CPlayerInterface> curInt; //current player interface
  114. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  115. const CGHeroInstance *attackingHeroInstance, *defendingHeroInstance;
  116. ui8 animCount;
  117. bool tacticsMode;
  118. bool creatureCasting; //if true, stack currently aims to cats a spell
  119. bool spellDestSelectMode; //if true, player is choosing destination for his spell - only for GUI / console
  120. std::shared_ptr<BattleAction> spellToCast; //spell for which player is choosing destination
  121. const CSpell *sp; //spell pointer for convenience
  122. std::vector<PossiblePlayerBattleAction> possibleActions; //all actions possible to call at the moment by player
  123. std::vector<PossiblePlayerBattleAction> localActions; //actions possible to take on hovered hex
  124. std::vector<PossiblePlayerBattleAction> illegalActions; //these actions display message in case of illegal target
  125. PossiblePlayerBattleAction currentAction; //action that will be performed on l-click
  126. PossiblePlayerBattleAction selectedAction; //last action chosen (and saved) by player
  127. PossiblePlayerBattleAction illegalAction; //most likely action that can't be performed here
  128. bool battleActionsStarted; //used for delaying battle actions until intro sound stops
  129. int battleIntroSoundChannel; //required as variable for disabling it via ESC key
  130. std::list<BattleEffect> battleEffects; //different animations to display on the screen like spell effects
  131. void trySetActivePlayer( PlayerColor player ); // if in hotseat, will activate interface of chosen player
  132. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  133. void requestAutofightingAIToTakeAction();
  134. std::vector<PossiblePlayerBattleAction> getPossibleActionsForStack (const CStack *stack); //called when stack gets its turn
  135. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or canceled)
  136. void reorderPossibleActionsPriority(const CStack * stack, MouseHoveredHexContext context);
  137. //force active stack to cast a spell if possible
  138. void enterCreatureCastingMode();
  139. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  140. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  141. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  142. void showInterface(SDL_Surface *to);
  143. void showBattlefieldObjects(SDL_Surface *to);
  144. void showBattleEffects(SDL_Surface *to, const std::vector<const BattleEffect *> &battleEffects);
  145. BattleObjectsByHex sortObjectsByHex();
  146. void setHeroAnimation(ui8 side, int phase);
  147. public:
  148. std::unique_ptr<CBattleProjectileController> projectilesController;
  149. std::unique_ptr<CBattleSiegeController> siegeController;
  150. std::unique_ptr<CBattleObstacleController> obstacleController;
  151. std::unique_ptr<CBattleFieldController> fieldController;
  152. std::unique_ptr<CBattleStacksController> stacksController;
  153. static CondSh<bool> animsAreDisplayed; //for waiting with the end of battle for end of anims
  154. static CondSh<BattleAction *> givenCommand; //data != nullptr if we have i.e. moved current unit
  155. bool myTurn; //if true, interface is active (commands can be ordered)
  156. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  157. int moveSoundHander; // sound handler used when moving a unit
  158. const BattleResult *bresult; //result of a battle; if non-zero then display when all animations end
  159. CBattleInterface(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);
  160. virtual ~CBattleInterface();
  161. //std::vector<TimeInterested*> timeinterested; //animation handling
  162. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  163. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  164. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  165. void setAnimSpeed(int set); //speed of animation; range 1..100
  166. int getAnimSpeed() const; //speed of animation; range 1..100
  167. CPlayerInterface *getCurrentPlayerInterface() const;
  168. void tacticNextStack(const CStack *current);
  169. void tacticPhaseEnd();
  170. void waitForAnims();
  171. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  172. void activate() override;
  173. void deactivate() override;
  174. void keyPressed(const SDL_KeyboardEvent & key) override;
  175. void mouseMoved(const SDL_MouseMotionEvent &sEvent) override;
  176. void clickRight(tribool down, bool previousState) override;
  177. void show(SDL_Surface *to) override;
  178. void showAll(SDL_Surface *to) override;
  179. //call-ins
  180. void startAction(const BattleAction* action);
  181. void stackReset(const CStack * stack);
  182. void stackAdded(const CStack * stack); //new stack appeared on battlefield
  183. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  184. void stackActivated(const CStack *stack); //active stack has been changed
  185. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  186. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  187. void stackAttacking(const CStack *attacker, BattleHex dest, const CStack *attacked, bool shooting); //called when stack with id ID is attacking something on hex dest
  188. void newRoundFirst( int round );
  189. void newRound(int number); //caled when round is ended; number is the number of round
  190. void hexLclicked(int whichOne); //hex only call-in
  191. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  192. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  193. void displayBattleFinished(); //displays battle result
  194. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  195. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  196. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  197. void displayBattleLog(const std::vector<MetaString> & battleLog);
  198. void displayCustomEffects(const std::vector<CustomEffectInfo> & customEffects);
  199. void displayEffect(ui32 effect, BattleHex destTile); //displays custom effect on the battlefield
  200. void displaySpellAnimationQueue(const CSpell::TAnimationQueue & q, BattleHex destinationTile);
  201. void displaySpellCast(SpellID spellID, BattleHex destinationTile); //displays spell`s cast animation
  202. void displaySpellEffect(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  203. void displaySpellHit(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  204. void battleTriggerEffect(const BattleTriggerEffect & bte);
  205. void endAction(const BattleAction* action);
  206. void hideQueue();
  207. void showQueue();
  208. void handleHex(BattleHex myNumber, int eventType);
  209. bool isCastingPossibleHere (const CStack *sactive, const CStack *shere, BattleHex myNumber);
  210. bool canStackMoveHere (const CStack *sactive, BattleHex MyNumber); //TODO: move to BattleState / callback
  211. void obstaclePlaced(const CObstacleInstance & oi);
  212. void gateStateChanged(const EGateState state);
  213. const CGHeroInstance *currentHero() const;
  214. InfoAboutHero enemyHero() const;
  215. friend class CPlayerInterface;
  216. friend class CButton;
  217. friend class CInGameConsole;
  218. friend class CBattleResultWindow;
  219. friend class CBattleHero;
  220. friend class CEffectAnimation;
  221. friend class CBattleStackAnimation;
  222. friend class CReverseAnimation;
  223. friend class CDefenceAnimation;
  224. friend class CMovementAnimation;
  225. friend class CMovementStartAnimation;
  226. friend class CAttackAnimation;
  227. friend class CMeleeAttackAnimation;
  228. friend class CShootingAnimation;
  229. friend class CCastAnimation;
  230. friend class CClickableHex;
  231. friend class CBattleProjectileController;
  232. friend class CBattleSiegeController;
  233. friend class CBattleObstacleController;
  234. friend class CBattleFieldController;
  235. friend class CBattleControlPanel;
  236. friend class CBattleStacksController;
  237. };