/* player */ void scriptapi_on_player_receive_fields(lua_State *L, ServerActiveObject *player, const std::string &formname, const std::map<std::string, std::string> &fields) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get minetest.registered_on_chat_messages lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_player_receive_fields"); // Call callbacks // param 1 objectref_get_or_create(L, player); // param 2 lua_pushstring(L, formname.c_str()); // param 3 lua_newtable(L); for(std::map<std::string, std::string>::const_iterator i = fields.begin(); i != fields.end(); i++){ const std::string &name = i->first; const std::string &value = i->second; lua_pushstring(L, name.c_str()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC); }
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider) { GameScripting *scriptIface = env->getScriptIface(); scriptIface->realityCheck(); lua_State *L = scriptIface->getStack(); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get minetest.registered_abms lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_abms"); luaL_checktype(L, -1, LUA_TTABLE); int registered_abms = lua_gettop(L); // Get minetest.registered_abms[m_id] lua_pushnumber(L, m_id); lua_gettable(L, registered_abms); if(lua_isnil(L, -1)) assert(0); // Call action luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, "action"); luaL_checktype(L, -1, LUA_TFUNCTION); push_v3s16(L, p); pushnode(L, n, env->getGameDef()->ndef()); lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); if(lua_pcall(L, 4, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); }
bool scriptapi_item_on_place(lua_State *L, ItemStack &item, ServerActiveObject *placer, const PointedThing &pointed) { actionstream<<"DebugTracePlaceEnteredLua scriptapi_item Line 250 "<<std::endl; realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); actionstream<<"DebugTracePlacePassedStackCheck scriptapi_item Line 254 "<<std::endl; // Push callback function on stack if(!get_item_callback(L, item.name.c_str(), "on_place")) actionstream<<"DebugTracePlaceLuaReturningFalse scriptapi_item Line 257 "<<std::endl; return false; // Call function LuaItemStack::create(L, item); objectref_get_or_create(L, placer); push_pointed_thing(L, pointed); actionstream<<"DebugTracePlaceClearToRunLua scriptapi_item Line 263 "<<std::endl; if(lua_pcall(L, 3, 1, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); if(!lua_isnil(L, -1)) actionstream<<"DebugTracePlaceItemReturned scriptapi_item Line 267 "<<std::endl; item = read_item(L, -1); actionstream<<"DebugTracePlaceLuaReturningTrue scriptapi_item Line 270 "<<std::endl; return true; }
void scriptapi_on_shutdown(lua_State *L) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get registered shutdown hooks lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_shutdown"); // Call callbacks scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST); }
void scriptapi_on_leaveplayer(lua_State *L, ServerActiveObject *player) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get minetest.registered_on_leaveplayers lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_leaveplayers"); // Call callbacks objectref_get_or_create(L, player); scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST); }
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider, MapNode neighbor, bool activate) { GameScripting *scriptIface = env->getScriptIface(); auto _script_lock = RecursiveMutexAutoLock(scriptIface->m_luastackmutex, std::try_to_lock); if (!_script_lock.owns_lock()) { return; } scriptIface->realityCheck(); lua_State *L = scriptIface->getStack(); sanity_check(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); int error_handler = PUSH_ERROR_HANDLER(L); // Get registered_abms lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_abms"); luaL_checktype(L, -1, LUA_TTABLE); lua_remove(L, -2); // Remove core // Get registered_abms[m_id] lua_pushnumber(L, m_id); lua_gettable(L, -2); if(lua_isnil(L, -1)) //FATAL_ERROR(""); return; lua_remove(L, -2); // Remove registered_abms scriptIface->setOriginFromTable(-1); // Call action luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, "action"); luaL_checktype(L, -1, LUA_TFUNCTION); lua_remove(L, -2); // Remove registered_abms[m_id] push_v3s16(L, p); pushnode(L, n, env->getGameDef()->ndef()); lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); pushnode(L, neighbor, env->getGameDef()->ndef()); lua_pushboolean(L, activate); int result = lua_pcall(L, 6, 0, error_handler); if (result) scriptIface->scriptError(result, "LuaABM::trigger"); lua_pop(L, 1); // Pop error handler }
bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get minetest.registered_on_respawnplayers lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_respawnplayers"); // Call callbacks objectref_get_or_create(L, player); scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR); bool positioning_handled_by_some = lua_toboolean(L, -1); return positioning_handled_by_some; }
void scriptapi_create_auth(lua_State *L, const std::string &playername, const std::string &password) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); get_auth_handler(L); lua_getfield(L, -1, "create_auth"); if(lua_type(L, -1) != LUA_TFUNCTION) throw LuaError(L, "Authentication handler missing create_auth"); lua_pushstring(L, playername.c_str()); lua_pushstring(L, password.c_str()); if(lua_pcall(L, 2, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); }
void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n) { GameScripting *scriptIface = env->getScriptIface(); auto _script_lock = RecursiveMutexAutoLock(scriptIface->m_luastackmutex, std::try_to_lock); if (!_script_lock.owns_lock()) { return; } scriptIface->realityCheck(); lua_State *L = scriptIface->getStack(); sanity_check(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); int error_handler = PUSH_ERROR_HANDLER(L); // Get registered_lbms lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_lbms"); luaL_checktype(L, -1, LUA_TTABLE); lua_remove(L, -2); // Remove core // Get registered_lbms[m_id] lua_pushnumber(L, m_id); lua_gettable(L, -2); //FATAL_ERROR_IF(lua_isnil(L, -1), "Entry with given id not found in registered_lbms table"); if (lua_isnil(L, -1)) { errorstream << "Entry with given id " << m_id << " not found in registered_lbms table" << std::endl; return; } lua_remove(L, -2); // Remove registered_lbms scriptIface->setOriginFromTable(-1); // Call action luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, "action"); luaL_checktype(L, -1, LUA_TFUNCTION); lua_remove(L, -2); // Remove registered_lbms[m_id] push_v3s16(L, p); pushnode(L, n, env->getGameDef()->ndef()); int result = lua_pcall(L, 2, 0, error_handler); if (result) scriptIface->scriptError(result, "LuaLBM::trigger"); lua_pop(L, 1); // Pop error handler }
void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); INodeDefManager *ndef = get_server(L)->ndef(); // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct")) return; // Call function push_v3s16(L, p); if(lua_pcall(L, 1, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); }
bool scriptapi_on_chat_message(lua_State *L, const std::string &name, const std::string &message) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Get minetest.registered_on_chat_messages lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_chat_messages"); // Call callbacks lua_pushstring(L, name.c_str()); lua_pushstring(L, message.c_str()); scriptapi_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC); bool ate = lua_toboolean(L, -1); return ate; }
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider) { GameScripting *scriptIface = env->getScriptIface(); scriptIface->realityCheck(); lua_State *L = scriptIface->getStack(); sanity_check(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); lua_pushcfunction(L, script_error_handler); int errorhandler = lua_gettop(L); // Get registered_abms lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_abms"); luaL_checktype(L, -1, LUA_TTABLE); lua_remove(L, -2); // Remove core // Get registered_abms[m_id] lua_pushnumber(L, m_id); lua_gettable(L, -2); if(lua_isnil(L, -1)) FATAL_ERROR(""); lua_remove(L, -2); // Remove registered_abms scriptIface->setOriginFromTable(-1); // Call action luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, "action"); luaL_checktype(L, -1, LUA_TFUNCTION); lua_remove(L, -2); // Remove registered_abms[m_id] push_v3s16(L, p); pushnode(L, n, env->getGameDef()->ndef()); lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); int result = lua_pcall(L, 4, 0, errorhandler); if (result) scriptIface->scriptError(result, "LuaABM::trigger"); lua_pop(L, 1); // Pop error handler }
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider, MapNode neighbor, bool activate) { GameScripting *scriptIface = env->getScriptIface(); auto _script_lock = std::unique_lock<std::recursive_mutex> (scriptIface->m_luastackmutex); scriptIface->realityCheck(); lua_State *L = scriptIface->getStack(); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); lua_pushcfunction(L, script_error_handler); int errorhandler = lua_gettop(L); // Get registered_abms lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_abms"); luaL_checktype(L, -1, LUA_TTABLE); lua_remove(L, -2); // Remove core // Get registered_abms[m_id] lua_pushnumber(L, m_id); lua_gettable(L, -2); if(lua_isnil(L, -1)) assert(0); lua_remove(L, -2); // Remove registered_abms // Call action luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, "action"); luaL_checktype(L, -1, LUA_TFUNCTION); lua_remove(L, -2); // Remove registered_abms[m_id] push_v3s16(L, p); pushnode(L, n, env->getGameDef()->ndef()); lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); pushnode(L, neighbor, env->getGameDef()->ndef()); lua_pushboolean(L, activate); if(lua_pcall(L, 6, 0, errorhandler)) script_error(L); lua_pop(L, 1); // Pop error handler }
bool scriptapi_item_on_use(lua_State *L, ItemStack &item, ServerActiveObject *user, const PointedThing &pointed) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); // Push callback function on stack if(!get_item_callback(L, item.name.c_str(), "on_use")) return false; // Call function LuaItemStack::create(L, item); objectref_get_or_create(L, user); push_pointed_thing(L, pointed); if(lua_pcall(L, 3, 1, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); if(!lua_isnil(L, -1)) item = read_item(L, -1); return true; }
bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node, ServerActiveObject *puncher) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); INodeDefManager *ndef = get_server(L)->ndef(); // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch")) return false; // Call function push_v3s16(L, p); pushnode(L, node, ndef); objectref_get_or_create(L, puncher); if(lua_pcall(L, 3, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); return true; }
void scriptapi_node_on_receive_fields(lua_State *L, v3s16 p, const std::string &formname, const std::map<std::string, std::string> &fields, ServerActiveObject *sender) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); INodeDefManager *ndef = get_server(L)->ndef(); // If node doesn't exist, we don't know what callback to call MapNode node = get_env(L)->getMap().getNodeNoEx(p); if(node.getContent() == CONTENT_IGNORE) return; // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields")) return; // Call function // param 1 push_v3s16(L, p); // param 2 lua_pushstring(L, formname.c_str()); // param 3 lua_newtable(L); for(std::map<std::string, std::string>::const_iterator i = fields.begin(); i != fields.end(); i++){ const std::string &name = i->first; const std::string &value = i->second; lua_pushstring(L, name.c_str()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } // param 4 objectref_get_or_create(L, sender); if(lua_pcall(L, 4, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); }
bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); INodeDefManager *ndef = get_server(L)->ndef(); // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer")) return false; // Call function push_v3s16(L, p); lua_pushnumber(L,dtime); if(lua_pcall(L, 2, 1, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true) return true; return false; }
bool scriptapi_get_auth(lua_State *L, const std::string &playername, std::string *dst_password, std::set<std::string> *dst_privs) { realitycheck(L); assert(lua_checkstack(L, 20)); StackUnroller stack_unroller(L); get_auth_handler(L); lua_getfield(L, -1, "get_auth"); if(lua_type(L, -1) != LUA_TFUNCTION) throw LuaError(L, "Authentication handler missing get_auth"); lua_pushstring(L, playername.c_str()); if(lua_pcall(L, 1, 1, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); // nil = login not allowed if(lua_isnil(L, -1)) return false; luaL_checktype(L, -1, LUA_TTABLE); std::string password; bool found = getstringfield(L, -1, "password", password); if(!found) throw LuaError(L, "Authentication handler didn't return password"); if(dst_password) *dst_password = password; lua_getfield(L, -1, "privileges"); if(!lua_istable(L, -1)) throw LuaError(L, "Authentication handler didn't return privilege table"); if(dst_privs) read_privileges(L, -1, *dst_privs); lua_pop(L, 1); return true; }
void scriptapi_export(lua_State *L, Server *server) { realitycheck(L); assert(lua_checkstack(L, 20)); verbosestream<<"scriptapi_export()"<<std::endl; StackUnroller stack_unroller(L); // Store server as light userdata in registry lua_pushlightuserdata(L, server); lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server"); // Register global functions in table minetest lua_newtable(L); luaL_register(L, NULL, minetest_f); lua_setglobal(L, "minetest"); // Get the main minetest table lua_getglobal(L, "minetest"); // Add tables to minetest lua_newtable(L); lua_setfield(L, -2, "object_refs"); lua_newtable(L); lua_setfield(L, -2, "luaentities"); // Register wrappers LuaItemStack::Register(L); InvRef::Register(L); NodeMetaRef::Register(L); NodeTimerRef::Register(L); ObjectRef::Register(L); EnvRef::Register(L); LuaPseudoRandom::Register(L); LuaPerlinNoise::Register(L); LuaPerlinNoiseMap::Register(L); }