aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorWeblate <42@minetest.ru>2013-06-01 13:18:52 +0200
committerWeblate <42@minetest.ru>2013-06-01 13:18:52 +0200
commit1dfc2e02b3c358af4bb20bb3cb60ac7bd3ed1124 (patch)
tree54660f353f3342f89b6259ea6a5dfedfa20dc16f /src/script/lua_api
parentec039a3d123120f9918812b13f5971e94546b9af (diff)
parent64627817fcca52f20948c24b60ce192b218f6ce2 (diff)
downloadhax-minetest-server-1dfc2e02b3c358af4bb20bb3cb60ac7bd3ed1124.tar.gz
hax-minetest-server-1dfc2e02b3c358af4bb20bb3cb60ac7bd3ed1124.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to '')
-rw-r--r--src/script/lua_api/CMakeLists.txt13
-rw-r--r--src/script/lua_api/l_base.cpp63
-rw-r--r--src/script/lua_api/l_base.h63
-rw-r--r--src/script/lua_api/l_craft.cpp (renamed from src/scriptapi_craft.cpp)80
-rw-r--r--src/script/lua_api/l_craft.h52
-rw-r--r--src/script/lua_api/l_env.cpp687
-rw-r--r--src/script/lua_api/l_env.h (renamed from src/scriptapi_env.h)146
-rw-r--r--src/script/lua_api/l_inventory.cpp (renamed from src/scriptapi_inventory.cpp)411
-rw-r--r--src/script/lua_api/l_inventory.h (renamed from src/scriptapi_inventory.h)74
-rw-r--r--src/script/lua_api/l_item.cpp (renamed from src/scriptapi_item.cpp)480
-rw-r--r--src/script/lua_api/l_item.h (renamed from src/scriptapi_item.h)55
-rw-r--r--src/script/lua_api/l_nodemeta.cpp (renamed from src/scriptapi_nodemeta.cpp)257
-rw-r--r--src/script/lua_api/l_nodemeta.h (renamed from src/scriptapi_nodemeta.h)38
-rw-r--r--src/script/lua_api/l_nodetimer.cpp (renamed from src/scriptapi_nodetimer.cpp)7
-rw-r--r--src/script/lua_api/l_nodetimer.h (renamed from src/scriptapi_nodetimer.h)8
-rw-r--r--src/script/lua_api/l_noise.cpp (renamed from src/scriptapi_noise.cpp)45
-rw-r--r--src/script/lua_api/l_noise.h (renamed from src/scriptapi_noise.h)9
-rw-r--r--src/script/lua_api/l_object.cpp (renamed from src/scriptapi_object.cpp)302
-rw-r--r--src/script/lua_api/l_object.h (renamed from src/scriptapi_object.h)35
-rw-r--r--src/script/lua_api/l_particles.cpp (renamed from src/scriptapi_particles.cpp)46
-rw-r--r--src/script/lua_api/l_particles.h (renamed from src/scriptapi_particles.h)21
-rw-r--r--src/script/lua_api/luaapi.cpp653
-rw-r--r--src/script/lua_api/luaapi.h140
23 files changed, 2237 insertions, 1448 deletions
diff --git a/src/script/lua_api/CMakeLists.txt b/src/script/lua_api/CMakeLists.txt
new file mode 100644
index 000000000..7610ce8f1
--- /dev/null
+++ b/src/script/lua_api/CMakeLists.txt
@@ -0,0 +1,13 @@
+set(SCRIPT_LUA_API_SRCS
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_base.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_craft.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_env.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_inventory.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_item.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/luaapi.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_nodemeta.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_nodetimer.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_noise.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_object.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/l_particles.cpp
+ PARENT_SCOPE)
diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp
new file mode 100644
index 000000000..b1766e6df
--- /dev/null
+++ b/src/script/lua_api/l_base.cpp
@@ -0,0 +1,63 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "cpp_api/scriptapi.h"
+#include "lua_api/l_base.h"
+#include "common/c_internal.h"
+#include "log.h"
+
+extern "C" {
+#include "lua.h"
+}
+
+ModApiBase::ModApiBase() {
+ ScriptApi::registerModApiModule(this);
+}
+
+Server* ModApiBase::getServer(lua_State* L) {
+ return get_scriptapi(L)->getServer();
+}
+
+Environment* ModApiBase::getEnv(lua_State* L) {
+ return get_scriptapi(L)->getEnv();
+}
+
+bool ModApiBase::registerFunction( lua_State* L,
+ const char* name,
+ lua_CFunction fct,
+ int top
+ ) {
+ //TODO check presence first!
+
+ lua_pushstring(L,name);
+ lua_pushcfunction(L,fct);
+ lua_settable(L, top);
+
+ return true;
+}
+
+struct EnumString es_BiomeTerrainType[] =
+{
+ {BIOME_TERRAIN_NORMAL, "normal"},
+ {BIOME_TERRAIN_LIQUID, "liquid"},
+ {BIOME_TERRAIN_NETHER, "nether"},
+ {BIOME_TERRAIN_AETHER, "aether"},
+ {BIOME_TERRAIN_FLAT, "flat"},
+ {0, NULL},
+};
diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h
new file mode 100644
index 000000000..f2e0d59a9
--- /dev/null
+++ b/src/script/lua_api/l_base.h
@@ -0,0 +1,63 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef L_BASE_H_
+#define L_BASE_H_
+
+#include "biome.h"
+#include "common/c_types.h"
+
+extern "C" {
+#include "lua.h"
+}
+
+extern struct EnumString es_BiomeTerrainType[];
+
+class ScriptApi;
+class Server;
+class Environment;
+
+typedef class ModApiBase {
+
+public:
+ ModApiBase();
+
+ virtual bool Initialize(lua_State* L, int top) = 0;
+ virtual ~ModApiBase() {};
+
+protected:
+ static Server* getServer( lua_State* L);
+ static Environment* getEnv( lua_State* L);
+ static bool registerFunction( lua_State* L,
+ const char* name,
+ lua_CFunction fct,
+ int top
+ );
+} ModApiBase;
+
+#if (defined(WIN32) || defined(_WIN32_WCE))
+#define NO_MAP_LOCK_REQUIRED
+#else
+#include "main.h"
+#include "profiler.h"
+#define NO_MAP_LOCK_REQUIRED ScopeProfiler nolocktime(g_profiler,"Scriptapi: unlockable time",SPT_ADD)
+//#define NO_ENVLOCK_REQUIRED assert(getServer(L).m_env_mutex.IsLocked() == false)
+#endif
+
+#endif /* L_BASE_H_ */
diff --git a/src/scriptapi_craft.cpp b/src/script/lua_api/l_craft.cpp
index 459908237..a32fb9dff 100644
--- a/src/scriptapi_craft.cpp
+++ b/src/script/lua_api/l_craft.cpp
@@ -17,20 +17,24 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_craft.h"
+
+#include "lua_api/l_craft.h"
+#include "common/c_internal.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "server.h"
+#include "lua_api/l_item.h"
extern "C" {
-#include <lauxlib.h>
+#include "lauxlib.h"
}
-#include "script.h"
-#include "scriptapi_types.h"
-#include "scriptapi_common.h"
-#include "scriptapi_item.h"
+ModApiCraft::ModApiCraft()
+ : ModApiBase() {
+}
-struct EnumString es_CraftMethod[] =
+struct EnumString ModApiCraft::es_CraftMethod[] =
{
{CRAFT_METHOD_NORMAL, "normal"},
{CRAFT_METHOD_COOKING, "cooking"},
@@ -40,7 +44,7 @@ struct EnumString es_CraftMethod[] =
// helper for register_craft
-bool read_craft_recipe_shaped(lua_State *L, int index,
+bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index,
int &width, std::vector<std::string> &recipe)
{
if(index < 0)
@@ -81,7 +85,7 @@ bool read_craft_recipe_shaped(lua_State *L, int index,
}
// helper for register_craft
-bool read_craft_recipe_shapeless(lua_State *L, int index,
+bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index,
std::vector<std::string> &recipe)
{
if(index < 0)
@@ -103,7 +107,7 @@ bool read_craft_recipe_shapeless(lua_State *L, int index,
}
// helper for register_craft
-bool read_craft_replacements(lua_State *L, int index,
+bool ModApiCraft::readCraftReplacements(lua_State *L, int index,
CraftReplacements &replacements)
{
if(index < 0)
@@ -135,15 +139,16 @@ bool read_craft_replacements(lua_State *L, int index,
return true;
}
// register_craft({output=item, recipe={{item00,item10},{item01,item11}})
-int l_register_craft(lua_State *L)
+int ModApiCraft::l_register_craft(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
//infostream<<"register_craft"<<std::endl;
luaL_checktype(L, 1, LUA_TTABLE);
int table = 1;
// Get the writable craft definition manager from the server
IWritableCraftDefManager *craftdef =
- get_server(L)->getWritableCraftDefManager();
+ getServer(L)->getWritableCraftDefManager();
std::string type = getstringfield_default(L, table, "type", "shaped");
@@ -161,7 +166,7 @@ int l_register_craft(lua_State *L)
if(lua_isnil(L, -1))
throw LuaError(L, "Crafting definition is missing a recipe"
" (output=\"" + output + "\")");
- if(!read_craft_recipe_shaped(L, -1, width, recipe))
+ if(!readCraftRecipeShaped(L, -1, width, recipe))
throw LuaError(L, "Invalid crafting recipe"
" (output=\"" + output + "\")");
@@ -169,7 +174,7 @@ int l_register_craft(lua_State *L)
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
- if(!read_craft_replacements(L, -1, replacements))
+ if(!readCraftReplacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (output=\"" + output + "\")");
}
@@ -193,7 +198,7 @@ int l_register_craft(lua_State *L)
throw LuaError(L, "Crafting definition (shapeless)"
" is missing a recipe"
" (output=\"" + output + "\")");
- if(!read_craft_recipe_shapeless(L, -1, recipe))
+ if(!readCraftRecipeShapeless(L, -1, recipe))
throw LuaError(L, "Invalid crafting recipe"
" (output=\"" + output + "\")");
@@ -201,7 +206,7 @@ int l_register_craft(lua_State *L)
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
- if(!read_craft_replacements(L, -1, replacements))
+ if(!readCraftReplacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (output=\"" + output + "\")");
}
@@ -242,7 +247,7 @@ int l_register_craft(lua_State *L)
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
- if(!read_craft_replacements(L, -1, replacements))
+ if(!readCraftReplacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (cooking output=\"" + output + "\")");
}
@@ -266,7 +271,7 @@ int l_register_craft(lua_State *L)
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
- if(!read_craft_replacements(L, -1, replacements))
+ if(!readCraftReplacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (fuel recipe=\"" + recipe + "\")");
}
@@ -285,8 +290,10 @@ int l_register_craft(lua_State *L)
}
// get_craft_result(input)
-int l_get_craft_result(lua_State *L)
+int ModApiCraft::l_get_craft_result(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
+
int input_i = 1;
std::string method_s = getstringfield_default(L, input_i, "method", "normal");
enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
@@ -297,10 +304,10 @@ int l_get_craft_result(lua_State *L)
width = luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, input_i, "items");
- std::vector<ItemStack> items = read_items(L, -1);
+ std::vector<ItemStack> items = read_items(L, -1,getServer(L));
lua_pop(L, 1); // items
- IGameDef *gdef = get_server(L);
+ IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef();
CraftInput input(method, width, items);
CraftOutput output;
@@ -328,13 +335,15 @@ int l_get_craft_result(lua_State *L)
}
// get_craft_recipe(result item)
-int l_get_craft_recipe(lua_State *L)
+int ModApiCraft::l_get_craft_recipe(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
+
int k = 1;
int input_i = 1;
std::string o_item = luaL_checkstring(L,input_i);
- IGameDef *gdef = get_server(L);
+ IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef();
CraftInput input;
CraftOutput output(o_item,0);
@@ -379,10 +388,12 @@ int l_get_craft_recipe(lua_State *L)
}
// get_all_craft_recipes(result item)
-int l_get_all_craft_recipes(lua_State *L)
+int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
+
std::string o_item = luaL_checkstring(L,1);
- IGameDef *gdef = get_server(L);
+ IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef();
CraftInput input;
CraftOutput output(o_item,0);
@@ -423,8 +434,8 @@ int l_get_all_craft_recipes(lua_State *L)
{
if (i->empty())
continue;
- lua_pushinteger(L, k);
- lua_pushstring(L, i->name.c_str());
+ lua_pushinteger(L,k);
+ lua_pushstring(L,i->name.c_str());
lua_settable(L, -3);
}
lua_setfield(L, -2, "items");
@@ -451,3 +462,16 @@ int l_get_all_craft_recipes(lua_State *L)
}
return 1;
}
+
+bool ModApiCraft::Initialize(lua_State* L, int top) {
+ bool retval = true;
+
+ retval &= API_FCT(get_all_craft_recipes);
+ retval &= API_FCT(get_craft_recipe);
+ retval &= API_FCT(get_craft_result);
+ retval &= API_FCT(register_craft);
+
+ return retval;
+}
+
+ModApiCraft modapicraft_prototype;
diff --git a/src/script/lua_api/l_craft.h b/src/script/lua_api/l_craft.h
new file mode 100644
index 000000000..d8319199d
--- /dev/null
+++ b/src/script/lua_api/l_craft.h
@@ -0,0 +1,52 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef L_CRAFT_H_
+#define L_CRAFT_H_
+
+#include <vector>
+
+extern "C" {
+#include <lua.h>
+}
+
+#include "lua_api/l_base.h"
+#include "craftdef.h"
+
+class ModApiCraft : public ModApiBase {
+public:
+ ModApiCraft();
+ bool Initialize(lua_State* L, int top);
+private:
+ static int l_register_craft(lua_State *L);
+ static int l_get_craft_recipe(lua_State *L);
+ static int l_get_all_craft_recipes(lua_State *L);
+ static int l_get_craft_result(lua_State *L);
+
+ static bool readCraftReplacements(lua_State *L, int index,
+ CraftReplacements &replacements);
+ static bool readCraftRecipeShapeless(lua_State *L, int index,
+ std::vector<std::string> &recipe);
+ static bool readCraftRecipeShaped(lua_State *L, int index,
+ int &width, std::vector<std::string> &recipe);
+
+ static struct EnumString es_CraftMethod[];
+};
+
+#endif /* L_CRAFT_H_ */
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
new file mode 100644
index 000000000..58cf337ed
--- /dev/null
+++ b/src/script/lua_api/l_env.cpp
@@ -0,0 +1,687 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "cpp_api/scriptapi.h"
+#include "lua_api/l_base.h"
+#include "lua_api/l_env.h"
+#include "environment.h"
+#include "server.h"
+#include "daynightratio.h"
+#include "util/pointedthing.h"
+#include "content_sao.h"
+
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "common/c_internal.h"
+#include "lua_api/l_nodemeta.h"
+#include "lua_api/l_nodetimer.h"
+#include "lua_api/l_noise.h"
+#include "treegen.h"
+#include "pathfinder.h"
+
+
+#define GET_ENV_PTR ServerEnvironment* env = \
+ dynamic_cast<ServerEnvironment*>(getEnv(L)); \
+ if( env == NULL) return 0
+
+void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
+ u32 active_object_count, u32 active_object_count_wider)
+{
+ ScriptApi* scriptIface = SERVER_TO_SA(env);
+ 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));
+}
+
+// Exported functions
+
+// minetest.set_node(pos, node)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_set_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ INodeDefManager *ndef = env->getGameDef()->ndef();
+ // parameters
+ v3s16 pos = read_v3s16(L, 1);
+ MapNode n = readnode(L, 2, ndef);
+ // Do it
+ bool succeeded = env->setNode(pos, n);
+ lua_pushboolean(L, succeeded);
+ return 1;
+}
+
+int ModApiEnvMod::l_add_node(lua_State *L)
+{
+ return l_set_node(L);
+}
+
+// minetest.remove_node(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_remove_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // parameters
+ v3s16 pos = read_v3s16(L, 1);
+ // Do it
+ bool succeeded = env->removeNode(pos);
+ lua_pushboolean(L, succeeded);
+ return 1;
+}
+
+// minetest.get_node(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // pos
+ v3s16 pos = read_v3s16(L, 1);
+ // Do it
+ MapNode n = env->getMap().getNodeNoEx(pos);
+ // Return node
+ pushnode(L, n, env->getGameDef()->ndef());
+ return 1;
+}
+
+// minetest.get_node_or_nil(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // pos
+ v3s16 pos = read_v3s16(L, 1);
+ // Do it
+ try{
+ MapNode n = env->getMap().getNode(pos);
+ // Return node
+ pushnode(L, n, env->getGameDef()->ndef());
+ return 1;
+ } catch(InvalidPositionException &e)
+ {
+ lua_pushnil(L);
+ return 1;
+ }
+}
+
+// minetest.get_node_light(pos, timeofday)
+// pos = {x=num, y=num, z=num}
+// timeofday: nil = current time, 0 = night, 0.5 = day
+int ModApiEnvMod::l_get_node_light(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ v3s16 pos = read_v3s16(L, 1);
+ u32 time_of_day = env->getTimeOfDay();
+ if(lua_isnumber(L, 2))
+ time_of_day = 24000.0 * lua_tonumber(L, 2);
+ time_of_day %= 24000;
+ u32 dnr = time_to_daynight_ratio(time_of_day, true);
+ try{
+ MapNode n = env->getMap().getNode(pos);
+ INodeDefManager *ndef = env->getGameDef()->ndef();
+ lua_pushinteger(L, n.getLightBlend(dnr, ndef));
+ return 1;
+ } catch(InvalidPositionException &e)
+ {
+ lua_pushnil(L);
+ return 1;
+ }
+}
+
+// minetest.place_node(pos, node)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_place_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 pos = read_v3s16(L, 1);
+ MapNode n = readnode(L, 2, env->getGameDef()->ndef());
+
+ // Don't attempt to load non-loaded area as of now
+ MapNode n_old = env->getMap().getNodeNoEx(pos);
+ if(n_old.getContent() == CONTENT_IGNORE){
+ lua_pushboolean(L, false);
+ return 1;
+ }
+ // Create item to place
+ INodeDefManager *ndef = getServer(L)->ndef();
+ IItemDefManager *idef = getServer(L)->idef();
+ ItemStack item(ndef->get(n).name, 1, 0, "", idef);
+ // Make pointed position
+ PointedThing pointed;
+ pointed.type = POINTEDTHING_NODE;
+ pointed.node_abovesurface = pos;
+ pointed.node_undersurface = pos + v3s16(0,-1,0);
+ // Place it with a NULL placer (appears in Lua as a non-functional
+ // ObjectRef)
+ bool success = get_scriptapi(L)->item_OnPlace(item, NULL, pointed);
+ lua_pushboolean(L, success);
+ return 1;
+}
+
+// minetest.dig_node(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_dig_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 pos = read_v3s16(L, 1);
+
+ // Don't attempt to load non-loaded area as of now
+ MapNode n = env->getMap().getNodeNoEx(pos);
+ if(n.getContent() == CONTENT_IGNORE){
+ lua_pushboolean(L, false);
+ return 1;
+ }
+ // Dig it out with a NULL digger (appears in Lua as a
+ // non-functional ObjectRef)
+ bool success = get_scriptapi(L)->node_on_dig(pos, n, NULL);
+ lua_pushboolean(L, success);
+ return 1;
+}
+
+// minetest.punch_node(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_punch_node(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 pos = read_v3s16(L, 1);
+
+ // Don't attempt to load non-loaded area as of now
+ MapNode n = env->getMap().getNodeNoEx(pos);
+ if(n.getContent() == CONTENT_IGNORE){
+ lua_pushboolean(L, false);
+ return 1;
+ }
+ // Punch it with a NULL puncher (appears in Lua as a non-functional
+ // ObjectRef)
+ bool success = get_scriptapi(L)->node_on_punch(pos, n, NULL);
+ lua_pushboolean(L, success);
+ return 1;
+}
+
+// minetest.get_meta(pos)
+int ModApiEnvMod::l_get_meta(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ v3s16 p = read_v3s16(L, 1);
+ NodeMetaRef::create(L, p, env);
+ return 1;
+}
+
+// minetest.get_node_timer(pos)
+int ModApiEnvMod::l_get_node_timer(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ v3s16 p = read_v3s16(L, 1);
+ NodeTimerRef::create(L, p, env);
+ return 1;
+}
+
+// minetest.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
+ get_scriptapi(L)->objectrefGetOrCreate(obj);
+ return 1;
+}
+
+// minetest.add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_add_item(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // pos
+ //v3f pos = checkFloatPos(L, 1);
+ // item
+ ItemStack item = read_item(L, 2,getServer(L));
+ if(item.empty() || !item.isKnown(getServer(L)->idef()))
+ return 0;
+ // Use minetest.spawn_item to spawn a __builtin:item
+ lua_getglobal(L, "minetest");
+ lua_getfield(L, -1, "spawn_item");
+ if(lua_isnil(L, -1))
+ return 0;
+ lua_pushvalue(L, 1);
+ lua_pushstring(L, item.getItemString().c_str());
+ if(lua_pcall(L, 2, 1, 0))
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ return 1;
+ /*lua_pushvalue(L, 1);
+ lua_pushstring(L, "__builtin:item");
+ lua_pushstring(L, item.getItemString().c_str());
+ return l_add_entity(L);*/
+ /*// Do it
+ ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
+ int objectid = env->addActiveObject(obj);
+ // If failed to add, return nothing (reads as nil)
+ if(objectid == 0)
+ return 0;
+ // Return ObjectRef
+ objectrefGetOrCreate(L, obj);
+ return 1;*/
+}
+
+// minetest.get_player_by_name(name)
+int ModApiEnvMod::l_get_player_by_name(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ const char *name = luaL_checkstring(L, 1);
+ Player *player = env->getPlayer(name);
+ if(player == NULL){
+ lua_pushnil(L);
+ return 1;
+ }
+ PlayerSAO *sao = player->getPlayerSAO();
+ if(sao == NULL){
+ lua_pushnil(L);
+ return 1;
+ }
+ // Put player on stack
+ get_scriptapi(L)->objectrefGetOrCreate(sao);
+ return 1;
+}
+
+// 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);
+ get_scriptapi(L)->objectrefGetOrCreate(obj);
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ }
+ return 1;
+}
+
+// minetest.set_timeofday(val)
+// val = 0...1
+int ModApiEnvMod::l_set_timeofday(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ float timeofday_f = luaL_checknumber(L, 1);
+ assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
+ int timeofday_mh = (int)(timeofday_f * 24000.0);
+ // This should be set directly in the environment but currently
+ // such changes aren't immediately sent to the clients, so call
+ // the server instead.
+ //env->setTimeOfDay(timeofday_mh);
+ getServer(L)->setTimeOfDay(timeofday_mh);
+ return 0;
+}
+
+// minetest.get_timeofday() -> 0...1
+int ModApiEnvMod::l_get_timeofday(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ // Do it
+ int timeofday_mh = env->getTimeOfDay();
+ float timeofday_f = (float)timeofday_mh / 24000.0;
+ lua_pushnumber(L, timeofday_f);
+ return 1;
+}
+
+
+// minetest.find_node_near(pos, radius, nodenames) -> pos or nil
+// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
+int ModApiEnvMod::l_find_node_near(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ INodeDefManager *ndef = getServer(L)->ndef();
+ v3s16 pos = read_v3s16(L, 1);
+ int radius = luaL_checkinteger(L, 2);
+ std::set<content_t> filter;
+ if(lua_istable(L, 3)){
+ int table = 3;
+ lua_pushnil(L);
+ while(lua_next(L, table) != 0){
+ // key at index -2 and value at index -1
+ luaL_checktype(L, -1, LUA_TSTRING);
+ ndef->getIds(lua_tostring(L, -1), filter);
+ // removes value, keeps key for next iteration
+ lua_pop(L, 1);
+ }
+ } else if(lua_isstring(L, 3)){
+ ndef->getIds(lua_tostring(L, 3), filter);
+ }
+
+ for(int d=1; d<=radius; d++){
+ std::list<v3s16> list;
+ getFacePositions(list, d);
+ for(std::list<v3s16>::iterator i = list.begin();
+ i != list.end(); ++i){
+ v3s16 p = pos + (*i);
+ content_t c = env->getMap().getNodeNoEx(p).getContent();
+ if(filter.count(c) != 0){
+ push_v3s16(L, p);
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+// minetest.find_nodes_in_area(minp, maxp, nodenames) -> list of positions
+// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
+int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ INodeDefManager *ndef = getServer(L)->ndef();
+ v3s16 minp = read_v3s16(L, 1);
+ v3s16 maxp = read_v3s16(L, 2);
+ std::set<content_t> filter;
+ if(lua_istable(L, 3)){
+ int table = 3;
+ lua_pushnil(L);
+ while(lua_next(L, table) != 0){
+ // key at index -2 and value at index -1
+ luaL_checktype(L, -1, LUA_TSTRING);
+ ndef->getIds(lua_tostring(L, -1), filter);
+ // removes value, keeps key for next iteration
+ lua_pop(L, 1);
+ }
+ } else if(lua_isstring(L, 3)){
+ ndef->getIds(lua_tostring(L, 3), filter);
+ }
+
+ // Get the table insert function
+ lua_getglobal(L, "table");
+ lua_getfield(L, -1, "insert");
+ int table_insert = lua_gettop(L);
+
+ lua_newtable(L);
+ int table = lua_gettop(L);
+ for(s16 x=minp.X; x<=maxp.X; x++)
+ for(s16 y=minp.Y; y<=maxp.Y; y++)
+ for(s16 z=minp.Z; z<=maxp.Z; z++)
+ {
+ v3s16 p(x,y,z);
+ content_t c = env->getMap().getNodeNoEx(p).getContent();
+ if(filter.count(c) != 0){
+ lua_pushvalue(L, table_insert);
+ lua_pushvalue(L, table);
+ push_v3s16(L, p);
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ }
+ }
+ return 1;
+}
+
+// minetest.get_perlin(seeddiff, octaves, persistence, scale)
+// returns world-specific PerlinNoise
+int ModApiEnvMod::l_get_perlin(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ int seeddiff = luaL_checkint(L, 1);
+ int octaves = luaL_checkint(L, 2);
+ float persistence = luaL_checknumber(L, 3);
+ float scale = luaL_checknumber(L, 4);
+
+ LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
+ luaL_getmetatable(L, "PerlinNoise");
+ lua_setmetatable(L, -2);
+ return 1;
+}
+
+// minetest.get_perlin_map(noiseparams, size)
+// returns world-specific PerlinNoiseMap
+int ModApiEnvMod::l_get_perlin_map(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ NoiseParams *np = read_noiseparams(L, 1);
+ if (!np)
+ return 0;
+ v3s16 size = read_v3s16(L, 2);
+
+ int seed = (int)(env->getServerMap().getSeed());
+ LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(np, seed, size);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
+ luaL_getmetatable(L, "PerlinNoiseMap");
+ lua_setmetatable(L, -2);
+ return 1;
+}
+
+// minetest.clear_objects()
+// clear all objects in the environment
+int ModApiEnvMod::l_clear_objects(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ env->clearAllObjects();
+ return 0;
+}
+
+// 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, 2);
+ // 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.find_path(pos1, pos2, searchdistance,
+// max_jump, max_drop, algorithm) -> table containing path
+int ModApiEnvMod::l_find_path(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 pos1 = read_v3s16(L, 2);
+ v3s16 pos2 = read_v3s16(L, 3);
+ unsigned int searchdistance = luaL_checkint(L, 4);
+ unsigned int max_jump = luaL_checkint(L, 5);
+ unsigned int max_drop = luaL_checkint(L, 6);
+ algorithm algo = A_PLAIN_NP;
+ if(! lua_isnil(L, 7)) {
+ std::string algorithm = luaL_checkstring(L,7);
+
+ if (algorithm == "A*")
+ algo = A_PLAIN;
+
+ if (algorithm == "Dijkstra")
+ algo = DIJKSTRA;
+ }
+
+ std::vector<v3s16> path =
+ get_Path(env,pos1,pos2,searchdistance,max_jump,max_drop,algo);
+
+ if (path.size() > 0)
+ {
+ lua_newtable(L);
+ int top = lua_gettop(L);
+ unsigned int index = 1;
+ for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
+ {
+ lua_pushnumber(L,index);
+ push_v3s16(L, *i);
+ lua_settable(L, top);
+ index++;
+ }
+ return 1;
+ }
+
+ return 0;
+}
+
+// minetest.spawn_tree(pos, treedef)
+int ModApiEnvMod::l_spawn_tree(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 p0 = read_v3s16(L, 1);
+
+ treegen::TreeDef tree_def;
+ std::string trunk,leaves,fruit;
+ INodeDefManager *ndef = env->getGameDef()->ndef();
+
+ if(lua_istable(L, 2))
+ {
+ getstringfield(L, 2, "axiom", tree_def.initial_axiom);
+ getstringfield(L, 2, "rules_a", tree_def.rules_a);
+ getstringfield(L, 2, "rules_b", tree_def.rules_b);
+ getstringfield(L, 2, "rules_c", tree_def.rules_c);
+ getstringfield(L, 2, "rules_d", tree_def.rules_d);
+ getstringfield(L, 2, "trunk", trunk);
+ tree_def.trunknode=ndef->getId(trunk);
+ getstringfield(L, 2, "leaves", leaves);
+ tree_def.leavesnode=ndef->getId(leaves);
+ tree_def.leaves2_chance=0;
+ getstringfield(L, 2, "leaves2", leaves);
+ if (leaves !="")
+ {
+ tree_def.leaves2node=ndef->getId(leaves);
+ getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
+ }
+ getintfield(L, 2, "angle", tree_def.angle);
+ getintfield(L, 2, "iterations", tree_def.iterations);
+ getintfield(L, 2, "random_level", tree_def.iterations_random_level);
+ getstringfield(L, 2, "trunk_type", tree_def.trunk_type);
+ getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
+ tree_def.fruit_chance=0;
+ getstringfield(L, 2, "fruit", fruit);
+ if (fruit != "")
+ {
+ tree_def.fruitnode=ndef->getId(fruit);
+ getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
+ }
+ getintfield(L, 2, "seed", tree_def.seed);
+ }
+ else
+ return 0;
+ treegen::spawn_ltree (env, p0, ndef, tree_def);
+ return 1;
+}
+
+bool ModApiEnvMod::Initialize(lua_State *L,int top)
+{
+
+ bool retval = true;
+
+ retval &= API_FCT(set_node);
+ retval &= API_FCT(add_node);
+ retval &= API_FCT(add_item);
+ retval &= API_FCT(remove_node);
+ retval &= API_FCT(get_node);
+ retval &= API_FCT(get_node_or_nil);
+ retval &= API_FCT(get_node_light);
+ retval &= API_FCT(place_node);
+ retval &= API_FCT(dig_node);
+ retval &= API_FCT(punch_node);
+ retval &= API_FCT(add_entity);
+ retval &= API_FCT(get_meta);
+ retval &= API_FCT(get_node_timer);
+ retval &= API_FCT(get_player_by_name);
+ retval &= API_FCT(get_objects_inside_radius);
+ retval &= API_FCT(set_timeofday);
+ retval &= API_FCT(get_timeofday);
+ retval &= API_FCT(find_node_near);
+ retval &= API_FCT(find_nodes_in_area);
+ retval &= API_FCT(get_perlin);
+ retval &= API_FCT(get_perlin_map);
+ retval &= API_FCT(clear_objects);
+ retval &= API_FCT(spawn_tree);
+
+ return retval;
+}
+
+ModApiEnvMod modapienv_prototype;
diff --git a/src/scriptapi_env.h b/src/script/lua_api/l_env.h
index 2b7ea9573..61ecaecc8 100644
--- a/src/scriptapi_env.h
+++ b/src/script/lua_api/l_env.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_ENVIRONMENT_H_
-#define LUA_ENVIRONMENT_H_
+#ifndef L_ENV_H_
+#define L_ENV_H_
extern "C" {
#include <lua.h>
@@ -26,145 +26,147 @@ extern "C" {
}
#include "environment.h"
+#include "lua_api/l_base.h"
-/*
- EnvRef
-*/
-
-class EnvRef
+class ModApiEnvMod
+ :public ModApiBase
{
private:
- ServerEnvironment *m_env;
-
- static const char className[];
- static const luaL_reg methods[];
-
- static int gc_object(lua_State *L) ;
-
- static EnvRef *checkobject(lua_State *L, int narg);
-
- // Exported functions
-
- // EnvRef:set_node(pos, node)
+ // minetest.set_node(pos, node)
// pos = {x=num, y=num, z=num}
static int l_set_node(lua_State *L);
static int l_add_node(lua_State *L);
- // EnvRef:remove_node(pos)
+ // minetest.remove_node(pos)
// pos = {x=num, y=num, z=num}
static int l_remove_node(lua_State *L);
- // EnvRef:get_node(pos)
+ // minetest.get_node(pos)
// pos = {x=num, y=num, z=num}
static int l_get_node(lua_State *L);
- // EnvRef:get_node_or_nil(pos)
+ // minetest.get_node_or_nil(pos)
// pos = {x=num, y=num, z=num}
static int l_get_node_or_nil(lua_State *L);
- // EnvRef:get_node_light(pos, timeofday)
+ // minetest.get_node_light(pos, timeofday)
// pos = {x=num, y=num, z=num}
// timeofday: nil = current time, 0 = night, 0.5 = day
static int l_get_node_light(lua_State *L);
- // EnvRef:place_node(pos, node)
+ // minetest.place_node(pos, node)
// pos = {x=num, y=num, z=num}
static int l_place_node(lua_State *L);
- // EnvRef:dig_node(pos)
+ // minetest.dig_node(pos)
// pos = {x=num, y=num, z=num}
static int l_dig_node(lua_State *L);
- // EnvRef:punch_node(pos)
+ // minetest.punch_node(pos)
// pos = {x=num, y=num, z=num}
static int l_punch_node(lua_State *L);
- // EnvRef:get_meta(pos)
+ // minetest.get_meta(pos)
static int l_get_meta(lua_State *L);
- // EnvRef:get_node_timer(pos)
+ // minetest.get_node_timer(pos)
static int l_get_node_timer(lua_State *L);
- // EnvRef:add_entity(pos, entityname) -> ObjectRef or nil
+ // minetest.add_entity(pos, entityname) -> ObjectRef or nil
// pos = {x=num, y=num, z=num}
static int l_add_entity(lua_State *L);
- // EnvRef:add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
+ // minetest.add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
// pos = {x=num, y=num, z=num}
static int l_add_item(lua_State *L);
- // EnvRef:add_rat(pos)
- // pos = {x=num, y=num, z=num}
- static int l_add_rat(lua_State *L);
-
- // EnvRef:add_firefly(pos)
- // pos = {x=num, y=num, z=num}
- static int l_add_firefly(lua_State *L);
-
- // EnvRef:get_player_by_name(name)
+ // minetest.get_player_by_name(name)
static int l_get_player_by_name(lua_State *L);
- // EnvRef:get_objects_inside_radius(pos, radius)
+ // minetest.get_objects_inside_radius(pos, radius)
static int l_get_objects_inside_radius(lua_State *L);
- // EnvRef:set_timeofday(val)
+ // minetest.set_timeofday(val)
// val = 0...1
static int l_set_timeofday(lua_State *L);
- // EnvRef:get_timeofday() -> 0...1
+ // minetest.get_timeofday() -> 0...1
static int l_get_timeofday(lua_State *L);
- // EnvRef:find_node_near(pos, radius, nodenames) -> pos or nil
+ // minetest.find_node_near(pos, radius, nodenames) -> pos or nil
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
static int l_find_node_near(lua_State *L);
- // EnvRef:find_nodes_in_area(minp, maxp, nodenames) -> list of positions
+ // minetest.find_nodes_in_area(minp, maxp, nodenames) -> list of positions
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
static int l_find_nodes_in_area(lua_State *L);
- // EnvRef:get_perlin(seeddiff, octaves, persistence, scale)
- // returns world-specific PerlinNoise
+ // minetest.get_perlin(seeddiff, octaves, persistence, scale)
+ // returns world-specific PerlinNoise
static int l_get_perlin(lua_State *L);
- // EnvRef:get_perlin_map(noiseparams, size)
- // returns world-specific PerlinNoiseMap
+ // minetest.get_perlin_map(noiseparams, size)
+ // returns world-specific PerlinNoiseMap
static int l_get_perlin_map(lua_State *L);
- // EnvRef:clear_objects()
+ // minetest.clear_objects()
// clear all objects in the environment
static int l_clear_objects(lua_State *L);
+ // minetest.spawn_tree(pos, treedef)
static int l_spawn_tree(lua_State *L);
-
+ // minetest.line_of_sight(pos1, pos2, stepsize) -> true/false
static int l_line_of_sight(lua_State *L);
- //find a path between two positions
+ // minetest.find_path(pos1, pos2, searchdistance,
+ // max_jump, max_drop, algorithm) -> table containing path
static int l_find_path(lua_State *L);
public:
- EnvRef(ServerEnvironment *env);
-
- ~EnvRef();
-
- // Creates an EnvRef and leaves it on top of stack
- // Not callable from Lua; all references are created on the C side.
- static void create(lua_State *L, ServerEnvironment *env);
+ bool Initialize(lua_State *L, int top);
+};
- static void set_null(lua_State *L);
+class LuaABM : public ActiveBlockModifier
+{
+private:
+ int m_id;
- static void Register(lua_State *L);
+ std::set<std::string> m_trigger_contents;
+ std::set<std::string> m_required_neighbors;
+ float m_trigger_interval;
+ u32 m_trigger_chance;
+public:
+ LuaABM(lua_State *L, int id,
+ const std::set<std::string> &trigger_contents,
+ const std::set<std::string> &required_neighbors,
+ float trigger_interval, u32 trigger_chance):
+ m_id(id),
+ m_trigger_contents(trigger_contents),
+ m_required_neighbors(required_neighbors),
+ m_trigger_interval(trigger_interval),
+ m_trigger_chance(trigger_chance)
+ {
+ }
+ virtual std::set<std::string> getTriggerContents()
+ {
+ return m_trigger_contents;
+ }
+ virtual std::set<std::string> getRequiredNeighbors()
+ {
+ return m_required_neighbors;
+ }
+ virtual float getTriggerInterval()
+ {
+ return m_trigger_interval;
+ }
+ virtual u32 getTriggerChance()
+ {
+ return m_trigger_chance;
+ }
+ virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
+ u32 active_object_count, u32 active_object_count_wider);
};
-/*****************************************************************************/
-/* Minetest interface */
-/*****************************************************************************/
-// On environment step
-void scriptapi_environment_step(lua_State *L, float dtime);
-// After generating a piece of map
-void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp,
- u32 blockseed);
-void scriptapi_add_environment(lua_State *L, ServerEnvironment *env);
-
-#endif /* LUA_ENVIRONMENT_H_ */
+#endif /* L_ENV_H_ */
diff --git a/src/scriptapi_inventory.cpp b/src/script/lua_api/l_inventory.cpp
index bb40748db..1404c3c8a 100644
--- a/src/scriptapi_inventory.cpp
+++ b/src/script/lua_api/l_inventory.cpp
@@ -17,17 +17,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_inventory.h"
+#include "cpp_api/scriptapi.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "lua_api/l_inventory.h"
+#include "lua_api/l_item.h"
+#include "common/c_internal.h"
#include "server.h"
-#include "script.h"
#include "log.h"
-#include "scriptapi_types.h"
-#include "scriptapi_common.h"
-#include "scriptapi_inventory.h"
-#include "scriptapi_item.h"
-#include "scriptapi_object.h"
-
+#include "inventorymanager.h"
/*
InvRef
@@ -42,12 +40,13 @@ InvRef* InvRef::checkobject(lua_State *L, int narg)
Inventory* InvRef::getinv(lua_State *L, InvRef *ref)
{
- return get_server(L)->getInventory(ref->m_loc);
+ return STACK_TO_SERVER(L)->getInventory(ref->m_loc);
}
InventoryList* InvRef::getlist(lua_State *L, InvRef *ref,
const char *listname)
{
+ NO_MAP_LOCK_REQUIRED;
Inventory *inv = getinv(L, ref);
if(!inv)
return NULL;
@@ -57,7 +56,7 @@ InventoryList* InvRef::getlist(lua_State *L, InvRef *ref,
void InvRef::reportInventoryChange(lua_State *L, InvRef *ref)
{
// Inform other things that the inventory has changed
- get_server(L)->setInventoryModified(ref->m_loc);
+ STACK_TO_SERVER(L)->setInventoryModified(ref->m_loc);
}
// Exported functions
@@ -72,6 +71,7 @@ int InvRef::gc_object(lua_State *L) {
// is_empty(self, listname) -> true/false
int InvRef::l_is_empty(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
@@ -86,6 +86,7 @@ int InvRef::l_is_empty(lua_State *L)
// get_size(self, listname)
int InvRef::l_get_size(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
@@ -100,6 +101,7 @@ int InvRef::l_get_size(lua_State *L)
// get_width(self, listname)
int InvRef::l_get_width(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
@@ -114,6 +116,7 @@ int InvRef::l_get_width(lua_State *L)
// set_size(self, listname, size)
int InvRef::l_set_size(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
int newsize = luaL_checknumber(L, 3);
@@ -136,6 +139,7 @@ int InvRef::l_set_size(lua_State *L)
// set_width(self, listname, size)
int InvRef::l_set_width(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
int newwidth = luaL_checknumber(L, 3);
@@ -153,6 +157,7 @@ int InvRef::l_set_width(lua_State *L)
// get_stack(self, listname, i) -> itemstack
int InvRef::l_get_stack(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3) - 1;
@@ -167,10 +172,11 @@ int InvRef::l_get_stack(lua_State *L)
// set_stack(self, listname, i, stack) -> true/false
int InvRef::l_set_stack(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3) - 1;
- ItemStack newitem = read_item(L, 4);
+ ItemStack newitem = read_item(L, 4,STACK_TO_SERVER(L));
InventoryList *list = getlist(L, ref, listname);
if(list != NULL && i >= 0 && i < (int) list->getSize()){
list->changeItem(i, newitem);
@@ -185,25 +191,27 @@ int InvRef::l_set_stack(lua_State *L)
// get_list(self, listname) -> list or nil
int InvRef::l_get_list(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv = getinv(L, ref);
- inventory_get_list_to_lua(inv, listname, L);
+ push_inventory_list(inv, listname, L);
return 1;
}
// set_list(self, listname, list)
int InvRef::l_set_list(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv = getinv(L, ref);
InventoryList *list = inv->getList(listname);
if(list)
- inventory_set_list_from_lua(inv, listname, L, 3,
- list->getSize());
+ read_inventory_list(inv, listname, L, 3,
+ STACK_TO_SERVER(L),list->getSize());
else
- inventory_set_list_from_lua(inv, listname, L, 3);
+ read_inventory_list(inv, listname, L, 3,STACK_TO_SERVER(L));
reportInventoryChange(L, ref);
return 0;
}
@@ -212,9 +220,10 @@ int InvRef::l_set_list(lua_State *L)
// Returns the leftover stack
int InvRef::l_add_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3);
+ ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
InventoryList *list = getlist(L, ref, listname);
if(list){
ItemStack leftover = list->addItem(item);
@@ -231,9 +240,10 @@ int InvRef::l_add_item(lua_State *L)
// Returns true if the item completely fits into the list
int InvRef::l_room_for_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3);
+ ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
InventoryList *list = getlist(L, ref, listname);
if(list){
lua_pushboolean(L, list->roomForItem(item));
@@ -247,9 +257,10 @@ int InvRef::l_room_for_item(lua_State *L)
// Returns true if the list contains the given count of the given item name
int InvRef::l_contains_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3);
+ ItemStack item = read_item(L, 3, STACK_TO_SERVER(L));
InventoryList *list = getlist(L, ref, listname);
if(list){
lua_pushboolean(L, list->containsItem(item));
@@ -263,9 +274,10 @@ int InvRef::l_contains_item(lua_State *L)
// Returns the items that were actually removed
int InvRef::l_remove_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3);
+ ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
InventoryList *list = getlist(L, ref, listname);
if(list){
ItemStack removed = list->removeItem(item);
@@ -281,6 +293,7 @@ int InvRef::l_remove_item(lua_State *L)
// get_location() -> location (like minetest.get_inventory(location))
int InvRef::l_get_location(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const InventoryLocation &loc = ref->m_loc;
switch(loc.type){
@@ -293,10 +306,10 @@ int InvRef::l_get_location(lua_State *L)
return 1;
case InventoryLocation::NODEMETA:
lua_newtable(L);
- lua_pushstring(L, "nodemeta");
+ lua_pushstring(L, "node");
lua_setfield(L, -2, "type");
push_v3s16(L, loc.p);
- lua_setfield(L, -2, "name");
+ lua_setfield(L, -2, "pos");
return 1;
case InventoryLocation::DETACHED:
lua_newtable(L);
@@ -329,6 +342,7 @@ InvRef::~InvRef()
// Not callable from Lua; all references are created on the C side.
void InvRef::create(lua_State *L, const InventoryLocation &loc)
{
+ NO_MAP_LOCK_REQUIRED;
InvRef *o = new InvRef(loc);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
@@ -336,6 +350,7 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc)
}
void InvRef::createPlayer(lua_State *L, Player *player)
{
+ NO_MAP_LOCK_REQUIRED;
InventoryLocation loc;
loc.setPlayer(player->getName());
create(L, loc);
@@ -394,328 +409,48 @@ const luaL_reg InvRef::methods[] = {
{0,0}
};
-void inventory_get_list_to_lua(Inventory *inv, const char *name,
- lua_State *L)
-{
- InventoryList *invlist = inv->getList(name);
- if(invlist == NULL){
- lua_pushnil(L);
- return;
- }
- std::vector<ItemStack> items;
- for(u32 i=0; i<invlist->getSize(); i++)
- items.push_back(invlist->getItem(i));
- push_items(L, items);
-}
-
-void inventory_set_list_from_lua(Inventory *inv, const char *name,
- lua_State *L, int tableindex, int forcesize)
-{
- if(tableindex < 0)
- tableindex = lua_gettop(L) + 1 + tableindex;
- // If nil, delete list
- if(lua_isnil(L, tableindex)){
- inv->deleteList(name);
- return;
- }
- // Otherwise set list
- std::vector<ItemStack> items = read_items(L, tableindex);
- int listsize = (forcesize != -1) ? forcesize : items.size();
- InventoryList *invlist = inv->addList(name, listsize);
- int index = 0;
- for(std::vector<ItemStack>::const_iterator
- i = items.begin(); i != items.end(); i++){
- if(forcesize != -1 && index == forcesize)
- break;
- invlist->changeItem(index, *i);
- index++;
- }
- while(forcesize != -1 && index < forcesize){
- invlist->deleteItem(index);
- index++;
- }
-}
-
// get_inventory(location)
-int l_get_inventory(lua_State *L)
+int ModApiInventory::l_get_inventory(lua_State *L)
{
InventoryLocation loc;
std::string type = checkstringfield(L, 1, "type");
- if(type == "player"){
- std::string name = checkstringfield(L, 1, "name");
- loc.setPlayer(name);
- } else if(type == "node"){
+
+ if(type == "node"){
lua_getfield(L, 1, "pos");
v3s16 pos = check_v3s16(L, -1);
loc.setNodeMeta(pos);
- } else if(type == "detached"){
- std::string name = checkstringfield(L, 1, "name");
- loc.setDetached(name);
- }
- if(get_server(L)->getInventory(loc) != NULL)
- InvRef::create(L, loc);
- else
- lua_pushnil(L);
- return 1;
-}
-
-/*
- Detached inventory callbacks
-*/
-
-// Retrieves minetest.detached_inventories[name][callbackname]
-// If that is nil or on error, return false and stack is unchanged
-// If that is a function, returns true and pushes the
-// function onto the stack
-static bool get_detached_inventory_callback(lua_State *L,
- const std::string &name, const char *callbackname)
-{
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "detached_inventories");
- lua_remove(L, -2);
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_getfield(L, -1, name.c_str());
- lua_remove(L, -2);
- // Should be a table
- if(lua_type(L, -1) != LUA_TTABLE)
- {
- errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
- lua_pop(L, 1);
- return false;
- }
- lua_getfield(L, -1, callbackname);
- lua_remove(L, -2);
- // Should be a function or nil
- if(lua_type(L, -1) == LUA_TFUNCTION)
- {
- return true;
- }
- else if(lua_isnil(L, -1))
- {
- lua_pop(L, 1);
- return false;
- }
- else
- {
- errorstream<<"Detached inventory \""<<name<<"\" callback \""
- <<callbackname<<"\" is not a function"<<std::endl;
- lua_pop(L, 1);
- return false;
+ if(getServer(L)->getInventory(loc) != NULL)
+ InvRef::create(L, loc);
+ else
+ lua_pushnil(L);
+ return 1;
+ } else {
+ NO_MAP_LOCK_REQUIRED;
+ if(type == "player"){
+ std::string name = checkstringfield(L, 1, "name");
+ loc.setPlayer(name);
+ } else if(type == "detached"){
+ std::string name = checkstringfield(L, 1, "name");
+ loc.setDetached(name);
+ }
+
+ if(getServer(L)->getInventory(loc) != NULL)
+ InvRef::create(L, loc);
+ else
+ lua_pushnil(L);
+ return 1;
+ // END NO_MAP_LOCK_REQUIRED;
}
}
-// Return number of accepted items to be moved
-int scriptapi_detached_inventory_allow_move(lua_State *L,
- const std::string &name,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "allow_move"))
- return count;
-
- // function(inv, from_list, from_index, to_list, to_index, count, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 7, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_move should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Return number of accepted items to be put
-int scriptapi_detached_inventory_allow_put(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "allow_put"))
- return stack.count; // All will be accepted
-
- // Call function(inv, listname, index, stack, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_put should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Return number of accepted items to be taken
-int scriptapi_detached_inventory_allow_take(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "allow_take"))
- return stack.count; // All will be accepted
-
- // Call function(inv, listname, index, stack, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_take should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Report moved items
-void scriptapi_detached_inventory_on_move(lua_State *L,
- const std::string &name,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "on_move"))
- return;
-
- // function(inv, from_list, from_index, to_list, to_index, count, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 7, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
-
-// Report put items
-void scriptapi_detached_inventory_on_put(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "on_put"))
- return;
-
- // Call function(inv, listname, index, stack, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
-
-// Report taken items
-void scriptapi_detached_inventory_on_take(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
-
- // Push callback function on stack
- if(!get_detached_inventory_callback(L, name, "on_take"))
- return;
-
- // Call function(inv, listname, index, stack, player)
- // inv
- InventoryLocation loc;
- loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
-
// create_detached_inventory_raw(name)
-int l_create_detached_inventory_raw(lua_State *L)
+int ModApiInventory::l_create_detached_inventory_raw(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
const char *name = luaL_checkstring(L, 1);
- if(get_server(L)->createDetachedInventory(name) != NULL){
+ if(getServer(L)->createDetachedInventory(name) != NULL){
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
@@ -724,3 +459,21 @@ int l_create_detached_inventory_raw(lua_State *L)
}
return 1;
}
+
+bool ModApiInventory::Initialize(lua_State *L, int top) {
+ bool retval = true;
+
+ retval &= API_FCT(create_detached_inventory_raw);
+ retval &= API_FCT(get_inventory);
+
+ InvRef::Register(L);
+
+ return retval;
+}
+
+ModApiInventory::ModApiInventory()
+ : ModApiBase() {
+
+}
+
+ModApiInventory modapiinventory_prototype;
diff --git a/src/scriptapi_inventory.h b/src/script/lua_api/l_inventory.h
index 14f4fe026..83e8039b8 100644
--- a/src/scriptapi_inventory.h
+++ b/src/script/lua_api/l_inventory.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_INVENTORY_H_
-#define LUA_INVENTORY_H_
+#ifndef L_INVENTORY_H_
+#define L_INVENTORY_H_
extern "C" {
#include <lua.h>
@@ -30,6 +30,7 @@ extern "C" {
#include "serverobject.h"
#include "inventory.h"
+#include "lua_api/l_base.h"
/*
InvRef
*/
@@ -115,51 +116,22 @@ public:
static void Register(lua_State *L);
};
-void inventory_get_list_to_lua(Inventory *inv, const char *name,lua_State *L);
-void inventory_set_list_from_lua(Inventory *inv, const char *name,
- lua_State *L, int tableindex, int forcesize=-1);
-
-/*****************************************************************************/
-/* Minetest interface */
-/*****************************************************************************/
-/* Detached inventory callbacks */
-// Return number of accepted items to be moved
-int scriptapi_detached_inventory_allow_move(lua_State *L,
- const std::string &name,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player);
-// Return number of accepted items to be put
-int scriptapi_detached_inventory_allow_put(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Return number of accepted items to be taken
-int scriptapi_detached_inventory_allow_take(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Report moved items
-void scriptapi_detached_inventory_on_move(lua_State *L,
- const std::string &name,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player);
-// Report put items
-void scriptapi_detached_inventory_on_put(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Report taken items
-void scriptapi_detached_inventory_on_take(lua_State *L,
- const std::string &name,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-
-/*****************************************************************************/
-/* Mod API */
-/*****************************************************************************/
-int l_create_detached_inventory_raw(lua_State *L);
-int l_get_inventory(lua_State *L);
-
-#endif /* LUA_INVENTORY_H_ */
+class ModApiInventory
+ : public ModApiBase
+{
+public:
+ ModApiInventory();
+
+ bool Initialize(lua_State *L, int top);
+
+ static int l_create_detached_inventory_raw(lua_State *L);
+ static int l_get_inventory(lua_State *L);
+private:
+ static void inventory_set_list_from_lua(Inventory *inv, const char *name,
+ lua_State *L, int tableindex, int forcesize);
+ static void inventory_get_list_to_lua(Inventory *inv, const char *name,
+ lua_State *L);
+
+};
+
+#endif /* L_INVENTORY_H_ */
diff --git a/src/scriptapi_item.cpp b/src/script/lua_api/l_item.cpp
index b266d856d..730dfd49b 100644
--- a/src/scriptapi_item.cpp
+++ b/src/script/lua_api/l_item.cpp
@@ -17,284 +17,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
+#include "lua_api/l_item.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "cpp_api/scriptapi.h"
#include "server.h"
-#include "script.h"
-#include "tool.h"
-#include "nodedef.h"
-#include "util/pointedthing.h"
-#include "scriptapi_item.h"
-#include "scriptapi_types.h"
-#include "scriptapi_common.h"
-#include "scriptapi_object.h"
-#include "scriptapi_content.h"
-
-
-struct EnumString es_ItemType[] =
-{
- {ITEM_NONE, "none"},
- {ITEM_NODE, "node"},
- {ITEM_CRAFT, "craft"},
- {ITEM_TOOL, "tool"},
- {0, NULL},
-};
-
-
-/*
- ItemDefinition
-*/
-
-ItemDefinition read_item_definition(lua_State *L, int index,
- ItemDefinition default_def)
-{
- if(index < 0)
- index = lua_gettop(L) + 1 + index;
-
- // Read the item definition
- ItemDefinition def = default_def;
-
- def.type = (ItemType)getenumfield(L, index, "type",
- es_ItemType, ITEM_NONE);
- getstringfield(L, index, "name", def.name);
- getstringfield(L, index, "description", def.description);
- getstringfield(L, index, "inventory_image", def.inventory_image);
- getstringfield(L, index, "wield_image", def.wield_image);
-
- lua_getfield(L, index, "wield_scale");
- if(lua_istable(L, -1)){
- def.wield_scale = check_v3f(L, -1);
- }
- lua_pop(L, 1);
-
- def.stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
- if(def.stack_max == 0)
- def.stack_max = 1;
-
- lua_getfield(L, index, "on_use");
- def.usable = lua_isfunction(L, -1);
- lua_pop(L, 1);
-
- getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
-
- warn_if_field_exists(L, index, "tool_digging_properties",
- "deprecated: use tool_capabilities");
-
- lua_getfield(L, index, "tool_capabilities");
- if(lua_istable(L, -1)){
- def.tool_capabilities = new ToolCapabilities(
- read_tool_capabilities(L, -1));
- }
-
- // If name is "" (hand), ensure there are ToolCapabilities
- // because it will be looked up there whenever any other item has
- // no ToolCapabilities
- if(def.name == "" && def.tool_capabilities == NULL){
- def.tool_capabilities = new ToolCapabilities();
- }
-
- lua_getfield(L, index, "groups");
- read_groups(L, -1, def.groups);
- lua_pop(L, 1);
-
- lua_getfield(L, index, "sounds");
- if(lua_istable(L, -1)){
- lua_getfield(L, -1, "place");
- read_soundspec(L, -1, def.sound_place);
- lua_pop(L, 1);
- }
- lua_pop(L, 1);
-
- // Client shall immediately place this node when player places the item.
- // Server will update the precise end result a moment later.
- // "" = no prediction
- getstringfield(L, index, "node_placement_prediction",
- def.node_placement_prediction);
-
- return def;
-}
-
-// register_item_raw({lots of stuff})
-int l_register_item_raw(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TTABLE);
- int table = 1;
-
- // Get the writable item and node definition managers from the server
- IWritableItemDefManager *idef =
- get_server(L)->getWritableItemDefManager();
- IWritableNodeDefManager *ndef =
- get_server(L)->getWritableNodeDefManager();
-
- // Check if name is defined
- std::string name;
- lua_getfield(L, table, "name");
- if(lua_isstring(L, -1)){
- name = lua_tostring(L, -1);
- verbosestream<<"register_item_raw: "<<name<<std::endl;
- } else {
- throw LuaError(L, "register_item_raw: name is not defined or not a string");
- }
-
- // Check if on_use is defined
-
- ItemDefinition def;
- // Set a distinctive default value to check if this is set
- def.node_placement_prediction = "__default";
-
- // Read the item definition
- def = read_item_definition(L, table, def);
-
- // Default to having client-side placement prediction for nodes
- // ("" in item definition sets it off)
- if(def.node_placement_prediction == "__default"){
- if(def.type == ITEM_NODE)
- def.node_placement_prediction = name;
- else
- def.node_placement_prediction = "";
- }
-
- // Register item definition
- idef->registerItem(def);
-
- // Read the node definition (content features) and register it
- if(def.type == ITEM_NODE)
- {
- ContentFeatures f = read_content_features(L, table);
- ndef->set(f.name, f);
- }
-
- return 0; /* number of results */
-}
-
-// register_alias_raw(name, convert_to_name)
-int l_register_alias_raw(lua_State *L)
-{
- std::string name = luaL_checkstring(L, 1);
- std::string convert_to = luaL_checkstring(L, 2);
-
- // Get the writable item definition manager from the server
- IWritableItemDefManager *idef =
- get_server(L)->getWritableItemDefManager();
-
- idef->registerAlias(name, convert_to);
-
- return 0; /* number of results */
-}
-
-// Retrieves minetest.registered_items[name][callbackname]
-// If that is nil or on error, return false and stack is unchanged
-// If that is a function, returns true and pushes the
-// function onto the stack
-// If minetest.registered_items[name] doesn't exist, minetest.nodedef_default
-// is tried instead so unknown items can still be manipulated to some degree
-bool get_item_callback(lua_State *L,
- const char *name, const char *callbackname)
-{
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "registered_items");
- lua_remove(L, -2);
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_getfield(L, -1, name);
- lua_remove(L, -2);
- // Should be a table
- if(lua_type(L, -1) != LUA_TTABLE)
- {
- // Report error and clean up
- errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
- lua_pop(L, 1);
-
- // Try minetest.nodedef_default instead
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "nodedef_default");
- lua_remove(L, -2);
- luaL_checktype(L, -1, LUA_TTABLE);
- }
- lua_getfield(L, -1, callbackname);
- lua_remove(L, -2);
- // Should be a function or nil
- if(lua_type(L, -1) == LUA_TFUNCTION)
- {
- return true;
- }
- else if(lua_isnil(L, -1))
- {
- lua_pop(L, 1);
- return false;
- }
- else
- {
- errorstream<<"Item \""<<name<<"\" callback \""
- <<callbackname<<" is not a function"<<std::endl;
- lua_pop(L, 1);
- return false;
- }
-}
-
-bool scriptapi_item_on_drop(lua_State *L, ItemStack &item,
- ServerActiveObject *dropper, v3f pos)
-{
- 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_drop"))
- return false;
-
- // Call function
- LuaItemStack::create(L, item);
- objectref_get_or_create(L, dropper);
- pushFloatPos(L, pos);
- 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_item_on_place(lua_State *L, ItemStack &item,
- ServerActiveObject *placer, 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_place"))
- return false;
-
- // Call function
- LuaItemStack::create(L, item);
- objectref_get_or_create(L, placer);
- 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_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;
-}
+#include "common/c_internal.h"
// garbage collector
int LuaItemStack::gc_object(lua_State *L)
@@ -307,6 +35,7 @@ int LuaItemStack::gc_object(lua_State *L)
// is_empty(self) -> true/false
int LuaItemStack::l_is_empty(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
lua_pushboolean(L, item.empty());
@@ -316,6 +45,7 @@ int LuaItemStack::l_is_empty(lua_State *L)
// get_name(self) -> string
int LuaItemStack::l_get_name(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
lua_pushstring(L, item.name.c_str());
@@ -325,6 +55,7 @@ int LuaItemStack::l_get_name(lua_State *L)
// get_count(self) -> number
int LuaItemStack::l_get_count(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
lua_pushinteger(L, item.count);
@@ -334,6 +65,7 @@ int LuaItemStack::l_get_count(lua_State *L)
// get_wear(self) -> number
int LuaItemStack::l_get_wear(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
lua_pushinteger(L, item.wear);
@@ -343,6 +75,7 @@ int LuaItemStack::l_get_wear(lua_State *L)
// get_metadata(self) -> string
int LuaItemStack::l_get_metadata(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
lua_pushlstring(L, item.metadata.c_str(), item.metadata.size());
@@ -352,6 +85,7 @@ int LuaItemStack::l_get_metadata(lua_State *L)
// clear(self) -> true
int LuaItemStack::l_clear(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
o->m_stack.clear();
lua_pushboolean(L, true);
@@ -361,8 +95,9 @@ int LuaItemStack::l_clear(lua_State *L)
// replace(self, itemstack or itemstring or table or nil) -> true
int LuaItemStack::l_replace(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
- o->m_stack = read_item(L, 2);
+ o->m_stack = read_item(L,2,STACK_TO_SERVER(L));
lua_pushboolean(L, true);
return 1;
}
@@ -370,6 +105,7 @@ int LuaItemStack::l_replace(lua_State *L)
// to_string(self) -> string
int LuaItemStack::l_to_string(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
std::string itemstring = o->m_stack.getItemString();
lua_pushstring(L, itemstring.c_str());
@@ -379,6 +115,7 @@ int LuaItemStack::l_to_string(lua_State *L)
// to_table(self) -> table or nil
int LuaItemStack::l_to_table(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
const ItemStack &item = o->m_stack;
if(item.empty())
@@ -403,18 +140,20 @@ int LuaItemStack::l_to_table(lua_State *L)
// get_stack_max(self) -> number
int LuaItemStack::l_get_stack_max(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- lua_pushinteger(L, item.getStackMax(get_server(L)->idef()));
+ lua_pushinteger(L, item.getStackMax(STACK_TO_SERVER(L)->idef()));
return 1;
}
// get_free_space(self) -> number
int LuaItemStack::l_get_free_space(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- lua_pushinteger(L, item.freeSpace(get_server(L)->idef()));
+ lua_pushinteger(L, item.freeSpace(STACK_TO_SERVER(L)->idef()));
return 1;
}
@@ -422,9 +161,10 @@ int LuaItemStack::l_get_free_space(lua_State *L)
// Checks if the item is defined.
int LuaItemStack::l_is_known(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- bool is_known = item.isKnown(get_server(L)->idef());
+ bool is_known = item.isKnown(STACK_TO_SERVER(L)->idef());
lua_pushboolean(L, is_known);
return 1;
}
@@ -434,6 +174,7 @@ int LuaItemStack::l_is_known(lua_State *L)
// or a fallback one (name="unknown")
int LuaItemStack::l_get_definition(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
@@ -455,10 +196,11 @@ int LuaItemStack::l_get_definition(lua_State *L)
// Returns those of the hand ("") if this item has none associated.
int LuaItemStack::l_get_tool_capabilities(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
const ToolCapabilities &prop =
- item.getToolCapabilities(get_server(L)->idef());
+ item.getToolCapabilities(STACK_TO_SERVER(L)->idef());
push_tool_capabilities(L, prop);
return 1;
}
@@ -469,10 +211,11 @@ int LuaItemStack::l_get_tool_capabilities(lua_State *L)
// Returns true if the item is (or was) a tool.
int LuaItemStack::l_add_wear(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
int amount = lua_tointeger(L, 2);
- bool result = item.addWear(amount, get_server(L)->idef());
+ bool result = item.addWear(amount, STACK_TO_SERVER(L)->idef());
lua_pushboolean(L, result);
return 1;
}
@@ -481,10 +224,11 @@ int LuaItemStack::l_add_wear(lua_State *L)
// Returns leftover item stack
int LuaItemStack::l_add_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- ItemStack newitem = read_item(L, 2);
- ItemStack leftover = item.addItem(newitem, get_server(L)->idef());
+ ItemStack newitem = read_item(L,-1, STACK_TO_SERVER(L));
+ ItemStack leftover = item.addItem(newitem, STACK_TO_SERVER(L)->idef());
create(L, leftover);
return 1;
}
@@ -494,11 +238,12 @@ int LuaItemStack::l_add_item(lua_State *L)
// Second return value is the would-be-left-over item stack
int LuaItemStack::l_item_fits(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- ItemStack newitem = read_item(L, 2);
+ ItemStack newitem = read_item(L, 2 ,STACK_TO_SERVER(L));
ItemStack restitem;
- bool fits = item.itemFits(newitem, &restitem, get_server(L)->idef());
+ bool fits = item.itemFits(newitem, &restitem, STACK_TO_SERVER(L)->idef());
lua_pushboolean(L, fits); // first return value
create(L, restitem); // second return value
return 2;
@@ -507,6 +252,7 @@ int LuaItemStack::l_item_fits(lua_State *L)
// take_item(self, takecount=1) -> itemstack
int LuaItemStack::l_take_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
u32 takecount = 1;
@@ -520,6 +266,7 @@ int LuaItemStack::l_take_item(lua_State *L)
// peek_item(self, peekcount=1) -> itemstack
int LuaItemStack::l_peek_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
u32 peekcount = 1;
@@ -552,7 +299,8 @@ ItemStack& LuaItemStack::getItem()
// Creates an LuaItemStack and leaves it on top of stack
int LuaItemStack::create_object(lua_State *L)
{
- ItemStack item = read_item(L, 1);
+ NO_MAP_LOCK_REQUIRED;
+ ItemStack item = read_item(L,1,STACK_TO_SERVER(L));
LuaItemStack *o = new LuaItemStack(item);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
@@ -562,6 +310,7 @@ int LuaItemStack::create_object(lua_State *L)
// Not callable from Lua
int LuaItemStack::create(lua_State *L, const ItemStack &item)
{
+ NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = new LuaItemStack(item);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
@@ -629,90 +378,95 @@ const luaL_reg LuaItemStack::methods[] = {
{0,0}
};
-ItemStack read_item(lua_State *L, int index)
+ModApiItemMod::ModApiItemMod() {
+}
+
+/*
+ ItemDefinition
+*/
+
+// register_item_raw({lots of stuff})
+int ModApiItemMod::l_register_item_raw(lua_State *L)
{
- if(index < 0)
- index = lua_gettop(L) + 1 + index;
+ NO_MAP_LOCK_REQUIRED;
+ luaL_checktype(L, 1, LUA_TTABLE);
+ int table = 1;
- if(lua_isnil(L, index))
- {
- return ItemStack();
- }
- else if(lua_isuserdata(L, index))
- {
- // Convert from LuaItemStack
- LuaItemStack *o = LuaItemStack::checkobject(L, index);
- return o->getItem();
- }
- else if(lua_isstring(L, index))
- {
- // Convert from itemstring
- std::string itemstring = lua_tostring(L, index);
- IItemDefManager *idef = get_server(L)->idef();
- try
- {
- ItemStack item;
- item.deSerialize(itemstring, idef);
- return item;
- }
- catch(SerializationError &e)
- {
- infostream<<"WARNING: unable to create item from itemstring"
- <<": "<<itemstring<<std::endl;
- return ItemStack();
- }
+ ScriptApi* scriptIface = get_scriptapi(L);
+
+ // Get the writable item and node definition managers from the server
+ IWritableItemDefManager *idef =
+ scriptIface->getServer()->getWritableItemDefManager();
+ IWritableNodeDefManager *ndef =
+ scriptIface->getServer()->getWritableNodeDefManager();
+
+ // Check if name is defined
+ std::string name;
+ lua_getfield(L, table, "name");
+ if(lua_isstring(L, -1)){
+ name = lua_tostring(L, -1);
+ verbosestream<<"register_item_raw: "<<name<<std::endl;
+ } else {
+ throw LuaError(L, "register_item_raw: name is not defined or not a string");
}
- else if(lua_istable(L, index))
- {
- // Convert from table
- IItemDefManager *idef = get_server(L)->idef();
- std::string name = getstringfield_default(L, index, "name", "");
- int count = getintfield_default(L, index, "count", 1);
- int wear = getintfield_default(L, index, "wear", 0);
- std::string metadata = getstringfield_default(L, index, "metadata", "");
- return ItemStack(name, count, wear, metadata, idef);
+
+ // Check if on_use is defined
+
+ ItemDefinition def;
+ // Set a distinctive default value to check if this is set
+ def.node_placement_prediction = "__default";
+
+ // Read the item definition
+ def = read_item_definition(L, table, def);
+
+ // Default to having client-side placement prediction for nodes
+ // ("" in item definition sets it off)
+ if(def.node_placement_prediction == "__default"){
+ if(def.type == ITEM_NODE)
+ def.node_placement_prediction = name;
+ else
+ def.node_placement_prediction = "";
}
- else
+
+ // Register item definition
+ idef->registerItem(def);
+
+ // Read the node definition (content features) and register it
+ if(def.type == ITEM_NODE)
{
- throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
+ ContentFeatures f = read_content_features(L, table);
+ ndef->set(f.name, f);
}
+
+ return 0; /* number of results */
}
-std::vector<ItemStack> read_items(lua_State *L, int index)
+// register_alias_raw(name, convert_to_name)
+int ModApiItemMod::l_register_alias_raw(lua_State *L)
{
- if(index < 0)
- index = lua_gettop(L) + 1 + index;
-
- std::vector<ItemStack> items;
- luaL_checktype(L, index, LUA_TTABLE);
- lua_pushnil(L);
- while(lua_next(L, index) != 0){
- // key at index -2 and value at index -1
- items.push_back(read_item(L, -1));
- // removes value, keeps key for next iteration
- lua_pop(L, 1);
- }
- return items;
+ NO_MAP_LOCK_REQUIRED;
+ std::string name = luaL_checkstring(L, 1);
+ std::string convert_to = luaL_checkstring(L, 2);
+
+ // Get the writable item definition manager from the server
+ IWritableItemDefManager *idef =
+ STACK_TO_SERVER(L)->getWritableItemDefManager();
+
+ idef->registerAlias(name, convert_to);
+
+ return 0; /* number of results */
}
-// creates a table of ItemStacks
-void push_items(lua_State *L, const std::vector<ItemStack> &items)
-{
- // Get the table insert function
- lua_getglobal(L, "table");
- lua_getfield(L, -1, "insert");
- int table_insert = lua_gettop(L);
- // Create and fill table
- lua_newtable(L);
- int table = lua_gettop(L);
- for(u32 i=0; i<items.size(); i++){
- ItemStack item = items[i];
- lua_pushvalue(L, table_insert);
- lua_pushvalue(L, table);
- LuaItemStack::create(L, item);
- if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- }
- lua_remove(L, -2); // Remove table
- lua_remove(L, -2); // Remove insert
+bool ModApiItemMod::Initialize(lua_State *L,int top) {
+
+ bool retval = true;
+
+ retval &= API_FCT(register_item_raw);
+ retval &= API_FCT(register_alias_raw);
+
+ LuaItemStack::Register(L);
+
+ return retval;
}
+
+ModApiItemMod modapi_item_prototyp;
diff --git a/src/scriptapi_item.h b/src/script/lua_api/l_item.h
index e0f213990..cbed4ae0b 100644
--- a/src/scriptapi_item.h
+++ b/src/script/lua_api/l_item.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_ITEM_H_
-#define LUA_ITEM_H_
+#ifndef L_ITEM_H_
+#define L_ITEM_H_
extern "C" {
#include <lua.h>
@@ -32,11 +32,9 @@ extern "C" {
#include "util/pointedthing.h"
#include "inventory.h"
-#include "inventory.h"
+#include "lua_api/l_base.h"
-ItemStack read_item(lua_State *L, int index);
-std::vector<ItemStack> read_items(lua_State *L, int index);
-void push_items(lua_State *L, const std::vector<ItemStack> &items);
+class ModApiInventory;
class LuaItemStack
{
@@ -136,31 +134,18 @@ public:
};
-/*****************************************************************************/
-/* Mod API */
-/*****************************************************************************/
-int l_register_item_raw(lua_State *L);
-int l_register_alias_raw(lua_State *L);
-
-/*****************************************************************************/
-/* scriptapi internal */
-/*****************************************************************************/
-bool get_item_callback(lua_State *L,
- const char *name, const char *callbackname);
-ItemDefinition read_item_definition(lua_State *L, int index,
- ItemDefinition default_def = ItemDefinition());
-
-extern struct EnumString es_ItemType[];
-
-/*****************************************************************************/
-/* Minetest interface */
-/*****************************************************************************/
-bool scriptapi_item_on_drop(lua_State *L, ItemStack &item,
- ServerActiveObject *dropper, v3f pos);
-bool scriptapi_item_on_place(lua_State *L, ItemStack &item,
- ServerActiveObject *placer, const PointedThing &pointed);
-bool scriptapi_item_on_use(lua_State *L, ItemStack &item,
- ServerActiveObject *user, const PointedThing &pointed);
-
-
-#endif /* LUA_ITEM_H_ */
+class ModApiItemMod
+ :virtual public ModApiBase
+{
+public:
+ ModApiItemMod();
+
+ bool Initialize(lua_State *L,int top);
+
+ static int l_register_item_raw(lua_State *L);
+ static int l_register_alias_raw(lua_State *L);
+};
+
+
+
+#endif /* L_ITEM_H_ */
diff --git a/src/scriptapi_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp
index b66c9a023..511fb38ce 100644
--- a/src/scriptapi_nodemeta.cpp
+++ b/src/script/lua_api/l_nodemeta.cpp
@@ -17,15 +17,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_nodemeta.h"
+#include "cpp_api/scriptapi.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
#include "map.h"
-#include "script.h"
-#include "scriptapi_types.h"
-#include "scriptapi_inventory.h"
-#include "scriptapi_common.h"
-#include "scriptapi_item.h"
-#include "scriptapi_object.h"
+#include "lua_api/l_nodemeta.h"
+#include "common/c_internal.h"
+#include "lua_api/l_inventory.h"
/*
@@ -213,7 +211,7 @@ int NodeMetaRef::l_to_table(lua_State *L)
std::vector<const InventoryList*> lists = inv->getLists();
for(std::vector<const InventoryList*>::const_iterator
i = lists.begin(); i != lists.end(); i++){
- inventory_get_list_to_lua(inv, (*i)->getName().c_str(), L);
+ push_inventory_list(inv, (*i)->getName().c_str(), L);
lua_setfield(L, -2, (*i)->getName().c_str());
}
}
@@ -259,7 +257,7 @@ int NodeMetaRef::l_from_table(lua_State *L)
while(lua_next(L, inventorytable) != 0){
// key at index -2 and value at index -1
std::string name = lua_tostring(L, -2);
- inventory_set_list_from_lua(inv, name.c_str(), L, -1);
+ read_inventory_list(inv, name.c_str(), L, -1,STACK_TO_SERVER(L));
lua_pop(L, 1); // removes value, keeps key for next iteration
}
reportMetadataChange(ref);
@@ -331,241 +329,4 @@ const luaL_reg NodeMetaRef::methods[] = {
{0,0}
};
-/*
- Node metadata inventory callbacks
-*/
-
-// Return number of accepted items to be moved
-int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player)
-{
- 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 0;
-
- // Push callback function on stack
- if(!get_item_callback(L, ndef->get(node).name.c_str(),
- "allow_metadata_inventory_move"))
- return count;
-
- // function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 7, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_move should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Return number of accepted items to be put
-int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- 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 0;
-
- // Push callback function on stack
- if(!get_item_callback(L, ndef->get(node).name.c_str(),
- "allow_metadata_inventory_put"))
- return stack.count;
-
- // Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_put should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Return number of accepted items to be taken
-int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- 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 0;
-
- // Push callback function on stack
- if(!get_item_callback(L, ndef->get(node).name.c_str(),
- "allow_metadata_inventory_take"))
- return stack.count;
-
- // Call function(pos, listname, index, count, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_take should return a number");
- return luaL_checkinteger(L, -1);
-}
-
-// Report moved items
-void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player)
-{
- 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_metadata_inventory_move"))
- return;
-
- // function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 7, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
-
-// Report put items
-void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- 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_metadata_inventory_put"))
- return;
-
- // Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
-
-// Report taken items
-void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player)
-{
- 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_metadata_inventory_take"))
- return;
-
- // Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectref_get_or_create(L, player);
- if(lua_pcall(L, 5, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
-}
+REGISTER_LUA_REF(NodeMetaRef);
diff --git a/src/scriptapi_nodemeta.h b/src/script/lua_api/l_nodemeta.h
index 017abe181..23404a084 100644
--- a/src/scriptapi_nodemeta.h
+++ b/src/script/lua_api/l_nodemeta.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -16,8 +16,8 @@ You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_NODEMETA_H_
-#define LUA_NODEMETA_H_
+#ifndef L_NODEMETA_H_
+#define L_NODEMETA_H_
extern "C" {
#include <lua.h>
@@ -90,34 +90,4 @@ public:
static void Register(lua_State *L);
};
-/*****************************************************************************/
-/* Minetest interface */
-/*****************************************************************************/
-// Return number of accepted items to be moved
-int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player);
-// Return number of accepted items to be put
-int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Return number of accepted items to be taken
-int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Report moved items
-void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p,
- const std::string &from_list, int from_index,
- const std::string &to_list, int to_index,
- int count, ServerActiveObject *player);
-// Report put items
-void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-// Report taken items
-void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
- const std::string &listname, int index, ItemStack &stack,
- ServerActiveObject *player);
-
-#endif //LUA_NODEMETA_H_
+#endif //L_NODEMETA_H_
diff --git a/src/scriptapi_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp
index 5e3289aee..60e4ec061 100644
--- a/src/scriptapi_nodetimer.cpp
+++ b/src/script/lua_api/l_nodetimer.cpp
@@ -17,8 +17,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_nodetimer.h"
+#include "cpp_api/scriptapi.h"
+#include "lua_api/l_nodetimer.h"
+#include "common/c_internal.h"
#include "map.h"
@@ -164,3 +165,5 @@ const luaL_reg NodeTimerRef::methods[] = {
luamethod(NodeTimerRef, get_elapsed),
{0,0}
};
+
+REGISTER_LUA_REF(NodeTimerRef);
diff --git a/src/scriptapi_nodetimer.h b/src/script/lua_api/l_nodetimer.h
index a4536d947..f652b4900 100644
--- a/src/scriptapi_nodetimer.h
+++ b/src/script/lua_api/l_nodetimer.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_NODETIMER_H_
-#define LUA_NODETIMER_H_
+#ifndef L_NODETIMER_H_
+#define L_NODETIMER_H_
extern "C" {
#include <lua.h>
@@ -67,4 +67,4 @@ public:
-#endif /* LUA_NODETIMER_H_ */
+#endif /* L_NODETIMER_H_ */
diff --git a/src/scriptapi_noise.cpp b/src/script/lua_api/l_noise.cpp
index 2c1a83c4c..e0abeae94 100644
--- a/src/scriptapi_noise.cpp
+++ b/src/script/lua_api/l_noise.cpp
@@ -17,10 +17,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_noise.h"
-#include "scriptapi_types.h"
-#include "script.h"
+#include "lua_api/l_noise.h"
+#include "common/c_internal.h"
+#include "common/c_converter.h"
+#include "log.h"
// garbage collector
int LuaPerlinNoise::gc_object(lua_State *L)
@@ -32,6 +32,7 @@ int LuaPerlinNoise::gc_object(lua_State *L)
int LuaPerlinNoise::l_get2d(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v2f pos2d = read_v2f(L,2);
lua_Number val = noise2d_perlin(pos2d.X/o->scale, pos2d.Y/o->scale, o->seed, o->octaves, o->persistence);
@@ -40,6 +41,7 @@ int LuaPerlinNoise::l_get2d(lua_State *L)
}
int LuaPerlinNoise::l_get3d(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v3f pos3d = read_v3f(L,2);
lua_Number val = noise3d_perlin(pos3d.X/o->scale, pos3d.Y/o->scale, pos3d.Z/o->scale, o->seed, o->octaves, o->persistence);
@@ -65,6 +67,7 @@ LuaPerlinNoise::~LuaPerlinNoise()
// Creates an LuaPerlinNoise and leaves it on top of stack
int LuaPerlinNoise::create_object(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
int seed = luaL_checkint(L, 1);
int octaves = luaL_checkint(L, 2);
float persistence = luaL_checknumber(L, 3);
@@ -78,6 +81,7 @@ int LuaPerlinNoise::create_object(lua_State *L)
LuaPerlinNoise* LuaPerlinNoise::checkobject(lua_State *L, int narg)
{
+ NO_MAP_LOCK_REQUIRED;
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if(!ud) luaL_typerror(L, narg, className);
@@ -133,6 +137,7 @@ int LuaPerlinNoiseMap::gc_object(lua_State *L)
int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
int i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
@@ -156,6 +161,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
int i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
@@ -253,32 +259,6 @@ const luaL_reg LuaPerlinNoiseMap::methods[] = {
};
/*
- NoiseParams
-*/
-NoiseParams *read_noiseparams(lua_State *L, int index)
-{
- if (index < 0)
- index = lua_gettop(L) + 1 + index;
-
- if (!lua_istable(L, index))
- return NULL;
-
- NoiseParams *np = new NoiseParams;
-
- np->offset = getfloatfield_default(L, index, "offset", 0.0);
- np->scale = getfloatfield_default(L, index, "scale", 0.0);
- lua_getfield(L, index, "spread");
- np->spread = read_v3f(L, -1);
- lua_pop(L, 1);
- np->seed = getintfield_default(L, index, "seed", 0);
- np->octaves = getintfield_default(L, index, "octaves", 0);
- np->persist = getfloatfield_default(L, index, "persist", 0.0);
-
- return np;
-}
-
-
-/*
LuaPseudoRandom
*/
@@ -293,6 +273,7 @@ int LuaPseudoRandom::gc_object(lua_State *L)
// next(self, min=0, max=32767) -> get next value
int LuaPseudoRandom::l_next(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
LuaPseudoRandom *o = checkobject(L, 1);
int min = 0;
int max = 32767;
@@ -386,3 +367,7 @@ const luaL_reg LuaPseudoRandom::methods[] = {
luamethod(LuaPseudoRandom, next),
{0,0}
};
+
+REGISTER_LUA_REF(LuaPseudoRandom);
+REGISTER_LUA_REF(LuaPerlinNoiseMap);
+REGISTER_LUA_REF(LuaPerlinNoise);
diff --git a/src/scriptapi_noise.h b/src/script/lua_api/l_noise.h
index a02383fde..d0b51d756 100644
--- a/src/scriptapi_noise.h
+++ b/src/script/lua_api/l_noise.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,14 +17,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_PERLIN_H_
-#define LUA_PERLIN_H_
+#ifndef L_NOISE_H_
+#define L_NOISE_H_
extern "C" {
#include <lua.h>
#include <lauxlib.h>
}
+#include "irr_v3d.h"
#include "noise.h"
class LuaPerlinNoise
@@ -130,4 +131,4 @@ public:
NoiseParams *read_noiseparams(lua_State *L, int index);
-#endif /* LUA_PERLIN_H_ */
+#endif /* L_NOISE_H_ */
diff --git a/src/scriptapi_object.cpp b/src/script/lua_api/l_object.cpp
index 4dfdeb8c8..1e45610a6 100644
--- a/src/scriptapi_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -17,15 +17,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_object.h"
+#include "cpp_api/scriptapi.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "lua_api/l_object.h"
+#include "common/c_internal.h"
+#include "lua_api/l_inventory.h"
+#include "lua_api/l_item.h"
#include "log.h"
#include "tool.h"
-#include "scriptapi_types.h"
-#include "scriptapi_inventory.h"
-#include "scriptapi_item.h"
-#include "scriptapi_entity.h"
-#include "scriptapi_common.h"
+#include "serverobject.h"
+#include "content_object.h"
+#include "content_sao.h"
+#include "server.h"
#include "hud.h"
@@ -35,7 +39,7 @@ struct EnumString es_HudElementType[] =
{HUD_ELEM_TEXT, "text"},
{HUD_ELEM_STATBAR, "statbar"},
{HUD_ELEM_INVENTORY, "inventory"},
- {0, NULL},
+{0, NULL},
};
struct EnumString es_HudElementStat[] =
@@ -61,7 +65,6 @@ struct EnumString es_HudBuiltinElement[] =
{0, NULL},
};
-
/*
ObjectRef
*/
@@ -122,6 +125,7 @@ int ObjectRef::gc_object(lua_State *L) {
// remove(self)
int ObjectRef::l_remove(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -134,6 +138,7 @@ int ObjectRef::l_remove(lua_State *L)
// returns: {x=num, y=num, z=num}
int ObjectRef::l_getpos(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -151,6 +156,7 @@ int ObjectRef::l_getpos(lua_State *L)
// setpos(self, pos)
int ObjectRef::l_setpos(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
//LuaEntitySAO *co = getluaobject(ref);
ServerActiveObject *co = getobject(ref);
@@ -165,6 +171,7 @@ int ObjectRef::l_setpos(lua_State *L)
// moveto(self, pos, continuous=false)
int ObjectRef::l_moveto(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
//LuaEntitySAO *co = getluaobject(ref);
ServerActiveObject *co = getobject(ref);
@@ -181,6 +188,7 @@ int ObjectRef::l_moveto(lua_State *L)
// punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
int ObjectRef::l_punch(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ObjectRef *puncher_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
@@ -205,6 +213,7 @@ int ObjectRef::l_punch(lua_State *L)
// right_click(self, clicker); clicker = an another ObjectRef
int ObjectRef::l_right_click(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ObjectRef *ref2 = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
@@ -221,6 +230,7 @@ int ObjectRef::l_right_click(lua_State *L)
// returns: nil
int ObjectRef::l_set_hp(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
luaL_checknumber(L, 2);
ServerActiveObject *co = getobject(ref);
@@ -239,6 +249,7 @@ int ObjectRef::l_set_hp(lua_State *L)
// 0 if not applicable to this type of object
int ObjectRef::l_get_hp(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL){
@@ -257,12 +268,13 @@ int ObjectRef::l_get_hp(lua_State *L)
// get_inventory(self)
int ObjectRef::l_get_inventory(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
// Do it
InventoryLocation loc = co->getInventoryLocation();
- if(get_server(L)->getInventory(loc) != NULL)
+ if(STACK_TO_SERVER(L)->getInventory(loc) != NULL)
InvRef::create(L, loc);
else
lua_pushnil(L); // An object may have no inventory (nil)
@@ -272,6 +284,7 @@ int ObjectRef::l_get_inventory(lua_State *L)
// get_wield_list(self)
int ObjectRef::l_get_wield_list(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -283,6 +296,7 @@ int ObjectRef::l_get_wield_list(lua_State *L)
// get_wield_index(self)
int ObjectRef::l_get_wield_index(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -294,6 +308,7 @@ int ObjectRef::l_get_wield_index(lua_State *L)
// get_wielded_item(self)
int ObjectRef::l_get_wielded_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL){
@@ -309,11 +324,12 @@ int ObjectRef::l_get_wielded_item(lua_State *L)
// set_wielded_item(self, itemstack or itemstring or table or nil)
int ObjectRef::l_set_wielded_item(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
// Do it
- ItemStack item = read_item(L, 2);
+ ItemStack item = read_item(L, 2,STACK_TO_SERVER(L));
bool success = co->setWieldedItem(item);
lua_pushboolean(L, success);
return 1;
@@ -322,6 +338,7 @@ int ObjectRef::l_set_wielded_item(lua_State *L)
// set_armor_groups(self, groups)
int ObjectRef::l_set_armor_groups(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -357,6 +374,7 @@ int ObjectRef::l_set_physics_override(lua_State *L)
// set_animation(self, frame_range, frame_speed, frame_blend)
int ObjectRef::l_set_animation(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -377,6 +395,7 @@ int ObjectRef::l_set_animation(lua_State *L)
// set_bone_position(self, std::string bone, v3f position, v3f rotation)
int ObjectRef::l_set_bone_position(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -397,6 +416,7 @@ int ObjectRef::l_set_bone_position(lua_State *L)
// set_attach(self, parent, bone, position, rotation)
int ObjectRef::l_set_attach(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ObjectRef *parent_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
@@ -420,6 +440,7 @@ int ObjectRef::l_set_attach(lua_State *L)
// set_detach(self)
int ObjectRef::l_set_detach(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -431,6 +452,7 @@ int ObjectRef::l_set_detach(lua_State *L)
// set_properties(self, properties)
int ObjectRef::l_set_properties(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
@@ -447,6 +469,7 @@ int ObjectRef::l_set_properties(lua_State *L)
// setvelocity(self, {x=num, y=num, z=num})
int ObjectRef::l_setvelocity(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -459,6 +482,7 @@ int ObjectRef::l_setvelocity(lua_State *L)
// getvelocity(self)
int ObjectRef::l_getvelocity(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -471,6 +495,7 @@ int ObjectRef::l_getvelocity(lua_State *L)
// 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;
@@ -484,6 +509,7 @@ int ObjectRef::l_setacceleration(lua_State *L)
// getacceleration(self)
int ObjectRef::l_getacceleration(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -496,6 +522,7 @@ int ObjectRef::l_getacceleration(lua_State *L)
// setyaw(self, radians)
int ObjectRef::l_setyaw(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -508,6 +535,7 @@ int ObjectRef::l_setyaw(lua_State *L)
// getyaw(self)
int ObjectRef::l_getyaw(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -520,6 +548,7 @@ int ObjectRef::l_getyaw(lua_State *L)
// settexturemod(self, mod)
int ObjectRef::l_settexturemod(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -533,6 +562,7 @@ int ObjectRef::l_settexturemod(lua_State *L)
// select_horiz_by_yawpitch=false)
int ObjectRef::l_setsprite(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -557,6 +587,7 @@ int ObjectRef::l_setsprite(lua_State *L)
// get_entity_name(self)
int ObjectRef::l_get_entity_name(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -569,6 +600,7 @@ int ObjectRef::l_get_entity_name(lua_State *L)
// get_luaentity(self)
int ObjectRef::l_get_luaentity(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
LuaEntitySAO *co = getluaobject(ref);
if(co == NULL) return 0;
@@ -582,6 +614,7 @@ int ObjectRef::l_get_luaentity(lua_State *L)
// is_player(self)
int ObjectRef::l_is_player(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
lua_pushboolean(L, (player != NULL));
@@ -591,6 +624,7 @@ int ObjectRef::l_is_player(lua_State *L)
// get_player_name(self)
int ObjectRef::l_get_player_name(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL){
@@ -605,6 +639,7 @@ int ObjectRef::l_get_player_name(lua_State *L)
// get_look_dir(self)
int ObjectRef::l_get_look_dir(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL) return 0;
@@ -619,6 +654,7 @@ int ObjectRef::l_get_look_dir(lua_State *L)
// get_look_pitch(self)
int ObjectRef::l_get_look_pitch(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL) return 0;
@@ -630,6 +666,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
// get_look_yaw(self)
int ObjectRef::l_get_look_yaw(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL) return 0;
@@ -641,6 +678,7 @@ int ObjectRef::l_get_look_yaw(lua_State *L)
// set_look_pitch(self, radians)
int ObjectRef::l_set_look_pitch(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0;
@@ -653,6 +691,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
// set_look_yaw(self, radians)
int ObjectRef::l_set_look_yaw(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if(co == NULL) return 0;
@@ -665,13 +704,14 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
// set_inventory_formspec(self, formspec)
int ObjectRef::l_set_inventory_formspec(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL) return 0;
std::string formspec = luaL_checkstring(L, 2);
player->inventory_formspec = formspec;
- get_server(L)->reportInventoryFormspecModified(player->getName());
+ STACK_TO_SERVER(L)->reportInventoryFormspecModified(player->getName());
lua_pushboolean(L, true);
return 1;
}
@@ -679,6 +719,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L)
// get_inventory_formspec(self) -> formspec
int ObjectRef::l_get_inventory_formspec(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL) return 0;
@@ -691,6 +732,7 @@ int ObjectRef::l_get_inventory_formspec(lua_State *L)
// get_player_control(self)
int ObjectRef::l_get_player_control(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL){
@@ -724,6 +766,7 @@ int ObjectRef::l_get_player_control(lua_State *L)
// get_player_control_bits(self)
int ObjectRef::l_get_player_control_bits(lua_State *L)
{
+ NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if(player == NULL){
@@ -744,23 +787,23 @@ int ObjectRef::l_hud_add(lua_State *L)
return 0;
HudElement *elem = new HudElement;
-
+
elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
es_HudElementType, HUD_ELEM_TEXT);
-
+
lua_getfield(L, 2, "position");
elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
lua_pop(L, 1);
-
+
lua_getfield(L, 2, "scale");
elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
lua_pop(L, 1);
-
+
elem->name = getstringfield_default(L, 2, "name", "");
elem->text = getstringfield_default(L, 2, "text", "");
elem->number = getintfield_default(L, 2, "number", 0);
elem->item = getintfield_default(L, 2, "item", 0);
- elem->dir = getintfield_default(L, 2, "direction", 0);
+ elem->dir = getintfield_default(L, 2, "dir", 0);
lua_getfield(L, 2, "alignment");
elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
@@ -770,7 +813,7 @@ int ObjectRef::l_hud_add(lua_State *L)
elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
lua_pop(L, 1);
- u32 id = get_server(L)->hudAdd(player, elem);
+ u32 id = STACK_TO_SERVER(L)->hudAdd(player, elem);
if (id == (u32)-1) {
delete elem;
return 0;
@@ -791,8 +834,8 @@ int ObjectRef::l_hud_remove(lua_State *L)
u32 id = -1;
if (!lua_isnil(L, 2))
id = lua_tonumber(L, 2);
-
- if (!get_server(L)->hudRemove(player, id))
+
+ if (!STACK_TO_SERVER(L)->hudRemove(player, id))
return 0;
lua_pushboolean(L, true);
@@ -807,23 +850,21 @@ int ObjectRef::l_hud_change(lua_State *L)
if (player == NULL)
return 0;
- u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
+ u32 id = -1;
+ if (!lua_isnil(L, 2))
+ id = lua_tonumber(L, 2);
+
+ HudElementStat stat = (HudElementStat)getenumfield(L, 3, "stat",
+ es_HudElementStat, HUD_STAT_NUMBER);
+
if (id >= player->hud.size())
return 0;
-
- HudElementStat stat = HUD_STAT_NUMBER;
- if (!lua_isnil(L, 3)) {
- int statint;
- std::string statstr = lua_tostring(L, 3);
- stat = string_to_enum(es_HudElementStat, statint, statstr) ?
- (HudElementStat)statint : HUD_STAT_NUMBER;
- }
-
+
void *value = NULL;
HudElement *e = player->hud[id];
if (!e)
return 0;
-
+
switch (stat) {
case HUD_STAT_POS:
e->pos = read_v2f(L, 4);
@@ -860,7 +901,7 @@ int ObjectRef::l_hud_change(lua_State *L)
value = &e->offset;
}
- get_server(L)->hudChange(player, id, stat, value);
+ STACK_TO_SERVER(L)->hudChange(player, id, stat, value);
lua_pushboolean(L, true);
return 1;
@@ -877,34 +918,34 @@ int ObjectRef::l_hud_get(lua_State *L)
u32 id = lua_tonumber(L, -1);
if (id >= player->hud.size())
return 0;
-
+
HudElement *e = player->hud[id];
if (!e)
return 0;
-
+
lua_newtable(L);
-
+
lua_pushstring(L, es_HudElementType[(u8)e->type].str);
lua_setfield(L, -2, "type");
-
+
push_v2f(L, e->pos);
lua_setfield(L, -2, "position");
-
+
lua_pushstring(L, e->name.c_str());
lua_setfield(L, -2, "name");
-
+
push_v2f(L, e->scale);
lua_setfield(L, -2, "scale");
-
+
lua_pushstring(L, e->text.c_str());
lua_setfield(L, -2, "text");
-
+
lua_pushnumber(L, e->number);
lua_setfield(L, -2, "number");
-
+
lua_pushnumber(L, e->item);
lua_setfield(L, -2, "item");
-
+
lua_pushnumber(L, e->dir);
lua_setfield(L, -2, "dir");
@@ -930,7 +971,24 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
mask |= esp[i].num;
}
}
- if (!get_server(L)->hudSetFlags(player, flags, mask))
+ if (!STACK_TO_SERVER(L)->hudSetFlags(player, flags, mask))
+ return 0;
+
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// hud_set_hotbar_itemcount(self, hotbar_itemcount)
+int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ s32 hotbar_itemcount = lua_tonumber(L, 2);
+
+ if (!STACK_TO_SERVER(L)->hudSetHotbarItemcount(player, hotbar_itemcount))
return 0;
lua_pushboolean(L, true);
@@ -1048,162 +1106,8 @@ const luaL_reg ObjectRef::methods[] = {
luamethod(ObjectRef, hud_change),
luamethod(ObjectRef, hud_get),
luamethod(ObjectRef, hud_set_flags),
+ luamethod(ObjectRef, hud_set_hotbar_itemcount),
{0,0}
};
-// Creates a new anonymous reference if cobj=NULL or id=0
-void objectref_get_or_create(lua_State *L,
- ServerActiveObject *cobj)
-{
- if(cobj == NULL || cobj->getId() == 0){
- ObjectRef::create(L, cobj);
- } else {
- objectref_get(L, cobj->getId());
- }
-}
-
-void objectref_get(lua_State *L, u16 id)
-{
- // Get minetest.object_refs[i]
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "object_refs");
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_pushnumber(L, id);
- lua_gettable(L, -2);
- lua_remove(L, -2); // object_refs
- lua_remove(L, -2); // minetest
-}
-
-/*
- ObjectProperties
-*/
-
-void read_object_properties(lua_State *L, int index,
- ObjectProperties *prop)
-{
- if(index < 0)
- index = lua_gettop(L) + 1 + index;
- if(!lua_istable(L, index))
- return;
-
- prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
-
- getboolfield(L, -1, "physical", prop->physical);
-
- getfloatfield(L, -1, "weight", prop->weight);
-
- lua_getfield(L, -1, "collisionbox");
- if(lua_istable(L, -1))
- prop->collisionbox = read_aabb3f(L, -1, 1.0);
- lua_pop(L, 1);
-
- getstringfield(L, -1, "visual", prop->visual);
-
- getstringfield(L, -1, "mesh", prop->mesh);
-
- lua_getfield(L, -1, "visual_size");
- if(lua_istable(L, -1))
- prop->visual_size = read_v2f(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "textures");
- if(lua_istable(L, -1)){
- prop->textures.clear();
- int table = lua_gettop(L);
- lua_pushnil(L);
- while(lua_next(L, table) != 0){
- // key at index -2 and value at index -1
- if(lua_isstring(L, -1))
- prop->textures.push_back(lua_tostring(L, -1));
- else
- prop->textures.push_back("");
- // removes value, keeps key for next iteration
- lua_pop(L, 1);
- }
- }
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "colors");
- if(lua_istable(L, -1)){
- prop->colors.clear();
- int table = lua_gettop(L);
- lua_pushnil(L);
- while(lua_next(L, table) != 0){
- // key at index -2 and value at index -1
- if(lua_isstring(L, -1))
- prop->colors.push_back(readARGB8(L, -1));
- else
- prop->colors.push_back(video::SColor(255, 255, 255, 255));
- // removes value, keeps key for next iteration
- lua_pop(L, 1);
- }
- }
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "spritediv");
- if(lua_istable(L, -1))
- prop->spritediv = read_v2s16(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "initial_sprite_basepos");
- if(lua_istable(L, -1))
- prop->initial_sprite_basepos = read_v2s16(L, -1);
- lua_pop(L, 1);
-
- getboolfield(L, -1, "is_visible", prop->is_visible);
- getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
- getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
-}
-
-/*
- object_reference
-*/
-
-void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
- StackUnroller stack_unroller(L);
-
- // Create object on stack
- ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
- int object = lua_gettop(L);
-
- // Get minetest.object_refs table
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "object_refs");
- luaL_checktype(L, -1, LUA_TTABLE);
- int objectstable = lua_gettop(L);
-
- // object_refs[id] = object
- lua_pushnumber(L, cobj->getId()); // Push id
- lua_pushvalue(L, object); // Copy object to top of stack
- lua_settable(L, objectstable);
-}
-
-void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
-{
- realitycheck(L);
- assert(lua_checkstack(L, 20));
- //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
- StackUnroller stack_unroller(L);
-
- // Get minetest.object_refs table
- lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "object_refs");
- luaL_checktype(L, -1, LUA_TTABLE);
- int objectstable = lua_gettop(L);
-
- // Get object_refs[id]
- lua_pushnumber(L, cobj->getId()); // Push id
- lua_gettable(L, objectstable);
- // Set object reference to NULL
- ObjectRef::set_null(L);
- lua_pop(L, 1); // pop object
-
- // Set object_refs[id] = nil
- lua_pushnumber(L, cobj->getId()); // Push id
- lua_pushnil(L);
- lua_settable(L, objectstable);
-}
+REGISTER_LUA_REF(ObjectRef)
diff --git a/src/scriptapi_object.h b/src/script/lua_api/l_object.h
index 82535ee94..57dac0e64 100644
--- a/src/scriptapi_object.h
+++ b/src/script/lua_api/l_object.h
@@ -17,17 +17,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_OBJECT_H_
-#define LUA_OBJECT_H_
+#ifndef L_OBJECT_H_
+#define L_OBJECT_H_
extern "C" {
#include <lua.h>
#include <lauxlib.h>
}
-#include "serverobject.h"
-#include "content_sao.h"
-#include "player.h"
+class ServerActiveObject;
+class LuaEntitySAO;
+class PlayerSAO;
+class Player;
/*
ObjectRef
@@ -199,12 +200,18 @@ private:
// hud_change(self, id, stat, data)
static int l_hud_change(lua_State *L);
+ // hud_get_next_id(self)
+ static u32 hud_get_next_id(lua_State *L);
+
// hud_get(self, id)
static int l_hud_get(lua_State *L);
// hud_set_flags(self, flags)
static int l_hud_set_flags(lua_State *L);
+ // hud_set_hotbar_itemcount(self, hotbar_itemcount)
+ static int l_hud_set_hotbar_itemcount(lua_State *L);
+
public:
ObjectRef(ServerActiveObject *object);
@@ -219,20 +226,4 @@ public:
static void Register(lua_State *L);
};
-/*****************************************************************************/
-/* scriptapi internal */
-/*****************************************************************************/
-// Creates a new anonymous reference if cobj=NULL or id=0
-void objectref_get_or_create(lua_State *L,
- ServerActiveObject *cobj);
-void objectref_get(lua_State *L, u16 id);
-void read_object_properties(lua_State *L, int index,
- ObjectProperties *prop);
-
-/*****************************************************************************/
-/* Minetest interface */
-/*****************************************************************************/
-void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
-void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj);
-
-#endif /* LUA_OBJECT_H_ */
+#endif /* L_OBJECT_H_ */
diff --git a/src/scriptapi_particles.cpp b/src/script/lua_api/l_particles.cpp
index dc9b3776e..c291cc21e 100644
--- a/src/scriptapi_particles.cpp
+++ b/src/script/lua_api/l_particles.cpp
@@ -17,12 +17,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "scriptapi.h"
-#include "scriptapi_particles.h"
+#include "cpp_api/scriptapi.h"
+#include "common/c_converter.h"
+#include "lua_api/l_base.h"
+#include "lua_api/l_particles.h"
#include "server.h"
-#include "script.h"
-#include "scriptapi_types.h"
-#include "scriptapi_common.h"
+#include "common/c_internal.h"
+
+bool ModApiParticles::Initialize(lua_State *L, int top) {
+ bool retval = true;
+
+ retval &= API_FCT(add_particle);
+ retval &= API_FCT(add_particlespawner);
+ retval &= API_FCT(delete_particlespawner);
+
+ return retval;
+}
// add_particle(pos, velocity, acceleration, expirationtime,
// size, collisiondetection, texture, player)
@@ -30,10 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// expirationtime = num (seconds)
// size = num
// texture = e.g."default_wood.png"
-int l_add_particle(lua_State *L)
+int ModApiParticles::l_add_particle(lua_State *L)
{
- // Get server from registry
- Server *server = get_server(L);
// Get parameters
v3f pos = check_v3f(L, 1);
v3f vel = check_v3f(L, 2);
@@ -46,13 +54,13 @@ int l_add_particle(lua_State *L)
if (lua_gettop(L) == 8) // only spawn for a single player
{
const char *playername = luaL_checkstring(L, 8);
- server->spawnParticle(playername,
+ getServer(L)->spawnParticle(playername,
pos, vel, acc, expirationtime,
size, collisiondetection, texture);
}
else // spawn for all players
{
- server->spawnParticleAll(pos, vel, acc,
+ getServer(L)->spawnParticleAll(pos, vel, acc,
expirationtime, size, collisiondetection, texture);
}
return 1;
@@ -72,10 +80,8 @@ int l_add_particle(lua_State *L)
// minsize/maxsize = num
// collisiondetection = bool
// texture = e.g."default_wood.png"
-int l_add_particlespawner(lua_State *L)
+int ModApiParticles::l_add_particlespawner(lua_State *L)
{
- // Get server from registry
- Server *server = get_server(L);
// Get parameters
u16 amount = luaL_checknumber(L, 1);
float time = luaL_checknumber(L, 2);
@@ -95,7 +101,7 @@ int l_add_particlespawner(lua_State *L)
if (lua_gettop(L) == 15) // only spawn for a single player
{
const char *playername = luaL_checkstring(L, 15);
- u32 id = server->addParticleSpawner(playername,
+ u32 id = getServer(L)->addParticleSpawner(playername,
amount, time,
minpos, maxpos,
minvel, maxvel,
@@ -108,7 +114,7 @@ int l_add_particlespawner(lua_State *L)
}
else // spawn for all players
{
- u32 id = server->addParticleSpawnerAll( amount, time,
+ u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
minpos, maxpos,
minvel, maxvel,
minacc, maxacc,
@@ -123,21 +129,21 @@ int l_add_particlespawner(lua_State *L)
// delete_particlespawner(id, player)
// player (string) is optional
-int l_delete_particlespawner(lua_State *L)
+int ModApiParticles::l_delete_particlespawner(lua_State *L)
{
- // Get server from registry
- Server *server = get_server(L);
// Get parameters
u32 id = luaL_checknumber(L, 1);
if (lua_gettop(L) == 2) // only delete for one player
{
const char *playername = luaL_checkstring(L, 2);
- server->deleteParticleSpawner(playername, id);
+ getServer(L)->deleteParticleSpawner(playername, id);
}
else // delete for all players
{
- server->deleteParticleSpawnerAll(id);
+ getServer(L)->deleteParticleSpawnerAll(id);
}
return 1;
}
+
+ModApiParticles modapiparticles_prototyp;
diff --git a/src/scriptapi_particles.h b/src/script/lua_api/l_particles.h
index 4b37d7ce1..3729f8761 100644
--- a/src/scriptapi_particles.h
+++ b/src/script/lua_api/l_particles.h
@@ -1,5 +1,5 @@
/*
-Minetest-c55
+Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -17,16 +17,23 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef LUA_PARTICLES_H_
-#define LUA_PARTICLES_H_
+#ifndef L_PARTICLES_H_
+#define L_PARTICLES_H_
extern "C" {
#include <lua.h>
#include <lauxlib.h>
}
-int l_add_particle(lua_State *L);
-int l_add_particlespawner(lua_State *L);
-int l_delete_particlespawner(lua_State *L);
+class ModApiParticles : public ModApiBase {
+public:
+ bool Initialize(lua_State *L, int top);
+private:
+ static int l_add_particle(lua_State *L);
+ static int l_add_particlespawner(lua_State *L);
+ static int l_delete_particlespawner(lua_State *L);
+};
-#endif
+
+
+#endif // L_PARTICLES_H_
diff --git a/src/script/lua_api/luaapi.cpp b/src/script/lua_api/luaapi.cpp
new file mode 100644
index 000000000..b75304b3a
--- /dev/null
+++ b/src/script/lua_api/luaapi.cpp
@@ -0,0 +1,653 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+extern "C" {
+#include "lua.h"
+#include "lauxlib.h"
+}
+
+#include "lua_api/l_base.h"
+#include "common/c_internal.h"
+#include "server.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "lua_api/luaapi.h"
+#include "settings.h"
+#include "tool.h"
+#include "rollback.h"
+#include "log.h"
+#include "emerge.h"
+#include "main.h" //required for g_settings
+
+struct EnumString ModApiBasic::es_OreType[] =
+{
+ {ORE_SCATTER, "scatter"},
+ {ORE_SHEET, "sheet"},
+ {ORE_CLAYLIKE, "claylike"},
+ {0, NULL},
+};
+
+
+ModApiBasic::ModApiBasic() : ModApiBase() {
+}
+
+bool ModApiBasic::Initialize(lua_State* L,int top) {
+
+ bool retval = true;
+
+ retval &= API_FCT(debug);
+ retval &= API_FCT(log);
+ retval &= API_FCT(request_shutdown);
+ retval &= API_FCT(get_server_status);
+
+ retval &= API_FCT(register_biome);
+
+ retval &= API_FCT(setting_set);
+ retval &= API_FCT(setting_get);
+ retval &= API_FCT(setting_getbool);
+ retval &= API_FCT(setting_save);
+
+ retval &= API_FCT(chat_send_all);
+ retval &= API_FCT(chat_send_player);
+ retval &= API_FCT(show_formspec);
+
+ retval &= API_FCT(get_player_privs);
+ retval &= API_FCT(get_player_ip);
+ retval &= API_FCT(get_ban_list);
+ retval &= API_FCT(get_ban_description);
+ retval &= API_FCT(ban_player);
+ retval &= API_FCT(unban_player_of_ip);
+ retval &= API_FCT(get_password_hash);
+ retval &= API_FCT(notify_authentication_modified);
+
+ retval &= API_FCT(get_dig_params);
+ retval &= API_FCT(get_hit_params);
+
+ retval &= API_FCT(get_current_modname);
+ retval &= API_FCT(get_modpath);
+ retval &= API_FCT(get_modnames);
+
+ retval &= API_FCT(get_worldpath);
+ retval &= API_FCT(is_singleplayer);
+ retval &= API_FCT(sound_play);
+ retval &= API_FCT(sound_stop);
+
+ retval &= API_FCT(rollback_get_last_node_actor);
+ retval &= API_FCT(rollback_revert_actions_by);
+
+ retval &= API_FCT(register_ore);
+
+ return retval;
+}
+
+// debug(text)
+// Writes a line to dstream
+int ModApiBasic::l_debug(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string text = lua_tostring(L, 1);
+ dstream << text << std::endl;
+ return 0;
+}
+
+// log([level,] text)
+// Writes a line to the logger.
+// The one-argument version logs to infostream.
+// The two-argument version accept a log level: error, action, info, or verbose.
+int ModApiBasic::l_log(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string text;
+ LogMessageLevel level = LMT_INFO;
+ if(lua_isnone(L, 2))
+ {
+ text = lua_tostring(L, 1);
+ }
+ else
+ {
+ std::string levelname = luaL_checkstring(L, 1);
+ text = luaL_checkstring(L, 2);
+ if(levelname == "error")
+ level = LMT_ERROR;
+ else if(levelname == "action")
+ level = LMT_ACTION;
+ else if(levelname == "verbose")
+ level = LMT_VERBOSE;
+ }
+ log_printline(level, text);
+ return 0;
+}
+
+// request_shutdown()
+int ModApiBasic::l_request_shutdown(lua_State *L)
+{
+ getServer(L)->requestShutdown();
+ return 0;
+}
+
+// get_server_status()
+int ModApiBasic::l_get_server_status(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
+ return 1;
+}
+
+// register_biome({lots of stuff})
+int ModApiBasic::l_register_biome(lua_State *L)
+{
+ int index = 1;
+ luaL_checktype(L, index, LUA_TTABLE);
+
+ BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
+ if (!bmgr) {
+ verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
+ return 0;
+ }
+
+ enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
+ "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
+ Biome *b = bmgr->createBiome(terrain);
+
+ b->name = getstringfield_default(L, index, "name", "");
+ b->top_nodename = getstringfield_default(L, index, "top_node", "");
+ b->top_depth = getintfield_default(L, index, "top_depth", 0);
+ b->filler_nodename = getstringfield_default(L, index, "filler_node", "");
+ b->filler_height = getintfield_default(L, index, "filler_height", 0);
+ b->height_min = getintfield_default(L, index, "height_min", 0);
+ b->height_max = getintfield_default(L, index, "height_max", 0);
+ b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
+ b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
+
+ b->flags = 0; //reserved
+ b->c_top = CONTENT_IGNORE;
+ b->c_filler = CONTENT_IGNORE;
+ bmgr->addBiome(b);
+
+ verbosestream << "register_biome: " << b->name << std::endl;
+ return 0;
+}
+
+
+
+// setting_set(name, value)
+int ModApiBasic::l_setting_set(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *name = luaL_checkstring(L, 1);
+ const char *value = luaL_checkstring(L, 2);
+ g_settings->set(name, value);
+ return 0;
+}
+
+// setting_get(name)
+int ModApiBasic::l_setting_get(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *name = luaL_checkstring(L, 1);
+ try{
+ std::string value = g_settings->get(name);
+ lua_pushstring(L, value.c_str());
+ } catch(SettingNotFoundException &e){
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+// setting_getbool(name)
+int ModApiBasic::l_setting_getbool(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *name = luaL_checkstring(L, 1);
+ try{
+ bool value = g_settings->getBool(name);
+ lua_pushboolean(L, value);
+ } catch(SettingNotFoundException &e){
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+// setting_save()
+int ModApiBasic::l_setting_save(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ getServer(L)->saveConfig();
+ return 0;
+}
+
+// chat_send_all(text)
+int ModApiBasic::l_chat_send_all(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *text = luaL_checkstring(L, 1);
+ // Get server from registry
+ Server *server = getServer(L);
+ // Send
+ server->notifyPlayers(narrow_to_wide(text));
+ return 0;
+}
+
+// chat_send_player(name, text, prepend)
+int ModApiBasic::l_chat_send_player(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *name = luaL_checkstring(L, 1);
+ const char *text = luaL_checkstring(L, 2);
+ bool prepend = true;
+
+ if (lua_isboolean(L, 3))
+ prepend = lua_toboolean(L, 3);
+
+ // Get server from registry
+ Server *server = getServer(L);
+ // Send
+ server->notifyPlayer(name, narrow_to_wide(text), prepend);
+ return 0;
+}
+
+// get_player_privs(name, text)
+int ModApiBasic::l_get_player_privs(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *name = luaL_checkstring(L, 1);
+ // Get server from registry
+ Server *server = getServer(L);
+ // Do it
+ lua_newtable(L);
+ int table = lua_gettop(L);
+ std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
+ for(std::set<std::string>::const_iterator
+ i = privs_s.begin(); i != privs_s.end(); i++){
+ lua_pushboolean(L, true);
+ lua_setfield(L, table, i->c_str());
+ }
+ lua_pushvalue(L, table);
+ return 1;
+}
+
+// get_player_ip()
+int ModApiBasic::l_get_player_ip(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char * name = luaL_checkstring(L, 1);
+ Player *player = getEnv(L)->getPlayer(name);
+ if(player == NULL)
+ {
+ lua_pushnil(L); // no such player
+ return 1;
+ }
+ try
+ {
+ Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
+ std::string ip_str = addr.serializeString();
+ lua_pushstring(L, ip_str.c_str());
+ return 1;
+ }
+ catch(con::PeerNotFoundException) // unlikely
+ {
+ dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
+ lua_pushnil(L); // error
+ return 1;
+ }
+}
+
+// get_ban_list()
+int ModApiBasic::l_get_ban_list(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
+ return 1;
+}
+
+// get_ban_description()
+int ModApiBasic::l_get_ban_description(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char * ip_or_name = luaL_checkstring(L, 1);
+ lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
+ return 1;
+}
+
+// ban_player()
+int ModApiBasic::l_ban_player(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char * name = luaL_checkstring(L, 1);
+ Player *player = getEnv(L)->getPlayer(name);
+ if(player == NULL)
+ {
+ lua_pushboolean(L, false); // no such player
+ return 1;
+ }
+ try
+ {
+ Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
+ std::string ip_str = addr.serializeString();
+ getServer(L)->setIpBanned(ip_str, name);
+ }
+ catch(con::PeerNotFoundException) // unlikely
+ {
+ dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
+ lua_pushboolean(L, false); // error
+ return 1;
+ }
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// unban_player_or_ip()
+int ModApiBasic::l_unban_player_of_ip(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char * ip_or_name = luaL_checkstring(L, 1);
+ getServer(L)->unsetIpBanned(ip_or_name);
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// show_formspec(playername,formname,formspec)
+int ModApiBasic::l_show_formspec(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *playername = luaL_checkstring(L, 1);
+ const char *formname = luaL_checkstring(L, 2);
+ const char *formspec = luaL_checkstring(L, 3);
+
+ if(getServer(L)->showFormspec(playername,formspec,formname))
+ {
+ lua_pushboolean(L, true);
+ }else{
+ lua_pushboolean(L, false);
+ }
+ return 1;
+}
+
+// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+int ModApiBasic::l_get_dig_params(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::map<std::string, int> groups;
+ read_groups(L, 1, groups);
+ ToolCapabilities tp = read_tool_capabilities(L, 2);
+ if(lua_isnoneornil(L, 3))
+ push_dig_params(L, getDigParams(groups, &tp));
+ else
+ push_dig_params(L, getDigParams(groups, &tp,
+ luaL_checknumber(L, 3)));
+ return 1;
+}
+
+// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+int ModApiBasic::l_get_hit_params(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::map<std::string, int> groups;
+ read_groups(L, 1, groups);
+ ToolCapabilities tp = read_tool_capabilities(L, 2);
+ if(lua_isnoneornil(L, 3))
+ push_hit_params(L, getHitParams(groups, &tp));
+ else
+ push_hit_params(L, getHitParams(groups, &tp,
+ luaL_checknumber(L, 3)));
+ return 1;
+}
+
+// get_current_modname()
+int ModApiBasic::l_get_current_modname(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
+ return 1;
+}
+
+// get_modpath(modname)
+int ModApiBasic::l_get_modpath(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string modname = luaL_checkstring(L, 1);
+ // Do it
+ if(modname == "__builtin"){
+ std::string path = getServer(L)->getBuiltinLuaPath();
+ lua_pushstring(L, path.c_str());
+ return 1;
+ }
+ const ModSpec *mod = getServer(L)->getModSpec(modname);
+ if(!mod){
+ lua_pushnil(L);
+ return 1;
+ }
+ lua_pushstring(L, mod->path.c_str());
+ return 1;
+}
+
+// get_modnames()
+// the returned list is sorted alphabetically for you
+int ModApiBasic::l_get_modnames(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ // Get a list of mods
+ std::list<std::string> mods_unsorted, mods_sorted;
+ getServer(L)->getModNames(mods_unsorted);
+
+ // Take unsorted items from mods_unsorted and sort them into
+ // mods_sorted; not great performance but the number of mods on a
+ // server will likely be small.
+ for(std::list<std::string>::iterator i = mods_unsorted.begin();
+ i != mods_unsorted.end(); ++i)
+ {
+ bool added = false;
+ for(std::list<std::string>::iterator x = mods_sorted.begin();
+ x != mods_sorted.end(); ++x)
+ {
+ // I doubt anybody using Minetest will be using
+ // anything not ASCII based :)
+ if((*i).compare(*x) <= 0)
+ {
+ mods_sorted.insert(x, *i);
+ added = true;
+ break;
+ }
+ }
+ if(!added)
+ mods_sorted.push_back(*i);
+ }
+
+ // Get the table insertion function from Lua.
+ lua_getglobal(L, "table");
+ lua_getfield(L, -1, "insert");
+ int insertion_func = lua_gettop(L);
+
+ // Package them up for Lua
+ lua_newtable(L);
+ int new_table = lua_gettop(L);
+ std::list<std::string>::iterator i = mods_sorted.begin();
+ while(i != mods_sorted.end())
+ {
+ lua_pushvalue(L, insertion_func);
+ lua_pushvalue(L, new_table);
+ lua_pushstring(L, (*i).c_str());
+ if(lua_pcall(L, 2, 0, 0) != 0)
+ {
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ }
+ ++i;
+ }
+ return 1;
+}
+
+// get_worldpath()
+int ModApiBasic::l_get_worldpath(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string worldpath = getServer(L)->getWorldPath();
+ lua_pushstring(L, worldpath.c_str());
+ return 1;
+}
+
+// sound_play(spec, parameters)
+int ModApiBasic::l_sound_play(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ SimpleSoundSpec spec;
+ read_soundspec(L, 1, spec);
+ ServerSoundParams params;
+ read_server_sound_params(L, 2, params);
+ s32 handle = getServer(L)->playSound(spec, params);
+ lua_pushinteger(L, handle);
+ return 1;
+}
+
+// sound_stop(handle)
+int ModApiBasic::l_sound_stop(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ int handle = luaL_checkinteger(L, 1);
+ getServer(L)->stopSound(handle);
+ return 0;
+}
+
+// is_singleplayer()
+int ModApiBasic::l_is_singleplayer(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ lua_pushboolean(L, getServer(L)->isSingleplayer());
+ return 1;
+}
+
+// get_password_hash(name, raw_password)
+int ModApiBasic::l_get_password_hash(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string name = luaL_checkstring(L, 1);
+ std::string raw_password = luaL_checkstring(L, 2);
+ std::string hash = translatePassword(name,
+ narrow_to_wide(raw_password));
+ lua_pushstring(L, hash.c_str());
+ return 1;
+}
+
+// notify_authentication_modified(name)
+int ModApiBasic::l_notify_authentication_modified(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ std::string name = "";
+ if(lua_isstring(L, 1))
+ name = lua_tostring(L, 1);
+ getServer(L)->reportPrivsModified(name);
+ return 0;
+}
+
+// rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
+int ModApiBasic::l_rollback_get_last_node_actor(lua_State *L)
+{
+ v3s16 p = read_v3s16(L, 1);
+ int range = luaL_checknumber(L, 2);
+ int seconds = luaL_checknumber(L, 3);
+ Server *server = getServer(L);
+ IRollbackManager *rollback = server->getRollbackManager();
+ v3s16 act_p;
+ int act_seconds = 0;
+ std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
+ lua_pushstring(L, actor.c_str());
+ push_v3s16(L, act_p);
+ lua_pushnumber(L, act_seconds);
+ return 3;
+}
+
+// rollback_revert_actions_by(actor, seconds) -> bool, log messages
+int ModApiBasic::l_rollback_revert_actions_by(lua_State *L)
+{
+ std::string actor = luaL_checkstring(L, 1);
+ int seconds = luaL_checknumber(L, 2);
+ Server *server = getServer(L);
+ IRollbackManager *rollback = server->getRollbackManager();
+ std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
+ std::list<std::string> log;
+ bool success = server->rollbackRevertActions(actions, &log);
+ // Push boolean result
+ lua_pushboolean(L, success);
+ // Get the table insert function and push the log table
+ lua_getglobal(L, "table");
+ lua_getfield(L, -1, "insert");
+ int table_insert = lua_gettop(L);
+ lua_newtable(L);
+ int table = lua_gettop(L);
+ for(std::list<std::string>::const_iterator i = log.begin();
+ i != log.end(); i++)
+ {
+ lua_pushvalue(L, table_insert);
+ lua_pushvalue(L, table);
+ lua_pushstring(L, i->c_str());
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ }
+ lua_remove(L, -2); // Remove table
+ lua_remove(L, -2); // Remove insert
+ return 2;
+}
+
+int ModApiBasic::l_register_ore(lua_State *L)
+{
+ int index = 1;
+ luaL_checktype(L, index, LUA_TTABLE);
+
+ EmergeManager *emerge = getServer(L)->getEmergeManager();
+
+ enum OreType oretype = (OreType)getenumfield(L, index,
+ "ore_type", es_OreType, ORE_SCATTER);
+ Ore *ore = createOre(oretype);
+ if (!ore) {
+ errorstream << "register_ore: ore_type "
+ << oretype << " not implemented";
+ return 0;
+ }
+
+ ore->ore_name = getstringfield_default(L, index, "ore", "");
+ ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
+ ore->wherein_name = getstringfield_default(L, index, "wherein", "");
+ ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
+ ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
+ ore->clust_size = getintfield_default(L, index, "clust_size", 0);
+ ore->height_min = getintfield_default(L, index, "height_min", 0);
+ ore->height_max = getintfield_default(L, index, "height_max", 0);
+ ore->flags = getflagsfield(L, index, "flags", flagdesc_ore);
+ ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
+
+ lua_getfield(L, index, "noise_params");
+ ore->np = read_noiseparams(L, -1);
+ lua_pop(L, 1);
+
+ ore->noise = NULL;
+
+ if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
+ errorstream << "register_ore: clust_scarcity and clust_num_ores"
+ "must be greater than 0" << std::endl;
+ delete ore;
+ return 0;
+ }
+
+ emerge->ores.push_back(ore);
+
+ verbosestream << "register_ore: ore '" << ore->ore_name
+ << "' registered" << std::endl;
+ return 0;
+}
+
+ModApiBasic modapibasic_prototype;
diff --git a/src/script/lua_api/luaapi.h b/src/script/lua_api/luaapi.h
new file mode 100644
index 000000000..592046965
--- /dev/null
+++ b/src/script/lua_api/luaapi.h
@@ -0,0 +1,140 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef LUAAPI_H_
+#define LUAAPI_H_
+
+
+class ModApiBasic : public ModApiBase {
+
+public:
+ ModApiBasic();
+
+ bool Initialize(lua_State* L,int top);
+
+private:
+ // debug(text)
+ // Writes a line to dstream
+ static int l_debug(lua_State *L);
+
+ // log([level,] text)
+ // Writes a line to the logger.
+ // The one-argument version logs to infostream.
+ // The two-argument version accept a log level: error, action, info, or verbose.
+ static int l_log(lua_State *L);
+
+ // request_shutdown()
+ static int l_request_shutdown(lua_State *L);
+
+ // get_server_status()
+ static int l_get_server_status(lua_State *L);
+
+ // register_biome_groups({frequencies})
+ static int l_register_biome_groups(lua_State *L);
+
+ // register_biome({lots of stuff})
+ static int l_register_biome(lua_State *L);
+
+ // setting_set(name, value)
+ static int l_setting_set(lua_State *L);
+
+ // setting_get(name)
+ static int l_setting_get(lua_State *L);
+
+ // setting_getbool(name)
+ static int l_setting_getbool(lua_State *L);
+
+ // setting_save()
+ static int l_setting_save(lua_State *L);
+
+ // chat_send_all(text)
+ static int l_chat_send_all(lua_State *L);
+
+ // chat_send_player(name, text)
+ static int l_chat_send_player(lua_State *L);
+
+ // get_player_privs(name, text)
+ static int l_get_player_privs(lua_State *L);
+
+ // get_player_ip()
+ static int l_get_player_ip(lua_State *L);
+
+ // get_ban_list()
+ static int l_get_ban_list(lua_State *L);
+
+ // get_ban_description()
+ static int l_get_ban_description(lua_State *L);
+
+ // ban_player()
+ static int l_ban_player(lua_State *L);
+
+ // unban_player_or_ip()
+ static int l_unban_player_of_ip(lua_State *L);
+
+ // show_formspec(playername,formname,formspec)
+ static int l_show_formspec(lua_State *L);
+
+ // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+ static int l_get_dig_params(lua_State *L);
+
+ // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+ static int l_get_hit_params(lua_State *L);
+
+ // get_current_modname()
+ static int l_get_current_modname(lua_State *L);
+
+ // get_modpath(modname)
+ static int l_get_modpath(lua_State *L);
+
+ // get_modnames()
+ // the returned list is sorted alphabetically for you
+ static int l_get_modnames(lua_State *L);
+
+ // get_worldpath()
+ static int l_get_worldpath(lua_State *L);
+
+ // sound_play(spec, parameters)
+ static int l_sound_play(lua_State *L);
+
+ // sound_stop(handle)
+ static int l_sound_stop(lua_State *L);
+
+ // is_singleplayer()
+ static int l_is_singleplayer(lua_State *L);
+
+ // get_password_hash(name, raw_password)
+ static int l_get_password_hash(lua_State *L);
+
+ // notify_authentication_modified(name)
+ static int l_notify_authentication_modified(lua_State *L);
+
+ // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
+ static int l_rollback_get_last_node_actor(lua_State *L);
+
+ // rollback_revert_actions_by(actor, seconds) -> bool, log messages
+ static int l_rollback_revert_actions_by(lua_State *L);
+
+ static int l_register_ore(lua_State *L);
+
+ static struct EnumString es_OreType[];
+};
+
+
+
+#endif /* LUAAPI_H_ */