Unit.h 5.1 KB

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