From 6b3169e4d0ebd82661d7cca9e3a381124a897f1a Mon Sep 17 00:00:00 2001
From: kwolekr <kwolekr@minetest.net>
Date: Thu, 27 Jun 2013 21:12:44 -0400
Subject: [PATCH] LuaVoxelManip: Separate VoxelManip data get/set from
 emerging/blitting data back to map

---
 doc/lua_api.txt                 | 22 +++++++++-----
 src/script/lua_api/l_env.cpp    | 11 +------
 src/script/lua_api/l_vmanip.cpp | 54 ++++++++++++++++++++++-----------
 src/script/lua_api/l_vmanip.h   |  7 +++--
 4 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index a4e1372da..622d292c4 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1553,17 +1553,23 @@ VoxelManip: An interface to the MapVoxelManipulator for Lua
 - Can be created via VoxelManip()
 - Also minetest.get_voxel_manip()
 methods:
-- read_chunk(p1, p2):  Read a chunk of map containing the region formed by p1 and p2.
-  ^ returns raw node data, actual emerged p1, actual emerged p2
-  ^ raw node data is in the form of a table mapping indicies to node content ids
-- write_chunk(data):  Write back the data 
-- update_map():  Update map after writing chunk.
-  ^ To be used only by VoxelManip objects created by the mod itself; not VoxelManips passed to callbacks
+- read_from_map(p1, p2):  Reads a chunk of map from the map containing the region formed by p1 and p2.
+  ^ returns actual emerged pmin, actual emerged pmax
+- write_to_map():  Writes the data loaded from the VoxelManip back to the map.
+  ^ important: data must be set using VoxelManip:set_data before calling this
+- get_data():  Gets the data read into the VoxelManip object
+  ^ returns raw node data is in the form of an array of node content ids
+- set_data(data):  Sets the data contents of the VoxelManip object
+- update_map():  Update map after writing chunk back to map.
+  ^ To be used only by VoxelManip objects created by the mod itself; not a VoxelManip that was 
+  ^ retrieved from minetest.get_mapgen_object
 - set_lighting(p1, p2, light):  Set the lighting in the region formed by p1 and p2 to light
   ^ light is a table containing two integer fields ranging from 0 to 15, day and night
-  ^ To be used only by VoxelManip objects passed to a callback; otherwise, set lighting will be ignored
+  ^ To be used only by a VoxelManip object from minetest.get_mapgen_object; otherwise, set lighting will
+  ^ be ignored
 - calc_lighting(p1, p2):  Calculate lighting in the region formed by p1 and p2
-  ^ To be used only by VoxelManip objects passed to a callback; otherwise, calculated lighting will be ignored
+  ^ To be used only by a VoxelManip object from minetest.get_mapgen_object; otherwise, calculated lighting 
+  ^ will be ignored
 - update_liquids():  Update liquid flow
 
 Mapgen objects
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index b6bafbcd9..02cafc0d5 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -594,22 +594,13 @@ int ModApiEnvMod::l_get_mapgen_object(lua_State *L)
 			luaL_getmetatable(L, "VoxelManip");
 			lua_setmetatable(L, -2);
 			
-			// VoxelManip data
-			int volume = vm->m_area.getVolume();
-			lua_newtable(L);
-			for (int i = 0; i != volume; i++) {
-				lua_Number cid = vm->m_data[i].getContent();
-				lua_pushnumber(L, cid);
-				lua_rawseti(L, -2, i + 1);
-			}
-			
 			// emerged min pos
 			push_v3s16(L, vm->m_area.MinEdge);
 
 			// emerged max pos
 			push_v3s16(L, vm->m_area.MaxEdge);
 			
-			nargs = 4;
+			nargs = 3;
 			
 			break; }
 		case MGOBJ_HEIGHTMAP: {
diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp
index 6743f40f9..f753f5f27 100644
--- a/src/script/lua_api/l_vmanip.cpp
+++ b/src/script/lua_api/l_vmanip.cpp
@@ -39,42 +39,52 @@ int LuaVoxelManip::gc_object(lua_State *L)
 	return 0;
 }
 
-int LuaVoxelManip::l_read_chunk(lua_State *L)
+int LuaVoxelManip::l_read_from_map(lua_State *L)
 {
 	LuaVoxelManip *o = checkobject(L, 1);
+	ManualMapVoxelManipulator *vm = o->vm;
 	
 	v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
 	v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
 	sortBoxVerticies(bp1, bp2);
-	ManualMapVoxelManipulator *vm = o->vm;
+	
 	vm->initialEmerge(bp1, bp2);
 	
-	v3s16 emerged_p1 = vm->m_area.MinEdge;
-	v3s16 emerged_p2 = vm->m_area.MaxEdge;
+	push_v3s16(L, vm->m_area.MinEdge);
+	push_v3s16(L, vm->m_area.MaxEdge);
+	
+	return 2;
+}
+
+int LuaVoxelManip::l_get_data(lua_State *L)
+{
+	NO_MAP_LOCK_REQUIRED;
+
+	LuaVoxelManip *o = checkobject(L, 1);
+	ManualMapVoxelManipulator *vm = o->vm;
 	
 	int volume = vm->m_area.getVolume();
 	
 	lua_newtable(L);
 	for (int i = 0; i != volume; i++) {
-		lua_Number cid = vm->m_data[i].getContent();
-		lua_pushnumber(L, cid);
+		lua_Integer cid = vm->m_data[i].getContent();
+		lua_pushinteger(L, cid);
 		lua_rawseti(L, -2, i + 1);
 	}
 	
-	push_v3s16(L, emerged_p1);
-	push_v3s16(L, emerged_p2);
-	
-	return 3;
+	return 1;
 }
 
-int LuaVoxelManip::l_write_chunk(lua_State *L)
+int LuaVoxelManip::l_set_data(lua_State *L)
 {
+	NO_MAP_LOCK_REQUIRED;
+	
 	LuaVoxelManip *o = checkobject(L, 1);
+	ManualMapVoxelManipulator *vm = o->vm;
+	
 	if (!lua_istable(L, 2))
 		return 0;
 	
-	ManualMapVoxelManipulator *vm = o->vm;
-	
 	int volume = vm->m_area.getVolume();
 	for (int i = 0; i != volume; i++) {
 		lua_rawgeti(L, 2, i + 1);
@@ -84,10 +94,18 @@ int LuaVoxelManip::l_write_chunk(lua_State *L)
 
 		lua_pop(L, 1);
 	}
+		
+	return 0;
+}
+
+int LuaVoxelManip::l_write_to_map(lua_State *L)
+{
+	LuaVoxelManip *o = checkobject(L, 1);
+	ManualMapVoxelManipulator *vm = o->vm;
 
 	vm->blitBackAll(&o->modified_blocks);
-	
-	return 0;
+
+	return 0;	
 }
 
 int LuaVoxelManip::l_update_liquids(lua_State *L)
@@ -258,8 +276,10 @@ void LuaVoxelManip::Register(lua_State *L)
 
 const char LuaVoxelManip::className[] = "VoxelManip";
 const luaL_reg LuaVoxelManip::methods[] = {
-	luamethod(LuaVoxelManip, read_chunk),
-	luamethod(LuaVoxelManip, write_chunk),
+	luamethod(LuaVoxelManip, read_from_map),
+	luamethod(LuaVoxelManip, get_data),
+	luamethod(LuaVoxelManip, set_data),
+	luamethod(LuaVoxelManip, write_to_map),
 	luamethod(LuaVoxelManip, update_map),
 	luamethod(LuaVoxelManip, update_liquids),
 	luamethod(LuaVoxelManip, calc_lighting),
diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h
index 5a57d6bfa..7720ec040 100644
--- a/src/script/lua_api/l_vmanip.h
+++ b/src/script/lua_api/l_vmanip.h
@@ -43,8 +43,11 @@ class LuaVoxelManip
 
 	static int gc_object(lua_State *L);
 
-	static int l_read_chunk(lua_State *L);
-	static int l_write_chunk(lua_State *L);
+	static int l_read_from_map(lua_State *L);
+	static int l_get_data(lua_State *L);
+	static int l_set_data(lua_State *L);
+	static int l_write_to_map(lua_State *L);
+
 	static int l_update_map(lua_State *L);
 	static int l_update_liquids(lua_State *L);
 	static int l_calc_lighting(lua_State *L);
-- 
GitLab