CBattleInterface.h 13 KB

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