itemdef.h 5.9 KB

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