Tool aliases
authorPerttu Ahola <celeron55@gmail.com>
Sat, 3 Dec 2011 22:22:34 +0000 (00:22 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sat, 3 Dec 2011 22:22:34 +0000 (00:22 +0200)
data/mods/default/init.lua
data/mods/legacy/init.lua
src/scriptapi.cpp
src/tooldef.cpp
src/tooldef.h

index c69d26454c92ac3b26c3d57ad62870742ea33970..003a02323457505df60a5dfbb61fdab1d1958eae 100644 (file)
@@ -92,6 +92,8 @@
 -- minetest.register_craftitem(name, craftitem definition)
 -- minetest.register_craft(recipe)
 -- minetest.register_abm(abm definition)
+-- minetest.alias_node(name, convert_to)
+-- minetest.alias_tool(name, convert_to)
 -- minetest.register_globalstep(func(dtime))
 -- minetest.register_on_placenode(func(pos, newnode, placer))
 -- minetest.register_on_dignode(func(pos, oldnode, digger))
index a51931e419c9778a73af629bcbc38599a051e970..6bd5526e5e32e49c2909c8713c86e60532e5ac4b 100644 (file)
@@ -40,6 +40,20 @@ minetest.alias_node("nyancat_rainbow", "default:nyancat_rainbow")
 minetest.alias_node("sapling", "default:sapling")
 minetest.alias_node("apple", "default:apple")
 
+minetest.alias_tool("WPick", "default:pick_wood")
+minetest.alias_tool("STPick", "default:pick_stone")
+minetest.alias_tool("SteelPick", "default:pick_steel")
+minetest.alias_tool("MesePick", "default:pick_mese")
+minetest.alias_tool("WShovel", "default:shovel_wood")
+minetest.alias_tool("STShovel", "default:shovel_stone")
+minetest.alias_tool("SteelShovel", "default:shovel_steel")
+minetest.alias_tool("WAxe", "default:axe_wood")
+minetest.alias_tool("STAxe", "default:axe_stone")
+minetest.alias_tool("SteelAxe", "default:axe_steel")
+minetest.alias_tool("WSword", "default:sword_wood")
+minetest.alias_tool("STSword", "default:sword_stone")
+minetest.alias_tool("SteelSword", "default:sword_steel")
+
 --[[
 WATER_ALPHA = 160
 WATER_VISC = 1
index c5574dd8ec9ad1a91481a439bedac6d9872cedb4..ab9f892129de5e5b1294ca3d81e63e62d7e52d68 100644 (file)
@@ -1140,6 +1140,24 @@ static int l_alias_node(lua_State *L)
        return 0; /* number of results */
 }
 
+// alias_tool(name, convert_to_name)
+static int l_alias_tool(lua_State *L)
+{
+       std::string name = luaL_checkstring(L, 1);
+       std::string convert_to = luaL_checkstring(L, 2);
+
+       // Get server from registry
+       lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
+       Server *server = (Server*)lua_touserdata(L, -1);
+       // And get the writable tool definition manager from the server
+       IWritableToolDefManager *tooldef =
+                       server->getWritableToolDefManager();
+       
+       tooldef->setAlias(name, convert_to);
+
+       return 0; /* number of results */
+}
+
 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
 static int l_register_craft(lua_State *L)
 {
@@ -1293,6 +1311,7 @@ static const struct luaL_Reg minetest_f [] = {
        {"register_craft", l_register_craft},
        {"register_abm", l_register_abm},
        {"alias_node", l_alias_node},
+       {"alias_tool", l_alias_tool},
        {"setting_get", l_setting_get},
        {"setting_getbool", l_setting_getbool},
        {"chat_send_all", l_chat_send_all},
index 45193f46bf770a27d94877c2f972703f58b19f57..6733dfff09a7183b9fca8140096b78e7e01a6fd0 100644 (file)
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include <sstream>
 #include "utility.h"
+#include <map>
 
 ToolDiggingProperties::ToolDiggingProperties(float full_punch_interval_,
                float a, float b, float c, float d, float e,
@@ -92,8 +93,16 @@ public:
        {
                clear();
        }
-       virtual const ToolDefinition* getToolDefinition(const std::string &toolname) const
+       virtual const ToolDefinition* getToolDefinition(const std::string &toolname_) const
        {
+               // Convert name according to possible alias
+               std::string toolname = toolname_;
+               std::map<std::string, std::string>::const_iterator i;
+               i = m_aliases.find(toolname);
+               if(i != m_aliases.end()){
+                       toolname = i->second;
+               }
+               // Get the definition
                core::map<std::string, ToolDefinition*>::Node *n;
                n = m_tool_definitions.find(toolname);
                if(n == NULL)
@@ -124,14 +133,14 @@ public:
        virtual bool registerTool(std::string toolname, const ToolDefinition &def)
        {
                infostream<<"registerTool: registering tool \""<<toolname<<"\""<<std::endl;
-               /*core::map<std::string, ToolDefinition*>::Node *n;
-               n = m_tool_definitions.find(toolname);
-               if(n != NULL){
-                       errorstream<<"registerTool: registering tool \""<<toolname
-                                       <<"\" failed: name is already registered"<<std::endl;
-                       return false;
-               }*/
                m_tool_definitions[toolname] = new ToolDefinition(def);
+
+               // Remove conflicting alias if it exists
+               bool alias_removed = (m_aliases.erase(toolname) != 0);
+               if(alias_removed)
+                       infostream<<"tdef: erased alias "<<toolname
+                                       <<" because node was defined"<<std::endl;
+               
                return true;
        }
        virtual void clear()
@@ -142,6 +151,19 @@ public:
                        delete i.getNode()->getValue();
                }
                m_tool_definitions.clear();
+               m_aliases.clear();
+       }
+       virtual void setAlias(const std::string &name,
+                       const std::string &convert_to)
+       {
+               if(getToolDefinition(name) != NULL){
+                       infostream<<"tdef: not setting alias "<<name<<" -> "<<convert_to
+                                       <<": "<<name<<" is already defined"<<std::endl;
+                       return;
+               }
+               infostream<<"tdef: setting alias "<<name<<" -> "<<convert_to
+                               <<std::endl;
+               m_aliases[name] = convert_to;
        }
        virtual void serialize(std::ostream &os)
        {
@@ -160,6 +182,14 @@ public:
                        def->serialize(tmp_os);
                        os<<serializeString(tmp_os.str());
                }
+
+               writeU16(os, m_aliases.size());
+               for(std::map<std::string, std::string>::const_iterator
+                               i = m_aliases.begin(); i != m_aliases.end(); i++)
+               {
+                       os<<serializeString(i->first);
+                       os<<serializeString(i->second);
+               }
        }
        virtual void deSerialize(std::istream &is)
        {
@@ -180,10 +210,21 @@ public:
                        // Register
                        registerTool(name, def);
                }
+
+               u16 num_aliases = readU16(is);
+               if(!is.eof()){
+                       for(u16 i=0; i<num_aliases; i++){
+                               std::string name = deSerializeString(is);
+                               std::string convert_to = deSerializeString(is);
+                               m_aliases[name] = convert_to;
+                       }
+               }
        }
 private:
        // Key is name
        core::map<std::string, ToolDefinition*> m_tool_definitions;
+       // Aliases for loading legacy crap
+       std::map<std::string, std::string> m_aliases;
 };
 
 IWritableToolDefManager* createToolDefManager()
index aa4cb6931ae3e912bb7f714fbb0340c220eb561a..e28935e4322b53d3a77a12b383c5e28e7d38c122 100644 (file)
@@ -85,6 +85,11 @@ public:
                        
        virtual bool registerTool(std::string toolname, const ToolDefinition &def)=0;
        virtual void clear()=0;
+       // Set an alias so that entries named <name> will load as <convert_to>.
+       // Alias is not set if <name> has already been defined.
+       // Alias will be removed if <name> is defined at a later point of time.
+       virtual void setAlias(const std::string &name,
+                       const std::string &convert_to)=0;
 
        virtual void serialize(std::ostream &os)=0;
        virtual void deSerialize(std::istream &is)=0;