Initial node definition stuff

This commit is contained in:
Perttu Ahola 2011-11-16 15:23:25 +02:00
parent a5545593ba
commit 62164d955c
2 changed files with 63 additions and 3 deletions

@ -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 = { local TNT = {
-- Maybe handle gravity and collision this way? dunno -- Maybe handle gravity and collision this way? dunno
physical = true, physical = true,

@ -36,6 +36,7 @@ extern "C" {
#include "luaentity_common.h" #include "luaentity_common.h"
#include "content_sao.h" // For LuaEntitySAO #include "content_sao.h" // For LuaEntitySAO
#include "tooldef.h" #include "tooldef.h"
#include "nodedef.h"
/* /*
TODO: TODO:
@ -46,7 +47,13 @@ TODO:
- Blink effect - Blink effect
- Spritesheets and animation - Spritesheets and animation
- LuaNodeMetadata - LuaNodeMetadata
blockdef.has_metadata = true/false blockdef.metadata_type =
""
"sign"
"furnace"
"chest"
"locked_chest"
"lua"
- Stores an inventory and stuff in a Settings object - Stores an inventory and stuff in a Settings object
meta.inventory_add_list("main") meta.inventory_add_list("main")
blockdef.on_inventory_modified blockdef.on_inventory_modified
@ -224,6 +231,7 @@ static int l_register_tool(lua_State *L)
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
infostream<<"register_tool: "<<name<<std::endl; infostream<<"register_tool: "<<name<<std::endl;
luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE);
int table = 2;
// Get server from registry // Get server from registry
lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server"); lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
@ -232,8 +240,6 @@ static int l_register_tool(lua_State *L)
IWritableToolDefManager *tooldef = IWritableToolDefManager *tooldef =
server->getWritableToolDefManager(); server->getWritableToolDefManager();
int table = 2;
ToolDefinition def; ToolDefinition def;
lua_getfield(L, table, "image"); lua_getfield(L, table, "image");
@ -282,7 +288,55 @@ static int l_register_tool(lua_State *L)
lua_pop(L, 1); lua_pop(L, 1);
tooldef->registerTool(name, def); 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 */ return 0; /* number of results */
} }
@ -291,6 +345,7 @@ static const struct luaL_Reg minetest_f [] = {
{"register_globalstep", l_register_globalstep}, {"register_globalstep", l_register_globalstep},
//{"deregister_tools", l_deregister_tools}, //{"deregister_tools", l_deregister_tools},
{"register_tool", l_register_tool}, {"register_tool", l_register_tool},
{"register_node", l_register_node},
{NULL, NULL} {NULL, NULL}
}; };