CBattleInterface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #ifndef __CBATTLEINTERFACE_H__
  2. #define __CBATTLEINTERFACE_H__
  3. #include "../global.h"
  4. #include <list>
  5. #include "GUIBase.h"
  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 CCreatureSet;
  16. class CGHeroInstance;
  17. class CDefHandler;
  18. class CStack;
  19. class CCallback;
  20. class AdventureMapButton;
  21. class CHighlightableButton;
  22. class CHighlightableButtonsGroup;
  23. struct BattleResult;
  24. struct SpellCast;
  25. template <typename T> struct CondSh;
  26. struct SetStackEffect;;
  27. struct BattleAction;
  28. class CGTownInstance;
  29. class CBattleInterface;
  30. class CBattleHero : public CIntObject
  31. {
  32. public:
  33. bool flip; //false if it's attacking hero, true otherwise
  34. CDefHandler * dh, *flag; //animation and flag
  35. const CGHeroInstance * myHero; //this animation's hero instance
  36. const CBattleInterface * myOwner; //battle interface to which this animation is assigned
  37. int phase; //stage of animation
  38. int nextPhase; //stage of animation to be set after current phase is fully displayed
  39. int image; //frame of animation
  40. unsigned char flagAnim, flagAnimCount; //for flag animation
  41. void show(SDL_Surface * to); //prints next frame of animation to to
  42. void activate();
  43. void deactivate();
  44. void setPhase(int newPhase); //sets phase of hero animation
  45. void clickLeft(tribool down, bool previousState); //call-in
  46. CBattleHero(const std::string & defName, int phaseG, int imageG, bool filpG, unsigned char player, const CGHeroInstance * hero, const CBattleInterface * owner); //c-tor
  47. ~CBattleHero(); //d-tor
  48. };
  49. class CBattleHex : public CIntObject
  50. {
  51. private:
  52. bool setAlterText; //if true, this hex has set alternative text in console and will clean it
  53. public:
  54. unsigned int myNumber; //number of hex in commonly used format
  55. bool accesible; //if true, this hex is accessible for units
  56. //CStack * ourStack;
  57. bool hovered, strictHovered; //for determining if hex is hovered by mouse (this is different problem than hex's graphic hovering)
  58. CBattleInterface * myInterface; //interface that owns me
  59. static std::pair<int, int> getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * creature); //returns (x, y) of left top corner of animation
  60. //for user interactions
  61. void hover (bool on);
  62. void activate();
  63. void deactivate();
  64. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  65. void clickLeft(tribool down, bool previousState);
  66. void clickRight(tribool down, bool previousState);
  67. CBattleHex();
  68. };
  69. class CBattleObstacle
  70. {
  71. std::vector<int> lockedHexes;
  72. };
  73. class CBattleConsole : public CIntObject
  74. {
  75. private:
  76. std::vector< std::string > texts; //a place where texts are stored
  77. int lastShown; //last shown line of text
  78. public:
  79. std::string alterTxt; //if it's not empty, this text is displayed
  80. std::string ingcAlter; //alternative text set by in-game console - very important!
  81. int whoSetAlter; //who set alter text; 0 - battle interface or none, 1 - button
  82. CBattleConsole(); //c-tor
  83. ~CBattleConsole(); //d-tor
  84. void show(SDL_Surface * to = 0);
  85. bool addText(const std::string & text); //adds text at the last position; returns false if failed (e.g. text longer than 70 characters)
  86. void eraseText(unsigned int pos); //erases added text at position pos
  87. void changeTextAt(const std::string & text, unsigned int pos); //if we have more than pos texts, pos-th is changed to given one
  88. void scrollUp(unsigned int by = 1); //scrolls console up by 'by' positions
  89. void scrollDown(unsigned int by = 1); //scrolls console up by 'by' positions
  90. };
  91. class CBattleResultWindow : public CIntObject
  92. {
  93. private:
  94. SDL_Surface * background;
  95. AdventureMapButton * exit;
  96. public:
  97. CBattleResultWindow(const BattleResult & br, const SDL_Rect & pos, const CBattleInterface * owner); //c-tor
  98. ~CBattleResultWindow(); //d-tor
  99. void bExitf(); //exit button callback
  100. void activate();
  101. void deactivate();
  102. void show(SDL_Surface * to = 0);
  103. };
  104. class CBattleOptionsWindow : public CIntObject
  105. {
  106. private:
  107. CBattleInterface * myInt;
  108. SDL_Surface * background;
  109. AdventureMapButton * setToDefault, * exit;
  110. CHighlightableButton * viewGrid, * movementShadow, * mouseShadow;
  111. CHighlightableButtonsGroup * animSpeeds;
  112. public:
  113. CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface * owner); //c-tor
  114. ~CBattleOptionsWindow(); //d-tor
  115. void bDefaultf(); //dafault button callback
  116. void bExitf(); //exit button callback
  117. void activate();
  118. void deactivate();
  119. void show(SDL_Surface * to = 0);
  120. };
  121. struct BattleSettings
  122. {
  123. BattleSettings()
  124. {
  125. printCellBorders = true;
  126. printStackRange = true;
  127. animSpeed = 2;
  128. printMouseShadow = true;
  129. }
  130. bool printCellBorders; //if true, cell borders will be printed
  131. bool printStackRange; //if true,range of active stack will be printed
  132. int animSpeed; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  133. bool printMouseShadow; //if true, hex under mouse will be shaded
  134. template <typename Handler> void serialize(Handler &h, const int version)
  135. {
  136. h & printCellBorders & printStackRange & printMouseShadow;
  137. }
  138. };
  139. class CBattleInterface : public CIntObject
  140. {
  141. private:
  142. SDL_Surface * background, * menu, * amountNormal, * amountNegative, * amountPositive, * amountEffNeutral, * cellBorders, * backgroundWithHexes;
  143. AdventureMapButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  144. * bWait, * bDefence, * bConsoleUp, * bConsoleDown;
  145. CBattleConsole * console;
  146. CBattleHero * attackingHero, * defendingHero; //fighting heroes
  147. CCreatureSet * army1, * army2; //fighting armies
  148. CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  149. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  150. std::map< int, CDefHandler * > idToProjectile; //projectiles of creaures (creatureID, defhandler)
  151. std::map< int, CDefHandler * > idToObstacle; //obstacles located on the battlefield
  152. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation>
  153. unsigned char animCount;
  154. int activeStack; //number of active stack; -1 - no one
  155. int mouseHoveredStack; //stack hovered by mouse; if -1 -> none
  156. std::vector<int> shadedHexes; //hexes available for active stack
  157. int previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  158. int currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  159. float getAnimSpeedMultiplier() const; //returns multiplier for number of frames in a group
  160. std::map<int, int> standingFrame; //number of frame in standing animation by stack ID, helps in showing 'random moves'
  161. bool spellDestSelectMode; //if true, player is choosing destination for his spell
  162. int spellSelMode; //0 - any location, 1 - any firendly creature, 2 - any hostile creature, 3 - any creature, 4 - obstacle,z -1 - no location
  163. BattleAction * spellToCast; //spell for which player is choosing destination
  164. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or cancelled)
  165. class CAttHelper
  166. {
  167. public:
  168. int ID; //attacking stack
  169. int IDby; //attacked stack
  170. int dest; //atacked hex
  171. int frame, maxframe; //frame of animation, number of frames of animation
  172. int hitCount; //for delaying animation
  173. bool reversing;
  174. int posShiftDueToDist;
  175. bool shooting;
  176. int shootingGroup; //if shooting is true, print this animation group
  177. int sh; // temporary sound handler
  178. } * attackingInfo;
  179. void attackingShowHelper();
  180. void redrawBackgroundWithHexes(int activeStack);
  181. void printConsoleAttacked(int ID, int dmg, int killed, int IDby);
  182. struct SProjectileInfo
  183. {
  184. int x, y; //position on the screen
  185. int dx, dy; //change in position in one step
  186. int step, lastStep; //to know when finish showing this projectile
  187. int creID; //ID of creature that shot this projectile
  188. int frameNum; //frame to display form projectile animation
  189. bool spin; //if true, frameNum will be increased
  190. int animStartDelay; //how many times projectile must be attempted to be shown till it's really show (decremented after hit)
  191. bool reverse; //if true, projectile will be flipped by vertical asix
  192. };
  193. std::list<SProjectileInfo> projectiles; //projectiles flying on battlefield
  194. void projectileShowHelper(SDL_Surface * to); //prints projectiles present on the battlefield
  195. void giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional=-1);
  196. bool isTileAttackable(const int & number) const; //returns true if tile 'number' is neighbouring any tile from active stack's range or is one of these tiles
  197. bool blockedByObstacle(int hex) const;
  198. void handleEndOfMove(int stackNumber, int destinationTile); //helper function
  199. struct SBattleEffect
  200. {
  201. int x, y; //position on the screen
  202. int frame, maxFrame;
  203. CDefHandler * anim; //animation to display
  204. };
  205. std::list<SBattleEffect> battleEffects; //different animations to display on the screen like spell effects
  206. class SiegeHelper
  207. {
  208. private:
  209. static std::string townTypeInfixes[F_NUMBER]; //for internal use only - to build filenames
  210. public:
  211. const CGTownInstance * town; //besieged town
  212. SiegeHelper(const CGTownInstance * siegeTown); //c-tor
  213. //filename getters
  214. std::string getBackgroundName() const;
  215. } * siegeH;
  216. public:
  217. CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect); //c-tor
  218. ~CBattleInterface(); //d-tor
  219. //std::vector<TimeInterested*> timeinterested; //animation handling
  220. static BattleSettings settings;
  221. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  222. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  223. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  224. void setAnimSpeed(int set); //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  225. int getAnimSpeed() const; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  226. CBattleHex bfield[BFIELD_SIZE]; //11 lines, 17 hexes on each
  227. //std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  228. SDL_Surface * cellBorder, * cellShade;
  229. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  230. bool myTurn; //if true, interface is active (commands can be ordered
  231. CBattleResultWindow * resWindow; //window of end of battle
  232. bool showStackQueue; //if true, queue of stacks will be shown
  233. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  234. int moveSh; // sound handler used when moving a unit
  235. //button handle funcs:
  236. void bOptionsf();
  237. void bSurrenderf();
  238. void bFleef();
  239. void reallyFlee(); //performs fleeing without asking player
  240. void bAutofightf();
  241. void bSpellf();
  242. void bWaitf();
  243. void bDefencef();
  244. void bConsoleUpf();
  245. void bConsoleDownf();
  246. //end of button handle funcs
  247. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  248. void activate();
  249. void deactivate();
  250. void show(SDL_Surface * to);
  251. void keyPressed(const SDL_KeyboardEvent & key);
  252. void mouseMoved(const SDL_MouseMotionEvent &sEvent);
  253. void clickRight(tribool down, bool previousState);
  254. bool reverseCreature(int number, int hex, bool wideTrick = false); //reverses animation of given creature playing animation of reversing
  255. void handleStartMoving(int number); //animation of starting move; some units don't have this animation (ie. halberdier)
  256. struct SStackAttackedInfo
  257. {
  258. int ID; //id of attacked stack
  259. int dmg; //damage dealt
  260. int amountKilled; //how many creatures in stack has been killed
  261. int IDby; //ID of attacking stack
  262. bool byShooting; //if true, stack has been attacked by shooting
  263. bool killed; //if true, stack has been killed
  264. };
  265. //call-ins
  266. void newStack(int stackID); //new stack appeared on battlefield
  267. void stackRemoved(int stackID); //stack disappeared from batlefiled
  268. //void stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting); //stack has been killed (but corpses remain)
  269. void stackActivated(int number); //active stack has been changed
  270. void stackMoved(int number, int destHex, bool endMoving, int distance); //stack with id number moved to destHex
  271. void stacksAreAttacked(std::vector<SStackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  272. void stackAttacking(int ID, int dest); //called when stack with id ID is attacking something on hex dest
  273. void newRound(int number); //caled when round is ended; number is the number of round
  274. void hexLclicked(int whichOne); //hex only call-in
  275. void stackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  276. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  277. void spellCast(SpellCast * sc); //called when a hero casts a spell
  278. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  279. void castThisSpell(int spellID); //called when player has chosen a spell from spellbook
  280. void displayEffect(ui32 effect, int destTile); //displays effect of a spell on the battlefield; affected: true - attacker. false - defender
  281. friend class CBattleHex;
  282. friend class CBattleResultWindow;
  283. friend class CPlayerInterface;
  284. friend class AdventureMapButton;
  285. friend class CInGameConsole;
  286. };
  287. #endif // __CBATTLEINTERFACE_H__