CGObjectInstance.h 6.0 KB

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