AObjectTypeHandler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * AObjectTypeHandler.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 "../constants/EntityIdentifiers.h"
  12. #include "RandomMapInfo.h"
  13. #include "SObjectSounds.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. namespace vstd
  16. {
  17. class RNG;
  18. }
  19. class ObjectTemplate;
  20. class CGObjectInstance;
  21. class IObjectInfo;
  22. class IGameInfoCallback;
  23. /// Class responsible for creation of objects of specific type & subtype
  24. class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
  25. {
  26. friend class CObjectClassesHandler;
  27. RandomMapInfo rmgInfo;
  28. std::unique_ptr<JsonNode> base; /// describes base template
  29. std::vector<std::shared_ptr<const ObjectTemplate>> templates;
  30. SObjectSounds sounds;
  31. std::optional<si32> aiValue;
  32. BattleField battlefield;
  33. std::string modScope;
  34. std::string typeName;
  35. std::string subTypeName;
  36. si32 type;
  37. si32 subtype;
  38. bool blockVisit;
  39. bool removable;
  40. protected:
  41. void preInitObject(CGObjectInstance * obj) const;
  42. virtual bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const;
  43. /// initialization for classes that inherit this one
  44. virtual void initTypeData(const JsonNode & input);
  45. virtual void onTemplateAdded(const std::shared_ptr<const ObjectTemplate>) {}
  46. public:
  47. AObjectTypeHandler();
  48. virtual ~AObjectTypeHandler();
  49. si32 getIndex() const;
  50. si32 getSubIndex() const;
  51. std::string getTypeName() const;
  52. std::string getSubTypeName() const;
  53. /// loads generic data from Json structure and passes it towards type-specific constructors
  54. void init(const JsonNode & input);
  55. /// returns full form of identifier of this object in form of modName:objectName
  56. std::string getJsonKey() const;
  57. std::string getModScope() const;
  58. /// Returns object-specific name, if set
  59. SObjectSounds getSounds() const;
  60. void addTemplate(const std::shared_ptr<const ObjectTemplate> & templ);
  61. void addTemplate(JsonNode config);
  62. void clearTemplates();
  63. /// returns all templates matching parameters
  64. std::vector<std::shared_ptr<const ObjectTemplate>> getTemplates() const;
  65. std::vector<std::shared_ptr<const ObjectTemplate>> getTemplates(const TerrainId terrainType) const;
  66. std::vector<std::shared_ptr<const ObjectTemplate>> getMostSpecificTemplates(TerrainId terrainType) const;
  67. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  68. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  69. virtual std::shared_ptr<const ObjectTemplate> getOverride(TerrainId terrainType, const CGObjectInstance * object) const;
  70. BattleField getBattlefield() const;
  71. const RandomMapInfo & getRMGInfo();
  72. std::optional<si32> getAiValue() const;
  73. /// returns true if this class provides custom text ID's instead of generic per-object name
  74. virtual bool hasNameTextID() const;
  75. /// returns base prefix for all translatable strings of this object
  76. std::string getBaseTextID() const;
  77. /// returns object's name in form of translatable text ID
  78. virtual std::string getNameTextID() const;
  79. /// returns object's name in form of human-readable text
  80. std::string getNameTranslated() const;
  81. virtual bool isStaticObject();
  82. virtual void afterLoadFinalization();
  83. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  84. /// to allow creating objects before game start (e.g. map loading)
  85. virtual std::shared_ptr<CGObjectInstance> create(IGameInfoCallback * cb, std::shared_ptr<const ObjectTemplate> tmpl) const = 0;
  86. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  87. /// This should set remaining properties, including randomized or depending on map
  88. virtual void configureObject(CGObjectInstance * object, vstd::RNG & rng) const = 0;
  89. /// Returns object configuration, if available. Otherwise returns NULL
  90. virtual std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const;
  91. };
  92. VCMI_LIB_NAMESPACE_END