]> git.lizzy.rs Git - worldedit.git/blob - worldedit/serialization.lua
Rename //find to //highlight and worldedit.find to worldedit.highlight, add //allocat...
[worldedit.git] / worldedit / serialization.lua
1 worldedit = worldedit or {}\r
2 \r
3 dofile(minetest.get_modpath("worldedit") .. "/table_save.lua") --wip: remove dependency\r
4 \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
11         end\r
12         if pos1.y > pos2.y then\r
13                 pos2.y, pos1.y = pos1.y, pos2.y\r
14         end\r
15         if pos1.z > pos2.z then\r
16                 pos2.z, pos1.z = pos1.z, pos2.z\r
17         end\r
18         return pos1, pos2\r
19 end\r
20 \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
25         local count = 0\r
26         local result = {}\r
27         local env = minetest.env\r
28         while pos.x <= pos2.x do\r
29                 pos.y = pos1.y\r
30                 while pos.y <= pos2.y do\r
31                         pos.z = pos1.z\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
35                                         count = count + 1\r
36                                         result[count] = pos.x - pos1.x .. " " .. pos.y - pos1.y .. " " .. pos.z - pos1.z .. " " .. node.name .. " " .. node.param1 .. " " .. node.param2\r
37                                 end\r
38                                 pos.z = pos.z + 1\r
39                         end\r
40                         pos.y = pos.y + 1\r
41                 end\r
42                 pos.x = pos.x + 1\r
43         end\r
44         result = table.concat(result, "\n") --join all node entries into single string\r
45         return result, count\r
46 end\r
47 \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
54         local count = 0\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
57                 if x < pos1.x then\r
58                         pos1.x = x\r
59                 end\r
60                 if y < pos1.y then\r
61                         pos1.y = y\r
62                 end\r
63                 if z < pos1.z then\r
64                         pos1.z = z\r
65                 end\r
66                 if x > pos2.x then\r
67                         pos2.x = x\r
68                 end\r
69                 if y > pos2.y then\r
70                         pos2.y = y\r
71                 end\r
72                 if z > pos2.z then\r
73                         pos2.z = z\r
74                 end\r
75                 count = count + 1\r
76         end\r
77         return pos1, pos2, count\r
78 end\r
79 \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
85         local count = 0\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
91                 node.name = name\r
92                 node.param1 = param1\r
93                 node.param2 = param2\r
94                 env:add_node(pos, node)\r
95                 count = count + 1\r
96         end\r
97         return count\r
98 end\r
99 \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
104         local count = 0\r
105         local get_tables = loadstring(value)\r
106         if get_tables == nil then --error loading value\r
107                 return count\r
108         end\r
109         local tables = get_tables()\r
110 \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
116                         end\r
117                 end\r
118         end\r
119 \r
120         --load the node array\r
121         local env = minetest.env\r
122         for i, v in ipairs(tables[1]) do\r
123                 local pos = v[1]\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
126                 count = count + 1\r
127         end\r
128         return count\r
129 end\r
130 \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
136         local rows = {}\r
137         local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
138         local pos = {x=pos1.x, y=0, z=0}\r
139         local count = 0\r
140         local result = {}\r
141         local env = minetest.env\r
142         while pos.x <= pos2.x do\r
143                 pos.y = pos1.y\r
144                 while pos.y <= pos2.y do\r
145                         pos.z = pos1.z\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
149                                         count = count + 1\r
150                                         local row = {\r
151                                                 x = pos.x-pos1.x,\r
152                                                 y = pos.y-pos1.y,\r
153                                                 z = pos.z-pos1.z,\r
154                                                 name = node.name,\r
155                                                 param1 = node.param1,\r
156                                                 param2 = node.param2,\r
157                                                 meta = env:get_meta(pos):to_table(),\r
158                                         }\r
159                                         table.insert(rows, row)\r
160                                 end\r
161                                 pos.z = pos.z + 1\r
162                         end\r
163                         pos.y = pos.y + 1\r
164                 end\r
165                 pos.x = pos.x + 1\r
166         end\r
167         local err = table.save(rows,filename)\r
168         if err then return _,err end\r
169         return count\r
170 end\r
171 \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
179         local count = 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
190                 count = count + 1\r
191         end\r
192         return count\r
193 end