aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-12-03 23:50:31 +0200
committerPerttu Ahola <celeron55@gmail.com>2011-12-03 23:50:31 +0200
commit4b00d4d9d2158fc65da39179448c03390f094987 (patch)
tree225075bfab4cd74f40b2c6e64cf4a9ee8ee04d2c /src
parent6a5829788e3dbd4347933f78740ff43d1bcf9e5c (diff)
downloadminetest-4b00d4d9d2158fc65da39179448c03390f094987.tar.gz
minetest-4b00d4d9d2158fc65da39179448c03390f094987.tar.bz2
minetest-4b00d4d9d2158fc65da39179448c03390f094987.zip
Node definition aliases
Diffstat (limited to 'src')
-rw-r--r--src/nodedef.cpp35
-rw-r--r--src/nodedef.h5
-rw-r--r--src/scriptapi.cpp19
3 files changed, 58 insertions, 1 deletions
diff --git a/src/nodedef.cpp b/src/nodedef.cpp
index 9f2d4f0af..e2e5616f1 100644
--- a/src/nodedef.cpp
+++ b/src/nodedef.cpp
@@ -398,8 +398,18 @@ public:
{
return get(n.getContent());
}
- virtual bool getId(const std::string &name, content_t &result) const
+ virtual bool getId(const std::string &name_, content_t &result) const
{
+ std::string name = name_;
+ // Convert name according to possible alias
+ std::map<std::string, std::string>::const_iterator i;
+ i = m_aliases.find(name);
+ if(i != m_aliases.end()){
+ /*infostream<<"ndef: alias active: "<<name<<" -> "<<i->second
+ <<std::endl;*/
+ name = i->second;
+ }
+ // Get id
return m_name_id_mapping.getId(name, result);
}
virtual content_t getId(const std::string &name) const
@@ -438,6 +448,12 @@ public:
m_content_features[c] = def;
if(def.name != "")
m_name_id_mapping.set(c, def.name);
+
+ // Remove conflicting alias if it exists
+ bool alias_removed = (m_aliases.erase(def.name) != 0);
+ if(alias_removed)
+ infostream<<"ndef: erased alias "<<def.name
+ <<" because node was defined"<<std::endl;
}
virtual content_t set(const std::string &name,
const ContentFeatures &def)
@@ -476,6 +492,19 @@ public:
f.material.diggability = DIGGABLE_NORMAL;
return set(name, f);
}
+ virtual void setAlias(const std::string &name,
+ const std::string &convert_to)
+ {
+ content_t id;
+ if(getId(name, id)){
+ infostream<<"ndef: not setting alias "<<name<<" -> "<<convert_to
+ <<": "<<name<<" is already defined"<<std::endl;
+ return;
+ }
+ infostream<<"ndef: setting alias "<<name<<" -> "<<convert_to
+ <<std::endl;
+ m_aliases[name] = convert_to;
+ }
virtual void updateTextures(ITextureSource *tsrc)
{
#ifndef SERVER
@@ -639,8 +668,12 @@ public:
}
}
private:
+ // Features indexed by id
ContentFeatures m_content_features[MAX_CONTENT+1];
+ // A mapping for fast converting back and forth between names and ids
NameIdMapping m_name_id_mapping;
+ // Aliases for loading legacy crap
+ std::map<std::string, std::string> m_aliases;
};
IWritableNodeDefManager* createNodeDefManager()
diff --git a/src/nodedef.h b/src/nodedef.h
index e0b5c864c..f69b66c9f 100644
--- a/src/nodedef.h
+++ b/src/nodedef.h
@@ -279,6 +279,11 @@ public:
const ContentFeatures &def)=0;
// If returns CONTENT_IGNORE, could not allocate id
virtual content_t allocateDummy(const std::string &name)=0;
+ // Set an alias so that nodes 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;
/*
Update tile textures to latest return values of TextueSource.
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index 624ca5d85..c5574dd8e 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -1122,6 +1122,24 @@ static int l_register_node(lua_State *L)
return 0; /* number of results */
}
+// alias_node(name, convert_to_name)
+static int l_alias_node(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 node definition manager from the server
+ IWritableNodeDefManager *nodedef =
+ server->getWritableNodeDefManager();
+
+ nodedef->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)
{
@@ -1274,6 +1292,7 @@ static const struct luaL_Reg minetest_f [] = {
{"register_node", l_register_node},
{"register_craft", l_register_craft},
{"register_abm", l_register_abm},
+ {"alias_node", l_alias_node},
{"setting_get", l_setting_get},
{"setting_getbool", l_setting_getbool},
{"chat_send_all", l_chat_send_all},