CBattleInterface.h 13 KB

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