Generic NodeMetadata text input
[oweals/minetest.git] / src / scriptapi.cpp
index 381664489587741e72b0d8a36b4c6598c7852366..27f46a8acd2665d727b3a6a07c2cda1772c48656 100644 (file)
@@ -38,12 +38,12 @@ extern "C" {
 
 /*
 TODO:
-- Global environment step function
+- Node type definition
 - Random node triggers
 - Object visual client-side stuff
        - Blink effect
        - Spritesheets and animation
-- Named node types and dynamic id allocation
+- Named node types and dynamic id allocation per MapBlock
 - LuaNodeMetadata
        blockdef.has_metadata = true/false
        - Stores an inventory and stuff in a Settings object
@@ -170,8 +170,35 @@ static int l_register_entity(lua_State *L)
        return 0; /* number of results */
 }
 
+// Register a global step function
+// register_globalstep(function)
+static int l_register_globalstep(lua_State *L)
+{
+       luaL_checktype(L, 1, LUA_TFUNCTION);
+       infostream<<"register_globalstep"<<std::endl;
+
+       lua_getglobal(L, "table");
+       lua_getfield(L, -1, "insert");
+       int table_insert = lua_gettop(L);
+       // Get minetest.registered_globalsteps
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "registered_globalsteps");
+       luaL_checktype(L, -1, LUA_TTABLE);
+       int registered_globalsteps = lua_gettop(L);
+       // table.insert(registered_globalsteps, func)
+       lua_pushvalue(L, table_insert);
+       lua_pushvalue(L, registered_globalsteps);
+       lua_pushvalue(L, 1); // push function from argument 1
+       // Call insert
+       if(lua_pcall(L, 2, 0, 0))
+               script_error(L, "error: %s\n", lua_tostring(L, -1));
+
+       return 0; /* number of results */
+}
+
 static const struct luaL_Reg minetest_f [] = {
        {"register_entity", l_register_entity},
+       {"register_globalstep", l_register_globalstep},
        {NULL, NULL}
 };
 
@@ -558,15 +585,20 @@ void scriptapi_export(lua_State *L, Server *server)
        // Get the main minetest table
        lua_getglobal(L, "minetest");
 
-       // Add registered_entities table in minetest
+       // Add tables to minetest
+       
+       /*lua_newtable(L);
+       lua_setfield(L, -2, "registered_blocks");*/
+
        lua_newtable(L);
        lua_setfield(L, -2, "registered_entities");
 
-       // Add object_refs table in minetest
+       lua_newtable(L);
+       lua_setfield(L, -2, "registered_globalsteps");
+
        lua_newtable(L);
        lua_setfield(L, -2, "object_refs");
 
-       // Add luaentities table in minetest
        lua_newtable(L);
        lua_setfield(L, -2, "luaentities");
 
@@ -682,19 +714,29 @@ void scriptapi_environment_step(lua_State *L, float dtime)
        //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
        StackUnroller stack_unroller(L);
 
-       lua_getglobal(L, "on_step");
-       if(lua_type(L, -1) != LUA_TFUNCTION)
-               return; // If no on_step function exist, do nothing
-       lua_pushnumber(L, dtime);
-       if(lua_pcall(L, 1, 0, 0))
-               script_error(L, "error: %s\n", lua_tostring(L, -1));
+       // Get minetest.registered_globalsteps
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "registered_globalsteps");
+       luaL_checktype(L, -1, LUA_TTABLE);
+       int table = lua_gettop(L);
+       // Foreach
+       lua_pushnil(L);
+       while(lua_next(L, table) != 0){
+               // key at index -2 and value at index -1
+               luaL_checktype(L, -1, LUA_TFUNCTION);
+               // Call function
+               lua_pushnumber(L, dtime);
+               if(lua_pcall(L, 1, 0, 0))
+                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+               // value removed, keep key for next iteration
+       }
 }
 
 /*
        luaentity
 */
 
-void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
+bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
                const char *init_state)
 {
        realitycheck(L);
@@ -712,7 +754,11 @@ void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
        lua_pushstring(L, name);
        lua_gettable(L, -2);
        // Should be a table, which we will use as a prototype
-       luaL_checktype(L, -1, LUA_TTABLE);
+       //luaL_checktype(L, -1, LUA_TTABLE);
+       if(lua_type(L, -1) != LUA_TTABLE){
+               errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
+               return false;
+       }
        int prototype_table = lua_gettop(L);
        //dump2(L, "prototype_table");
        
@@ -750,6 +796,8 @@ void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
        if(lua_pcall(L, 1, 0, 0))
                script_error(L, "error running function %s:on_activate: %s\n",
                                name, lua_tostring(L, -1));*/
+
+       return true;
 }
 
 void scriptapi_luaentity_rm(lua_State *L, u16 id)