]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_env.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / script / lua_api / l_env.cpp
index a287281a90d4013c5aaa64fd9c3c0af217881875..dbaf6fb368cb1fb538f646f7da37dda17527dd80 100644 (file)
@@ -17,21 +17,21 @@ 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 "lua_api/l_internal.h"
+#include "lua_api/l_nodemeta.h"
+#include "lua_api/l_nodetimer.h"
+#include "lua_api/l_noise.h"
+#include "lua_api/l_vmanip.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
+#include "scripting_game.h"
 #include "environment.h"
 #include "server.h"
+#include "nodedef.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"
 
@@ -39,14 +39,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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);
+       GameScripting *scriptIface = env->getScriptIface();
        scriptIface->realityCheck();
 
-       lua_StateL = scriptIface->getStack();
+       lua_State *L = scriptIface->getStack();
        assert(lua_checkstack(L, 20));
        StackUnroller stack_unroller(L);
 
@@ -179,8 +182,13 @@ int ModApiEnvMod::l_place_node(lua_State *L)
 {
        GET_ENV_PTR;
 
+       ScriptApiItem *scriptIfaceItem = getScriptApi<ScriptApiItem>(L);
+       Server *server = getServer(L);
+       INodeDefManager *ndef = server->ndef();
+       IItemDefManager *idef = server->idef();
+
        v3s16 pos = read_v3s16(L, 1);
-       MapNode n = readnode(L, 2, env->getGameDef()->ndef());
+       MapNode n = readnode(L, 2, ndef);
 
        // Don't attempt to load non-loaded area as of now
        MapNode n_old = env->getMap().getNodeNoEx(pos);
@@ -189,8 +197,6 @@ int ModApiEnvMod::l_place_node(lua_State *L)
                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;
@@ -199,7 +205,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
        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);
+       bool success = scriptIfaceItem->item_OnPlace(item, NULL, pointed);
        lua_pushboolean(L, success);
        return 1;
 }
@@ -210,6 +216,8 @@ int ModApiEnvMod::l_dig_node(lua_State *L)
 {
        GET_ENV_PTR;
 
+       ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
+
        v3s16 pos = read_v3s16(L, 1);
 
        // Don't attempt to load non-loaded area as of now
@@ -220,7 +228,7 @@ int ModApiEnvMod::l_dig_node(lua_State *L)
        }
        // 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);
+       bool success = scriptIfaceNode->node_on_dig(pos, n, NULL);
        lua_pushboolean(L, success);
        return 1;
 }
@@ -231,6 +239,8 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
 {
        GET_ENV_PTR;
 
+       ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
+
        v3s16 pos = read_v3s16(L, 1);
 
        // Don't attempt to load non-loaded area as of now
@@ -241,11 +251,70 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
        }
        // 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);
+       bool success = scriptIfaceNode->node_on_punch(pos, n, NULL);
        lua_pushboolean(L, success);
        return 1;
 }
 
+// minetest.get_node_max_level(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_node_max_level(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       MapNode n = env->getMap().getNodeNoEx(pos);
+       lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
+       return 1;
+}
+
+// minetest.get_node_level(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_node_level(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       MapNode n = env->getMap().getNodeNoEx(pos);
+       lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
+       return 1;
+}
+
+// minetest.set_node_level(pos, level)
+// pos = {x=num, y=num, z=num}
+// level: 0..63
+int ModApiEnvMod::l_set_node_level(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       u8 level = 1;
+       if(lua_isnumber(L, 2))
+               level = lua_tonumber(L, 2);
+       MapNode n = env->getMap().getNodeNoEx(pos);
+       lua_pushnumber(L, n.setLevel(env->getGameDef()->ndef(), level));
+       env->setNode(pos, n);
+       return 1;
+}
+
+// minetest.add_node_level(pos, level)
+// pos = {x=num, y=num, z=num}
+// level: 0..63
+int ModApiEnvMod::l_add_node_level(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       u8 level = 1;
+       if(lua_isnumber(L, 2))
+               level = lua_tonumber(L, 2);
+       MapNode n = env->getMap().getNodeNoEx(pos);
+       lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level));
+       env->setNode(pos, n);
+       return 1;
+}
+
+
 // minetest.get_meta(pos)
 int ModApiEnvMod::l_get_meta(lua_State *L)
 {
@@ -285,7 +354,7 @@ int ModApiEnvMod::l_add_entity(lua_State *L)
        if(objectid == 0)
                return 0;
        // Return ObjectRef
-       get_scriptapi(L)->objectrefGetOrCreate(obj);
+       getScriptApiBase(L)->objectrefGetOrCreate(obj);
        return 1;
 }
 
@@ -344,7 +413,7 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
                return 1;
        }
        // Put player on stack
-       get_scriptapi(L)->objectrefGetOrCreate(sao);
+       getScriptApiBase(L)->objectrefGetOrCreate(sao);
        return 1;
 }
 
@@ -370,7 +439,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
                // Insert object reference into table
                lua_pushvalue(L, table_insert);
                lua_pushvalue(L, table);
-               get_scriptapi(L)->objectrefGetOrCreate(obj);
+               getScriptApiBase(L)->objectrefGetOrCreate(obj);
                if(lua_pcall(L, 2, 0, 0))
                        script_error(L, "error: %s", lua_tostring(L, -1));
        }
@@ -533,6 +602,21 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L)
        return 1;
 }
 
+// minetest.get_voxel_manip()
+// returns voxel manipulator
+int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       Map *map = &(env->getMap());
+       LuaVoxelManip *o = new LuaVoxelManip(map);
+       
+       *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+       luaL_getmetatable(L, "VoxelManip");
+       lua_setmetatable(L, -2);
+       return 1;
+}
+
 // minetest.clear_objects()
 // clear all objects in the environment
 int ModApiEnvMod::l_clear_objects(lua_State *L)
@@ -554,8 +638,8 @@ int ModApiEnvMod::l_line_of_sight(lua_State *L) {
        // 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);
+       if (lua_isnumber(L, 3))
+               stepsize = lua_tonumber(L, 3);
 
        return (env->line_of_sight(pos1,pos2,stepsize));
 }
@@ -572,8 +656,8 @@ int ModApiEnvMod::l_find_path(lua_State *L)
        unsigned int max_jump       = luaL_checkint(L, 4);
        unsigned int max_drop       = luaL_checkint(L, 5);
        algorithm algo              = A_PLAIN_NP;
-       if(! lua_isnil(L, 6)) {
-               std::string algorithm       = luaL_checkstring(L,6);
+       if (!lua_isnil(L, 6)) {
+               std::string algorithm = luaL_checkstring(L,6);
 
                if (algorithm == "A*")
                        algo = A_PLAIN;
@@ -652,38 +736,73 @@ int ModApiEnvMod::l_spawn_tree(lua_State *L)
        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);
-       retval &= API_FCT(find_path);
-       retval &= API_FCT(line_of_sight);
-
-       return retval;
-}
-
-ModApiEnvMod modapienv_prototype;
+
+// minetest.transforming_liquid_add(pos)
+int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 p0 = read_v3s16(L, 1);
+       env->getMap().transforming_liquid_add(p0);
+       return 1;
+}
+
+// minetest.get_heat(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_heat(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       lua_pushnumber(L, env->getServerMap().getHeat(env, pos));
+       return 1;
+}
+
+// minetest.get_humidity(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiEnvMod::l_get_humidity(lua_State *L)
+{
+       GET_ENV_PTR;
+
+       v3s16 pos = read_v3s16(L, 1);
+       lua_pushnumber(L, env->getServerMap().getHumidity(env, pos));
+       return 1;
+}
+
+
+void ModApiEnvMod::Initialize(lua_State *L, int top)
+{
+       API_FCT(set_node);
+       API_FCT(add_node);
+       API_FCT(add_item);
+       API_FCT(remove_node);
+       API_FCT(get_node);
+       API_FCT(get_node_or_nil);
+       API_FCT(get_node_light);
+       API_FCT(place_node);
+       API_FCT(dig_node);
+       API_FCT(punch_node);
+       API_FCT(get_node_max_level);
+       API_FCT(get_node_level);
+       API_FCT(set_node_level);
+       API_FCT(add_node_level);
+       API_FCT(add_entity);
+       API_FCT(get_meta);
+       API_FCT(get_node_timer);
+       API_FCT(get_player_by_name);
+       API_FCT(get_objects_inside_radius);
+       API_FCT(set_timeofday);
+       API_FCT(get_timeofday);
+       API_FCT(find_node_near);
+       API_FCT(find_nodes_in_area);
+       API_FCT(get_perlin);
+       API_FCT(get_perlin_map);
+       API_FCT(get_voxel_manip);
+       API_FCT(clear_objects);
+       API_FCT(spawn_tree);
+       API_FCT(find_path);
+       API_FCT(line_of_sight);
+       API_FCT(transforming_liquid_add);
+       API_FCT(get_heat);
+       API_FCT(get_humidity);
+}