CBattleInterface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #pragma once
  2. #include "../../lib/CCreatureSet.h"
  3. #include "../../lib/ConstTransitivePtr.h" //may be reundant
  4. #include "../CAnimation.h"
  5. #include "../../lib/GameConstants.h"
  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 CLabel;
  16. class CCreatureSet;
  17. class CGHeroInstance;
  18. class CDefHandler;
  19. class CStack;
  20. class CCallback;
  21. class CAdventureMapButton;
  22. class CHighlightableButton;
  23. class CHighlightableButtonsGroup;
  24. struct BattleResult;
  25. struct BattleSpellCast;
  26. struct CObstacleInstance;
  27. template <typename T> struct CondSh;
  28. struct SetStackEffect;;
  29. struct BattleAction;
  30. class CGTownInstance;
  31. struct CatapultAttack;
  32. struct CatapultProjectileInfo;
  33. struct BattleTriggerEffect;
  34. class CBattleAnimation;
  35. class CBattleHero;
  36. class CBattleConsole;
  37. class CBattleResultWindow;
  38. class CStackQueue;
  39. class CPlayerInterface;
  40. class CCreatureAnimation;
  41. struct ProjectileInfo;
  42. class CClickableHex;
  43. struct BattleHex;
  44. /// Class which manages the locked hex fields that are blocked e.g. by obstacles
  45. class CBattleObstacle
  46. {
  47. std::vector<int> lockedHexes;
  48. };
  49. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  50. struct StackAttackedInfo
  51. {
  52. const CStack * defender; //attacked stack
  53. int dmg; //damage dealt
  54. int amountKilled; //how many creatures in stack has been killed
  55. const CStack * attacker; //attacking stack
  56. bool byShooting; //if true, stack has been attacked by shooting
  57. bool killed; //if true, stack has been killed
  58. bool rebirth; //if true, play rebirth animation after all
  59. bool cloneKilled;
  60. };
  61. /// Struct for battle effect animation e.g. morale, prayer, armageddon, bless,...
  62. struct BattleEffect
  63. {
  64. int x, y; //position on the screen
  65. int frame, maxFrame;
  66. CDefHandler * anim; //animation to display
  67. int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim
  68. };
  69. /// Small struct which is needed for drawing the parabolic trajectory of the catapult cannon
  70. struct CatapultProjectileInfo
  71. {
  72. const double facA, facB, facC;
  73. const int fromX, toX;
  74. CatapultProjectileInfo() : facA(0), facB(0), facC(0), fromX(0), toX(0) { };
  75. CatapultProjectileInfo(double factorA, double factorB, double factorC, int fromXX, int toXX) : facA(factorA), facB(factorB), facC(factorC),
  76. fromX(fromXX), toX(toXX) { };
  77. double calculateY(double x);
  78. };
  79. /// Big class which handles the overall battle interface actions and it is also responsible for
  80. /// drawing everything correctly.
  81. class CBattleInterface : public CIntObject
  82. {
  83. enum SpellSelectionType
  84. {
  85. ANY_LOCATION = 0, FRIENDLY_CREATURE, HOSTILE_CREATURE, ANY_CREATURE, OBSTACLE, TELEPORT, NO_LOCATION = -1, STACK_SPELL_CANCELLED = -2
  86. };
  87. private:
  88. SDL_Surface * background, * menu, * amountNormal, * amountNegative, * amountPositive, * amountEffNeutral, * cellBorders, * backgroundWithHexes;
  89. CAdventureMapButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  90. * bWait, * bDefence, * bConsoleUp, * bConsoleDown, *btactNext, *btactEnd;
  91. CBattleConsole * console;
  92. CBattleHero * attackingHero, * defendingHero; //fighting heroes
  93. CStackQueue *queue;
  94. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  95. const CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  96. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  97. std::map< int, CDefHandler * > idToProjectile; //projectiles of creatures (creatureID, defhandler)
  98. std::map< int, CDefHandler * > idToObstacle; //obstacles located on the battlefield
  99. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation>
  100. ui8 animCount;
  101. const CStack * activeStack; //number of active stack; NULL - no one
  102. const CStack * stackToActivate; //when animation is playing, we should wait till the end to make the next stack active; NULL of none
  103. void activateStack(); //sets activeStack to stackToActivate etc.
  104. int mouseHoveredStack; //stack hovered by mouse; if -1 -> none
  105. time_t lastMouseHoveredStackAnimationTime; // time when last mouse hovered animation occurred
  106. static const time_t HOVER_ANIM_DELTA;
  107. std::vector<BattleHex> occupyableHexes, //hexes available for active stack
  108. attackableHexes; //hexes attackable by active stack
  109. bool stackCountOutsideHexes[GameConstants::BFIELD_SIZE]; // hexes that when in front of a unit cause it's amount box to move back
  110. int previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  111. int currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  112. int attackingHex; //hex from which the stack would perform attack with current cursor
  113. double getAnimSpeedMultiplier() const; //returns multiplier for number of frames in a group
  114. std::map<int, int> standingFrame; //number of frame in standing animation by stack ID, helps in showing 'random moves'
  115. CPlayerInterface * tacticianInterface; //used during tactics mode, points to the interface of player with higher tactics (can be either attacker or defender in hot-seat), valid onloy for human players
  116. bool tacticsMode;
  117. bool stackCanCastSpell; //if true, active stack could possibly cats some target spell
  118. bool spellDestSelectMode; //if true, player is choosing destination for his spell
  119. SpellSelectionType spellSelMode;
  120. BattleAction * spellToCast; //spell for which player is choosing destination
  121. si32 creatureSpellToCast;
  122. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or canceled)
  123. void showAliveStack(const CStack *stack, SDL_Surface * to); //helper function for function show
  124. void showAliveStacks(std::vector<const CStack *> *aliveStacks, int hex, std::vector<const CStack *> *flyingStacks, SDL_Surface *to); // loops through all stacks at a given hex position
  125. void showPieceOfWall(SDL_Surface * to, int hex, const std::vector<const CStack*> & stacks); //helper function for show
  126. void showObstacles(std::multimap<BattleHex, int> *hexToObstacle, std::vector<CObstacleInstance> &obstacles, int hex, SDL_Surface *to); // show all obstacles at a given hex position
  127. void redrawBackgroundWithHexes(const CStack * activeStack);
  128. void printConsoleAttacked(const CStack * defender, int dmg, int killed, const CStack * attacker, bool Multiple);
  129. std::list<ProjectileInfo> projectiles; //projectiles flying on battlefield
  130. void projectileShowHelper(SDL_Surface * to); //prints projectiles present on the battlefield
  131. void giveCommand(ui8 action, BattleHex tile, ui32 stackID, si32 additional=-1);
  132. bool isTileAttackable(const BattleHex & number) const; //returns true if tile 'number' is neighboring any tile from active stack's range or is one of these tiles
  133. bool isCatapultAttackable(BattleHex hex) const; //returns true if given tile can be attacked by catapult
  134. std::list<BattleEffect> battleEffects; //different animations to display on the screen like spell effects
  135. /// Class which is responsible for drawing the wall of a siege during battle
  136. class SiegeHelper
  137. {
  138. private:
  139. static std::string townTypeInfixes[GameConstants::F_NUMBER]; //for internal use only - to build filenames
  140. SDL_Surface* walls[18];
  141. const CBattleInterface * owner;
  142. public:
  143. const CGTownInstance * town; //besieged town
  144. SiegeHelper(const CGTownInstance * siegeTown, const CBattleInterface * _owner); //c-tor
  145. ~SiegeHelper(); //d-tor
  146. //filename getters
  147. std::string getSiegeName(ui16 what, ui16 additInfo = 1) const; //what: 0 - background, 1 - background wall, 2 - keep, 3 - bottom tower, 4 - bottom wall, 5 - below gate, 6 - over gate, 7 - upper wall, 8 - uppert tower, 9 - gate, 10 - gate arch, 11 - bottom static wall, 12 - upper static wall, 13 - moat, 14 - mlip, 15 - keep creature cover, 16 - bottom turret creature cover, 17 - upper turret creature cover; additInfo: 1 - intact, 2 - damaged, 3 - destroyed
  148. void printPartOfWall(SDL_Surface * to, int what);//what: 1 - background wall, 2 - keep, 3 - bottom tower, 4 - bottom wall, 5 - below gate, 6 - over gate, 7 - upper wall, 8 - uppert tower, 9 - gate, 10 - gate arch, 11 - bottom static wall, 12 - upper static wall, 15 - keep creature cover, 16 - bottom turret creature cover, 17 - upper turret creature cover
  149. friend class CBattleInterface;
  150. } * siegeH;
  151. CPlayerInterface * attackerInt, * defenderInt; //because LOCPLINT is not enough in hotSeat
  152. const CGHeroInstance * getActiveHero(); //returns hero that can currently cast a spell
  153. public:
  154. CPlayerInterface * curInt; //current player interface
  155. std::list<std::pair<CBattleAnimation *, bool> > pendingAnims; //currently displayed animations <anim, initialized>
  156. void addNewAnim(CBattleAnimation * anim); //adds new anim to pendingAnims
  157. ui32 animIDhelper; //for giving IDs for animations
  158. static CondSh<bool> animsAreDisplayed; //for waiting with the end of battle for end of anims
  159. CBattleInterface(const CCreatureSet * army1, const CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect, CPlayerInterface * att, CPlayerInterface * defen); //c-tor
  160. ~CBattleInterface(); //d-tor
  161. //std::vector<TimeInterested*> timeinterested; //animation handling
  162. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  163. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  164. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  165. void setAnimSpeed(int set); //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  166. int getAnimSpeed() const; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  167. std::vector<CClickableHex> bfield; //11 lines, 17 hexes on each
  168. //std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  169. SDL_Surface * cellBorder, * cellShade;
  170. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  171. bool myTurn; //if true, interface is active (commands can be ordered)
  172. CBattleResultWindow * resWindow; //window of end of battle
  173. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  174. int moveSh; // sound handler used when moving a unit
  175. //button handle funcs:
  176. void bOptionsf();
  177. void bSurrenderf();
  178. void bFleef();
  179. void reallyFlee(); //performs fleeing without asking player
  180. void reallySurrender(); //performs surrendering without asking player
  181. void bAutofightf();
  182. void bSpellf();
  183. void bWaitf();
  184. void bDefencef();
  185. void bConsoleUpf();
  186. void bConsoleDownf();
  187. void bTacticNextStack(const CStack *current = NULL);
  188. void bEndTacticPhase();
  189. //end of button handle funcs
  190. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  191. void activate();
  192. void deactivate();
  193. void show(SDL_Surface * to);
  194. void keyPressed(const SDL_KeyboardEvent & key);
  195. void mouseMoved(const SDL_MouseMotionEvent &sEvent);
  196. void clickRight(tribool down, bool previousState);
  197. //call-ins
  198. void startAction(const BattleAction* action);
  199. void newStack(const CStack * stack); //new stack appeared on battlefield
  200. void stackRemoved(int stackID); //stack disappeared from batlefiled
  201. void stackActivated(const CStack * stack); //active stack has been changed
  202. void stackMoved(const CStack * stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  203. void waitForAnims();
  204. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  205. void stackAttacking(const CStack * attacker, BattleHex dest, const CStack * attacked, bool shooting); //called when stack with id ID is attacking something on hex dest
  206. void newRoundFirst( int round );
  207. void newRound(int number); //caled when round is ended; number is the number of round
  208. void hexLclicked(int whichOne); //hex only call-in
  209. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  210. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  211. const BattleResult * bresult; //result of a battle; if non-zero then display when all animations end
  212. void displayBattleFinished(); //displays battle result
  213. void spellCast(const BattleSpellCast * sc); //called when a hero casts a spell
  214. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  215. void castThisSpell(int spellID); //called when player has chosen a spell from spellbook
  216. void displayEffect(ui32 effect, int destTile); //displays effect of a spell on the battlefield; affected: true - attacker. false - defender
  217. void battleTriggerEffect(const BattleTriggerEffect & bte);
  218. void setBattleCursor(const int myNumber); //really complex and messy, sets attackingHex
  219. void endAction(const BattleAction* action);
  220. void hideQueue();
  221. void showQueue();
  222. SpellSelectionType selectionTypeByPositiveness(const CSpell & spell);
  223. void handleHex(BattleHex myNumber, int eventType);
  224. BattleHex fromWhichHexAttack(BattleHex myNumber);
  225. friend class CPlayerInterface;
  226. friend class CAdventureMapButton;
  227. friend class CInGameConsole;
  228. friend class CBattleResultWindow;
  229. friend class CBattleHero;
  230. friend class CSpellEffectAnimation;
  231. friend class CBattleStackAnimation;
  232. friend class CReverseAnimation;
  233. friend class CDefenceAnimation;
  234. friend class CMovementAnimation;
  235. friend class CMovementStartAnimation;
  236. friend class CAttackAnimation;
  237. friend class CMeleeAttackAnimation;
  238. friend class CShootingAnimation;
  239. friend class CClickableHex;
  240. };