CGObjectInstance.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 DLL_LINKAGE CGObjectInstance : public IObjectInterface
  22. {
  23. public:
  24. /// Position of bottom-right corner of object on map
  25. int3 pos;
  26. /// Type of object, e.g. town, hero, creature.
  27. Obj ID;
  28. /// Subtype of object, depends on type
  29. si32 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. std::string instanceName;
  37. std::string typeName;
  38. std::string subTypeName;
  39. CGObjectInstance(); //TODO: remove constructor
  40. ~CGObjectInstance() override;
  41. int32_t getObjGroupIndex() const override;
  42. int32_t getObjTypeIndex() const override;
  43. /// "center" tile from which the sight distance is calculated
  44. int3 getSightCenter() const;
  45. /// If true hero can visit this object only from neighbouring tiles and can't stand on this object
  46. bool blockVisit;
  47. PlayerColor getOwner() const override
  48. {
  49. return this->tempOwner;
  50. }
  51. void setOwner(const PlayerColor & ow);
  52. /** APPEARANCE ACCESSORS **/
  53. int getWidth() const; //returns width of object graphic in tiles
  54. int getHeight() const; //returns height of object graphic in tiles
  55. int3 visitablePos() const override;
  56. int3 getPosition() const override;
  57. int3 getTopVisiblePos() const;
  58. bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  59. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  60. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  61. bool visitableAt(const int3 & pos) const; //returns true if object is visitable at location (x, y) (h3m pos)
  62. bool blockingAt (const int3 & pos) const; //returns true if object is blocking location (x, y) (h3m pos)
  63. bool coveringAt (const int3 & pos) const; //returns true if object covers with picture location (x, y) (h3m pos)
  64. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  65. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  66. /// returns true if object is visitable
  67. bool isVisitable() const;
  68. /// If true hero can visit this object only from neighbouring tiles and can't stand on this object
  69. virtual bool isBlockedVisitable() const;
  70. /// If true this object can be visited by hero standing on the coast
  71. virtual bool isCoastVisitable() const;
  72. virtual BattleField getBattlefield() const;
  73. virtual bool isTile2Terrain() const { return false; }
  74. std::optional<AudioPath> getAmbientSound() const;
  75. std::optional<AudioPath> getVisitSound() const;
  76. std::optional<AudioPath> getRemovalSound() const;
  77. /** VIRTUAL METHODS **/
  78. /// Returns true if player can pass through visitable tiles of this object
  79. virtual bool passableFor(PlayerColor color) const;
  80. /// Range of revealed map around this object, counting from getSightCenter()
  81. virtual int getSightRadius() const;
  82. /// returns (x,y,0) offset to a visitable tile of object
  83. virtual int3 getVisitableOffset() const;
  84. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  85. virtual void setType(si32 ID, si32 subID);
  86. /// returns text visible in status bar with specific hero/player active.
  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 onHeroVisit(const CGHeroInstance * h) const override;
  100. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  101. void setProperty(ui8 what, ui32 val) final;
  102. virtual void afterAddToMap(CMap * map);
  103. virtual void afterRemoveFromMap(CMap * map);
  104. ///Entry point of binary (de-)serialization
  105. template <typename Handler> void serialize(Handler &h, const int version)
  106. {
  107. h & instanceName;
  108. h & typeName;
  109. h & subTypeName;
  110. h & pos;
  111. h & ID;
  112. h & subID;
  113. h & id;
  114. h & tempOwner;
  115. h & blockVisit;
  116. h & appearance;
  117. //definfo is handled by map serializer
  118. }
  119. ///Entry point of Json (de-)serialization
  120. void serializeJson(JsonSerializeFormat & handler);
  121. virtual void updateFrom(const JsonNode & data);
  122. protected:
  123. /// virtual method that allows synchronously update object state on server and all clients
  124. virtual void setPropertyDer(ui8 what, ui32 val);
  125. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  126. void giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration = BonusDuration::ONE_DAY) const;
  127. ///Serialize object-type specific options
  128. virtual void serializeJsonOptions(JsonSerializeFormat & handler);
  129. void serializeJsonOwner(JsonSerializeFormat & handler);
  130. };
  131. VCMI_LIB_NAMESPACE_END