2
0

CObjectClassesHandler.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #include "ObjectTemplate.h"
  3. #include "../GameConstants.h"
  4. #include "../ConstTransitivePtr.h"
  5. #include "../IHandlerBase.h"
  6. /*
  7. * CObjectClassesHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class JsonNode;
  16. class CRandomGenerator;
  17. class IObjectInfo
  18. {
  19. public:
  20. virtual bool givesResources() const = 0;
  21. virtual bool givesExperience() const = 0;
  22. virtual bool givesMana() const = 0;
  23. virtual bool givesMovement() const = 0;
  24. virtual bool givesPrimarySkills() const = 0;
  25. virtual bool givesSecondarySkills() const = 0;
  26. virtual bool givesArtifacts() const = 0;
  27. virtual bool givesCreatures() const = 0;
  28. virtual bool givesSpells() const = 0;
  29. virtual bool givesBonuses() const = 0;
  30. };
  31. class CGObjectInstance;
  32. class AObjectTypeHandler
  33. {
  34. si32 type;
  35. si32 subtype;
  36. JsonNode base; /// describes base template
  37. std::vector<ObjectTemplate> templates;
  38. protected:
  39. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  40. public:
  41. virtual ~AObjectTypeHandler(){}
  42. void setType(si32 type, si32 subtype);
  43. /// loads templates from Json structure using fields "base" and "templates"
  44. virtual void init(const JsonNode & input);
  45. void addTemplate(ObjectTemplate templ);
  46. void addTemplate(JsonNode config);
  47. /// returns all templates, without any filters
  48. std::vector<ObjectTemplate> getTemplates() const;
  49. /// returns all templates that can be placed on specific terrain type
  50. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  51. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  52. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  53. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  54. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  55. /// to allow creating objects before game start (e.g. map loading)
  56. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  57. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  58. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  59. /// Returns object configuration, if available. Othervice returns NULL
  60. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & type & subtype & templates;
  64. }
  65. };
  66. /// Class that is used for objects that do not have dedicated handler
  67. template<class ObjectType>
  68. class CDefaultObjectTypeHandler : public AObjectTypeHandler
  69. {
  70. CGObjectInstance * create(ObjectTemplate tmpl) const
  71. {
  72. auto obj = new ObjectType();
  73. obj->ID = tmpl.id;
  74. obj->subID = tmpl.subid;
  75. obj->appearance = tmpl;
  76. return obj;
  77. }
  78. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  79. {
  80. }
  81. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const
  82. {
  83. return nullptr;
  84. }
  85. };
  86. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  87. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  88. {
  89. /// Small internal structure that contains information on specific group of objects
  90. /// (creating separate entity is overcomplicating at least at this point)
  91. struct ObjectContainter
  92. {
  93. si32 id;
  94. std::string name; // human-readable name
  95. std::string handlerName; // ID of handler that controls this object, shoul be determined using hadlerConstructor map
  96. JsonNode base;
  97. std::map<si32, TObjectTypeHandler> objects;
  98. template <typename Handler> void serialize(Handler &h, const int version)
  99. {
  100. h & base & objects;
  101. }
  102. };
  103. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  104. /// list of object handlers, each of them handles only one type
  105. std::map<si32, ObjectContainter * > objects;
  106. /// map that is filled during contruction with all known handlers. Not serializeable
  107. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  108. /// container with H3 templates, used only during loading
  109. TTemplatesContainer legacyTemplates;
  110. void loadObjectEntry(const JsonNode & entry, ObjectContainter * obj);
  111. ObjectContainter * loadFromJson(const JsonNode & json);
  112. public:
  113. CObjectClassesHandler();
  114. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  115. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  116. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  117. void createObject(std::string name, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  118. void eraseObject(si32 ID, si32 subID);
  119. void beforeValidate(JsonNode & object) override;
  120. void afterLoadFinalization() override;
  121. std::vector<bool> getDefaultAllowed() const override;
  122. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  123. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  124. std::string getObjectName(si32 type) const;
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & objects;
  128. }
  129. };