ObjectTemplate.h 3.5 KB

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