CBattleInterface.h 11 KB

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