CBattleInterface.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include "CBattleAnimations.h"
  7. /*
  8. * CBattleInterface.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CLabel;
  17. class CCreatureSet;
  18. class CGHeroInstance;
  19. class CDefHandler;
  20. class CStack;
  21. class CCallback;
  22. class CAdventureMapButton;
  23. class CHighlightableButton;
  24. class CHighlightableButtonsGroup;
  25. struct BattleResult;
  26. struct BattleSpellCast;
  27. struct CObstacleInstance;
  28. template <typename T> struct CondSh;
  29. struct SetStackEffect;
  30. struct BattleAction;
  31. class CGTownInstance;
  32. struct CatapultAttack;
  33. struct CatapultProjectileInfo;
  34. struct BattleTriggerEffect;
  35. class CBattleAnimation;
  36. class CBattleHero;
  37. class CBattleConsole;
  38. class CBattleResultWindow;
  39. class CStackQueue;
  40. class CPlayerInterface;
  41. class CCreatureAnimation;
  42. struct ProjectileInfo;
  43. class CClickableHex;
  44. struct BattleHex;
  45. struct InfoAboutHero;
  46. struct BattleAction;
  47. class CBattleGameInterface;
  48. /// Class which manages the locked hex fields that are blocked e.g. by obstacles
  49. class CBattleObstacle
  50. {
  51. std::vector<int> lockedHexes;
  52. };
  53. /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
  54. struct StackAttackedInfo
  55. {
  56. const CStack * defender; //attacked stack
  57. unsigned int dmg; //damage dealt
  58. unsigned int amountKilled; //how many creatures in stack has been killed
  59. const CStack * attacker; //attacking stack
  60. bool byShooting; //if true, stack has been attacked by shooting
  61. bool killed; //if true, stack has been killed
  62. bool rebirth; //if true, play rebirth animation after all
  63. bool cloneKilled;
  64. };
  65. /// Struct for battle effect animation e.g. morale, prayer, armageddon, bless,...
  66. struct BattleEffect
  67. {
  68. int x, y; //position on the screen
  69. int frame, maxFrame;
  70. CDefHandler * anim; //animation to display
  71. int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim
  72. };
  73. /// Small struct which is needed for drawing the parabolic trajectory of the catapult cannon
  74. struct CatapultProjectileInfo
  75. {
  76. CatapultProjectileInfo(Point from, Point dest);
  77. double facA, facB, facC;
  78. double calculateY(double x);
  79. };
  80. /// Big class which handles the overall battle interface actions and it is also responsible for
  81. /// drawing everything correctly.
  82. class CBattleInterface : public CIntObject
  83. {
  84. enum PossibleActions // actions performed at l-click
  85. {
  86. INVALID = -1, CREATURE_INFO,
  87. MOVE_TACTICS, CHOOSE_TACTICS_STACK,
  88. MOVE_STACK, ATTACK, WALK_AND_ATTACK, ATTACK_AND_RETURN, SHOOT, //OPEN_GATE, //we can open castle gate during siege
  89. NO_LOCATION, ANY_LOCATION, FRIENDLY_CREATURE_SPELL, HOSTILE_CREATURE_SPELL, RISING_SPELL, ANY_CREATURE, OBSTACLE, TELEPORT, SACRIFICE, RANDOM_GENIE_SPELL,
  90. FREE_LOCATION, //used with Force Field and Fire Wall - all tiles affected by spell must be free
  91. CATAPULT, HEAL, RISE_DEMONS
  92. };
  93. private:
  94. SDL_Surface * background, * menu, * amountNormal, * amountNegative, * amountPositive, * amountEffNeutral, * cellBorders, * backgroundWithHexes;
  95. CAdventureMapButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  96. * bWait, * bDefence, * bConsoleUp, * bConsoleDown, *btactNext, *btactEnd;
  97. CBattleConsole * console;
  98. CBattleHero * attackingHero, * defendingHero; //fighting heroes
  99. CStackQueue *queue;
  100. const CCreatureSet *army1, *army2; //copy of initial armies (for result window)
  101. const CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  102. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  103. std::map< int, CDefHandler * > idToProjectile; //projectiles of creatures (creatureID, defhandler)
  104. std::map< int, CDefHandler * > idToObstacle; //obstacles located on the battlefield
  105. std::map< int, SDL_Surface * > idToAbsoluteObstacle; //obstacles located on the battlefield
  106. //TODO these should be loaded only when needed (and freed then) but I believe it's rather work for resource manager,
  107. //so I didn't implement that (having ongoing RM development)
  108. CDefHandler *landMine;
  109. CDefHandler *quicksand;
  110. CDefHandler *fireWall;
  111. CDefHandler *smallForceField[2], *bigForceField[2]; // [side]
  112. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation> //TODO: move it to battle callback
  113. ui8 animCount;
  114. const CStack * activeStack; //number of active stack; NULL - no one
  115. const CStack * stackToActivate; //when animation is playing, we should wait till the end to make the next stack active; NULL of none
  116. const CStack * selectedStack; //for Teleport / Sacrifice
  117. void activateStack(); //sets activeStack to stackToActivate etc.
  118. int mouseHoveredStack; //stack hovered by mouse; if -1 -> none
  119. time_t lastMouseHoveredStackAnimationTime; // time when last mouse hovered animation occurred
  120. static const time_t HOVER_ANIM_DELTA;
  121. std::vector<BattleHex> occupyableHexes, //hexes available for active stack
  122. attackableHexes; //hexes attackable by active stack
  123. bool stackCountOutsideHexes[GameConstants::BFIELD_SIZE]; // hexes that when in front of a unit cause it's amount box to move back
  124. int previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  125. int currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  126. int attackingHex; //hex from which the stack would perform attack with current cursor
  127. double getAnimSpeedMultiplier() const; //returns multiplier for number of frames in a group
  128. std::map<int, int> standingFrame; //number of frame in standing animation by stack ID, helps in showing 'random moves'
  129. 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
  130. bool tacticsMode;
  131. bool stackCanCastSpell; //if true, active stack could possibly cats some target spell
  132. bool creatureCasting; //if true, stack currently aims to cats a spell
  133. bool spellDestSelectMode; //if true, player is choosing destination for his spell - only for GUI / console
  134. PossibleActions spellSelMode;
  135. BattleAction * spellToCast; //spell for which player is choosing destination
  136. const CSpell * sp; //spell pointer for convenience
  137. si32 creatureSpellToCast;
  138. std::vector<PossibleActions> possibleActions; //all actions possible to call at the moment by player
  139. std::vector<PossibleActions> localActions; //actions possible to take on hovered hex
  140. std::vector<PossibleActions> illegalActions; //these actions display message in case of illegal target
  141. PossibleActions currentAction; //action that will be performed on l-click
  142. PossibleActions selectedAction; //last action chosen (and saved) by player
  143. PossibleActions illegalAction; //most likely action that can't be performed here
  144. CBattleGameInterface *autofightingAI;
  145. void getPossibleActionsForStack (const CStack * stack); //called when stack gets its turn
  146. void endCastingSpell(); //ends casting spell (eg. when spell has been cast or canceled)
  147. void showAliveStack(const CStack *stack, SDL_Surface * to); //helper function for function show
  148. 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
  149. void showPieceOfWall(SDL_Surface * to, int hex, const std::vector<const CStack*> & stacks); //helper function for show
  150. void showObstacles(std::multimap<BattleHex, int> *hexToObstacle, std::vector<shared_ptr<const CObstacleInstance> > &obstacles, int hex, SDL_Surface *to); // show all obstacles at a given hex position
  151. SDL_Surface *imageOfObstacle(const CObstacleInstance &oi) const;
  152. Point whereToBlitObstacleImage(SDL_Surface *image, const CObstacleInstance &obstacle) const;
  153. void redrawBackgroundWithHexes(const CStack * activeStack);
  154. void printConsoleAttacked(const CStack * defender, int dmg, int killed, const CStack * attacker, bool Multiple);
  155. std::list<ProjectileInfo> projectiles; //projectiles flying on battlefield
  156. void projectileShowHelper(SDL_Surface * to); //prints projectiles present on the battlefield
  157. void giveCommand(Battle::ActionType action, BattleHex tile, ui32 stackID, si32 additional=-1, si32 selectedStack = -1);
  158. 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
  159. bool isCatapultAttackable(BattleHex hex) const; //returns true if given tile can be attacked by catapult
  160. std::list<BattleEffect> battleEffects; //different animations to display on the screen like spell effects
  161. /// Class which is responsible for drawing the wall of a siege during battle
  162. class SiegeHelper
  163. {
  164. private:
  165. SDL_Surface* walls[18];
  166. const CBattleInterface * owner;
  167. public:
  168. const CGTownInstance * town; //besieged town
  169. SiegeHelper(const CGTownInstance * siegeTown, const CBattleInterface * _owner); //c-tor
  170. ~SiegeHelper(); //d-tor
  171. //filename getters
  172. 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
  173. 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
  174. friend class CBattleInterface;
  175. } * siegeH;
  176. CPlayerInterface * attackerInt, * defenderInt; //because LOCPLINT is not enough in hotSeat
  177. const CGHeroInstance * getActiveHero(); //returns hero that can currently cast a spell
  178. public:
  179. CPlayerInterface * curInt; //current player interface
  180. std::list<std::pair<CBattleAnimation *, bool> > pendingAnims; //currently displayed animations <anim, initialized>
  181. void addNewAnim(CBattleAnimation * anim); //adds new anim to pendingAnims
  182. ui32 animIDhelper; //for giving IDs for animations
  183. static CondSh<bool> animsAreDisplayed; //for waiting with the end of battle for end of anims
  184. CBattleInterface(const CCreatureSet * army1, const CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect, CPlayerInterface * att, CPlayerInterface * defen); //c-tor
  185. ~CBattleInterface(); //d-tor
  186. //std::vector<TimeInterested*> timeinterested; //animation handling
  187. void setPrintCellBorders(bool set); //if true, cell borders will be printed
  188. void setPrintStackRange(bool set); //if true,range of active stack will be printed
  189. void setPrintMouseShadow(bool set); //if true, hex under mouse will be shaded
  190. void setAnimSpeed(int set); //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  191. int getAnimSpeed() const; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  192. std::vector<CClickableHex*> bfield; //11 lines, 17 hexes on each
  193. //std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  194. SDL_Surface * cellBorder, * cellShade;
  195. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  196. bool myTurn; //if true, interface is active (commands can be ordered)
  197. CBattleResultWindow * resWindow; //window of end of battle
  198. bool moveStarted; //if true, the creature that is already moving is going to make its first step
  199. int moveSh; // sound handler used when moving a unit
  200. //button handle funcs:
  201. void bOptionsf();
  202. void bSurrenderf();
  203. void bFleef();
  204. void reallyFlee(); //performs fleeing without asking player
  205. void reallySurrender(); //performs surrendering without asking player
  206. void bAutofightf();
  207. void bSpellf();
  208. void bWaitf();
  209. void bDefencef();
  210. void bConsoleUpf();
  211. void bConsoleDownf();
  212. void bTacticNextStack(const CStack *current = NULL);
  213. void bEndTacticPhase();
  214. //end of button handle funcs
  215. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  216. void activate();
  217. void deactivate();
  218. void showAll(SDL_Surface * to);
  219. void show(SDL_Surface * to);
  220. void keyPressed(const SDL_KeyboardEvent & key);
  221. void mouseMoved(const SDL_MouseMotionEvent &sEvent);
  222. void clickRight(tribool down, bool previousState);
  223. //call-ins
  224. void startAction(const BattleAction* action);
  225. void newStack(const CStack * stack); //new stack appeared on battlefield
  226. void stackRemoved(int stackID); //stack disappeared from batlefiled
  227. void stackActivated(const CStack * stack); //active stack has been changed
  228. void stackMoved(const CStack * stack, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  229. void waitForAnims();
  230. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  231. void stackAttacking(const CStack * attacker, BattleHex dest, const CStack * attacked, bool shooting); //called when stack with id ID is attacking something on hex dest
  232. void newRoundFirst( int round );
  233. void newRound(int number); //caled when round is ended; number is the number of round
  234. void hexLclicked(int whichOne); //hex only call-in
  235. void stackIsCatapulting(const CatapultAttack & ca); //called when a stack is attacking walls
  236. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  237. const BattleResult * bresult; //result of a battle; if non-zero then display when all animations end
  238. void displayBattleFinished(); //displays battle result
  239. void spellCast(const BattleSpellCast * sc); //called when a hero casts a spell
  240. void battleStacksEffectsSet(const SetStackEffect & sse); //called when a specific effect is set to stacks
  241. void castThisSpell(int spellID); //called when player has chosen a spell from spellbook
  242. void displayEffect(ui32 effect, int destTile); //displays effect of a spell on the battlefield; affected: true - attacker. false - defender
  243. void battleTriggerEffect(const BattleTriggerEffect & bte);
  244. void setBattleCursor(const int myNumber); //really complex and messy, sets attackingHex
  245. void endAction(const BattleAction* action);
  246. void hideQueue();
  247. void showQueue();
  248. PossibleActions selectionTypeByPositiveness(const CSpell & spell);
  249. Rect hexPosition(BattleHex hex) const;
  250. void handleHex(BattleHex myNumber, int eventType);
  251. bool isCastingPossibleHere (const CStack * sactive, const CStack * shere, BattleHex myNumber);
  252. bool canStackMoveHere (const CStack * sactive, BattleHex MyNumber); //TODO: move to BattleState / callback
  253. BattleHex fromWhichHexAttack(BattleHex myNumber);
  254. void obstaclePlaced(const CObstacleInstance & oi);
  255. const CGHeroInstance * currentHero() const;
  256. InfoAboutHero enemyHero() const;
  257. friend class CPlayerInterface;
  258. friend class CAdventureMapButton;
  259. friend class CInGameConsole;
  260. friend class CBattleResultWindow;
  261. friend class CBattleHero;
  262. friend class CSpellEffectAnimation;
  263. friend class CBattleStackAnimation;
  264. friend class CReverseAnimation;
  265. friend class CDefenceAnimation;
  266. friend class CMovementAnimation;
  267. friend class CMovementStartAnimation;
  268. friend class CAttackAnimation;
  269. friend class CMeleeAttackAnimation;
  270. friend class CShootingAnimation;
  271. friend class CClickableHex;
  272. };