CBattleInterface.h 13 KB

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