CDefObjInfoHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include "GameConstants.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. /*
  5. * CDefObjInfoHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CBinaryReader;
  14. class CLegacyConfigParser;
  15. class JsonNode;
  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. public:
  31. /// H3 ID/subID of this object
  32. Obj id;
  33. si32 subid;
  34. /// print priority, objects with higher priority will be print first, below everything else
  35. si32 printPriority;
  36. /// animation file that should be used to display object
  37. std::string animationFile;
  38. ui32 getWidth() const;
  39. ui32 getHeight() const;
  40. void setSize(ui32 width, ui32 height);
  41. bool isVisitable() const;
  42. // Checks object used tiles
  43. // Position is relative to bottom-right corner of the object, can not be negative
  44. bool isWithin(si32 X, si32 Y) const;
  45. bool isVisitableAt(si32 X, si32 Y) const;
  46. bool isVisibleAt(si32 X, si32 Y) const;
  47. bool isBlockedAt(si32 X, si32 Y) const;
  48. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  49. bool isVisitableFrom(si8 X, si8 Y) const;
  50. // Checks if object can be placed on specific terrain
  51. bool canBePlacedAt(ETerrainType terrain) const;
  52. ObjectTemplate();
  53. void readTxt(CLegacyConfigParser & parser);
  54. void readMsk();
  55. void readMap(CBinaryReader & reader);
  56. void readJson(const JsonNode & node);
  57. template <typename Handler> void serialize(Handler &h, const int version)
  58. {
  59. h & usedTiles & allowedTerrains & animationFile;
  60. h & id & subid & printPriority & visitDir;
  61. }
  62. };
  63. class DLL_LINKAGE CDefObjInfoHandler
  64. {
  65. /// list of all object templates loaded from text files
  66. /// actual object have ObjectTemplate as member "appearance"
  67. std::vector<ObjectTemplate> objects;
  68. /// reads one of H3 text files that contain object templates description
  69. void readTextFile(std::string path);
  70. public:
  71. CDefObjInfoHandler();
  72. /// Erases all templates with given type/subtype
  73. void eraseAll(Obj type, si32 subtype);
  74. /// Add new template into the list
  75. void registerTemplate(ObjectTemplate obj);
  76. /// picks all possible candidates for specific pair <type, subtype>
  77. std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype) const;
  78. /// picks all candidates for <type, subtype> and of possible - also filters them by terrain
  79. std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype, ETerrainType terrain) const;
  80. // TODO: as above, but also filters out templates that are not applicable according to accepted test
  81. // std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype, ETerrainType terrain, std::function<bool(ObjectTemplate &)> accept) const;
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & objects;
  85. }
  86. };