CGObjectInstance.h 5.2 KB

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