CBattleInterface.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/battle/CBattleInfoCallback.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CCreatureSet;
  19. class CGHeroInstance;
  20. class CStack;
  21. struct BattleResult;
  22. struct BattleSpellCast;
  23. struct CObstacleInstance;
  24. template <typename T> struct CondSh;
  25. struct SetStackEffect;
  26. class BattleAction;
  27. class CGTownInstance;
  28. struct CatapultAttack;
  29. struct BattleTriggerEffect;
  30. struct BattleHex;
  31. struct InfoAboutHero;
  32. class CBattleGameInterface;
  33. struct CustomEffectInfo;
  34. class CSpell;
  35. VCMI_LIB_NAMESPACE_END
  36. class CLabel;
  37. class CCallback;
  38. class CButton;
  39. class CToggleButton;
  40. class CToggleGroup;
  41. struct CatapultProjectileInfo;
  42. class CBattleAnimation;
  43. class CBattleHero;
  44. class CBattleConsole;
  45. class CBattleResultWindow;
  46. class CStackQueue;
  47. class CPlayerInterface;
  48. class CCreatureAnimation;
  49. struct ProjectileInfo;
  50. class CClickableHex;
  51. class CAnimation;
  52. class IImage;
  53. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  54. struct StackAttackedInfo
  55. {
  56. const CStack *defender; //attacked stack
  57. int64_t dmg; //damage dealt
  58. unsigned int amountKilled; //how many creatures in stack has been killed
  59. const CStack *attacker; //attacking stack
  60. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  61. bool killed; //if true, stack has been killed
  62. bool rebirth; //if true, play rebirth animation after all
  63. bool cloneKilled;
  64. };
  65. /// Struct for battle effect animation e.g. morale, prayer, armageddon, bless,...
  66. struct BattleEffect
  67. {
  68. int x, y; //position on the screen
  69. float currentFrame;
  70. std::shared_ptr<CAnimation> animation;
  71. int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim
  72. BattleHex position; //Indicates if effect which hex the effect is drawn on
  73. };
  74. struct BattleObjectsByHex
  75. {
  76. typedef std::vector<int> TWallList;
  77. typedef std::vector<const CStack *> TStackList;
  78. typedef std::vector<const BattleEffect *> TEffectList;
  79. typedef std::vector<std::shared_ptr<const CObstacleInstance>> TObstacleList;
  80. struct HexData
  81. {
  82. TWallList walls;
  83. TStackList dead;
  84. TStackList alive;
  85. TEffectList effects;
  86. TObstacleList obstacles;
  87. };
  88. HexData beforeAll;
  89. HexData afterAll;
  90. std::array<HexData, GameConstants::BFIELD_SIZE> hex;
  91. };
  92. /// Small struct which is needed for drawing the parabolic trajectory of the catapult cannon
  93. struct CatapultProjectileInfo
  94. {
  95. CatapultProjectileInfo(Point from, Point dest);
  96. double facA, facB, facC;
  97. double calculateY(double x);
  98. };
  99. enum class MouseHoveredHexContext
  100. {
  101. UNOCCUPIED_HEX,
  102. OCCUPIED_HEX
  103. };
  104. /// Big class which handles the overall battle interface actions and it is also responsible for
  105. /// drawing everything correctly.
  106. class CBattleInterface : public WindowBase
  107. {
  108. private:
  109. SDL_Surface *background, *menu, *amountNormal, *amountNegative, *amountPositive, *amountEffNeutral, *cellBorders, *backgroundWithHexes;
  110. std::shared_ptr<CButton> bOptions;
  111. std::shared_ptr<CButton> bSurrender;
  112. std::shared_ptr<CButton> bFlee;
  113. std::shared_ptr<CButton> bAutofight;
  114. std::shared_ptr<CButton> bSpell;
  115. std::shared_ptr<CButton> bWait;
  116. std::shared_ptr<CButton> bDefence;
  117. std::shared_ptr<CButton> bConsoleUp;
  118. std::shared_ptr<CButton> bConsoleDown;
  119. std::shared_ptr<CButton> btactNext;
  120. std::shared_ptr<CButton> btactEnd;
  121. std::shared_ptr<CBattleConsole> console;
  122. std::shared_ptr<CBattleHero> attackingHero;
  123. std::shared_ptr<CBattleHero> defendingHero;
  124. std::shared_ptr<CStackQueue> queue;
  125. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  126. const CGHeroInstance *attackingHeroInstance, *defendingHeroInstance;
  127. std::map<int32_t, std::shared_ptr<CCreatureAnimation>> creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  128. std::map<int, std::shared_ptr<CAnimation>> idToProjectile;
  129. std::map<std::string, std::shared_ptr<CAnimation>> animationsCache;
  130. std::map<si32, std::shared_ptr<CAnimation>> obstacleAnimations;
  131. std::map<int, bool> creDir; // <creatureID, if false reverse creature's animation> //TODO: move it to battle callback
  132. ui8 animCount;
  133. const CStack *activeStack; //number of active stack; nullptr - no one
  134. const CStack *mouseHoveredStack; // stack below mouse pointer, used for border animation
  135. const CStack *stackToActivate; //when animation is playing, we should wait till the end to make the next stack active; nullptr of none
  136. const CStack *selectedStack; //for Teleport / Sacrifice
  137. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  138. std::vector<BattleHex> occupyableHexes, //hexes available for active stack
  139. attackableHexes; //hexes attackable by active stack
  140. std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes; // hexes that when in front of a unit cause it's amount box to move back
  141. BattleHex previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  142. BattleHex currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  143. int attackingHex; //hex from which the stack would perform attack with current cursor
  144. 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
  145. bool tacticsMode;
  146. bool stackCanCastSpell; //if true, active stack could possibly cast some target spell
  147. bool creatureCasting; //if true, stack currently aims to cats a spell
  148. bool spellDestSelectMode; //if true, player is choosing destination for his spell - only for GUI / console
  149. std::shared_ptr<BattleAction> spellToCast; //spell for which player is choosing destination
  150. const CSpell *sp; //spell pointer for convenience
  151. si32 creatureSpellToCast;
  152. std::vector<PossiblePlayerBattleAction> possibleActions; //all actions possible to call at the moment by player
  153. std::vector<PossiblePlayerBattleAction> localActions; //actions possible to take on hovered hex
  154. std::vector<PossiblePlayerBattleAction> illegalActions; //these actions display message in case of illegal target
  155. PossiblePlayerBattleAction currentAction; //action that will be performed on l-click
  156. PossiblePlayerBattleAction selectedAction; //last action chosen (and saved) by player
  157. PossiblePlayerBattleAction illegalAction; //most likely action that can't be performed here
  158. bool battleActionsStarted; //used for delaying battle actions until intro sound stops
  159. int battleIntroSoundChannel; //required as variable for disabling it via ESC key
  160. void setActiveStack(const CStack *stack);
  161. void setHoveredStack(const CStack *stack);
  162. void requestAutofightingAIToTakeAction();
  163. std::vector<PossiblePlayerBattleAction> getPossibleActionsForStack (const CStack *stack); //called when stack gets its turn
  164. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or canceled)
  165. void reorderPossibleActionsPriority(const CStack * stack, MouseHoveredHexContext context);
  166. //force active stack to cast a spell if possible
  167. void enterCreatureCastingMode();
  168. std::list<ProjectileInfo> projectiles; //projectiles flying on battlefield
  169. void giveCommand(EActionType action, BattleHex tile = BattleHex(), si32 additional = -1);
  170. void sendCommand(BattleAction *& command, const CStack * actor = nullptr);
  171. bool isTileAttackable(const BattleHex & number) const; //returns true if tile 'number' is neighboring any tile from active stack's range or is one of these tiles
  172. bool isCatapultAttackable(BattleHex hex) const; //returns true if given tile can be attacked by catapult
  173. std::list<BattleEffect> battleEffects; //different animations to display on the screen like spell effects
  174. /// Class which is responsible for drawing the wall of a siege during battle
  175. class SiegeHelper
  176. {
  177. private:
  178. SDL_Surface* walls[18];
  179. const CBattleInterface *owner;
  180. public:
  181. const CGTownInstance *town; //besieged town
  182. SiegeHelper(const CGTownInstance *siegeTown, const CBattleInterface *_owner);
  183. ~SiegeHelper();
  184. std::string getSiegeName(ui16 what) const;
  185. std::string getSiegeName(ui16 what, int state) const; // state uses EWallState enum
  186. void printPartOfWall(SDL_Surface *to, int what);
  187. enum EWallVisual
  188. {
  189. BACKGROUND = 0,
  190. BACKGROUND_WALL = 1,
  191. KEEP,
  192. BOTTOM_TOWER,
  193. BOTTOM_WALL,
  194. WALL_BELLOW_GATE,
  195. WALL_OVER_GATE,
  196. UPPER_WALL,
  197. UPPER_TOWER,
  198. GATE,
  199. GATE_ARCH,
  200. BOTTOM_STATIC_WALL,
  201. UPPER_STATIC_WALL,
  202. MOAT,
  203. BACKGROUND_MOAT,
  204. KEEP_BATTLEMENT,
  205. BOTTOM_BATTLEMENT,
  206. UPPER_BATTLEMENT
  207. };
  208. friend class CBattleInterface;
  209. } *siegeH;
  210. std::shared_ptr<CPlayerInterface> attackerInt, defenderInt; //because LOCPLINT is not enough in hotSeat
  211. std::shared_ptr<CPlayerInterface> curInt; //current player interface
  212. const CGHeroInstance *getActiveHero(); //returns hero that can currently cast a spell
  213. /** Methods for displaying battle screen */
  214. void showBackground(SDL_Surface *to);
  215. void showBackgroundImage(SDL_Surface *to);
  216. void showAbsoluteObstacles(SDL_Surface *to);
  217. void showHighlightedHexes(SDL_Surface *to);
  218. void showHighlightedHex(SDL_Surface *to, BattleHex hex, bool darkBorder = false);
  219. void showInterface(SDL_Surface *to);
  220. void showBattlefieldObjects(SDL_Surface *to);
  221. void showAliveStacks(SDL_Surface *to, std::vector<const CStack *> stacks);
  222. void showStacks(SDL_Surface *to, std::vector<const CStack *> stacks);
  223. void showObstacles(SDL_Surface *to, std::vector<std::shared_ptr<const CObstacleInstance>> &obstacles);
  224. void showPiecesOfWall(SDL_Surface *to, std::vector<int> pieces);
  225. void showBattleEffects(SDL_Surface *to, const std::vector<const BattleEffect *> &battleEffects);
  226. void showProjectiles(SDL_Surface *to);
  227. BattleObjectsByHex sortObjectsByHex();
  228. void updateBattleAnimations();
  229. std::shared_ptr<IImage> getObstacleImage(const CObstacleInstance & oi);
  230. Point getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle);
  231. void redrawBackgroundWithHexes(const CStack *activeStack);
  232. /** End of battle screen blitting methods */
  233. void setHeroAnimation(ui8 side, int phase);
  234. public:
  235. static CondSh<bool> animsAreDisplayed; //for waiting with the end of battle for end of anims
  236. static CondSh<BattleAction *> givenCommand; //data != nullptr if we have i.e. moved current unit
  237. std::list<std::pair<CBattleAnimation *, bool>> pendingAnims; //currently displayed animations <anim, initialized>
  238. void addNewAnim(CBattleAnimation *anim); //adds new anim to pendingAnims
  239. ui32 animIDhelper; //for giving IDs for animations
  240. 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);
  241. virtual ~CBattleInterface();
  242. //std::vector<TimeInterested*> timeinterested; //animation handling
  243. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  244. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  245. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  246. void setAnimSpeed(int set); //speed of animation; range 1..100
  247. int getAnimSpeed() const; //speed of animation; range 1..100
  248. CPlayerInterface *getCurrentPlayerInterface() const;
  249. bool shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex);
  250. std::vector<std::shared_ptr<CClickableHex>> bfield; //11 lines, 17 hexes on each
  251. SDL_Surface *cellBorder, *cellShade;
  252. bool myTurn; //if true, interface is active (commands can be ordered)
  253. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  254. int moveSoundHander; // sound handler used when moving a unit
  255. const BattleResult *bresult; //result of a battle; if non-zero then display when all animations end
  256. // block all UI elements, e.g. during enemy turn
  257. // unlike activate/deactivate this method will correctly grey-out all elements
  258. void blockUI(bool on);
  259. //button handle funcs:
  260. void bOptionsf();
  261. void bSurrenderf();
  262. void bFleef();
  263. void reallyFlee(); //performs fleeing without asking player
  264. void reallySurrender(); //performs surrendering without asking player
  265. void bAutofightf();
  266. void bSpellf();
  267. void bWaitf();
  268. void bDefencef();
  269. void bConsoleUpf();
  270. void bConsoleDownf();
  271. void bTacticNextStack(const CStack *current = nullptr);
  272. void bEndTacticPhase();
  273. //end of button handle funcs
  274. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  275. void activate() override;
  276. void deactivate() override;
  277. void keyPressed(const SDL_KeyboardEvent & key) override;
  278. void mouseMoved(const SDL_MouseMotionEvent &sEvent) override;
  279. void clickRight(tribool down, bool previousState) override;
  280. void show(SDL_Surface *to) override;
  281. void showAll(SDL_Surface *to) override;
  282. //call-ins
  283. void startAction(const BattleAction* action);
  284. void unitAdded(const CStack * stack); //new stack appeared on battlefield
  285. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  286. void stackActivated(const CStack *stack); //active stack has been changed
  287. void stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  288. void waitForAnims();
  289. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  290. void stackAttacking(const CStack *attacker, BattleHex dest, const CStack *attacked, bool shooting); //called when stack with id ID is attacking something on hex dest
  291. void newRoundFirst( int round );
  292. void newRound(int number); //caled when round is ended; number is the number of round
  293. void hexLclicked(int whichOne); //hex only call-in
  294. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  295. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  296. void displayBattleFinished(); //displays battle result
  297. void spellCast(const BattleSpellCast *sc); //called when a hero casts a spell
  298. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  299. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  300. void displayBattleLog(const std::vector<MetaString> & battleLog);
  301. void displayCustomEffects(const std::vector<CustomEffectInfo> & customEffects);
  302. void displayEffect(ui32 effect, BattleHex destTile); //displays custom effect on the battlefield
  303. void displaySpellAnimationQueue(const CSpell::TAnimationQueue & q, BattleHex destinationTile);
  304. void displaySpellCast(SpellID spellID, BattleHex destinationTile); //displays spell`s cast animation
  305. void displaySpellEffect(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  306. void displaySpellHit(SpellID spellID, BattleHex destinationTile); //displays spell`s affected animation
  307. void battleTriggerEffect(const BattleTriggerEffect & bte);
  308. void setBattleCursor(const int myNumber); //really complex and messy, sets attackingHex
  309. void endAction(const BattleAction* action);
  310. void hideQueue();
  311. void showQueue();
  312. Rect hexPosition(BattleHex hex) const;
  313. void handleHex(BattleHex myNumber, int eventType);
  314. bool isCastingPossibleHere (const CStack *sactive, const CStack *shere, BattleHex myNumber);
  315. bool canStackMoveHere (const CStack *sactive, BattleHex MyNumber); //TODO: move to BattleState / callback
  316. BattleHex fromWhichHexAttack(BattleHex myNumber);
  317. void obstaclePlaced(const CObstacleInstance & oi);
  318. void gateStateChanged(const EGateState state);
  319. void initStackProjectile(const CStack * stack);
  320. const CGHeroInstance *currentHero() const;
  321. InfoAboutHero enemyHero() const;
  322. friend class CPlayerInterface;
  323. friend class CButton;
  324. friend class CInGameConsole;
  325. friend class CBattleResultWindow;
  326. friend class CBattleHero;
  327. friend class CEffectAnimation;
  328. friend class CBattleStackAnimation;
  329. friend class CReverseAnimation;
  330. friend class CDefenceAnimation;
  331. friend class CMovementAnimation;
  332. friend class CMovementStartAnimation;
  333. friend class CAttackAnimation;
  334. friend class CMeleeAttackAnimation;
  335. friend class CShootingAnimation;
  336. friend class CCastAnimation;
  337. friend class CClickableHex;
  338. };