Unit.h 4.7 KB

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