ObjectTemplate.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Checks if object can be placed on specific terrain
  86. bool canBePlacedAt(TerrainId terrain) const;
  87. ObjectTemplate();
  88. //custom copy constructor is required
  89. ObjectTemplate(const ObjectTemplate & other);
  90. ObjectTemplate& operator=(const ObjectTemplate & rhs);
  91. void readTxt(CLegacyConfigParser & parser);
  92. void readMsk();
  93. void readMap(CBinaryReader & reader);
  94. void readJson(const JsonNode & node, bool withTerrain = true);
  95. void writeJson(JsonNode & node, bool withTerrain = true) const;
  96. bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }
  97. private:
  98. ui32 width;
  99. ui32 height;
  100. bool visitable;
  101. std::set<int3> blockedOffsets;
  102. int3 blockMapOffset;
  103. int3 visitableOffset;
  104. void recalculate();
  105. void calculateWidth();
  106. void calculateHeight();
  107. void calculateVisitable();
  108. void calculateBlockedOffsets();
  109. void calculateBlockMapOffset();
  110. void calculateVisitableOffset();
  111. public:
  112. template <typename Handler> void serialize(Handler &h, const int version)
  113. {
  114. h & usedTiles;
  115. h & allowedTerrains;
  116. h & animationFile;
  117. h & stringID;
  118. h & id;
  119. h & subid;
  120. h & printPriority;
  121. h & visitDir;
  122. h & editorAnimationFile;
  123. if (!h.saving)
  124. {
  125. recalculate();
  126. }
  127. }
  128. };
  129. VCMI_LIB_NAMESPACE_END