Unit.h 4.4 KB

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