BattleState.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma once
  2. #include "../global.h"
  3. #include "HeroBonus.h"
  4. #include "CCreatureSet.h"
  5. #include "ConstTransitivePtr.h"
  6. /*
  7. * BattleState.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 CGHeroInstance;
  16. class CStack;
  17. class CArmedInstance;
  18. class CGTownInstance;
  19. class CStackInstance;
  20. struct DLL_EXPORT CObstacleInstance
  21. {
  22. int uniqueID;
  23. int ID; //ID of obstacle (defines type of it)
  24. int pos; //position on battlefield
  25. template <typename Handler> void serialize(Handler &h, const int version)
  26. {
  27. h & ID & pos & uniqueID;
  28. }
  29. };
  30. //only for use in BattleInfo
  31. struct DLL_EXPORT SiegeInfo
  32. {
  33. ui8 wallState[8]; //[0] - keep, [1] - bottom tower, [2] - bottom wall, [3] - below gate, [4] - over gate, [5] - upper wall, [6] - uppert tower, [7] - gate; 1 - intact, 2 - damaged, 3 - destroyed
  34. template <typename Handler> void serialize(Handler &h, const int version)
  35. {
  36. h & wallState;
  37. }
  38. };
  39. struct DLL_EXPORT BattleInfo : public CBonusSystemNode
  40. {
  41. ui8 side1, side2; //side1 - attacker, side2 - defender
  42. si32 round, activeStack;
  43. ui8 siege; // = 0 ordinary battle = 1 a siege with a Fort = 2 a siege with a Citadel = 3 a siege with a Castle
  44. si32 tid; //used during town siege - id of attacked town; -1 if not town defence
  45. int3 tile; //for background and bonuses
  46. CGHeroInstance *heroes[2];
  47. CArmedInstance *belligerents[2]; //may be same as heroes
  48. std::vector<CStack*> stacks;
  49. std::vector<CObstacleInstance> obstacles;
  50. ui8 castSpells[2]; //[0] - attacker, [1] - defender
  51. SiegeInfo si;
  52. si32 battlefieldType;
  53. template <typename Handler> void serialize(Handler &h, const int version)
  54. {
  55. h & side1 & side2 & round & activeStack & siege & tid & tile & stacks & belligerents & obstacles
  56. & castSpells & si & battlefieldType;
  57. h & heroes;
  58. h & static_cast<CBonusSystemNode&>(*this);
  59. }
  60. //////////////////////////////////////////////////////////////////////////
  61. //void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
  62. //////////////////////////////////////////////////////////////////////////
  63. const CStack * getNextStack() const; //which stack will have turn after current one
  64. void getStackQueue(std::vector<const CStack *> &out, int howMany, int turn = 0, int lastMoved = -1) const; //returns stack in order of their movement action
  65. CStack * getStack(int stackID, bool onlyAlive = true);
  66. const CStack * getStack(int stackID, bool onlyAlive = true) const;
  67. CStack * getStackT(THex tileID, bool onlyAlive = true);
  68. const CStack * getStackT(THex tileID, bool onlyAlive = true) const;
  69. void getAccessibilityMap(bool *accessibility, bool twoHex, bool attackerOwned, bool addOccupiable, std::set<int> & occupyable, bool flying, int stackToOmmit=-1) const; //send pointer to at least 187 allocated bytes
  70. static bool isAccessible(int hex, bool * accessibility, bool twoHex, bool attackerOwned, bool flying, bool lastPos); //helper for makeBFS
  71. void makeBFS(int start, bool*accessibility, int *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
  72. std::pair< std::vector<int>, int > getPath(int start, int dest, bool*accessibility, bool flyingCreature, bool twoHex, bool attackerOwned); //returned value: pair<path, length>; length may be different than number of elements in path since flying vreatures jump between distant hexes
  73. std::vector<int> getAccessibility(int stackID, bool addOccupiable) const; //returns vector of accessible tiles (taking into account the creature range)
  74. bool isStackBlocked(int ID); //returns true if there is neighboring enemy stack
  75. static signed char mutualPosition(THex hex1, THex hex2); //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
  76. static std::vector<int> neighbouringTiles(int hex);
  77. static si8 getDistance(THex hex1, THex hex2); //returns distance between given hexes
  78. ui32 calculateDmg(const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero, bool shooting, ui8 charge, bool lucky); //charge - number of hexes travelled before attack (for champion's jousting)
  79. std::pair<ui32, ui32> calculateDmgRange(const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero, bool shooting, ui8 charge, bool lucky); //charge - number of hexes travelled before attack (for champion's jousting); returns pair <min dmg, max dmg>
  80. void calculateCasualties(std::map<ui32,si32> *casualties) const; //casualties are array of maps size 2 (attacker, defeneder), maps are (crid => amount)
  81. std::set<CStack*> getAttackedCreatures(const CSpell * s, int skillLevel, ui8 attackerOwner, int destinationTile); //calculates stack affected by given spell
  82. static int calculateSpellDuration(const CSpell * spell, const CGHeroInstance * caster, int usedSpellPower);
  83. CStack * generateNewStack(const CStackInstance &base, int stackID, bool attackerOwned, int slot, int position) const; //helper for CGameHandler::setupBattle and spells addign new stacks to the battlefield
  84. CStack * generateNewStack(const CStackBasicDescriptor &base, int stackID, bool attackerOwned, int slot, int position) const; //helper for CGameHandler::setupBattle and spells addign new stacks to the battlefield
  85. ui32 getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
  86. int hexToWallPart(int hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  87. int lineToWallHex(int line) const; //returns hex with wall in given line
  88. std::pair<const CStack *, int> getNearestStack(const CStack * closest, boost::logic::tribool attackerOwned) const; //if attackerOwned is indetermnate, returened stack is of any owner; hex is the number of hex we should be looking from; returns (nerarest creature, predecessorHex)
  89. ui32 calculateSpellBonus(ui32 baseDamage, const CSpell * sp, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  90. ui32 calculateSpellDmg(const CSpell * sp, const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const; //calculates damage inflicted by spell
  91. ui32 calculateHealedHP(const CGHeroInstance * caster, const CSpell * spell, const CStack * stack) const;
  92. si8 hasDistancePenalty(int stackID, int destHex); //determines if given stack has distance penalty shooting given pos
  93. si8 sameSideOfWall(int pos1, int pos2); //determines if given positions are on the same side of wall
  94. si8 hasWallPenalty(int stackID, int destHex); //determines if given stack has wall penalty shooting given pos
  95. si8 canTeleportTo(int stackID, int destHex, int telportLevel); //determines if given stack can teleport to given place
  96. void localInit();
  97. static BattleInfo * BattleInfo::setupBattle( int3 tile, int terrain, int terType, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town );
  98. };
  99. class DLL_EXPORT CStack : public CBonusSystemNode, public CStackBasicDescriptor
  100. {
  101. public:
  102. const CStackInstance *base;
  103. ui32 ID; //unique ID of stack
  104. ui32 baseAmount;
  105. ui32 firstHPleft; //HP of first creature in stack
  106. ui8 owner, slot; //owner - player colour (255 for neutrals), slot - position in garrison (may be 255 for neutrals/called creatures)
  107. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  108. THex position; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  109. ui8 counterAttacks; //how many counter attacks can be performed more in this turn (by default set at the beginning of the round to 1)
  110. si16 shots; //how many shots left
  111. std::set<ECombatInfo> state;
  112. //overrides
  113. const CCreature* getCreature() const {return type;}
  114. CStack(const CStackInstance *base, int O, int I, bool AO, int S); //c-tor
  115. CStack(const CStackBasicDescriptor *stack, int O, int I, bool AO, int S = 255); //c-tor
  116. CStack(); //c-tor
  117. ~CStack();
  118. std::string nodeName() const OVERRIDE;
  119. void init(); //set initial (invalid) values
  120. void postInit(); //used to finish initialization when inheriting creature parameters is working
  121. const Bonus * getEffect(ui16 id, int turn = 0) const; //effect id (SP)
  122. ui8 howManyEffectsSet(ui16 id) const; //returns amount of effects with given id set for this stack
  123. bool willMove(int turn = 0) const; //if stack has remaining move this turn
  124. bool moved(int turn = 0) const; //if stack was already moved this turn
  125. bool canMove(int turn = 0) const; //if stack can move
  126. ui32 Speed(int turn = 0) const; //get speed of creature with all modificators
  127. BonusList getSpellBonuses() const;
  128. void stackEffectToFeature(BonusList & sf, const Bonus & sse);
  129. std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  130. const CGHeroInstance *getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, NULL otherwise
  131. static inline Bonus *featureGenerator(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0, si32 limit = Bonus::NO_LIMIT)
  132. {
  133. Bonus *hb = makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain, additionalInfo);
  134. hb->effectRange = limit;
  135. hb->source = Bonus::CASTED_SPELL; //right?
  136. return hb;
  137. }
  138. static inline Bonus *featureGeneratorVT(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, ui8 valType)
  139. {
  140. Bonus *ret(makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain));
  141. ret->valType = valType;
  142. ret->source = Bonus::CASTED_SPELL; //right?
  143. return ret;
  144. }
  145. bool doubleWide() const;
  146. int occupiedHex() const; //returns number of occupied hex (not the position) if stack is double wide; otherwise -1
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. assert(isIndependentNode());
  150. h & static_cast<CBonusSystemNode&>(*this);
  151. h & static_cast<CStackBasicDescriptor&>(*this);
  152. h & ID & baseAmount & firstHPleft & owner & slot & attackerOwned & position & state & counterAttacks
  153. & shots & count;
  154. TSlot slot = (base ? base->armyObj->findStack(base) : -1);
  155. const CArmedInstance *army = (base ? base->armyObj : NULL);
  156. if(h.saving)
  157. {
  158. h & army & slot;
  159. }
  160. else
  161. {
  162. h & army & slot;
  163. if(!army || slot == -1 || !army->hasStackAtSlot(slot))
  164. {
  165. base = NULL;
  166. tlog3 << type->nameSing << " don't have a base stack!\n";
  167. }
  168. else
  169. {
  170. base = &army->getStack(slot);
  171. }
  172. }
  173. }
  174. bool alive() const //determines if stack is alive
  175. {
  176. return vstd::contains(state,ALIVE);
  177. }
  178. };
  179. class DLL_EXPORT CMP_stack
  180. {
  181. int phase; //rules of which phase will be used
  182. int turn;
  183. public:
  184. bool operator ()(const CStack* a, const CStack* b);
  185. CMP_stack(int Phase = 1, int Turn = 0);
  186. };