ObjectTemplate.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // TODO: get compound id
  42. /// print priority, objects with higher priority will be print first, below everything else
  43. si32 printPriority;
  44. /// animation file that should be used to display object
  45. AnimationPath animationFile;
  46. /// map editor only animation file
  47. AnimationPath editorAnimationFile;
  48. /// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
  49. std::string stringID;
  50. inline ui32 getWidth() const
  51. {
  52. return width;
  53. };
  54. inline ui32 getHeight() const
  55. {
  56. return height;
  57. };
  58. void setSize(ui32 width, ui32 height);
  59. inline bool isVisitable() const
  60. {
  61. return visitable;
  62. };
  63. // Checks object used tiles
  64. // Position is relative to bottom-right corner of the object, can not be negative
  65. bool isWithin(si32 X, si32 Y) const;
  66. bool isVisitableAt(si32 X, si32 Y) const;
  67. bool isVisibleAt(si32 X, si32 Y) const;
  68. bool isBlockedAt(si32 X, si32 Y) const;
  69. inline const std::set<int3> & getBlockedOffsets() const
  70. {
  71. return blockedOffsets;
  72. };
  73. inline int3 getBlockMapOffset() const
  74. {
  75. return blockMapOffset;
  76. }
  77. inline int3 getTopVisibleOffset() const
  78. {
  79. return topVisibleOffset;
  80. }
  81. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  82. bool isVisitableFrom(si8 X, si8 Y) const;
  83. inline int3 getVisitableOffset() const
  84. {
  85. //logGlobal->warn("Warning: getVisitableOffset called on non-visitable obj!");
  86. return visitableOffset;
  87. };
  88. inline bool isVisitableFromTop() const
  89. {
  90. return visitDir & 2;
  91. };
  92. inline bool canBePlacedAtAnyTerrain() const
  93. {
  94. return anyLandTerrain;
  95. };
  96. const std::set<TerrainId>& getAllowedTerrains() const
  97. {
  98. return allowedTerrains;
  99. }
  100. // Checks if object can be placed on specific terrain
  101. bool canBePlacedAt(TerrainId terrain) const;
  102. ObjectTemplate();
  103. void readTxt(CLegacyConfigParser & parser);
  104. void readMsk();
  105. void readMap(CBinaryReader & reader);
  106. void readJson(const JsonNode & node, bool withTerrain = true);
  107. void writeJson(JsonNode & node, bool withTerrain = true) const;
  108. bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }
  109. private:
  110. ui32 width;
  111. ui32 height;
  112. bool visitable;
  113. std::set<int3> blockedOffsets;
  114. int3 blockMapOffset;
  115. int3 visitableOffset;
  116. int3 topVisibleOffset;
  117. void recalculate();
  118. void calculateWidth();
  119. void calculateHeight();
  120. void calculateVisitable();
  121. void calculateBlockedOffsets();
  122. void calculateBlockMapOffset();
  123. void calculateVisitableOffset();
  124. void calculateTopVisibleOffset();
  125. public:
  126. template <typename Handler> void serialize(Handler &h)
  127. {
  128. h & usedTiles;
  129. h & allowedTerrains;
  130. h & anyLandTerrain;
  131. h & animationFile;
  132. h & stringID;
  133. h & id;
  134. subid.serializeIdentifier(h, id);
  135. h & printPriority;
  136. h & visitDir;
  137. h & editorAnimationFile;
  138. if (!h.saving)
  139. {
  140. recalculate();
  141. }
  142. }
  143. };
  144. VCMI_LIB_NAMESPACE_END