ObjectTemplate.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class CBinaryReader;
  13. class CLegacyConfigParser;
  14. class JsonNode;
  15. class int3;
  16. class DLL_LINKAGE ObjectTemplate
  17. {
  18. enum EBlockMapBits
  19. {
  20. VISIBLE = 1,
  21. VISITABLE = 2,
  22. BLOCKED = 4
  23. };
  24. /// tiles that are covered by this object, uses EBlockMapBits enum as flags
  25. std::vector<std::vector<ui8>> usedTiles;
  26. /// directions from which object can be entered, format same as for moveDir in CGHeroInstance(but 0 - 7)
  27. ui8 visitDir;
  28. /// list of terrains on which this object can be placed
  29. std::set<ETerrainType> allowedTerrains;
  30. void afterLoadFixup();
  31. public:
  32. /// H3 ID/subID of this object
  33. Obj id;
  34. si32 subid;
  35. /// print priority, objects with higher priority will be print first, below everything else
  36. si32 printPriority;
  37. /// animation file that should be used to display object
  38. std::string animationFile;
  39. /// map editor only animation file
  40. std::string editorAnimationFile;
  41. /// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
  42. std::string stringID;
  43. ui32 getWidth() const;
  44. ui32 getHeight() const;
  45. void setSize(ui32 width, ui32 height);
  46. bool isVisitable() const;
  47. // Checks object used tiles
  48. // Position is relative to bottom-right corner of the object, can not be negative
  49. bool isWithin(si32 X, si32 Y) const;
  50. bool isVisitableAt(si32 X, si32 Y) const;
  51. bool isVisibleAt(si32 X, si32 Y) const;
  52. bool isBlockedAt(si32 X, si32 Y) const;
  53. std::set<int3> getBlockedOffsets() const;
  54. int3 getBlockMapOffset() const; //bottom-right corner when firts blocked tile is
  55. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  56. bool isVisitableFrom(si8 X, si8 Y) const;
  57. int3 getVisitableOffset() const;
  58. bool isVisitableFromTop() const;
  59. // Checks if object can be placed on specific terrain
  60. bool canBePlacedAt(ETerrainType terrain) const;
  61. ObjectTemplate();
  62. //custom copy constructor is required
  63. ObjectTemplate(const ObjectTemplate & other);
  64. ObjectTemplate& operator=(const ObjectTemplate & rhs);
  65. void readTxt(CLegacyConfigParser & parser);
  66. void readMsk();
  67. void readMap(CBinaryReader & reader);
  68. void readJson(const JsonNode & node, const bool withTerrain = true);
  69. void writeJson(JsonNode & node, const bool withTerrain = true) const;
  70. bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }
  71. template <typename Handler> void serialize(Handler &h, const int version)
  72. {
  73. h & usedTiles;
  74. h & allowedTerrains;
  75. h & animationFile;
  76. h & stringID;
  77. h & id;
  78. h & subid;
  79. h & printPriority;
  80. h & visitDir;
  81. if(version >= 770)
  82. {
  83. h & editorAnimationFile;
  84. }
  85. }
  86. };