CBattleInterface.h 17 KB

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