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. #include "../mapObjects/CompoundMapObjectID.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CBinaryReader;
  18. class CLegacyConfigParser;
  19. class JsonNode;
  20. class int3;
  21. class DLL_LINKAGE ObjectTemplate : public Serializeable
  22. {
  23. enum EBlockMapBits
  24. {
  25. VISIBLE = 1,
  26. VISITABLE = 2,
  27. BLOCKED = 4
  28. };
  29. /// tiles that are covered by this object, uses EBlockMapBits enum as flags
  30. std::vector<std::vector<ui8>> usedTiles;
  31. /// directions from which object can be entered, format same as for moveDir in CGHeroInstance(but 0 - 7)
  32. ui8 visitDir;
  33. /// list of terrains on which this object can be placed
  34. std::set<TerrainId> allowedTerrains;
  35. /// or, allow placing object on any terrain
  36. bool anyLandTerrain;
  37. void afterLoadFixup();
  38. public:
  39. /// H3 ID/subID of this object
  40. MapObjectID id;
  41. MapObjectSubID subid;
  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. CompoundMapObjectID getCompoundID() const;
  103. ObjectTemplate();
  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)
  128. {
  129. h & usedTiles;
  130. h & allowedTerrains;
  131. h & anyLandTerrain;
  132. h & animationFile;
  133. h & stringID;
  134. h & id;
  135. subid.serializeIdentifier(h, id);
  136. h & printPriority;
  137. h & visitDir;
  138. h & editorAnimationFile;
  139. if (!h.saving)
  140. {
  141. recalculate();
  142. }
  143. }
  144. };
  145. VCMI_LIB_NAMESPACE_END