itemdef.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. f32 range;
  74. // Client shall immediately place this node when player places the item.
  75. // Server will update the precise end result a moment later.
  76. // "" = no prediction
  77. std::string node_placement_prediction;
  78. u8 place_param2;
  79. /*
  80. Some helpful methods
  81. */
  82. ItemDefinition();
  83. ItemDefinition(const ItemDefinition &def);
  84. ItemDefinition& operator=(const ItemDefinition &def);
  85. ~ItemDefinition();
  86. void reset();
  87. void serialize(std::ostream &os, u16 protocol_version) const;
  88. void deSerialize(std::istream &is);
  89. private:
  90. void resetInitial();
  91. };
  92. class IItemDefManager
  93. {
  94. public:
  95. IItemDefManager() = default;
  96. virtual ~IItemDefManager() = default;
  97. // Get item definition
  98. virtual const ItemDefinition& get(const std::string &name) const=0;
  99. // Get alias definition
  100. virtual const std::string &getAlias(const std::string &name) const=0;
  101. // Get set of all defined item names and aliases
  102. virtual void getAll(std::set<std::string> &result) const=0;
  103. // Check if item is known
  104. virtual bool isKnown(const std::string &name) const=0;
  105. #ifndef SERVER
  106. // Get item inventory texture
  107. virtual video::ITexture* getInventoryTexture(const std::string &name,
  108. Client *client) const=0;
  109. // Get item wield mesh
  110. virtual ItemMesh* getWieldMesh(const std::string &name,
  111. Client *client) const=0;
  112. // Get item palette
  113. virtual Palette* getPalette(const std::string &name,
  114. Client *client) const = 0;
  115. // Returns the base color of an item stack: the color of all
  116. // tiles that do not define their own color.
  117. virtual video::SColor getItemstackColor(const ItemStack &stack,
  118. Client *client) const = 0;
  119. #endif
  120. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  121. };
  122. class IWritableItemDefManager : public IItemDefManager
  123. {
  124. public:
  125. IWritableItemDefManager() = default;
  126. virtual ~IWritableItemDefManager() = default;
  127. // Get item definition
  128. virtual const ItemDefinition& get(const std::string &name) const=0;
  129. // Get alias definition
  130. virtual const std::string &getAlias(const std::string &name) const=0;
  131. // Get set of all defined item names and aliases
  132. virtual void getAll(std::set<std::string> &result) const=0;
  133. // Check if item is known
  134. virtual bool isKnown(const std::string &name) const=0;
  135. #ifndef SERVER
  136. // Get item inventory texture
  137. virtual video::ITexture* getInventoryTexture(const std::string &name,
  138. Client *client) const=0;
  139. // Get item wield mesh
  140. virtual ItemMesh* getWieldMesh(const std::string &name,
  141. Client *client) const=0;
  142. #endif
  143. // Replace the textures of registered nodes with the ones specified in
  144. // the texture pack's override.txt files
  145. virtual void applyTextureOverrides(const std::vector<TextureOverride> &overrides)=0;
  146. // Remove all registered item and node definitions and aliases
  147. // Then re-add the builtin item definitions
  148. virtual void clear()=0;
  149. // Register item definition
  150. virtual void registerItem(const ItemDefinition &def)=0;
  151. virtual void unregisterItem(const std::string &name)=0;
  152. // Set an alias so that items named <name> will load as <convert_to>.
  153. // Alias is not set if <name> has already been defined.
  154. // Alias will be removed if <name> is defined at a later point of time.
  155. virtual void registerAlias(const std::string &name,
  156. const std::string &convert_to)=0;
  157. virtual void serialize(std::ostream &os, u16 protocol_version)=0;
  158. virtual void deSerialize(std::istream &is)=0;
  159. // Do stuff asked by threads that can only be done in the main thread
  160. virtual void processQueue(IGameDef *gamedef)=0;
  161. };
  162. IWritableItemDefManager* createItemDefManager();