1 worldedit = worldedit or {}
\r
3 dofile(minetest.get_modpath("worldedit") .. "/table_save.lua") --wip: remove dependency
\r
5 --modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions
\r
6 worldedit.sort_pos = function(pos1, pos2)
\r
7 pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
\r
8 pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
\r
9 if pos1.x > pos2.x then
\r
10 pos2.x, pos1.x = pos1.x, pos2.x
\r
12 if pos1.y > pos2.y then
\r
13 pos2.y, pos1.y = pos1.y, pos2.y
\r
15 if pos1.z > pos2.z then
\r
16 pos2.z, pos1.z = pos1.z, pos2.z
\r
21 --converts the region defined by positions `pos1` and `pos2` into a single string, returning the serialized data and the number of nodes serialized
\r
22 worldedit.serialize = function(pos1, pos2)
\r
23 local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
\r
24 local pos = {x=pos1.x, y=0, z=0}
\r
27 local env = minetest.env
\r
28 while pos.x <= pos2.x do
\r
30 while pos.y <= pos2.y do
\r
32 while pos.z <= pos2.z do
\r
33 local node = env:get_node(pos)
\r
34 if node.name ~= "air" and node.name ~= "ignore" then
\r
36 result[count] = pos.x - pos1.x .. " " .. pos.y - pos1.y .. " " .. pos.z - pos1.z .. " " .. node.name .. " " .. node.param1 .. " " .. node.param2
\r
44 result = table.concat(result, "\n") --join all node entries into single string
\r
45 return result, count
\r
48 --determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`, returning the two corner positions and the number of nodes
\r
49 worldedit.allocate = function(originpos, value)
\r
50 local huge = math.huge
\r
51 local pos1 = {x=huge, y=huge, z=huge}
\r
52 local pos2 = {x=-huge, y=-huge, z=-huge}
\r
53 local originx, originy, originz = originpos.x, originpos.y, originpos.z
\r
55 for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries
\r
56 x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)
\r
77 return pos1, pos2, count
\r
80 --loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized
\r
81 worldedit.deserialize = function(originpos, value)
\r
82 local pos = {x=0, y=0, z=0}
\r
83 local node = {name="", param1=0, param2=0}
\r
84 local originx, originy, originz = originpos.x, originpos.y, originpos.z
\r
86 local env = minetest.env
\r
87 for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries
\r
88 pos.x = originx + tonumber(x)
\r
89 pos.y = originy + tonumber(y)
\r
90 pos.z = originz + tonumber(z)
\r
92 node.param1 = param1
\r
93 node.param2 = param2
\r
94 env:add_node(pos, node)
\r
100 --loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized
\r
101 --based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)
\r
102 worldedit.deserialize_old = function(originpos, value)
\r
103 --obtain the node table
\r
105 local get_tables = loadstring(value)
\r
106 if get_tables == nil then --error loading value
\r
109 local tables = get_tables()
\r
111 --transform the node table into an array of nodes
\r
112 for i = 1, #tables do
\r
113 for j, v in pairs(tables[i]) do
\r
114 if type(v) == "table" then
\r
115 tables[i][j] = tables[v[1]]
\r
120 --load the node array
\r
121 local env = minetest.env
\r
122 for i, v in ipairs(tables[1]) do
\r
124 pos.x, pos.y, pos.z = originpos.x + pos.x, originpos.y + pos.y, originpos.z + pos.z
\r
125 env:add_node(pos, v[2])
\r
131 --saves the nodes and meta defined by positions `pos1` and `pos2` into a file, returning the number of nodes saved
\r
132 worldedit.metasave = function(pos1, pos2, file) --wip: simply work with strings instead of doing IO
\r
133 local path = minetest.get_worldpath() .. "/schems"
\r
134 local filename = path .. "/" .. file .. ".wem"
\r
135 os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
\r
137 local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
\r
138 local pos = {x=pos1.x, y=0, z=0}
\r
141 local env = minetest.env
\r
142 while pos.x <= pos2.x do
\r
144 while pos.y <= pos2.y do
\r
146 while pos.z <= pos2.z do
\r
147 local node = env:get_node(pos)
\r
148 if node.name ~= "air" and node.name ~= "ignore" then
\r
155 param1 = node.param1,
\r
156 param2 = node.param2,
\r
157 meta = env:get_meta(pos):to_table(),
\r
159 table.insert(rows, row)
\r
167 local err = table.save(rows,filename)
\r
168 if err then return _,err end
\r
172 --loads the nodes and meta from `file` to position `pos1`, returning the number of nodes loaded
\r
173 worldedit.metaload = function(pos1, file) --wip: simply work with strings instead of doing IO
\r
174 local filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"
\r
175 local rows, err = table.load(filename)
\r
176 if err then return _,err end
\r
177 local pos = {x=0, y=0, z=0}
\r
178 local node = {name="", param1=0, param2=0}
\r
180 local env = minetest.env
\r
181 for i,row in pairs(rows) do
\r
182 pos.x = pos1.x + tonumber(row.x)
\r
183 pos.y = pos1.y + tonumber(row.y)
\r
184 pos.z = pos1.z + tonumber(row.z)
\r
185 node.name = row.name
\r
186 node.param1 = row.param1
\r
187 node.param2 = row.param2
\r
188 env:add_node(pos, node)
\r
189 env:get_meta(pos):from_table(row.meta)
\r