CBattleInterface.h 11 KB

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