CGObjectInstance.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * CGObjectInstance.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 "IObjectInterface.h"
  12. #include "../int3.h"
  13. #include "../bonuses/BonusEnum.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class JsonSerializeFormat;
  16. class ObjectTemplate;
  17. class CMap;
  18. class DLL_LINKAGE CGObjectInstance : public IObjectInterface
  19. {
  20. public:
  21. /// Position of bottom-right corner of object on map
  22. int3 pos;
  23. /// Type of object, e.g. town, hero, creature.
  24. Obj ID;
  25. /// Subtype of object, depends on type
  26. si32 subID;
  27. /// Current owner of an object (when below PLAYER_LIMIT)
  28. PlayerColor tempOwner;
  29. /// Index of object in map's list of objects
  30. ObjectInstanceID id;
  31. /// Defines appearance of object on map (animation, blocked tiles, blit order, etc)
  32. std::shared_ptr<const ObjectTemplate> appearance;
  33. /// If true hero can visit this object only from neighbouring tiles and can't stand on this object
  34. bool blockVisit;
  35. std::string instanceName;
  36. std::string typeName;
  37. std::string subTypeName;
  38. CGObjectInstance(); //TODO: remove constructor
  39. ~CGObjectInstance() override;
  40. int32_t getObjGroupIndex() const override;
  41. int32_t getObjTypeIndex() const override;
  42. /// "center" tile from which the sight distance is calculated
  43. int3 getSightCenter() const;
  44. PlayerColor getOwner() const override
  45. {
  46. return this->tempOwner;
  47. }
  48. void setOwner(const PlayerColor & ow);
  49. /** APPEARANCE ACCESSORS **/
  50. int getWidth() const; //returns width of object graphic in tiles
  51. int getHeight() const; //returns height of object graphic in tiles
  52. bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  53. int3 visitablePos() const override;
  54. int3 getPosition() const override;
  55. int3 getTopVisiblePos() const;
  56. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  57. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  58. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  59. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  60. bool isVisitable() const; //returns true if object is visitable
  61. virtual BattleField getBattlefield() const;
  62. virtual bool isTile2Terrain() const { return false; }
  63. std::optional<std::string> getAmbientSound() const;
  64. std::optional<std::string> getVisitSound() const;
  65. std::optional<std::string> getRemovalSound() const;
  66. /** VIRTUAL METHODS **/
  67. /// Returns true if player can pass through visitable tiles of this object
  68. virtual bool passableFor(PlayerColor color) const;
  69. /// Range of revealed map around this object, counting from getSightCenter()
  70. virtual int getSightRadius() const;
  71. /// returns (x,y,0) offset to a visitable tile of object
  72. virtual int3 getVisitableOffset() const;
  73. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  74. virtual void setType(si32 ID, si32 subID);
  75. /// returns text visible in status bar with specific hero/player active.
  76. /// Returns generic name of object, without any player-specific info
  77. virtual std::string getObjectName() const;
  78. /// Returns hover name for situation when there are no selected heroes. Default = object name
  79. virtual std::string getHoverText(PlayerColor player) const;
  80. /// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
  81. virtual std::string getHoverText(const CGHeroInstance * hero) const;
  82. /** OVERRIDES OF IObjectInterface **/
  83. void initObj(CRandomGenerator & rand) override;
  84. void onHeroVisit(const CGHeroInstance * h) const override;
  85. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  86. void setProperty(ui8 what, ui32 val) final;
  87. virtual void afterAddToMap(CMap * map);
  88. virtual void afterRemoveFromMap(CMap * map);
  89. ///Entry point of binary (de-)serialization
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & instanceName;
  93. h & typeName;
  94. h & subTypeName;
  95. h & pos;
  96. h & ID;
  97. h & subID;
  98. h & id;
  99. h & tempOwner;
  100. h & blockVisit;
  101. h & appearance;
  102. //definfo is handled by map serializer
  103. }
  104. ///Entry point of Json (de-)serialization
  105. void serializeJson(JsonSerializeFormat & handler);
  106. virtual void updateFrom(const JsonNode & data);
  107. protected:
  108. /// virtual method that allows synchronously update object state on server and all clients
  109. virtual void setPropertyDer(ui8 what, ui32 val);
  110. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  111. void giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration = BonusDuration::ONE_DAY) const;
  112. ///Serialize object-type specific options
  113. virtual void serializeJsonOptions(JsonSerializeFormat & handler);
  114. void serializeJsonOwner(JsonSerializeFormat & handler);
  115. };
  116. VCMI_LIB_NAMESPACE_END