itemdef.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "texture_override.h" // TextureOverride
  25. class IGameDef;
  26. class Client;
  27. struct ToolCapabilities;
  28. #ifndef SERVER
  29. #include "client/tile.h"
  30. struct ItemMesh;
  31. struct ItemStack;
  32. #endif
  33. /*
  34. Base item definition
  35. */
  36. enum ItemType
  37. {
  38. ITEM_NONE,
  39. ITEM_NODE,
  40. ITEM_CRAFT,
  41. ITEM_TOOL,
  42. };
  43. struct ItemDefinition
  44. {
  45. /*
  46. Basic item properties
  47. */
  48. ItemType type;
  49. std::string name; // "" = hand
  50. std::string description; // Shown in tooltip.
  51. std::string short_description;
  52. /*
  53. Visual properties
  54. */
  55. std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
  56. std::string inventory_overlay; // Overlay of inventory_image.
  57. std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
  58. std::string wield_overlay; // Overlay of wield_image.
  59. std::string palette_image; // If specified, the item will be colorized based on this
  60. video::SColor color; // The fallback color of the node.
  61. v3f wield_scale;
  62. /*
  63. Item stack and interaction properties
  64. */
  65. u16 stack_max;
  66. bool usable;
  67. bool liquids_pointable;
  68. // May be NULL. If non-NULL, deleted by destructor
  69. ToolCapabilities *tool_capabilities;
  70. ItemGroupList groups;
  71. SimpleSoundSpec sound_place;
  72. SimpleSoundSpec sound_place_failed;
  73. SimpleSoundSpec sound_use, sound_use_air;
  74. f32 range;
  75. // Client shall immediately place this node when player places the item.
  76. // Server will update the precise end result a moment later.
  77. // "" = no prediction
  78. std::string node_placement_prediction;
  79. u8 place_param2;
  80. /*
  81. Some helpful methods
  82. */
  83. ItemDefinition();
  84. ItemDefinition(const ItemDefinition &def);
  85. ItemDefinition& operator=(const ItemDefinition &def);
  86. ~ItemDefinition();
  87. void reset();
  88. void serialize(std::ostream &os, u16 protocol_version) const;
  89. void deSerialize(std::istream &is, u16 protocol_version);
  90. private:
  91. void resetInitial();
  92. };
  93. class IItemDefManager
  94. {
  95. public:
  96. IItemDefManager() = default;
  97. virtual ~IItemDefManager() = default;
  98. // Get item definition
  99. virtual const ItemDefinition& get(const std::string &name) const=0;
  100. // Get alias definition
  101. virtual const std::string &getAlias(const std::string &name) const=0;
  102. // Get set of all defined item names and aliases
  103. virtual void getAll(std::set<std::string> &result) const=0;
  104. // Check if item is known
  105. virtual bool isKnown(const std::string &name) const=0;
  106. #ifndef SERVER
  107. // Get item inventory texture
  108. virtual video::ITexture* getInventoryTexture(const std::string &name,
  109. Client *client) const=0;
  110. // Get item wield mesh
  111. virtual ItemMesh* getWieldMesh(const std::string &name,
  112. Client *client) const=0;
  113. // Get item palette
  114. virtual Palette* getPalette(const std::string &name,
  115. Client *client) const = 0;
  116. // Returns the base color of an item stack: the color of all
  117. // tiles that do not define their own color.
  118. virtual video::SColor getItemstackColor(const ItemStack &stack,
  119. Client *client) const = 0;
  120. #endif
  121. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  122. };
  123. class IWritableItemDefManager : public IItemDefManager
  124. {
  125. public:
  126. IWritableItemDefManager() = default;
  127. virtual ~IWritableItemDefManager() = default;
  128. // Get item definition
  129. virtual const ItemDefinition& get(const std::string &name) const=0;
  130. // Get alias definition
  131. virtual const std::string &getAlias(const std::string &name) const=0;
  132. // Get set of all defined item names and aliases
  133. virtual void getAll(std::set<std::string> &result) const=0;
  134. // Check if item is known
  135. virtual bool isKnown(const std::string &name) const=0;
  136. #ifndef SERVER
  137. // Get item inventory texture
  138. virtual video::ITexture* getInventoryTexture(const std::string &name,
  139. Client *client) const=0;
  140. // Get item wield mesh
  141. virtual ItemMesh* getWieldMesh(const std::string &name,
  142. Client *client) const=0;
  143. #endif
  144. // Replace the textures of registered nodes with the ones specified in
  145. // the texture pack's override.txt files
  146. virtual void applyTextureOverrides(const std::vector<TextureOverride> &overrides)=0;
  147. // Remove all registered item and node definitions and aliases
  148. // Then re-add the builtin item definitions
  149. virtual void clear()=0;
  150. // Register item definition
  151. virtual void registerItem(const ItemDefinition &def)=0;
  152. virtual void unregisterItem(const std::string &name)=0;
  153. // Set an alias so that items named <name> will load as <convert_to>.
  154. // Alias is not set if <name> has already been defined.
  155. // Alias will be removed if <name> is defined at a later point of time.
  156. virtual void registerAlias(const std::string &name,
  157. const std::string &convert_to)=0;
  158. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  159. virtual void deSerialize(std::istream &is, u16 protocol_version)=0;
  160. // Do stuff asked by threads that can only be done in the main thread
  161. virtual void processQueue(IGameDef *gamedef)=0;
  162. };
  163. IWritableItemDefManager* createItemDefManager();