CDefObjInfoHandler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 CRandomGenerator;
  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<ETerrainType> allowedTerrains;
  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. /// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
  40. std::string stringID;
  41. ui32 getWidth() const;
  42. ui32 getHeight() const;
  43. void setSize(ui32 width, ui32 height);
  44. bool isVisitable() const;
  45. // Checks object used tiles
  46. // Position is relative to bottom-right corner of the object, can not be negative
  47. bool isWithin(si32 X, si32 Y) const;
  48. bool isVisitableAt(si32 X, si32 Y) const;
  49. bool isVisibleAt(si32 X, si32 Y) const;
  50. bool isBlockedAt(si32 X, si32 Y) const;
  51. // Checks if object is visitable from certain direction. X and Y must be between -1..+1
  52. bool isVisitableFrom(si8 X, si8 Y) const;
  53. // Checks if object can be placed on specific terrain
  54. bool canBePlacedAt(ETerrainType terrain) const;
  55. ObjectTemplate();
  56. void readTxt(CLegacyConfigParser & parser);
  57. void readMsk();
  58. void readMap(CBinaryReader & reader);
  59. void readJson(const JsonNode & node);
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & usedTiles & allowedTerrains & animationFile & stringID;
  63. h & id & subid & printPriority & visitDir;
  64. }
  65. };
  66. class IObjectInfo
  67. {
  68. public:
  69. virtual bool givesResources() const = 0;
  70. virtual bool givesExperience() const = 0;
  71. virtual bool givesMana() const = 0;
  72. virtual bool givesMovement() const = 0;
  73. virtual bool givesPrimarySkills() const = 0;
  74. virtual bool givesSecondarySkills() const = 0;
  75. virtual bool givesArtifacts() const = 0;
  76. virtual bool givesCreatures() const = 0;
  77. virtual bool givesSpells() const = 0;
  78. virtual bool givesBonuses() const = 0;
  79. };
  80. class CGObjectInstance;
  81. class AObjectTypeHandler
  82. {
  83. si32 type;
  84. si32 subtype;
  85. std::vector<ObjectTemplate> templates;
  86. protected:
  87. void init(si32 type, si32 subtype);
  88. /// loads templates from Json structure using fields "base" and "templates"
  89. void load(const JsonNode & input);
  90. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  91. public:
  92. void addTemplate(const ObjectTemplate & templ);
  93. /// returns all templates, without any filters
  94. std::vector<ObjectTemplate> getTemplates() const;
  95. /// returns all templates that can be placed on specific terrain type
  96. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  97. /// returns template suitable for object. If returned template is not equal to current one
  98. /// it must be replaced with this one (and properly updated on all clients)
  99. ObjectTemplate selectTemplate(si32 terrainType, CGObjectInstance * object) const;
  100. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  101. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  102. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
  103. };
  104. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  105. class CObjectTypesHandler
  106. {
  107. /// list of object handlers, each of them handles only one type
  108. std::map<si32, std::map<si32, TObjectTypeHandler> > objectTypes;
  109. public:
  110. void init();
  111. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  112. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  113. template <typename Handler> void serialize(Handler &h, const int version)
  114. {
  115. //h & objects;
  116. if (!h.saving)
  117. init(); // TODO: implement serialization
  118. }
  119. };