ObjectTemplate.h 3.5 KB

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