LuaVoxelManip: Throw warning or error instead of silently doing nothing (#7567)
[oweals/minetest.git] / src / script / lua_api / l_vmanip.cpp
index ac6c10303d183ea8c38688c1d7df8dcad6e1df62..c92983bd3d332dcca373a0781fcecf48e83f4378 100644 (file)
@@ -25,12 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "emerge.h"
 #include "environment.h"
 #include "map.h"
+#include "mapblock.h"
 #include "server.h"
-#include "mapgen.h"
-
-#define GET_ENV_PTR ServerEnvironment* env =                                   \
-                               dynamic_cast<ServerEnvironment*>(getEnv(L));                   \
-                               if (env == NULL) return 0
+#include "mapgen/mapgen.h"
+#include "voxelalgorithms.h"
 
 // garbage collector
 int LuaVoxelManip::gc_object(lua_State *L)
@@ -43,6 +41,8 @@ int LuaVoxelManip::gc_object(lua_State *L)
 
 int LuaVoxelManip::l_read_from_map(lua_State *L)
 {
+       MAP_LOCK_REQUIRED;
+
        LuaVoxelManip *o = checkobject(L, 1);
        MMVManip *vm = o->vm;
 
@@ -91,7 +91,7 @@ int LuaVoxelManip::l_set_data(lua_State *L)
        MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
-               return 0;
+               throw LuaError("VoxelManip:set_data called with missing parameter");
 
        u32 volume = vm->m_area.getVolume();
        for (u32 i = 0; i != volume; i++) {
@@ -108,34 +108,52 @@ int LuaVoxelManip::l_set_data(lua_State *L)
 
 int LuaVoxelManip::l_write_to_map(lua_State *L)
 {
+       MAP_LOCK_REQUIRED;
+
        LuaVoxelManip *o = checkobject(L, 1);
-       MMVManip *vm = o->vm;
+       bool update_light = !lua_isboolean(L, 2) || readParam<bool>(L, 2);
+       GET_ENV_PTR;
+       ServerMap *map = &(env->getServerMap());
+       if (o->is_mapgen_vm || !update_light) {
+               o->vm->blitBackAll(&(o->modified_blocks));
+       } else {
+               voxalgo::blit_back_with_light(map, o->vm,
+                       &(o->modified_blocks));
+       }
 
-       vm->blitBackAll(&o->modified_blocks);
+       MapEditEvent event;
+       event.type = MEET_OTHER;
+       for (const auto &modified_block : o->modified_blocks)
+               event.modified_blocks.insert(modified_block.first);
+
+       map->dispatchEvent(&event);
 
+       o->modified_blocks.clear();
        return 0;
 }
 
 int LuaVoxelManip::l_get_node_at(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       GET_ENV_PTR;
+
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
        v3s16 pos        = check_v3s16(L, 2);
 
-       pushnode(L, o->vm->getNodeNoExNoEmerge(pos), env->getGameDef()->ndef());
+       pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef);
        return 1;
 }
 
 int LuaVoxelManip::l_set_node_at(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       GET_ENV_PTR;
+
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
        v3s16 pos        = check_v3s16(L, 2);
-       MapNode n        = readnode(L, 3, env->getGameDef()->ndef());
+       MapNode n        = readnode(L, 3, ndef);
 
        o->vm->setNodeNoEmerge(pos, n);
 
@@ -149,7 +167,7 @@ int LuaVoxelManip::l_update_liquids(lua_State *L)
        LuaVoxelManip *o = checkobject(L, 1);
 
        Map *map = &(env->getMap());
-       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
        MMVManip *vm = o->vm;
 
        Mapgen mg;
@@ -167,10 +185,13 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       if (!o->is_mapgen_vm)
+       if (!o->is_mapgen_vm) {
+               warningstream << "VoxelManip:calc_lighting called for a non-mapgen "
+                       "VoxelManip object" << std::endl;
                return 0;
+       }
 
-       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
        EmergeManager *emerge = getServer(L)->getEmergeManager();
        MMVManip *vm = o->vm;
 
@@ -179,6 +200,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
        v3s16 fpmax  = vm->m_area.MaxEdge;
        v3s16 pmin   = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
        v3s16 pmax   = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
+       bool propagate_shadow = !lua_isboolean(L, 4) || readParam<bool>(L, 4);
 
        sortBoxVerticies(pmin, pmax);
        if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
@@ -187,9 +209,9 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
        Mapgen mg;
        mg.vm          = vm;
        mg.ndef        = ndef;
-       mg.water_level = emerge->params.water_level;
+       mg.water_level = emerge->mgparams->water_level;
 
-       mg.calcLighting(pmin, pmax, fpmin, fpmax);
+       mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
 
        return 0;
 }
@@ -199,11 +221,14 @@ int LuaVoxelManip::l_set_lighting(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       if (!o->is_mapgen_vm)
+       if (!o->is_mapgen_vm) {
+               warningstream << "VoxelManip:set_lighting called for a non-mapgen "
+                       "VoxelManip object" << std::endl;
                return 0;
+       }
 
        if (!lua_istable(L, 2))
-               return 0;
+               throw LuaError("VoxelManip:set_lighting called with missing parameter");
 
        u8 light;
        light  = (getintfield_default(L, 2, "day",   0) & 0x0F);
@@ -254,7 +279,8 @@ int LuaVoxelManip::l_set_light_data(lua_State *L)
        MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
-               return 0;
+               throw LuaError("VoxelManip:set_light_data called with missing "
+                               "parameter");
 
        u32 volume = vm->m_area.getVolume();
        for (u32 i = 0; i != volume; i++) {
@@ -274,11 +300,17 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
+       bool use_buffer  = lua_istable(L, 2);
+
        MMVManip *vm = o->vm;
 
        u32 volume = vm->m_area.getVolume();
 
-       lua_newtable(L);
+       if (use_buffer)
+               lua_pushvalue(L, 2);
+       else
+               lua_newtable(L);
+
        for (u32 i = 0; i != volume; i++) {
                lua_Integer param2 = vm->m_data[i].param2;
                lua_pushinteger(L, param2);
@@ -296,7 +328,8 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L)
        MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
-               return 0;
+               throw LuaError("VoxelManip:set_param2_data called with missing "
+                               "parameter");
 
        u32 volume = vm->m_area.getVolume();
        for (u32 i = 0; i != volume; i++) {
@@ -313,35 +346,6 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L)
 
 int LuaVoxelManip::l_update_map(lua_State *L)
 {
-       LuaVoxelManip *o = checkobject(L, 1);
-       if (o->is_mapgen_vm)
-               return 0;
-
-       Environment *env = getEnv(L);
-       if (!env)
-               return 0;
-
-       Map *map = &(env->getMap());
-
-       // TODO: Optimize this by using Mapgen::calcLighting() instead
-       std::map<v3s16, MapBlock *> lighting_mblocks;
-       std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
-
-       lighting_mblocks.insert(mblocks->begin(), mblocks->end());
-
-       map->updateLighting(lighting_mblocks, *mblocks);
-
-       MapEditEvent event;
-       event.type = MEET_OTHER;
-       for (std::map<v3s16, MapBlock *>::iterator
-               it = mblocks->begin();
-               it != mblocks->end(); ++it)
-               event.modified_blocks.insert(it->first);
-
-       map->dispatchEvent(&event);
-
-       mblocks->clear();
-
        return 0;
 }
 
@@ -359,6 +363,8 @@ int LuaVoxelManip::l_was_modified(lua_State *L)
 
 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
 {
+       NO_MAP_LOCK_REQUIRED;
+
        LuaVoxelManip *o = checkobject(L, 1);
 
        push_v3s16(L, o->vm->m_area.MinEdge);
@@ -367,22 +373,19 @@ int LuaVoxelManip::l_get_emerged_area(lua_State *L)
        return 2;
 }
 
-LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
+LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm) :
+       is_mapgen_vm(is_mg_vm),
+       vm(mmvm)
 {
-       this->vm           = mmvm;
-       this->is_mapgen_vm = is_mg_vm;
 }
 
-LuaVoxelManip::LuaVoxelManip(Map *map)
+LuaVoxelManip::LuaVoxelManip(Map *map) : vm(new MMVManip(map))
 {
-       this->vm = new MMVManip(map);
-       this->is_mapgen_vm = false;
 }
 
 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
 {
-       this->vm = new MMVManip(map);
-       this->is_mapgen_vm = false;
+       vm = new MMVManip(map);
 
        v3s16 bp1 = getNodeBlockPos(p1);
        v3s16 bp2 = getNodeBlockPos(p2);
@@ -400,11 +403,7 @@ LuaVoxelManip::~LuaVoxelManip()
 // Creates an LuaVoxelManip and leaves it on top of stack
 int LuaVoxelManip::create_object(lua_State *L)
 {
-       NO_MAP_LOCK_REQUIRED;
-
-       Environment *env = getEnv(L);
-       if (!env)
-               return 0;
+       GET_ENV_PTR;
 
        Map *map = &(env->getMap());
        LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
@@ -459,7 +458,7 @@ void LuaVoxelManip::Register(lua_State *L)
 }
 
 const char LuaVoxelManip::className[] = "VoxelManip";
-const luaL_reg LuaVoxelManip::methods[] = {
+const luaL_Reg LuaVoxelManip::methods[] = {
        luamethod(LuaVoxelManip, read_from_map),
        luamethod(LuaVoxelManip, get_data),
        luamethod(LuaVoxelManip, set_data),