CGObjectInstance.h 6.3 KB

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