// minetest.line_of_sight(pos1, pos2, stepsize) -> true/false int ModApiEnvMod::l_line_of_sight(lua_State *L) { float stepsize = 1.0; GET_ENV_PTR; // read position 1 from lua v3f pos1 = checkFloatPos(L, 1); // read position 2 from lua v3f pos2 = checkFloatPos(L, 2); //read step size from lua if (lua_isnumber(L, 3)) stepsize = lua_tonumber(L, 3); return (env->line_of_sight(pos1,pos2,stepsize)); }
// minetest.get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { // Get the table insert function lua_getglobal(L, "table"); lua_getfield(L, -1, "insert"); int table_insert = lua_gettop(L); GET_ENV_PTR; // Do it v3f pos = checkFloatPos(L, 1); float radius = luaL_checknumber(L, 2) * BS; std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); lua_newtable(L); int table = lua_gettop(L); for(std::set<u16>::const_iterator i = ids.begin(); i != ids.end(); i++){ ServerActiveObject *obj = env->getActiveObject(*i); // Insert object reference into table lua_pushvalue(L, table_insert); lua_pushvalue(L, table); getScriptApiBase(L)->objectrefGetOrCreate(obj); if(lua_pcall(L, 2, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); } return 1; }
// set_velocity(self, {x=num, y=num, z=num}) int ObjectRef::l_set_velocity(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); LuaEntitySAO *co = getluaobject(ref); if (co == NULL) return 0; v3f pos = checkFloatPos(L, 2); // Do it co->setVelocity(pos); return 0; }
// setacceleration(self, {x=num, y=num, z=num}) int ObjectRef::l_setacceleration(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); LuaEntitySAO *co = getluaobject(ref); if (co == NULL) return 0; // pos v3f pos = checkFloatPos(L, 2); // Do it co->setAcceleration(pos); return 0; }
// set_pos(self, pos) int ObjectRef::l_set_pos(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); //LuaEntitySAO *co = getluaobject(ref); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // pos v3f pos = checkFloatPos(L, 2); // Do it co->setPos(pos); return 0; }
// line_of_sight(pos1, pos2, stepsize) -> true/false, pos int ModApiEnvMod::l_line_of_sight(lua_State *L) { float stepsize = 1.0; GET_ENV_PTR; // read position 1 from lua v3f pos1 = checkFloatPos(L, 1); // read position 2 from lua v3f pos2 = checkFloatPos(L, 2); //read step size from lua if (lua_isnumber(L, 3)) { stepsize = lua_tonumber(L, 3); } v3s16 p; bool success = env->line_of_sight(pos1, pos2, stepsize, &p); lua_pushboolean(L, success); if (!success) { push_v3s16(L, p); return 2; } return 1; }
int LuaRaycast::create_object(lua_State *L) { NO_MAP_LOCK_REQUIRED; bool objects = true; bool liquids = false; v3f pos1 = checkFloatPos(L, 1); v3f pos2 = checkFloatPos(L, 2); if (lua_isboolean(L, 3)) { objects = lua_toboolean(L, 3); } if (lua_isboolean(L, 4)) { liquids = lua_toboolean(L, 4); } LuaRaycast *o = new LuaRaycast(core::line3d<f32>(pos1, pos2), objects, liquids); *(void **) (lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); return 1; }
// move_to(self, pos, continuous=false) int ObjectRef::l_move_to(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); //LuaEntitySAO *co = getluaobject(ref); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // pos v3f pos = checkFloatPos(L, 2); // continuous bool continuous = readParam<bool>(L, 3); // Do it co->moveTo(pos, continuous); return 0; }
// add_entity(pos, entityname) -> ObjectRef or nil // pos = {x=num, y=num, z=num} int ModApiEnvMod::l_add_entity(lua_State *L) { GET_ENV_PTR; // pos v3f pos = checkFloatPos(L, 1); // content const char *name = luaL_checkstring(L, 2); // Do it ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, ""); int objectid = env->addActiveObject(obj); // If failed to add, return nothing (reads as nil) if(objectid == 0) return 0; // Return ObjectRef getScriptApiBase(L)->objectrefGetOrCreate(obj); return 1; }
// get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { GET_ENV_PTR; // Do it v3f pos = checkFloatPos(L, 1); float radius = luaL_checknumber(L, 2) * BS; std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); ScriptApiBase *script = getScriptApiBase(L); lua_createtable(L, ids.size(), 0); std::set<u16>::const_iterator iter = ids.begin(); for(u32 i = 0; iter != ids.end(); iter++) { ServerActiveObject *obj = env->getActiveObject(*iter); // Insert object reference into table script->objectrefGetOrCreate(obj); lua_rawseti(L, -2, ++i); } return 1; }
// setvelocity(self, {x=num, y=num, z=num}) int ObjectRef::l_setvelocity(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); v3f pos = checkFloatPos(L, 2); PlayerSAO* ps = getplayersao(ref); if (ps) { ps->addSpeed(pos); return 0; } LuaEntitySAO *co = getluaobject(ref); if (co == NULL) return 0; // Do it co->setVelocity(pos); return 0; }