CBattleInterface.h 11 KB

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