]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_vmanip.cpp
Light update for map blocks
[dragonfireclient.git] / src / script / lua_api / l_vmanip.cpp
index 29f04f0520bd55aa5038de34beb197df88670596..254a7e5a64fbfaf92455576d3aefeeb11a3d445a 100644 (file)
@@ -27,10 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "map.h"
 #include "server.h"
 #include "mapgen.h"
-
-#define GET_ENV_PTR ServerEnvironment* env =                                   \
-                               dynamic_cast<ServerEnvironment*>(getEnv(L));                   \
-                               if (env == NULL) return 0
+#include "voxelalgorithms.h"
 
 // garbage collector
 int LuaVoxelManip::gc_object(lua_State *L)
@@ -43,11 +40,13 @@ int LuaVoxelManip::gc_object(lua_State *L)
 
 int LuaVoxelManip::l_read_from_map(lua_State *L)
 {
+       MAP_LOCK_REQUIRED;
+
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
-       v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
-       v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
+       v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
+       v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 3));
        sortBoxVerticies(bp1, bp2);
 
        vm->initialEmerge(bp1, bp2);
@@ -63,12 +62,18 @@ int LuaVoxelManip::l_get_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       bool use_buffer  = lua_istable(L, 2);
 
-       int volume = vm->m_area.getVolume();
+       MMVManip *vm = o->vm;
 
-       lua_newtable(L);
-       for (int i = 0; i != volume; i++) {
+       u32 volume = vm->m_area.getVolume();
+
+       if (use_buffer)
+               lua_pushvalue(L, 2);
+       else
+               lua_newtable(L);
+
+       for (u32 i = 0; i != volume; i++) {
                lua_Integer cid = vm->m_data[i].getContent();
                lua_pushinteger(L, cid);
                lua_rawseti(L, -2, i + 1);
@@ -82,13 +87,13 @@ int LuaVoxelManip::l_set_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
                return 0;
 
-       int volume = vm->m_area.getVolume();
-       for (int i = 0; i != volume; i++) {
+       u32 volume = vm->m_area.getVolume();
+       for (u32 i = 0; i != volume; i++) {
                lua_rawgeti(L, 2, i + 1);
                content_t c = lua_tointeger(L, -1);
 
@@ -102,34 +107,53 @@ 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);
-       ManualMapVoxelManipulator *vm = o->vm;
+       bool update_light = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true;
+       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 (std::map<v3s16, MapBlock *>::iterator it = o->modified_blocks.begin();
+                       it != o->modified_blocks.end(); ++it)
+               event.modified_blocks.insert(it->first);
 
-       vm->blitBackAll(&o->modified_blocks);
+       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;
+
+       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
-       v3s16 pos        = read_v3s16(L, 2);
+       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;
+
+       INodeDefManager *ndef = getServer(L)->getNodeDefManager();
 
        LuaVoxelManip *o = checkobject(L, 1);
-       v3s16 pos        = read_v3s16(L, 2);
-       MapNode n        = readnode(L, 3, env->getGameDef()->ndef());
+       v3s16 pos        = check_v3s16(L, 2);
+       MapNode n        = readnode(L, 3, ndef);
 
        o->vm->setNodeNoEmerge(pos, n);
 
@@ -144,7 +168,7 @@ int LuaVoxelManip::l_update_liquids(lua_State *L)
 
        Map *map = &(env->getMap());
        INodeDefManager *ndef = getServer(L)->getNodeDefManager();
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
        Mapgen mg;
        mg.vm   = vm;
@@ -166,24 +190,25 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
 
        INodeDefManager *ndef = getServer(L)->getNodeDefManager();
        EmergeManager *emerge = getServer(L)->getEmergeManager();
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
+
+       v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
+       v3s16 fpmin  = vm->m_area.MinEdge;
+       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;
 
-       v3s16 p1 = lua_istable(L, 2) ? read_v3s16(L, 2) : vm->m_area.MinEdge;
-       v3s16 p2 = lua_istable(L, 3) ? read_v3s16(L, 3) : vm->m_area.MaxEdge;
-       sortBoxVerticies(p1, p2);
-       if (!vm->m_area.contains(VoxelArea(p1, p2)))
+       sortBoxVerticies(pmin, pmax);
+       if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
                throw LuaError("Specified voxel area out of VoxelManipulator bounds");
 
        Mapgen mg;
        mg.vm          = vm;
        mg.ndef        = ndef;
-       mg.water_level = emerge->params.water_level;
+       mg.water_level = emerge->mgparams->water_level;
 
-       // Mapgen::calcLighting assumes the coordinates of
-       // the central chunk; correct for this
-       mg.calcLighting(
-               p1 + v3s16(1, 1, 1) * MAP_BLOCKSIZE,
-               p2 - v3s16(1, 1, 1) * MAP_BLOCKSIZE);
+       mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
 
        return 0;
 }
@@ -203,18 +228,20 @@ int LuaVoxelManip::l_set_lighting(lua_State *L)
        light  = (getintfield_default(L, 2, "day",   0) & 0x0F);
        light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
 
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
+
+       v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
+       v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
+       v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
 
-       v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) : vm->m_area.MinEdge;
-       v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) : vm->m_area.MaxEdge;
-       sortBoxVerticies(p1, p2);
-       if (!vm->m_area.contains(VoxelArea(p1, p2)))
+       sortBoxVerticies(pmin, pmax);
+       if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
                throw LuaError("Specified voxel area out of VoxelManipulator bounds");
 
        Mapgen mg;
        mg.vm = vm;
 
-       mg.setLighting(p1, p2, light);
+       mg.setLighting(light, pmin, pmax);
 
        return 0;
 }
@@ -224,12 +251,12 @@ int LuaVoxelManip::l_get_light_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
-       int volume = vm->m_area.getVolume();
+       u32 volume = vm->m_area.getVolume();
 
        lua_newtable(L);
-       for (int i = 0; i != volume; i++) {
+       for (u32 i = 0; i != volume; i++) {
                lua_Integer light = vm->m_data[i].param1;
                lua_pushinteger(L, light);
                lua_rawseti(L, -2, i + 1);
@@ -243,13 +270,13 @@ int LuaVoxelManip::l_set_light_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
                return 0;
 
-       int volume = vm->m_area.getVolume();
-       for (int i = 0; i != volume; i++) {
+       u32 volume = vm->m_area.getVolume();
+       for (u32 i = 0; i != volume; i++) {
                lua_rawgeti(L, 2, i + 1);
                u8 light = lua_tointeger(L, -1);
 
@@ -266,12 +293,18 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       bool use_buffer  = lua_istable(L, 2);
 
-       int volume = vm->m_area.getVolume();
+       MMVManip *vm = o->vm;
 
-       lua_newtable(L);
-       for (int i = 0; i != volume; i++) {
+       u32 volume = vm->m_area.getVolume();
+
+       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);
                lua_rawseti(L, -2, i + 1);
@@ -285,13 +318,13 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
        if (!lua_istable(L, 2))
                return 0;
 
-       int volume = vm->m_area.getVolume();
-       for (int i = 0; i != volume; i++) {
+       u32 volume = vm->m_area.getVolume();
+       for (u32 i = 0; i != volume; i++) {
                lua_rawgeti(L, 2, i + 1);
                u8 param2 = lua_tointeger(L, -1);
 
@@ -305,35 +338,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;
 }
 
@@ -342,7 +346,7 @@ int LuaVoxelManip::l_was_modified(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaVoxelManip *o = checkobject(L, 1);
-       ManualMapVoxelManipulator *vm = o->vm;
+       MMVManip *vm = o->vm;
 
        lua_pushboolean(L, vm->m_is_dirty);
 
@@ -351,6 +355,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);
@@ -359,7 +365,7 @@ int LuaVoxelManip::l_get_emerged_area(lua_State *L)
        return 2;
 }
 
-LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
+LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
 {
        this->vm           = mmvm;
        this->is_mapgen_vm = is_mg_vm;
@@ -367,13 +373,13 @@ LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
 
 LuaVoxelManip::LuaVoxelManip(Map *map)
 {
-       this->vm = new ManualMapVoxelManipulator(map);
+       this->vm = new MMVManip(map);
        this->is_mapgen_vm = false;
 }
 
 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
 {
-       this->vm = new ManualMapVoxelManipulator(map);
+       this->vm = new MMVManip(map);
        this->is_mapgen_vm = false;
 
        v3s16 bp1 = getNodeBlockPos(p1);
@@ -392,15 +398,11 @@ 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)) ?
-               new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) :
+               new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
                new LuaVoxelManip(map);
 
        *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
@@ -451,7 +453,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),