ObjectTemplate.h 3.9 KB

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