-- - add_node(pos, node)
-- - remove_node(pos)
-- - get_node(pos)
+-- - add_luaentity(pos, name)
--
-- ObjectRef is basically ServerActiveObject.
-- ObjectRef methods:
-- - Functions receive a "luaentity" as self:
-- - It has the member .object, which is an ObjectRef pointing to the object
-- - The original prototype stuff is visible directly via a metatable
+-- - Callbacks:
+-- - on_activate(self, staticdata)
+-- - on_step(self, dtime)
+-- - on_punch(self, hitter)
+-- - on_rightclick(self, clicker)
+-- - get_staticdata(self): return string
--
-- MapNode representation:
-- {name="name", param1=num, param2=num}
--textures = {"mese.png^[forcesingle"},
-- Initial value for our timer
timer = 0,
+ -- Number of punches required to defuse
+ health = 3,
-- List names of state variables, for serializing object state
-- (NOTE: not implemented and implementation will not be like this)
-- state_variables = {"timer"},
}
+-- Called when a TNT object is created
+function TNT:on_activate(staticdata)
+ print("TNT:on_activate()")
+ self.object:setvelocity({x=0, y=1, z=0})
+ self.object:setacceleration({x=0, y=-5, z=0})
+end
+
-- Called periodically
function TNT:on_step(dtime)
--print("TNT:on_step()")
-- Called when object is punched
function TNT:on_punch(hitter)
print("TNT:on_punch()")
- self.object:remove()
- hitter:add_to_inventory("CraftItem testobject1 1")
+ self.health = self.health - 1
+ if self.health <= 0 then
+ self.object:remove()
+ hitter:add_to_inventory("NodeItem TNT 1")
+ end
end
-- Called when object is right-clicked
function TNT:on_rightclick(clicker)
- pos = self.object:getpos()
- pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
- self.object:moveto(pos, false)
+ --pos = self.object:getpos()
+ --pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
+ --self.object:moveto(pos, false)
end
print("TNT dump: "..dump(TNT))
visual = "cube",
textures = {texture,texture,texture,texture,texture,texture},
-- State
- fallspeed = 0,
-- Methods
on_step = function(self, dtime)
- -- Apply gravity manually
- self.fallspeed = self.fallspeed + dtime * 5
- fp = self.object:getpos()
- fp.y = fp.y - self.fallspeed * dtime
- self.object:moveto(fp, true)
+ -- Set gravity
+ self.object:setacceleration({x=0, y=-10, z=0})
-- Turn to actual sand when collides to ground or just move
- bcp = {x=fp.x, y=fp.y-0.5, z=fp.z} -- Position of bottom center point
+ pos = self.object:getpos()
+ bcp = {x=pos.x, y=pos.y-0.5, z=pos.z} -- Position of bottom center point
bcn = minetest.env:get_node(bcp)
if bcn.name ~= "air" then
-- Turn to a sand node
end
minetest.register_on_dignode(on_dignode)
+function on_punchnode(p, node)
+ print("on_punchnode")
+ if node.name == "TNT" then
+ minetest.env:remove_node(p)
+ minetest.env:add_luaentity(p, "TNT")
+ nodeupdate(p)
+ end
+end
+minetest.register_on_punchnode(on_punchnode)
+
--
-- Done, print some random stuff
--
meta.set("owner", playername)
meta.get("owner")
- Item definition (actually, only CraftItem)
+- (not scripting) Putting items in node metadata (virtual)
*/
static void stackDump(lua_State *L, std::ostream &o)
return 0; /* number of results */
}
-// Register a global step function
-// register_globalstep(function)
-static int l_register_globalstep(lua_State *L)
+static int register_lua_callback(lua_State *L, const char *tablename)
{
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");
+ lua_getfield(L, -1, tablename);
luaL_checktype(L, -1, LUA_TTABLE);
- int registered_globalsteps = lua_gettop(L);
+ int registered = lua_gettop(L);
// table.insert(registered_globalsteps, func)
lua_pushvalue(L, table_insert);
- lua_pushvalue(L, registered_globalsteps);
+ lua_pushvalue(L, registered);
lua_pushvalue(L, 1); // push function from argument 1
// Call insert
if(lua_pcall(L, 2, 0, 0))
return 0; /* number of results */
}
+// Register a global step function
+// register_globalstep(function)
+static int l_register_globalstep(lua_State *L)
+{
+ infostream<<"register_globalstep"<<std::endl;
+ return register_lua_callback(L, "registered_globalsteps");
+}
+
// register_on_placenode(function)
static int l_register_on_placenode(lua_State *L)
{
- luaL_checktype(L, 1, LUA_TFUNCTION);
infostream<<"register_on_placenode"<<std::endl;
-
- lua_getglobal(L, "table");
- lua_getfield(L, -1, "insert");
- int table_insert = lua_gettop(L);
- // Get minetest.registered_on_placenodes
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "registered_on_placenodes");
- luaL_checktype(L, -1, LUA_TTABLE);
- int registered_on_placenodes = lua_gettop(L);
- // table.insert(registered_on_placenodes, func)
- lua_pushvalue(L, table_insert);
- lua_pushvalue(L, registered_on_placenodes);
- 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 */
+ return register_lua_callback(L, "registered_on_placenodes");
}
// register_on_dignode(function)
static int l_register_on_dignode(lua_State *L)
{
- luaL_checktype(L, 1, LUA_TFUNCTION);
infostream<<"register_on_dignode"<<std::endl;
+ return register_lua_callback(L, "registered_on_dignodes");
+}
- lua_getglobal(L, "table");
- lua_getfield(L, -1, "insert");
- int table_insert = lua_gettop(L);
- // Get minetest.registered_on_dignodes
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "registered_on_dignodes");
- luaL_checktype(L, -1, LUA_TTABLE);
- int registered_on_dignodes = lua_gettop(L);
- // table.insert(registered_on_dignodes, func)
- lua_pushvalue(L, table_insert);
- lua_pushvalue(L, registered_on_dignodes);
- 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 */
+// register_on_punchnode(function)
+static int l_register_on_punchnode(lua_State *L)
+{
+ infostream<<"register_on_punchnode"<<std::endl;
+ return register_lua_callback(L, "registered_on_punchnodes");
}
static const struct luaL_Reg minetest_f [] = {
{"register_globalstep", l_register_globalstep},
{"register_on_placenode", l_register_on_placenode},
{"register_on_dignode", l_register_on_dignode},
+ {"register_on_punchnode", l_register_on_punchnode},
{NULL, NULL}
};
// content
MapNode n = readnode(L, 3, env->getGameDef()->ndef());
// Do it
- env->getMap().addNodeWithEvent(pos, n);
- return 0;
+ bool succeeded = env->getMap().addNodeWithEvent(pos, n);
+ lua_pushboolean(L, succeeded);
+ return 1;
}
// EnvRef:remove_node(pos)
// pos
v3s16 pos = readpos(L, 2);
// Do it
- env->getMap().removeNodeWithEvent(pos);
- return 0;
+ bool succeeded = env->getMap().removeNodeWithEvent(pos);
+ lua_pushboolean(L, succeeded);
+ return 1;
}
// EnvRef:get_node(pos)
return 0;
}
+ // setvelocity(self, velocity)
+ static int l_setvelocity(lua_State *L)
+ {
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if(co == NULL) return 0;
+ // pos
+ v3f pos = readFloatPos(L, 2);
+ // Do it
+ co->setVelocity(pos);
+ return 0;
+ }
+
+ // setacceleration(self, acceleration)
+ static int l_setacceleration(lua_State *L)
+ {
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if(co == NULL) return 0;
+ // pos
+ v3f pos = readFloatPos(L, 2);
+ // Do it
+ co->setAcceleration(pos);
+ return 0;
+ }
+
// add_to_inventory(self, itemstring)
// returns: true if item was added, false otherwise
static int l_add_to_inventory(lua_State *L)
method(ObjectRef, getpos),
method(ObjectRef, setpos),
method(ObjectRef, moveto),
+ method(ObjectRef, setvelocity),
+ method(ObjectRef, setacceleration),
method(ObjectRef, add_to_inventory),
{0,0}
};
lua_newtable(L);
lua_setfield(L, -2, "registered_on_dignodes");
+ lua_newtable(L);
+ lua_setfield(L, -2, "registered_on_punchnodes");
+
lua_newtable(L);
lua_setfield(L, -2, "object_refs");
}
}
+void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode oldnode)
+{
+ realitycheck(L);
+ assert(lua_checkstack(L, 20));
+ //infostream<<"scriptapi_environment_on_punchnode"<<std::endl;
+ StackUnroller stack_unroller(L);
+
+ // 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 *ndef =
+ server->getWritableNodeDefManager();
+
+ // Get minetest.registered_on_punchnodes
+ lua_getglobal(L, "minetest");
+ lua_getfield(L, -1, "registered_on_punchnodes");
+ 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
+ pushpos(L, p);
+ pushnode(L, oldnode, ndef);
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error: %s\n", lua_tostring(L, -1));
+ // value removed, keep key for next iteration
+ }
+}
+
/*
luaentity
*/
bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
- const char *init_state)
+ const std::string &staticdata)
{
realitycheck(L);
assert(lua_checkstack(L, 20));
<<name<<"\""<<std::endl;
StackUnroller stack_unroller(L);
- // Create object as a dummy string (TODO: Create properly)
-
// Get minetest.registered_entities[name]
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "registered_entities");
lua_pushvalue(L, object); // Copy object to top of stack
lua_settable(L, -3);
- // This callback doesn't really make sense
- /*// Get on_activate function
+ // Get on_activate function
lua_pushvalue(L, object);
lua_getfield(L, -1, "on_activate");
- luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_pushvalue(L, object); // self
- // Call with 1 arguments, 0 results
- if(lua_pcall(L, 1, 0, 0))
- script_error(L, "error running function %s:on_activate: %s\n",
- name, lua_tostring(L, -1));*/
-
+ if(!lua_isnil(L, -1)){
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_pushvalue(L, object); // self
+ lua_pushlstring(L, staticdata.c_str(), staticdata.size());
+ // Call with 2 arguments, 0 results
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error running function %s:on_activate: %s\n",
+ name, lua_tostring(L, -1));
+ }
+
return true;
}
lua_pop(L, 2); // pop luaentities, minetest
}
-std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
+std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id)
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_luaentity_get_state: id="<<id<<std::endl;
+ infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
+ StackUnroller stack_unroller(L);
+
+ // Get minetest.luaentities[id]
+ luaentity_get(L, id);
+ int object = lua_gettop(L);
+
+ // Get get_staticdata function
+ lua_pushvalue(L, object);
+ lua_getfield(L, -1, "get_staticdata");
+ if(lua_isnil(L, -1))
+ return "";
+
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_pushvalue(L, object); // self
+ // Call with 1 arguments, 1 results
+ if(lua_pcall(L, 1, 1, 0))
+ script_error(L, "error running function get_staticdata: %s\n",
+ lua_tostring(L, -1));
- return "";
+ size_t len=0;
+ const char *s = lua_tolstring(L, -1, &len);
+ return std::string(s, len);
}
void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
lua_pushnumber(L, dtime); // dtime
// Call with 2 arguments, 0 results
if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));
+ script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
}
// Calls entity:on_punch(ObjectRef puncher)