ObjectTemplate.h 3.6 KB

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