Unit.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Unit.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 <vcmi/Creature.h>
  12. #include <vcmi/spells/Caster.h>
  13. #include "../bonuses/Bonus.h"
  14. #include "../bonuses/IBonusBearer.h"
  15. #include "IUnitInfo.h"
  16. #include "BattleHex.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. struct MetaString;
  19. class JsonNode;
  20. class JsonSerializeFormat;
  21. namespace battle
  22. {
  23. namespace BattlePhases
  24. {
  25. enum Type
  26. {
  27. SIEGE, // turrets/catapult,
  28. NORMAL, // normal (unmoved) creatures, other war machines,
  29. WAIT_MORALE, // waited creatures that had morale,
  30. WAIT, // rest of waited creatures
  31. NUMBER_OF_PHASES // number of phases.
  32. };
  33. }
  34. class CUnitState;
  35. class DLL_LINKAGE Unit : public IUnitInfo, public spells::Caster, public virtual IBonusBearer, public ACreature
  36. {
  37. public:
  38. virtual ~Unit();
  39. virtual bool doubleWide() const = 0;
  40. virtual int32_t creatureIndex() const = 0;
  41. virtual CreatureID creatureId() const = 0;
  42. virtual int32_t creatureLevel() const = 0;
  43. virtual int32_t creatureCost() const = 0;
  44. virtual int32_t creatureIconIndex() const = 0;
  45. virtual bool ableToRetaliate() const = 0;
  46. virtual bool alive() const = 0;
  47. virtual bool isGhost() const = 0;
  48. virtual bool isFrozen() const = 0;
  49. bool isDead() const;
  50. bool isTurret() const;
  51. virtual bool isValidTarget(bool allowDead = false) const = 0; //non-turret non-ghost stacks (can be attacked or be object of magic effect)
  52. virtual bool isClone() const = 0;
  53. virtual bool hasClone() const = 0;
  54. virtual bool canCast() const = 0;
  55. virtual bool isCaster() const = 0;
  56. virtual bool canShoot() const = 0;
  57. virtual bool isShooter() const = 0;
  58. /// returns initial size of this unit
  59. virtual int32_t getCount() const = 0;
  60. /// returns remaining health of first unit
  61. virtual int32_t getFirstHPleft() const = 0;
  62. /// returns total amount of killed in this unit
  63. virtual int32_t getKilled() const = 0;
  64. /// returns total health that unit still has
  65. virtual int64_t getAvailableHealth() const = 0;
  66. /// returns total health that unit had initially
  67. virtual int64_t getTotalHealth() const = 0;
  68. virtual int getTotalAttacks(bool ranged) const = 0;
  69. virtual BattleHex getPosition() const = 0;
  70. virtual void setPosition(BattleHex hex) = 0;
  71. virtual int32_t getInitiative(int turn = 0) const = 0;
  72. virtual bool canMove(int turn = 0) const = 0; //if stack can move
  73. virtual bool defended(int turn = 0) const = 0;
  74. virtual bool moved(int turn = 0) const = 0; //if stack was already moved this turn
  75. virtual bool willMove(int turn = 0) const = 0; //if stack has remaining move this turn
  76. virtual bool waited(int turn = 0) const = 0;
  77. virtual std::shared_ptr<Unit> acquire() const = 0;
  78. virtual std::shared_ptr<CUnitState> acquireState() const = 0;
  79. virtual BattlePhases::Type battleQueuePhase(int turn) const = 0;
  80. virtual std::string getDescription() const;
  81. std::vector<BattleHex> getSurroundingHexes(BattleHex assumedPosition = BattleHex::INVALID) const; // get six or 8 surrounding hexes depending on creature size
  82. std::vector<BattleHex> getAttackableHexes(const Unit * attacker) const;
  83. static std::vector<BattleHex> getSurroundingHexes(BattleHex position, bool twoHex, ui8 side);
  84. bool coversPos(BattleHex position) const; //checks also if unit is double-wide
  85. std::vector<BattleHex> getHexes() const; //up to two occupied hexes, starting from front
  86. std::vector<BattleHex> getHexes(BattleHex assumedPos) const; //up to two occupied hexes, starting from front
  87. static std::vector<BattleHex> getHexes(BattleHex assumedPos, bool twoHex, ui8 side);
  88. BattleHex occupiedHex() const; //returns number of occupied hex (not the position) if stack is double wide; otherwise -1
  89. BattleHex occupiedHex(BattleHex assumedPos) const; //returns number of occupied hex (not the position) if stack is double wide and would stand on assumedPos; otherwise -1
  90. static BattleHex occupiedHex(BattleHex assumedPos, bool twoHex, ui8 side);
  91. ///MetaStrings
  92. void addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  93. void addNameReplacement(MetaString & text, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  94. std::string formatGeneralMessage(const int32_t baseTextId) const;
  95. int getRawSurrenderCost() const;
  96. //IConstBonusProvider
  97. const IBonusBearer* getBonusBearer() const override;
  98. //NOTE: save could possibly be const, but this requires heavy changes to Json serialization,
  99. //also this method should be called only after modifying object
  100. virtual void save(JsonNode & data) = 0;
  101. virtual void load(const JsonNode & data) = 0;
  102. virtual void damage(int64_t & amount) = 0;
  103. virtual void heal(int64_t & amount, EHealLevel level, EHealPower power) = 0;
  104. };
  105. class DLL_LINKAGE UnitInfo
  106. {
  107. public:
  108. uint32_t id = 0;
  109. TQuantity count = 0;
  110. CreatureID type;
  111. ui8 side = 0;
  112. BattleHex position;
  113. bool summoned = false;
  114. void serializeJson(JsonSerializeFormat & handler);
  115. void save(JsonNode & data);
  116. void load(uint32_t id_, const JsonNode & data);
  117. };
  118. }
  119. VCMI_LIB_NAMESPACE_END