CDefObjInfoHandler.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
  39. std::string stringID;
  40. ui32 getWidth() const;
  41. ui32 getHeight() const;
  42. void setSize(ui32 width, ui32 height);
  43. bool isVisitable() const;
  44. // Checks object used tiles
  45. // Position is relative to bottom-right corner of the object, can not be negative
  46. bool isWithin(si32 X, si32 Y) const;
  47. bool isVisitableAt(si32 X, si32 Y) const;
  48. bool isVisibleAt(si32 X, si32 Y) const;
  49. bool isBlockedAt(si32 X, si32 Y) const;
  50. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  51. bool isVisitableFrom(si8 X, si8 Y) const;
  52. // Checks if object can be placed on specific terrain
  53. bool canBePlacedAt(ETerrainType terrain) const;
  54. ObjectTemplate();
  55. void readTxt(CLegacyConfigParser & parser);
  56. void readMsk();
  57. void readMap(CBinaryReader & reader);
  58. void readJson(const JsonNode & node);
  59. template <typename Handler> void serialize(Handler &h, const int version)
  60. {
  61. h & usedTiles & allowedTerrains & animationFile & stringID;
  62. h & id & subid & printPriority & visitDir;
  63. }
  64. };
  65. class DLL_LINKAGE CDefObjInfoHandler
  66. {
  67. /// list of all object templates loaded from text files
  68. /// actual object have ObjectTemplate as member "appearance"
  69. std::vector<ObjectTemplate> objects;
  70. /// reads one of H3 text files that contain object templates description
  71. void readTextFile(std::string path);
  72. public:
  73. CDefObjInfoHandler();
  74. /// Erases all templates with given type/subtype
  75. void eraseAll(Obj type, si32 subtype);
  76. /// Add new template into the list
  77. void registerTemplate(ObjectTemplate obj);
  78. /// picks all possible candidates for specific pair <type, subtype>
  79. std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype) const;
  80. /// picks all candidates for <type, subtype> and of possible - also filters them by terrain
  81. std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype, ETerrainType terrain) const;
  82. /// as above, but also filters out templates that are not applicable according to accepted test
  83. std::vector<ObjectTemplate> pickCandidates(Obj type, si32 subtype, ETerrainType terrain, std::function<bool(ObjectTemplate &)> filter) const;
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & objects;
  87. }
  88. };
  89. class IObjectInfo
  90. {
  91. public:
  92. virtual bool givesResources() const = 0;
  93. virtual bool givesExperience() const = 0;
  94. virtual bool givesMana() const = 0;
  95. virtual bool givesMovement() const = 0;
  96. virtual bool givesPrimarySkills() const = 0;
  97. virtual bool givesSecondarySkills() const = 0;
  98. virtual bool givesArtifacts() const = 0;
  99. virtual bool givesCreatures() const = 0;
  100. virtual bool givesSpells() const = 0;
  101. virtual bool givesBonuses() const = 0;
  102. };
  103. class CGObjectInstance;
  104. class IObjectTypesHandler
  105. {
  106. public:
  107. virtual std::vector<ObjectTemplate> getTemplates(si32 type, si32 subType) const = 0;
  108. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  109. virtual bool handlesID(ObjectTemplate tmpl) const = 0;
  110. virtual void configureObject(CGObjectInstance * object) const = 0;
  111. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
  112. };
  113. typedef std::shared_ptr<IObjectTypesHandler> TObjectTypeHandler;
  114. class CObjectGroupsHandler
  115. {
  116. /// list of object handlers, each of them handles 1 or more object type
  117. std::vector<TObjectTypeHandler> objectTypes;
  118. public:
  119. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  120. IObjectTypesHandler * getHandlerFor(ObjectTemplate tmpl) const;
  121. /// creates object based on specified template
  122. CGObjectInstance * createObject(ObjectTemplate tmpl);
  123. template<typename CObjectClass>
  124. CObjectClass * createObjectTyped(ObjectTemplate tmpl)
  125. {
  126. auto objInst = createObject(tmpl);
  127. auto objClass = dynamic_cast<CObjectClass*>(objInst);
  128. assert(objClass);
  129. return objClass;
  130. }
  131. };