CBattleInterface.h 17 KB

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