CBattleInterface.h 10 KB

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