Refactoring and code style fixes in preparation of adding mesh typed items
[oweals/minetest.git] / src / itemdef.h
index 4a9e16966f668e20fdb7b85adb705e95ea833b58..fb157705f2b04006ae7d9e54374e657e65c73924 100644 (file)
@@ -25,8 +25,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <string>
 #include <iostream>
 #include <set>
+#include <map>
 #include "itemgroup.h"
+#include "sound.h"
+#include "util/container.h"
+#include "util/thread.h"
+
+#ifndef SERVER
+#include "client/tile.h"
+#endif
+
 class IGameDef;
+class INodeDefManager;
 struct ToolCapabilities;
 
 /*
@@ -38,7 +48,7 @@ enum ItemType
        ITEM_NONE,
        ITEM_NODE,
        ITEM_CRAFT,
-       ITEM_TOOL,
+       ITEM_TOOL
 };
 
 struct ItemDefinition
@@ -62,12 +72,13 @@ struct ItemDefinition
        */
        s16 stack_max;
        bool usable;
-       // If true, don't use node placement prediction
-       bool rightclickable;
        bool liquids_pointable;
        // May be NULL. If non-NULL, deleted by destructor
        ToolCapabilities *tool_capabilities;
        ItemGroupList groups;
+       SimpleSoundSpec sound_place;
+       SimpleSoundSpec sound_place_failed;
+       f32 range;
 
        // Client shall immediately place this node when player places the item.
        // Server will update the precise end result a moment later.
@@ -82,7 +93,7 @@ struct ItemDefinition
        ItemDefinition& operator=(const ItemDefinition &def);
        ~ItemDefinition();
        void reset();
-       void serialize(std::ostream &os) const;
+       void serialize(std::ostream &os, u16 protocol_version) const;
        void deSerialize(std::istream &is);
 private:
        void resetInitial();
@@ -111,7 +122,7 @@ public:
                IGameDef *gamedef) const=0;
 #endif
 
-       virtual void serialize(std::ostream &os)=0;
+       virtual void serialize(std::ostream &os, u16 protocol_version)=0;
 };
 
 class IWritableItemDefManager : public IItemDefManager
@@ -148,13 +159,84 @@ public:
        virtual void registerAlias(const std::string &name,
                        const std::string &convert_to)=0;
 
-       virtual void serialize(std::ostream &os)=0;
+       virtual void serialize(std::ostream &os, u16 protocol_version)=0;
        virtual void deSerialize(std::istream &is)=0;
 
        // Do stuff asked by threads that can only be done in the main thread
        virtual void processQueue(IGameDef *gamedef)=0;
 };
 
+
+class CItemDefManager: public IWritableItemDefManager
+{
+public:
+       CItemDefManager();
+       virtual ~CItemDefManager();
+       virtual const ItemDefinition& get(const std::string &name_) const;
+       virtual std::string getAlias(const std::string &name) const;
+       virtual std::set<std::string> getAll() const;
+       virtual bool isKnown(const std::string &name_) const;
+
+#ifndef SERVER
+       // Get item inventory texture
+       virtual video::ITexture* getInventoryTexture(const std::string &name,
+                       IGameDef *gamedef) const;
+
+       // Get item wield mesh
+       virtual scene::IMesh* getWieldMesh(const std::string &name,
+                       IGameDef *gamedef) const;
+#endif
+       void clear();
+
+       virtual void registerItem(const ItemDefinition &def);
+       virtual void registerAlias(const std::string &name,
+                       const std::string &convert_to);
+       void serialize(std::ostream &os, u16 protocol_version);
+       void deSerialize(std::istream &is);
+
+       void processQueue(IGameDef *gamedef);
+
+private:
+
+#ifndef SERVER
+       struct ClientCached
+       {
+               video::ITexture *inventory_texture;
+               scene::IMesh *wield_mesh;
+
+               ClientCached();
+       };
+
+       void createNodeItemTexture(const std::string& name,
+                       const ItemDefinition& def, INodeDefManager* nodedef,
+                       ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
+
+       ClientCached* createClientCachedDirect(const std::string &name,
+                       IGameDef *gamedef) const;
+
+       ClientCached* getClientCached(const std::string &name,
+                       IGameDef *gamedef) const;
+
+       // The id of the thread that is allowed to use irrlicht directly
+       threadid_t m_main_thread;
+
+       // A reference to this can be returned when nothing is found, to avoid NULLs
+       mutable ClientCached m_dummy_clientcached;
+
+       // Cached textures and meshes
+       mutable MutexedMap<std::string, ClientCached*> m_clientcached;
+
+       // Queued clientcached fetches (to be processed by the main thread)
+       mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
+#endif
+
+       // Key is name
+       std::map<std::string, ItemDefinition*> m_item_definitions;
+
+       // Aliases
+       std::map<std::string, std::string> m_aliases;
+};
+
 IWritableItemDefManager* createItemDefManager();
 
 #endif