CBattleInterface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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); //sets phase of hero animation
  33. void clickLeft(boost::logic::tribool down); //call-in
  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; //number of hex in commonly used format
  43. bool accesible; //if true, this hex is accessible for units
  44. //CStack * ourStack;
  45. bool hovered, strictHovered; //for determining if hex is hovered by mouse (this is different problem than hex's graphic hovering)
  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(); //exit button callback
  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(); //dafault button callback
  103. void bExitf(); //exit button callback
  104. void activate();
  105. void deactivate();
  106. void show(SDL_Surface * to = 0);
  107. };
  108. struct BattleSettings
  109. {
  110. BattleSettings()
  111. {
  112. printCellBorders = true;
  113. printStackRange = true;
  114. animSpeed = 2;
  115. printMouseShadow = true;
  116. }
  117. bool printCellBorders; //if true, cell borders will be printed
  118. bool printStackRange; //if true,range of active stack will be printed
  119. int animSpeed; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  120. bool printMouseShadow; //if true, hex under mouse will be shaded
  121. template <typename Handler> void serialize(Handler &h, const int version)
  122. {
  123. h & printCellBorders & printStackRange & printMouseShadow;
  124. }
  125. };
  126. class CBattleInterface : public CMainInterface, public MotionInterested, public KeyInterested
  127. {
  128. private:
  129. SDL_Surface * background, * menu, * amountNormal, * amountNegative, * amountPositive, * amountEffNeutral, * cellBorders, * backgroundWithHexes;
  130. AdventureMapButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  131. * bWait, * bDefence, * bConsoleUp, * bConsoleDown;
  132. CBattleConsole * console;
  133. CBattleHero * attackingHero, * defendingHero; //fighting heroes
  134. CCreatureSet * army1, * army2; //fighting armies
  135. CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  136. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  137. std::map< int, CDefHandler * > idToProjectile; //projectiles of creaures (creatureID, defhandler)
  138. std::map< int, CDefHandler * > idToObstacle; //obstacles located on the battlefield
  139. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation>
  140. std::map< int, int > spellToEffect; //which effect should be played when different spells are casted (spellID, effectID)
  141. unsigned char animCount;
  142. int activeStack; //number of active stack; -1 - no one
  143. int mouseHoveredStack; //stack hovered by mouse; if -1 -> none
  144. std::vector<int> shadedHexes; //hexes available for active stack
  145. int previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  146. int currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  147. float getAnimSpeedMultiplier() const; //returns multiplier for number of frames in a group
  148. std::map<int, int> standingFrame; //number of frame in standing animation by stack ID, helps in showing 'random moves'
  149. bool spellDestSelectMode; //if true, player is choosing destination for his spell
  150. int spellSelMode; //0 - any location, 1 - any firendly creature, 2 - any hostile creature, 3 - any creature, 4 - obstacle, -1 - no location
  151. BattleAction * spellToCast; //spell for which player is choosing destination
  152. class CAttHelper
  153. {
  154. public:
  155. int ID; //attacking stack
  156. int IDby; //attacked stack
  157. int dest; //atacked hex
  158. int frame, maxframe; //frame of animation, number of frames of animation
  159. int hitCount; //for delaying animation
  160. bool reversing;
  161. int posShiftDueToDist;
  162. bool shooting;
  163. int shootingGroup; //if shooting is true, print this animation group
  164. } * attackingInfo;
  165. void attackingShowHelper();
  166. void redrawBackgroundWithHexes(int activeStack);
  167. void printConsoleAttacked(int ID, int dmg, int killed, int IDby);
  168. struct SProjectileInfo
  169. {
  170. int x, y; //position on the screen
  171. int dx, dy; //change in position in one step
  172. int step, lastStep; //to know when finish showing this projectile
  173. int creID; //ID of creature that shot this projectile
  174. int frameNum; //frame to display form projectile animation
  175. bool spin; //if true, frameNum will be increased
  176. int animStartDelay; //how many times projectile must be attempted to be shown till it's really show (decremented after hit)
  177. bool reverse; //if true, projectile will be flipped by vertical asix
  178. };
  179. std::list<SProjectileInfo> projectiles; //projectiles flying on battlefield
  180. void projectileShowHelper(SDL_Surface * to=NULL); //prints projectiles present on the battlefield
  181. void giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional=-1);
  182. 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
  183. struct SBattleEffect
  184. {
  185. int x, y; //position on the screen
  186. int frame, maxFrame;
  187. CDefHandler * anim; //animation to display
  188. };
  189. std::list<SBattleEffect> battleEffects; //different animations to display on the screen like spell effects
  190. public:
  191. CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect); //c-tor
  192. ~CBattleInterface(); //d-tor
  193. //std::vector<TimeInterested*> timeinterested; //animation handling
  194. static BattleSettings settings;
  195. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  196. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  197. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  198. void setAnimSpeed(int set); //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  199. int getAnimSpeed() const; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  200. CBattleHex bfield[BFIELD_SIZE]; //11 lines, 17 hexes on each
  201. std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  202. SDL_Surface * cellBorder, * cellShade;
  203. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  204. bool myTurn; //if true, interface is active (commands can be ordered
  205. CBattleReslutWindow * resWindow; //window of end of battle
  206. bool showStackQueue; //if true, queue of stacks will be shown
  207. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  208. //button handle funcs:
  209. void bOptionsf();
  210. void bSurrenderf();
  211. void bFleef();
  212. void reallyFlee(); //performs fleeing without asking player
  213. void bAutofightf();
  214. void bSpellf();
  215. void bWaitf();
  216. void bDefencef();
  217. void bConsoleUpf();
  218. void bConsoleDownf();
  219. //end of button handle funcs
  220. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  221. void activate();
  222. void deactivate();
  223. void show(SDL_Surface * to = NULL);
  224. void keyPressed(const SDL_KeyboardEvent & key);
  225. void mouseMoved(const SDL_MouseMotionEvent &sEvent);
  226. bool reverseCreature(int number, int hex, bool wideTrick = false); //reverses animation of given creature playing animation of reversing
  227. void handleStartMoving(int number); //animation of starting move; some units don't have this animation (ie. halberdier)
  228. struct SStackAttackedInfo
  229. {
  230. int ID; //id of attacked stack
  231. int dmg; //damage dealt
  232. int amountKilled; //how many creatures in stack has been killed
  233. int IDby; //ID of attacking stack
  234. bool byShooting; //if true, stack has been attacked by shooting
  235. bool killed; //if true, stack has been killed
  236. };
  237. //call-ins
  238. void newStack(CStack stack); //new stack appeared on battlefield
  239. void stackRemoved(CStack stack); //stack disappeared from batlefiled
  240. //void stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting); //stack has been killed (but corpses remain)
  241. void stackActivated(int number); //active stack has been changed
  242. void stackMoved(int number, int destHex, bool endMoving, int distance); //stack with id number moved to destHex
  243. void stacksAreAttacked(std::vector<SStackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  244. void stackAttacking(int ID, int dest); //called when stack with id ID is attacking something on hex dest
  245. void newRound(int number); //caled when round is ended; number is the number of round
  246. void hexLclicked(int whichOne); //hex only call-in
  247. void stackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  248. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  249. void spellCasted(SpellCasted * sc); //called when a hero casts a spell
  250. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  251. void castThisSpell(int spellID); //called when player has chosen a spell from spellbook
  252. void displayEffect(ui32 effect, int destTile); //displays effect of a spell on the battlefield; affected: true - attacker. false - defender
  253. friend class CBattleHex;
  254. friend class CBattleReslutWindow;
  255. friend class CPlayerInterface;
  256. friend class AdventureMapButton;
  257. };
  258. #endif // __CBATTLEINTERFACE_H__