CBattleInterface.h 11 KB

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