CGObjectInstance.h 6.2 KB

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