CGObjectInstance.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(); //TODO: remove constructor
  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. PlayerColor getOwner() const override
  50. {
  51. return this->tempOwner;
  52. }
  53. void setOwner(const PlayerColor & ow);
  54. /** APPEARANCE ACCESSORS **/
  55. int getWidth() const; //returns width of object graphic in tiles
  56. int getHeight() const; //returns height of object graphic in tiles
  57. int3 visitablePos() const override;
  58. int3 getPosition() const override;
  59. int3 getTopVisiblePos() const;
  60. bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  61. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  62. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  63. bool visitableAt(const int3 & pos) const; //returns true if object is visitable at location (x, y) (h3m pos)
  64. bool blockingAt (const int3 & pos) const; //returns true if object is blocking location (x, y) (h3m pos)
  65. bool coveringAt (const int3 & pos) const; //returns true if object covers with picture location (x, y) (h3m pos)
  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 this object can be visited by hero standing on the coast
  73. virtual bool isCoastVisitable() const;
  74. virtual BattleField getBattlefield() const;
  75. virtual bool isTile2Terrain() const { return false; }
  76. std::optional<AudioPath> getAmbientSound() const;
  77. std::optional<AudioPath> getVisitSound() const;
  78. std::optional<AudioPath> getRemovalSound() const;
  79. TObjectTypeHandler getObjectHandler() const;
  80. /** VIRTUAL METHODS **/
  81. /// Returns true if player can pass through visitable tiles of this object
  82. virtual bool passableFor(PlayerColor color) const;
  83. /// Range of revealed map around this object, counting from getSightCenter()
  84. virtual int getSightRadius() const;
  85. /// returns (x,y,0) offset to a visitable tile of object
  86. virtual int3 getVisitableOffset() const;
  87. /// Returns generic name of object, without any player-specific info
  88. virtual std::string getObjectName() const;
  89. /// Returns hover name for situation when there are no selected heroes. Default = object name
  90. virtual std::string getHoverText(PlayerColor player) const;
  91. /// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
  92. virtual std::string getHoverText(const CGHeroInstance * hero) const;
  93. virtual std::string getPopupText(PlayerColor player) const;
  94. virtual std::string getPopupText(const CGHeroInstance * hero) const;
  95. virtual std::vector<Component> getPopupComponents(PlayerColor player) const;
  96. virtual std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const;
  97. /** OVERRIDES OF IObjectInterface **/
  98. void initObj(CRandomGenerator & rand) override;
  99. void pickRandomObject(CRandomGenerator & rand) override;
  100. void onHeroVisit(const CGHeroInstance * h) const override;
  101. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  102. void setProperty(ObjProperty what, ObjPropertyID identifier) final;
  103. virtual void afterAddToMap(CMap * map);
  104. virtual void afterRemoveFromMap(CMap * map);
  105. ///Entry point of binary (de-)serialization
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & instanceName;
  109. h & typeName;
  110. h & subTypeName;
  111. h & pos;
  112. h & ID;
  113. h & subID;
  114. h & id;
  115. h & tempOwner;
  116. h & blockVisit;
  117. h & appearance;
  118. //definfo is handled by map serializer
  119. }
  120. ///Entry point of Json (de-)serialization
  121. void serializeJson(JsonSerializeFormat & handler);
  122. virtual void updateFrom(const JsonNode & data);
  123. protected:
  124. /// virtual method that allows synchronously update object state on server and all clients
  125. virtual void setPropertyDer(ObjProperty what, ObjPropertyID identifier);
  126. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  127. void setType(MapObjectID ID, MapObjectSubID subID);
  128. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  129. void giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration = BonusDuration::ONE_DAY) const;
  130. ///Serialize object-type specific options
  131. virtual void serializeJsonOptions(JsonSerializeFormat & handler);
  132. void serializeJsonOwner(JsonSerializeFormat & handler);
  133. };
  134. VCMI_LIB_NAMESPACE_END