]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_vmanip.cpp
Revert "Make Lint Happy"
[dragonfireclient.git] / src / script / lua_api / l_vmanip.cpp
index bdf720f0a10f5f480e53787787a451f11a65d201..b99b1d98cfd32e0b129a59118e56eaa07fc72d0c 100644 (file)
@@ -25,8 +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"
+#include "mapgen/mapgen.h"
+#include "voxelalgorithms.h"
 
 // garbage collector
 int LuaVoxelManip::gc_object(lua_State *L)
@@ -70,7 +72,7 @@ int LuaVoxelManip::l_get_data(lua_State *L)
        if (use_buffer)
                lua_pushvalue(L, 2);
        else
-               lua_newtable(L);
+               lua_createtable(L, volume, 0);
 
        for (u32 i = 0; i != volume; i++) {
                lua_Integer cid = vm->m_data[i].getContent();
@@ -89,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++) {
@@ -109,10 +111,24 @@ 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));
+       }
+
+       MapEditEvent event;
+       event.type = MEET_OTHER;
+       for (const auto &modified_block : o->modified_blocks)
+               event.modified_blocks.insert(modified_block.first);
 
-       vm->blitBackAll(&o->modified_blocks);
+       map->dispatchEvent(event);
 
+       o->modified_blocks.clear();
        return 0;
 }
 
@@ -120,7 +136,7 @@ int LuaVoxelManip::l_get_node_at(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
 
-       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
        v3s16 pos        = check_v3s16(L, 2);
@@ -133,7 +149,7 @@ int LuaVoxelManip::l_set_node_at(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
 
-       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+       const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
        v3s16 pos        = check_v3s16(L, 2);
@@ -151,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;
@@ -169,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;
 
@@ -181,7 +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) ? lua_toboolean(L, 4) : true;
+       bool propagate_shadow = !lua_isboolean(L, 4) || readParam<bool>(L, 4);
 
        sortBoxVerticies(pmin, pmax);
        if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
@@ -202,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);
@@ -239,7 +261,7 @@ int LuaVoxelManip::l_get_light_data(lua_State *L)
 
        u32 volume = vm->m_area.getVolume();
 
-       lua_newtable(L);
+       lua_createtable(L, volume, 0);
        for (u32 i = 0; i != volume; i++) {
                lua_Integer light = vm->m_data[i].param1;
                lua_pushinteger(L, light);
@@ -257,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++) {
@@ -286,7 +309,7 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L)
        if (use_buffer)
                lua_pushvalue(L, 2);
        else
-               lua_newtable(L);
+               lua_createtable(L, volume, 0);
 
        for (u32 i = 0; i != volume; i++) {
                lua_Integer param2 = vm->m_data[i].param2;
@@ -305,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++) {
@@ -322,33 +346,6 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L)
 
 int LuaVoxelManip::l_update_map(lua_State *L)
 {
-       GET_ENV_PTR;
-
-       LuaVoxelManip *o = checkobject(L, 1);
-       if (o->is_mapgen_vm)
-               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;
 }
 
@@ -376,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);
@@ -464,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),