CBattleInterface.h 17 KB

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