Unit.h 4.2 KB

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