CBattleInterface.h 13 KB

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