ObjectTemplate.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Terrain;
  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<Terrain> 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. ui32 getWidth() const;
  45. ui32 getHeight() const;
  46. void setSize(ui32 width, ui32 height);
  47. bool isVisitable() const;
  48. // Checks object used tiles
  49. // Position is relative to bottom-right corner of the object, can not be negative
  50. bool isWithin(si32 X, si32 Y) const;
  51. bool isVisitableAt(si32 X, si32 Y) const;
  52. bool isVisibleAt(si32 X, si32 Y) const;
  53. bool isBlockedAt(si32 X, si32 Y) const;
  54. std::set<int3> getBlockedOffsets() const;
  55. int3 getBlockMapOffset() const; //bottom-right corner when firts blocked tile is
  56. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  57. bool isVisitableFrom(si8 X, si8 Y) const;
  58. int3 getVisitableOffset() const;
  59. bool isVisitableFromTop() const;
  60. // Checks if object can be placed on specific terrain
  61. bool canBePlacedAt(Terrain terrain) const;
  62. ObjectTemplate();
  63. //custom copy constructor is required
  64. ObjectTemplate(const ObjectTemplate & other);
  65. ObjectTemplate& operator=(const ObjectTemplate & rhs);
  66. void readTxt(CLegacyConfigParser & parser);
  67. void readMsk();
  68. void readMap(CBinaryReader & reader);
  69. void readJson(const JsonNode & node, const bool withTerrain = true);
  70. void writeJson(JsonNode & node, const bool withTerrain = true) const;
  71. bool operator==(const ObjectTemplate& ot) const { return (id == ot.id && subid == ot.subid); }
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & usedTiles;
  75. h & allowedTerrains;
  76. h & animationFile;
  77. h & stringID;
  78. h & id;
  79. h & subid;
  80. h & printPriority;
  81. h & visitDir;
  82. h & editorAnimationFile;
  83. }
  84. };