BattleInfo.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * BattleInfo.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 "../SiegeInfo.h"
  12. #include "../SideInBattle.h"
  13. #include "../HeroBonus.h"
  14. #include "CBattleInfoCallback.h"
  15. #include "../int3.h"
  16. class CStack;
  17. class CStackInstance;
  18. class CStackBasicDescriptor;
  19. struct DLL_LINKAGE BattleInfo : public CBonusSystemNode, public CBattleInfoCallback
  20. {
  21. std::array<SideInBattle, 2> sides; //sides[0] - attacker, sides[1] - defender
  22. si32 round, activeStack, selectedStack;
  23. const CGTownInstance * town; //used during town siege, nullptr if this is not a siege (note that fortless town IS also a siege)
  24. int3 tile; //for background and bonuses
  25. std::vector<CStack*> stacks;
  26. std::vector<std::shared_ptr<CObstacleInstance> > obstacles;
  27. SiegeInfo si;
  28. BFieldType battlefieldType; //like !!BA:B
  29. ETerrainType terrainType; //used for some stack nativity checks (not the bonus limiters though that have their own copy)
  30. ui8 tacticsSide; //which side is requested to play tactics phase
  31. ui8 tacticDistance; //how many hexes we can go forward (1 = only hexes adjacent to margin line)
  32. template <typename Handler> void serialize(Handler &h, const int version)
  33. {
  34. h & sides;
  35. h & round & activeStack & selectedStack & town & tile & stacks & obstacles
  36. & si & battlefieldType & terrainType;
  37. h & tacticsSide & tacticDistance;
  38. h & static_cast<CBonusSystemNode&>(*this);
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. BattleInfo();
  42. ~BattleInfo(){};
  43. //////////////////////////////////////////////////////////////////////////
  44. CStack * getStack(int stackID, bool onlyAlive = true);
  45. using CBattleInfoEssentials::battleGetArmyObject;
  46. CArmedInstance * battleGetArmyObject(ui8 side) const;
  47. using CBattleInfoEssentials::battleGetFightingHero;
  48. CGHeroInstance * battleGetFightingHero(ui8 side) const;
  49. const CStack * getNextStack() const; //which stack will have turn after current one
  50. //void getStackQueue(std::vector<const CStack *> &out, int howMany, int turn = 0, int lastMoved = -1) const; //returns stack in order of their movement action
  51. //void getAccessibilityMap(bool *accessibility, bool twoHex, bool attackerOwned, bool addOccupiable, std::set<BattleHex> & occupyable, bool flying, const CStack* stackToOmmit = nullptr) const; //send pointer to at least 187 allocated bytes
  52. //static bool isAccessible(BattleHex hex, bool * accessibility, bool twoHex, bool attackerOwned, bool flying, bool lastPos); //helper for makeBFS
  53. int getAvaliableHex(CreatureID creID, bool attackerOwned, int initialPos = -1) const; //find place for summon / clone effects
  54. //void makeBFS(BattleHex start, bool*accessibility, BattleHex *predecessor, int *dists, bool twoHex, bool attackerOwned, bool flying, bool fillPredecessors) const; //*accessibility must be prepared bool[187] array; last two pointers must point to the at least 187-elements int arrays - there is written result
  55. std::pair< std::vector<BattleHex>, int > getPath(BattleHex start, BattleHex dest, const CStack * stack); //returned value: pair<path, length>; length may be different than number of elements in path since flying vreatures jump between distant hexes
  56. //std::vector<BattleHex> getAccessibility(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable = nullptr, bool forPassingBy = false) const; //returns vector of accessible tiles (taking into account the creature range)
  57. //bool isObstacleVisibleForSide(const CObstacleInstance &obstacle, ui8 side) const;
  58. std::shared_ptr<CObstacleInstance> getObstacleOnTile(BattleHex tile) const;
  59. std::set<BattleHex> getStoppers(bool whichSidePerspective) const;
  60. ui32 calculateDmg(const CStack * attacker, const CStack * defender, bool shooting, ui8 charge, bool lucky, bool unlucky, bool deathBlow, bool ballistaDoubleDmg, CRandomGenerator & rand); //charge - number of hexes travelled before attack (for champion's jousting)
  61. void calculateCasualties(std::map<ui32,si32> * casualties) const; //casualties are array of maps size 2 (attacker, defeneder), maps are (crid => amount)
  62. //void getPotentiallyAttackableHexes(AttackableTiles &at, const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos); //hexes around target that could be attacked in melee
  63. //std::set<CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID); //calculates range of multi-hex attacks
  64. //std::set<BattleHex> getAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID); //calculates range of multi-hex attacks
  65. CStack * generateNewStack(const CStackInstance &base, bool attackerOwned, SlotID slot, BattleHex position) const; //helper for CGameHandler::setupBattle and spells addign new stacks to the battlefield
  66. CStack * generateNewStack(const CStackBasicDescriptor &base, bool attackerOwned, SlotID slot, BattleHex position) const; //helper for CGameHandler::setupBattle and spells addign new stacks to the battlefield
  67. int getIdForNewStack() const; //suggest a currently unused ID that'd suitable for generating a new stack
  68. const CGHeroInstance * getHero(PlayerColor player) const; //returns fighting hero that belongs to given player
  69. void localInit();
  70. void localInitStack(CStack * s);
  71. static BattleInfo * setupBattle(int3 tile, ETerrainType terrain, BFieldType battlefieldType, const CArmedInstance * armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance * town);
  72. //bool hasNativeStack(ui8 side) const;
  73. PlayerColor theOtherPlayer(PlayerColor player) const;
  74. ui8 whatSide(PlayerColor player) const;
  75. static BattlefieldBI::BattlefieldBI battlefieldTypeToBI(BFieldType bfieldType); //converts above to ERM BI format
  76. static int battlefieldTypeToTerrain(int bfieldType); //converts above to ERM BI format
  77. };
  78. class DLL_LINKAGE CMP_stack
  79. {
  80. int phase; //rules of which phase will be used
  81. int turn;
  82. public:
  83. bool operator ()(const CStack* a, const CStack* b);
  84. CMP_stack(int Phase = 1, int Turn = 0);
  85. };