Scripting WIP: Add global environment step function on_step
authorPerttu Ahola <celeron55@gmail.com>
Sat, 12 Nov 2011 15:46:06 +0000 (17:46 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:40 +0000 (19:13 +0200)
data/scripts/default.lua
src/environment.cpp
src/scriptapi.cpp
src/scriptapi.h

index dcc58097bc7ae1093a9873e71554111a79997e70..4197cd788dcfed4b49da4e18e6658e9cb79c27ec 100644 (file)
@@ -138,7 +138,10 @@ end
 print("omg lol")
 print("minetest dump: "..dump(minetest))
 
---local TNT = minetest.new_entity {
+-- Global environment step function
+function on_step(dtime)
+end
+
 local TNT = {
        -- Maybe handle gravity and collision this way? dunno
        physical = true,
index d3e8fa12cbbb2dab21ae226686fa3d22eca7694c..29432bc5e58788be05369417d1894536ad70b9a1 100644 (file)
@@ -1141,6 +1141,11 @@ void ServerEnvironment::step(float dtime)
                }
        }
        
+       /*
+               Step script environment (run global on_step())
+       */
+       scriptapi_environment_step(m_lua, dtime);
+
        /*
                Step active objects
        */
index 530c1719e10eb0b6d053a40103e0fe6a5f811107..381664489587741e72b0d8a36b4c6598c7852366 100644 (file)
@@ -40,7 +40,9 @@ extern "C" {
 TODO:
 - Global environment step function
 - Random node triggers
-- Object network and client-side stuff
+- Object visual client-side stuff
+       - Blink effect
+       - Spritesheets and animation
 - Named node types and dynamic id allocation
 - LuaNodeMetadata
        blockdef.has_metadata = true/false
@@ -669,6 +671,25 @@ void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
        lua_settable(L, objectstable);
 }
 
+/*
+       environment
+*/
+
+void scriptapi_environment_step(lua_State *L, float dtime)
+{
+       realitycheck(L);
+       assert(lua_checkstack(L, 20));
+       //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));
+}
+
 /*
        luaentity
 */
index 458f90acbb2cc35ec331b8062353de52f59ee227..c63977fc6b476a68c0b2e98939651e170c32155e 100644 (file)
@@ -35,6 +35,10 @@ void scriptapi_add_environment(lua_State *L, ServerEnvironment *env);
 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj);
 
+/* environment */
+void scriptapi_environment_step(lua_State *L, float dtime);
+
+/* luaentity */
 void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
                const char *init_state);
 void scriptapi_luaentity_rm(lua_State *L, u16 id);