Initial node definition stuff
authorPerttu Ahola <celeron55@gmail.com>
Wed, 16 Nov 2011 13:23:25 +0000 (15:23 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:47 +0000 (19:13 +0200)
data/mods/default/init.lua
src/scriptapi.cpp

index a60979ff4b27a260e0bf1a85aa01c9b008d987f9..c2e1d330014acce700d82a68634e30a469923df9 100644 (file)
@@ -316,6 +316,11 @@ minetest.register_tool("horribletool", {
 })
 --]]
 
+minetest.register_node("somenode", {
+       tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
+       inventory_image = "treeprop.png"
+})
+
 local TNT = {
        -- Maybe handle gravity and collision this way? dunno
        physical = true,
index 7074160fd3b59a939e8368dce8f4f7d64817f6f0..b37c50bc072613b7da79ff900a9bb576e0d1090e 100644 (file)
@@ -36,6 +36,7 @@ extern "C" {
 #include "luaentity_common.h"
 #include "content_sao.h" // For LuaEntitySAO
 #include "tooldef.h"
+#include "nodedef.h"
 
 /*
 TODO:
@@ -46,7 +47,13 @@ TODO:
        - Blink effect
        - Spritesheets and animation
 - LuaNodeMetadata
-       blockdef.has_metadata = true/false
+       blockdef.metadata_type =
+               ""
+               "sign"
+               "furnace"
+               "chest"
+               "locked_chest"
+               "lua"
        - Stores an inventory and stuff in a Settings object
        meta.inventory_add_list("main")
        blockdef.on_inventory_modified
@@ -224,6 +231,7 @@ static int l_register_tool(lua_State *L)
        const char *name = luaL_checkstring(L, 1);
        infostream<<"register_tool: "<<name<<std::endl;
        luaL_checktype(L, 2, LUA_TTABLE);
+       int table = 2;
 
        // Get server from registry
        lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
@@ -232,8 +240,6 @@ static int l_register_tool(lua_State *L)
        IWritableToolDefManager *tooldef =
                        server->getWritableToolDefManager();
        
-       int table = 2;
-       
        ToolDefinition def;
 
        lua_getfield(L, table, "image");
@@ -282,7 +288,55 @@ static int l_register_tool(lua_State *L)
        lua_pop(L, 1);
 
        tooldef->registerTool(name, def);
+       return 0; /* number of results */
+}
+
+// register_node(name, {lots of stuff})
+static int l_register_node(lua_State *L)
+{
+       const char *name = luaL_checkstring(L, 1);
+       infostream<<"register_node: "<<name<<std::endl;
+       luaL_checktype(L, 2, LUA_TTABLE);
+       int table0 = 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();
+       
+       ContentFeatures f;
+       f.name = name;
+
+       lua_getfield(L, table0, "tile_images");
+       if(lua_istable(L, -1)){
+               int table = lua_gettop(L);
+               lua_pushnil(L);
+               int i = 0;
+               while(lua_next(L, table) != 0){
+                       // key at index -2 and value at index -1
+                       if(lua_isstring(L, -1))
+                               f.tname_tiles[i] = lua_tostring(L, -1);
+                       else
+                               f.tname_tiles[i] = "";
+                       // removes value, keeps key for next iteration
+                       lua_pop(L, 1);
+                       i++;
+                       if(i==6){
+                               lua_pop(L, 1);
+                               break;
+                       }
+               }
+       }
+       lua_pop(L, 1);
+
+       lua_getfield(L, table0, "inventory_image");
+       if(lua_isstring(L, -1))
+               f.tname_inventory = lua_tostring(L, -1);
+       lua_pop(L, 1);
 
+       nodedef->set(name, f);
        return 0; /* number of results */
 }
 
@@ -291,6 +345,7 @@ static const struct luaL_Reg minetest_f [] = {
        {"register_globalstep", l_register_globalstep},
        //{"deregister_tools", l_deregister_tools},
        {"register_tool", l_register_tool},
+       {"register_node", l_register_node},
        {NULL, NULL}
 };