CBattleInterface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  122. int currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  123. int animSpeed; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  124. float getAnimSpeedMultiplier() const; //returns multiplier for number of frames in a group
  125. bool spellDestSelectMode; //if true, player is choosing destination for his spell
  126. int spellSelMode; //0 - any location, 1 - any firendly creature, 2 - any hostile creature, 3 - any creature, 4 - obstacle
  127. BattleAction * spellToCast; //spell for which player is choosing destination
  128. class CAttHelper
  129. {
  130. public:
  131. int ID; //attacking stack
  132. int IDby; //attacked stack
  133. int dest; //atacked hex
  134. int frame, maxframe; //frame of animation, number of frames of animation
  135. int hitCount; //for delaying animation
  136. bool reversing;
  137. int posShiftDueToDist;
  138. bool shooting;
  139. int shootingGroup; //if shooting is true, print this animation group
  140. } * attackingInfo;
  141. void attackingShowHelper();
  142. void redrawBackgroundWithHexes(int activeStack);
  143. void printConsoleAttacked(int ID, int dmg, int killed, int IDby);
  144. struct SProjectileInfo
  145. {
  146. int x, y; //position on the screen
  147. int dx, dy; //change in position in one step
  148. int step, lastStep; //to know when finish showing this projectile
  149. int creID; //ID of creature that shot this projectile
  150. int frameNum; //frame to display form projectile animation
  151. bool spin; //if true, frameNum will be increased
  152. int animStartDelay; //how many times projectile must be attempted to be shown till it's really show (decremented after hit)
  153. };
  154. std::list<SProjectileInfo> projectiles;
  155. void projectileShowHelper(SDL_Surface * to=NULL); //prints projectiles present on the battlefield
  156. void giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional=-1);
  157. 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
  158. struct SBattleEffect
  159. {
  160. int x, y; //position on the screen
  161. int frame, maxFrame;
  162. CDefHandler * anim; //animation to display
  163. };
  164. std::list<SBattleEffect> battleEffects; //different animations to display on the screen like spell effects
  165. public:
  166. CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor
  167. ~CBattleInterface(); //d-tor
  168. //std::vector<TimeInterested*> timeinterested; //animation handling
  169. bool printCellBorders; //if true, cell borders will be printed
  170. void setPrintCellBorders(bool set); //set for above member
  171. bool printStackRange; //if true,range of active stack will be printed
  172. void setPrintStackRange(bool set); //set for above member
  173. bool printMouseShadow; //if true, hex under mouse will be shaded
  174. void setPrintMouseShadow(bool set); //set for above member
  175. void setAnimSpeed(int set); //set for animSpeed
  176. int getAnimSpeed() const; //get for animSpeed
  177. CBattleHex bfield[BFIELD_SIZE]; //11 lines, 17 hexes on each
  178. std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  179. SDL_Surface * cellBorder, * cellShade;
  180. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  181. bool myTurn; //if true, interface is active (commands can be ordered
  182. CBattleReslutWindow * resWindow; //window of end of battle
  183. bool showStackQueue; //if true, queue of stacks will be shown
  184. //button handle funcs:
  185. void bOptionsf();
  186. void bSurrenderf();
  187. void bFleef();
  188. void reallyFlee(); //performs fleeing without asking player
  189. void bAutofightf();
  190. void bSpellf();
  191. void bWaitf();
  192. void bDefencef();
  193. void bConsoleUpf();
  194. void bConsoleDownf();
  195. //end of button handle funcs
  196. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  197. void activate();
  198. void deactivate();
  199. void show(SDL_Surface * to = NULL);
  200. void keyPressed(const SDL_KeyboardEvent & key);
  201. void mouseMoved(const SDL_MouseMotionEvent &sEvent);
  202. bool reverseCreature(int number, int hex, bool wideTrick = false); //reverses animation of given creature playing animation of reversing
  203. struct SStackAttackedInfo
  204. {
  205. int ID; //id of attacked stack
  206. int dmg; //damage dealt
  207. int amountKilled; //how many creatures in stack has been killed
  208. int IDby; //ID of attacking stack
  209. bool byShooting; //if true, stack has been attacked by shooting
  210. bool killed; //if true, stack has been killed
  211. };
  212. //call-ins
  213. void newStack(CStack stack); //new stack appeared on battlefield
  214. void stackRemoved(CStack stack); //stack disappeared from batlefiled
  215. //void stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting); //stack has been killed (but corpses remain)
  216. void stackActivated(int number); //active stack has been changed
  217. void stackMoved(int number, int destHex, bool endMoving); //stack with id number moved to destHex
  218. void stacksAreAttacked(std::vector<SStackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  219. void stackAttacking(int ID, int dest); //called when stack with id ID is attacking something on hex dest
  220. void newRound(int number); //caled when round is ended; number is the number of round
  221. void hexLclicked(int whichOne); //hex only call-in
  222. void stackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  223. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  224. void spellCasted(SpellCasted * sc); //called when a hero casts a spell
  225. void castThisSpell(int spellID); //called when player has chosen a spell from spellbook
  226. void displayEffect(ui32 effect, int destTile); //displays effect of a spell on the battlefield; affected: true - attacker. false - defender
  227. friend class CBattleHex;
  228. friend class CBattleReslutWindow;
  229. friend class CPlayerInterface;
  230. };