CBattleInterface.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #pragma once
  2. #include "../../lib/ConstTransitivePtr.h" //may be reundant
  3. #include "../../lib/GameConstants.h"
  4. #include "CBattleAnimations.h"
  5. #include "../../lib/spells/CSpellHandler.h" //CSpell::TAnimation
  6. /*
  7. * CBattleInterface.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CLabel;
  16. class CCreatureSet;
  17. class CGHeroInstance;
  18. class CDefHandler;
  19. class CStack;
  20. class CCallback;
  21. class CButton;
  22. class CToggleButton;
  23. class CToggleGroup;
  24. struct BattleResult;
  25. struct BattleSpellCast;
  26. struct CObstacleInstance;
  27. template <typename T> struct CondSh;
  28. struct SetStackEffect;
  29. struct BattleAction;
  30. class CGTownInstance;
  31. struct CatapultAttack;
  32. struct CatapultProjectileInfo;
  33. struct BattleTriggerEffect;
  34. class CBattleAnimation;
  35. class CBattleHero;
  36. class CBattleConsole;
  37. class CBattleResultWindow;
  38. class CStackQueue;
  39. class CPlayerInterface;
  40. class CCreatureAnimation;
  41. struct ProjectileInfo;
  42. class CClickableHex;
  43. struct BattleHex;
  44. struct InfoAboutHero;
  45. struct BattleAction;
  46. class CBattleGameInterface;
  47. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  48. struct StackAttackedInfo
  49. {
  50. const CStack * defender; //attacked stack
  51. unsigned int dmg; //damage dealt
  52. unsigned int amountKilled; //how many creatures in stack has been killed
  53. const CStack * attacker; //attacking stack
  54. bool indirectAttack; //if true, stack was attacked indirectly - spell or ranged attack
  55. bool killed; //if true, stack has been killed
  56. bool rebirth; //if true, play rebirth animation after all
  57. bool cloneKilled;
  58. };
  59. /// Struct for battle effect animation e.g. morale, prayer, armageddon, bless,...
  60. struct BattleEffect
  61. {
  62. int x, y; //position on the screen
  63. float currentFrame;
  64. int maxFrame;
  65. CDefHandler * anim; //animation to display
  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<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, FRIENDLY_CREATURE_SPELL, HOSTILE_CREATURE_SPELL, RISING_SPELL, ANY_CREATURE, 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. };
  107. private:
  108. SDL_Surface * background, * menu, * amountNormal, * amountNegative, * amountPositive, * amountEffNeutral, * cellBorders, * backgroundWithHexes;
  109. CButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  110. * bWait, * bDefence, * bConsoleUp, * bConsoleDown, *btactNext, *btactEnd;
  111. CBattleConsole * console;
  112. CBattleHero * attackingHero, * defendingHero; //fighting heroes
  113. CStackQueue *queue;
  114. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  115. const CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  116. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  117. std::map< int, CDefHandler * > idToProjectile; //projectiles of creatures (creatureID, defhandler)
  118. std::map< int, CDefHandler * > idToObstacle; //obstacles located on the battlefield
  119. std::map< int, SDL_Surface * > idToAbsoluteObstacle; //obstacles located on the battlefield
  120. //TODO these should be loaded only when needed (and freed then) but I believe it's rather work for resource manager,
  121. //so I didn't implement that (having ongoing RM development)
  122. CDefHandler *landMine;
  123. CDefHandler *quicksand;
  124. CDefHandler *fireWall;
  125. CDefHandler *smallForceField[2], *bigForceField[2]; // [side]
  126. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation> //TODO: move it to battle callback
  127. ui8 animCount;
  128. const CStack * activeStack; //number of active stack; nullptr - no one
  129. const CStack * mouseHoveredStack; // stack below mouse pointer, used for border animation
  130. const CStack * stackToActivate; //when animation is playing, we should wait till the end to make the next stack active; nullptr of none
  131. const CStack * selectedStack; //for Teleport / Sacrifice
  132. void activateStack(); //sets activeStack to stackToActivate etc. //FIXME: No, it's not clear at all
  133. std::vector<BattleHex> occupyableHexes, //hexes available for active stack
  134. attackableHexes; //hexes attackable by active stack
  135. bool stackCountOutsideHexes[GameConstants::BFIELD_SIZE]; // hexes that when in front of a unit cause it's amount box to move back
  136. BattleHex previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  137. BattleHex currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  138. int attackingHex; //hex from which the stack would perform attack with current cursor
  139. 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
  140. bool tacticsMode;
  141. bool stackCanCastSpell; //if true, active stack could possibly cast some target spell
  142. bool creatureCasting; //if true, stack currently aims to cats a spell
  143. bool spellDestSelectMode; //if true, player is choosing destination for his spell - only for GUI / console
  144. PossibleActions spellSelMode;
  145. BattleAction * spellToCast; //spell for which player is choosing destination
  146. const CSpell * sp; //spell pointer for convenience
  147. si32 creatureSpellToCast;
  148. std::vector<PossibleActions> possibleActions; //all actions possible to call at the moment by player
  149. std::vector<PossibleActions> localActions; //actions possible to take on hovered hex
  150. std::vector<PossibleActions> illegalActions; //these actions display message in case of illegal target
  151. PossibleActions currentAction; //action that will be performed on l-click
  152. PossibleActions selectedAction; //last action chosen (and saved) by player
  153. PossibleActions illegalAction; //most likely action that can't be performed here
  154. void setActiveStack(const CStack * stack);
  155. void setHoveredStack(const CStack * stack);
  156. void requestAutofightingAIToTakeAction();
  157. void getPossibleActionsForStack (const CStack * stack); //called when stack gets its turn
  158. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or canceled)
  159. void printConsoleAttacked(const CStack * defender, int dmg, int killed, const CStack * attacker, bool Multiple);
  160. std::list<ProjectileInfo> projectiles; //projectiles flying on battlefield
  161. void giveCommand(Battle::ActionType action, BattleHex tile, ui32 stackID, si32 additional=-1, si32 selectedStack = -1);
  162. 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
  163. bool isCatapultAttackable(BattleHex hex) const; //returns true if given tile can be attacked by catapult
  164. std::list<BattleEffect> battleEffects; //different animations to display on the screen like spell effects
  165. /// Class which is responsible for drawing the wall of a siege during battle
  166. class SiegeHelper
  167. {
  168. private:
  169. SDL_Surface* walls[18];
  170. const CBattleInterface * owner;
  171. public:
  172. const CGTownInstance * town; //besieged town
  173. SiegeHelper(const CGTownInstance * siegeTown, const CBattleInterface * _owner); //c-tor
  174. ~SiegeHelper(); //d-tor
  175. //filename getters
  176. //what: 0 - background, 1 - background wall, 2 - keep, 3 - bottom tower, 4 - bottom wall,
  177. // 5 - below gate, 6 - over gate, 7 - upper wall, 8 - upper tower, 9 - gate,
  178. // 10 - gate arch, 11 - bottom static 12 - upper static, 13 - moat, 14 - moat background,
  179. // 15 - keep battlement, 16 - bottom battlement, 17 - upper battlement;
  180. // state uses EWallState enum
  181. std::string getSiegeName(ui16 what) const;
  182. std::string getSiegeName(ui16 what, int state) const;
  183. void printPartOfWall(SDL_Surface * to, int what);//what: 1 - background wall, 2 - keep, 3 - bottom tower, 4 - bottom wall, 5 - below gate, 6 - over gate, 7 - upper wall, 8 - uppert tower, 9 - gate, 10 - gate arch, 11 - bottom static wall, 12 - upper static wall, 15 - keep creature cover, 16 - bottom turret creature cover, 17 - upper turret creature cover
  184. friend class CBattleInterface;
  185. } * siegeH;
  186. shared_ptr<CPlayerInterface> attackerInt, defenderInt; //because LOCPLINT is not enough in hotSeat
  187. shared_ptr<CPlayerInterface> curInt; //current player interface
  188. const CGHeroInstance * getActiveHero(); //returns hero that can currently cast a spell
  189. /** Methods for displaying battle screen */
  190. void showBackground(SDL_Surface * to);
  191. void showBackgroundImage(SDL_Surface * to);
  192. void showAbsoluteObstacles(SDL_Surface * to);
  193. void showHighlightedHexes(SDL_Surface * to);
  194. void showHighlightedHex(SDL_Surface * to, BattleHex hex);
  195. void showInterface(SDL_Surface * to);
  196. void showBattlefieldObjects(SDL_Surface * to);
  197. void showAliveStacks(SDL_Surface * to, std::vector<const CStack *> stacks);
  198. void showStacks(SDL_Surface * to, std::vector<const CStack *> stacks);
  199. void showObstacles(SDL_Surface *to, std::vector<shared_ptr<const CObstacleInstance> > &obstacles);
  200. void showPiecesOfWall(SDL_Surface * to, std::vector<int> pieces);
  201. void showBattleEffects(SDL_Surface *to, const std::vector<const BattleEffect *> &battleEffects);
  202. void showProjectiles(SDL_Surface * to);
  203. BattleObjectsByHex sortObjectsByHex();
  204. void updateBattleAnimations();
  205. SDL_Surface *getObstacleImage(const CObstacleInstance &oi);
  206. Point getObstaclePosition(SDL_Surface *image, const CObstacleInstance &obstacle);
  207. void redrawBackgroundWithHexes(const CStack * activeStack);
  208. /** End of battle screen blitting methods */
  209. public:
  210. std::list<std::pair<CBattleAnimation *, bool> > pendingAnims; //currently displayed animations <anim, initialized>
  211. void addNewAnim(CBattleAnimation * anim); //adds new anim to pendingAnims
  212. ui32 animIDhelper; //for giving IDs for animations
  213. static CondSh<bool> animsAreDisplayed; //for waiting with the end of battle for end of anims
  214. CBattleInterface(const CCreatureSet * army1, const CCreatureSet * army2, const CGHeroInstance *hero1, const CGHeroInstance *hero2, const SDL_Rect & myRect, shared_ptr<CPlayerInterface> att, shared_ptr<CPlayerInterface> defen); //c-tor
  215. ~CBattleInterface(); //d-tor
  216. //std::vector<TimeInterested*> timeinterested; //animation handling
  217. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  218. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  219. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  220. void setAnimSpeed(int set); //speed of animation; range 1..100
  221. int getAnimSpeed() const; //speed of animation; range 1..100
  222. CPlayerInterface * getCurrentPlayerInterface() const;
  223. std::vector<CClickableHex*> bfield; //11 lines, 17 hexes on each
  224. SDL_Surface * cellBorder, * cellShade;
  225. CondSh<BattleAction *> *givenCommand; //data != nullptr if we have i.e. moved current unit
  226. bool myTurn; //if true, interface is active (commands can be ordered)
  227. CBattleResultWindow * resWindow; //window of end of battle
  228. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  229. int moveSoundHander; // sound handler used when moving a unit
  230. const BattleResult * bresult; //result of a battle; if non-zero then display when all animations end
  231. // block all UI elements, e.g. during enemy turn
  232. // unlike activate/deactivate this method will correctly grey-out all elements
  233. void blockUI(bool on);
  234. //button handle funcs:
  235. void bOptionsf();
  236. void bSurrenderf();
  237. void bFleef();
  238. void reallyFlee(); //performs fleeing without asking player
  239. void reallySurrender(); //performs surrendering without asking player
  240. void bAutofightf();
  241. void bSpellf();
  242. void bWaitf();
  243. void bDefencef();
  244. void bConsoleUpf();
  245. void bConsoleDownf();
  246. void bTacticNextStack(const CStack *current = nullptr);
  247. void bEndTacticPhase();
  248. //end of button handle funcs
  249. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  250. void activate() override;
  251. void deactivate() override;
  252. void keyPressed(const SDL_KeyboardEvent & key) override;
  253. void mouseMoved(const SDL_MouseMotionEvent &sEvent) override;
  254. void clickRight(tribool down, bool previousState) override;
  255. void show(SDL_Surface *to) override;
  256. void showAll(SDL_Surface *to) override;
  257. //call-ins
  258. void startAction(const BattleAction* action);
  259. void newStack(const CStack * stack); //new stack appeared on battlefield
  260. void stackRemoved(int stackID); //stack disappeared from batlefiled
  261. void stackActivated(const CStack * stack); //active stack has been changed
  262. void stackMoved(const CStack * stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  263. void waitForAnims();
  264. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  265. void stackAttacking(const CStack * attacker, BattleHex dest, const CStack * attacked, bool shooting); //called when stack with id ID is attacking something on hex dest
  266. void newRoundFirst( int round );
  267. void newRound(int number); //caled when round is ended; number is the number of round
  268. void hexLclicked(int whichOne); //hex only call-in
  269. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  270. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  271. void displayBattleFinished(); //displays battle result
  272. void spellCast(const BattleSpellCast * sc); //called when a hero casts a spell
  273. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  274. void castThisSpell(SpellID spellID); //called when player has chosen a spell from spellbook
  275. void displayEffect(ui32 effect, int destTile, bool areaEffect = true); //displays custom effect on the battlefield
  276. void displaySpellCast(SpellID spellID, BattleHex destinationTile, bool areaEffect = true); //displays spell`s cast animation
  277. void displaySpellEffect(SpellID spellID, BattleHex destinationTile, bool areaEffect = true); //displays spell`s affected animation
  278. void displaySpellHit(SpellID spellID, BattleHex destinationTile, bool areaEffect = true); //displays spell`s affected animation
  279. void displaySpellAnimation(const CSpell::TAnimation & animation, BattleHex destinationTile, bool areaEffect = true);
  280. void battleTriggerEffect(const BattleTriggerEffect & bte);
  281. void setBattleCursor(const int myNumber); //really complex and messy, sets attackingHex
  282. void endAction(const BattleAction* action);
  283. void hideQueue();
  284. void showQueue();
  285. PossibleActions selectionTypeByPositiveness(const CSpell & spell);
  286. Rect hexPosition(BattleHex hex) const;
  287. void handleHex(BattleHex myNumber, int eventType);
  288. bool isCastingPossibleHere (const CStack * sactive, const CStack * shere, BattleHex myNumber);
  289. bool canStackMoveHere (const CStack * sactive, BattleHex MyNumber); //TODO: move to BattleState / callback
  290. BattleHex fromWhichHexAttack(BattleHex myNumber);
  291. void obstaclePlaced(const CObstacleInstance & oi);
  292. const CGHeroInstance * currentHero() const;
  293. InfoAboutHero enemyHero() const;
  294. friend class CPlayerInterface;
  295. friend class CButton;
  296. friend class CInGameConsole;
  297. friend class CBattleResultWindow;
  298. friend class CBattleHero;
  299. friend class CSpellEffectAnimation;
  300. friend class CBattleStackAnimation;
  301. friend class CReverseAnimation;
  302. friend class CDefenceAnimation;
  303. friend class CMovementAnimation;
  304. friend class CMovementStartAnimation;
  305. friend class CAttackAnimation;
  306. friend class CMeleeAttackAnimation;
  307. friend class CShootingAnimation;
  308. friend class CClickableHex;
  309. };