itemdef.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2013 Kahrl <kahrl@gmx.net>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #pragma once
  18. #include "irrlichttypes_extrabloated.h"
  19. #include <string>
  20. #include <iostream>
  21. #include <set>
  22. #include "itemgroup.h"
  23. #include "sound.h"
  24. class IGameDef;
  25. class Client;
  26. struct ToolCapabilities;
  27. #ifndef SERVER
  28. #include "client/tile.h"
  29. struct ItemMesh;
  30. struct ItemStack;
  31. #endif
  32. /*
  33. Base item definition
  34. */
  35. enum ItemType
  36. {
  37. ITEM_NONE,
  38. ITEM_NODE,
  39. ITEM_CRAFT,
  40. ITEM_TOOL,
  41. };
  42. struct ItemDefinition
  43. {
  44. /*
  45. Basic item properties
  46. */
  47. ItemType type;
  48. std::string name; // "" = hand
  49. std::string description; // Shown in tooltip.
  50. /*
  51. Visual properties
  52. */
  53. std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
  54. std::string inventory_overlay; // Overlay of inventory_image.
  55. std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
  56. std::string wield_overlay; // Overlay of wield_image.
  57. std::string palette_image; // If specified, the item will be colorized based on this
  58. video::SColor color; // The fallback color of the node.
  59. v3f wield_scale;
  60. /*
  61. Item stack and interaction properties
  62. */
  63. u16 stack_max;
  64. bool usable;
  65. bool liquids_pointable;
  66. // May be NULL. If non-NULL, deleted by destructor
  67. ToolCapabilities *tool_capabilities;
  68. ItemGroupList groups;
  69. SimpleSoundSpec sound_place;
  70. SimpleSoundSpec sound_place_failed;
  71. f32 range;
  72. // Client shall immediately place this node when player places the item.
  73. // Server will update the precise end result a moment later.
  74. // "" = no prediction
  75. std::string node_placement_prediction;
  76. /*
  77. Some helpful methods
  78. */
  79. ItemDefinition();
  80. ItemDefinition(const ItemDefinition &def);
  81. ItemDefinition& operator=(const ItemDefinition &def);
  82. ~ItemDefinition();
  83. void reset();
  84. void serialize(std::ostream &os, u16 protocol_version) const;
  85. void deSerialize(std::istream &is);
  86. private:
  87. void resetInitial();
  88. };
  89. class IItemDefManager
  90. {
  91. public:
  92. IItemDefManager() = default;
  93. virtual ~IItemDefManager() = default;
  94. // Get item definition
  95. virtual const ItemDefinition& get(const std::string &name) const=0;
  96. // Get alias definition
  97. virtual const std::string &getAlias(const std::string &name) const=0;
  98. // Get set of all defined item names and aliases
  99. virtual void getAll(std::set<std::string> &result) const=0;
  100. // Check if item is known
  101. virtual bool isKnown(const std::string &name) const=0;
  102. #ifndef SERVER
  103. // Get item inventory texture
  104. virtual video::ITexture* getInventoryTexture(const std::string &name,
  105. Client *client) const=0;
  106. // Get item wield mesh
  107. virtual ItemMesh* getWieldMesh(const std::string &name,
  108. Client *client) const=0;
  109. // Get item palette
  110. virtual Palette* getPalette(const std::string &name,
  111. Client *client) const = 0;
  112. // Returns the base color of an item stack: the color of all
  113. // tiles that do not define their own color.
  114. virtual video::SColor getItemstackColor(const ItemStack &stack,
  115. Client *client) const = 0;
  116. #endif
  117. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  118. };
  119. class IWritableItemDefManager : public IItemDefManager
  120. {
  121. public:
  122. IWritableItemDefManager() = default;
  123. virtual ~IWritableItemDefManager() = default;
  124. // Get item definition
  125. virtual const ItemDefinition& get(const std::string &name) const=0;
  126. // Get alias definition
  127. virtual const std::string &getAlias(const std::string &name) const=0;
  128. // Get set of all defined item names and aliases
  129. virtual void getAll(std::set<std::string> &result) const=0;
  130. // Check if item is known
  131. virtual bool isKnown(const std::string &name) const=0;
  132. #ifndef SERVER
  133. // Get item inventory texture
  134. virtual video::ITexture* getInventoryTexture(const std::string &name,
  135. Client *client) const=0;
  136. // Get item wield mesh
  137. virtual ItemMesh* getWieldMesh(const std::string &name,
  138. Client *client) const=0;
  139. #endif
  140. // Remove all registered item and node definitions and aliases
  141. // Then re-add the builtin item definitions
  142. virtual void clear()=0;
  143. // Register item definition
  144. virtual void registerItem(const ItemDefinition &def)=0;
  145. virtual void unregisterItem(const std::string &name)=0;
  146. // Set an alias so that items named <name> will load as <convert_to>.
  147. // Alias is not set if <name> has already been defined.
  148. // Alias will be removed if <name> is defined at a later point of time.
  149. virtual void registerAlias(const std::string &name,
  150. const std::string &convert_to)=0;
  151. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  152. virtual void deSerialize(std::istream &is)=0;
  153. // Do stuff asked by threads that can only be done in the main thread
  154. virtual void processQueue(IGameDef *gamedef)=0;
  155. };
  156. IWritableItemDefManager* createItemDefManager();