BattleProcessor.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * BattleProcessor.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/GameConstants.h"
  12. #include "../../lib/NetPacks.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. struct SideInBattle;
  16. namespace battle {
  17. class CUnitState;
  18. }
  19. VCMI_LIB_NAMESPACE_END
  20. class CBattleQuery;
  21. struct CasualtiesAfterBattle
  22. {
  23. using TStackAndItsNewCount = std::pair<StackLocation, int>;
  24. using TSummoned = std::map<CreatureID, TQuantity>;
  25. enum {ERASE = -1};
  26. const CArmedInstance * army;
  27. std::vector<TStackAndItsNewCount> newStackCounts;
  28. std::vector<ArtifactLocation> removedWarMachines;
  29. TSummoned summoned;
  30. ObjectInstanceID heroWithDeadCommander; //TODO: unify stack locations
  31. CasualtiesAfterBattle(const SideInBattle & battleSide, const BattleInfo * bat);
  32. void updateArmy(CGameHandler *gh);
  33. };
  34. struct FinishingBattleHelper
  35. {
  36. FinishingBattleHelper();
  37. FinishingBattleHelper(std::shared_ptr<const CBattleQuery> Query, int RemainingBattleQueriesCount);
  38. inline bool isDraw() const {return winnerSide == 2;}
  39. const CGHeroInstance *winnerHero, *loserHero;
  40. PlayerColor victor, loser;
  41. ui8 winnerSide;
  42. int remainingBattleQueriesCount;
  43. template <typename Handler> void serialize(Handler &h, const int version)
  44. {
  45. h & winnerHero;
  46. h & loserHero;
  47. h & victor;
  48. h & loser;
  49. h & winnerSide;
  50. h & remainingBattleQueriesCount;
  51. }
  52. };
  53. using FireShieldInfo = std::vector<std::pair<const CStack *, int64_t>>;
  54. class BattleProcessor : boost::noncopyable
  55. {
  56. ////used only in endBattle - don't touch elsewhere
  57. bool visitObjectAfterVictory;
  58. std::unique_ptr<boost::thread> battleThread;
  59. std::unique_ptr<FinishingBattleHelper> finishingBattle;
  60. void removeObstacle(const CObstacleInstance &obstacle);
  61. void makeStackDoNothing(const CStack * next);
  62. void updateGateState();
  63. bool makeAutomaticAction(const CStack *stack, BattleAction &ba); //used when action is taken by stack without volition of player (eg. unguided catapult attack)
  64. void stackEnchantedTrigger(const CStack * stack);
  65. void stackTurnTrigger(const CStack *stack);
  66. void engageIntoBattle( PlayerColor player );
  67. void handleAttackBeforeCasting(bool ranged, const CStack * attacker, const CStack * defender);
  68. void handleAfterAttackCasting(bool ranged, const CStack * attacker, const CStack * defender);
  69. void attackCasting(bool ranged, BonusType attackMode, const battle::Unit * attacker, const battle::Unit * defender);
  70. int moveStack(int stack, BattleHex dest); //returned value - travelled distance
  71. void runBattle();
  72. void makeAttack(const CStack * attacker, const CStack * defender, int distance, BattleHex targetHex, bool first, bool ranged, bool counter);
  73. // damage, drain life & fire shield; returns amount of drained life
  74. int64_t applyBattleEffects(BattleAttack & bat, std::shared_ptr<battle::CUnitState> attackerState, FireShieldInfo & fireShield, const CStack * def, int distance, bool secondary);
  75. void sendGenericKilledLog(const CStack * defender, int32_t killed, bool multiple);
  76. void addGenericKilledLog(BattleLogMessage & blm, const CStack * defender, int32_t killed, bool multiple);
  77. void checkBattleStateChanges();
  78. void setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town);
  79. void setBattleResult(BattleResult::EResult resultType, int victoriusSide);
  80. public:
  81. CGameHandler * gameHandler;
  82. BattleProcessor(CGameHandler * gameHandler);
  83. BattleProcessor();
  84. ~BattleProcessor();
  85. void startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, const CGTownInstance *town = nullptr); //use hero=nullptr for no hero
  86. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank = false); //if any of armies is hero, hero will be used
  87. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank = false); //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  88. void battleAfterLevelUp(const BattleResult &result);
  89. bool makeBattleAction(BattleAction &ba);
  90. bool makeCustomAction(BattleAction &ba);
  91. void endBattle(int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2); //ends battle
  92. void endBattleConfirm(const BattleInfo * battleInfo);
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. }
  96. };