ObjectTemplate.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ObjectTemplate.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 "../GameConstants.h"
  12. #include "../int3.h"
  13. #include "../filesystem/ResourcePath.h"
  14. #include "../serializer/Serializeable.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CBinaryReader;
  17. class CLegacyConfigParser;
  18. class JsonNode;
  19. class int3;
  20. class DLL_LINKAGE ObjectTemplate : public Serializeable
  21. {
  22. enum EBlockMapBits
  23. {
  24. VISIBLE = 1,
  25. VISITABLE = 2,
  26. BLOCKED = 4
  27. };
  28. /// tiles that are covered by this object, uses EBlockMapBits enum as flags
  29. std::vector<std::vector<ui8>> usedTiles;
  30. /// directions from which object can be entered, format same as for moveDir in CGHeroInstance(but 0 - 7)
  31. ui8 visitDir;
  32. /// list of terrains on which this object can be placed
  33. std::set<TerrainId> allowedTerrains;
  34. /// or, allow placing object on any terrain
  35. bool anyLandTerrain;
  36. void afterLoadFixup();
  37. public:
  38. /// H3 ID/subID of this object
  39. MapObjectID id;
  40. MapObjectSubID subid;
  41. /// print priority, objects with higher priority will be print first, below everything else
  42. si32 printPriority;
  43. /// animation file that should be used to display object
  44. AnimationPath animationFile;
  45. /// map editor only animation file
  46. AnimationPath editorAnimationFile;
  47. /// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
  48. std::string stringID;
  49. inline ui32 getWidth() const
  50. {
  51. return width;
  52. };
  53. inline ui32 getHeight() const
  54. {
  55. return height;
  56. };
  57. void setSize(ui32 width, ui32 height);
  58. inline bool isVisitable() const
  59. {
  60. return visitable;
  61. };
  62. // Checks object used tiles
  63. // Position is relative to bottom-right corner of the object, can not be negative
  64. bool isWithin(si32 X, si32 Y) const;
  65. bool isVisitableAt(si32 X, si32 Y) const;
  66. bool isVisibleAt(si32 X, si32 Y) const;
  67. bool isBlockedAt(si32 X, si32 Y) const;
  68. inline const std::set<int3> & getBlockedOffsets() const
  69. {
  70. return blockedOffsets;
  71. };
  72. inline int3 getBlockMapOffset() const
  73. {
  74. return blockMapOffset;
  75. }
  76. inline int3 getTopVisibleOffset() const
  77. {
  78. return topVisibleOffset;
  79. }
  80. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  81. bool isVisitableFrom(si8 X, si8 Y) const;
  82. inline int3 getVisitableOffset() const
  83. {
  84. //logGlobal->warn("Warning: getVisitableOffset called on non-visitable obj!");
  85. return visitableOffset;
  86. };
  87. inline bool isVisitableFromTop() const
  88. {
  89. return visitDir & 2;
  90. };
  91. inline bool canBePlacedAtAnyTerrain() const
  92. {
  93. return anyLandTerrain;
  94. };
  95. const std::set<TerrainId>& getAllowedTerrains() const
  96. {
  97. return allowedTerrains;
  98. }
  99. // Checks if object can be placed on specific terrain
  100. bool canBePlacedAt(TerrainId terrain) const;
  101. ObjectTemplate();
  102. //custom copy constructor is required
  103. ObjectTemplate(const ObjectTemplate & other);
  104. ObjectTemplate& operator=(const ObjectTemplate & rhs);
  105. void readTxt(CLegacyConfigParser & parser);
  106. void readMsk();
  107. void readMap(CBinaryReader & reader);
  108. void readJson(const JsonNode & node, bool withTerrain = true);
  109. void writeJson(JsonNode & node, bool withTerrain = true) const;
  110. bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }
  111. private:
  112. ui32 width;
  113. ui32 height;
  114. bool visitable;
  115. std::set<int3> blockedOffsets;
  116. int3 blockMapOffset;
  117. int3 visitableOffset;
  118. int3 topVisibleOffset;
  119. void recalculate();
  120. void calculateWidth();
  121. void calculateHeight();
  122. void calculateVisitable();
  123. void calculateBlockedOffsets();
  124. void calculateBlockMapOffset();
  125. void calculateVisitableOffset();
  126. void calculateTopVisibleOffset();
  127. public:
  128. template <typename Handler> void serialize(Handler &h)
  129. {
  130. h & usedTiles;
  131. h & allowedTerrains;
  132. h & anyLandTerrain;
  133. h & animationFile;
  134. h & stringID;
  135. h & id;
  136. subid.serializeIdentifier(h, id);
  137. h & printPriority;
  138. h & visitDir;
  139. h & editorAnimationFile;
  140. if (!h.saving)
  141. {
  142. recalculate();
  143. }
  144. }
  145. };
  146. VCMI_LIB_NAMESPACE_END