ObjectTemplate.h 3.5 KB

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