]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/init.lua
Dual-based cylinder
[worldedit.git] / worldedit_commands / init.lua
1 minetest.register_privilege("worldedit", "Can use WorldEdit commands")\r
2 \r
3 worldedit.set_pos = {}\r
4 worldedit.inspect = {}\r
5 \r
6 worldedit.pos1 = {}\r
7 worldedit.pos2 = {}\r
8 if minetest.place_schematic then\r
9         worldedit.prob_pos = {}\r
10         worldedit.prob_list = {}\r
11 end\r
12 \r
13 dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua")\r
14 dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua")\r
15 dofile(minetest.get_modpath("worldedit_commands") .. "/wand.lua")\r
16 local safe_region, check_region, reset_pending = dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua")\r
17 \r
18 local function get_position(name) --position 1 retrieval function for when not using `safe_region`\r
19         local pos1 = worldedit.pos1[name]\r
20         if pos1 == nil then\r
21                 worldedit.player_notify(name, "no position 1 selected")\r
22         end\r
23         return pos1\r
24 end\r
25 \r
26 local function get_node(name, nodename)\r
27         local node = worldedit.normalize_nodename(nodename)\r
28         if not node then\r
29                 worldedit.player_notify(name, "invalid node name: " .. nodename)\r
30                 return nil\r
31         end\r
32         return node\r
33 end\r
34 \r
35 function worldedit.player_notify(name, message)\r
36         minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)\r
37 end\r
38 \r
39 --determines whether `nodename` is a valid node name, returning a boolean\r
40 worldedit.normalize_nodename = function(nodename)\r
41         nodename = nodename:gsub("^%s*(.-)%s*$", "%1")\r
42         if nodename == "" then return nil end\r
43         local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names\r
44         if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename\r
45                 return fullname\r
46         end\r
47         for key, value in pairs(minetest.registered_nodes) do\r
48                 if key:find(":" .. nodename, 1, true) then --found in mod\r
49                         return key\r
50                 end\r
51         end\r
52         nodename = nodename:lower() --lowercase both for case insensitive comparison\r
53         for key, value in pairs(minetest.registered_nodes) do\r
54                 if value.description:lower() == nodename then --found in description\r
55                         return key\r
56                 end\r
57         end\r
58         return nil\r
59 end\r
60 \r
61 -- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)\r
62 function worldedit.player_axis(name)\r
63         local dir = minetest.get_player_by_name(name):get_look_dir()\r
64         local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z)\r
65         if x > y then\r
66                 if x > z then\r
67                         return "x", dir.x > 0 and 1 or -1\r
68                 end\r
69         elseif y > z then\r
70                 return "y", dir.y > 0 and 1 or -1\r
71         end\r
72         return "z", dir.z > 0 and 1 or -1\r
73 end\r
74 \r
75 local function mkdir(path)\r
76         if minetest.mkdir then\r
77                 minetest.mkdir(path)\r
78         else\r
79                 os.execute('mkdir "' .. path .. '"')\r
80         end\r
81 end\r
82 \r
83 local function check_filename(name)\r
84         return name:find("^[%w%s%^&'@{}%[%],%$=!%-#%(%)%%%.%+~_]+$") ~= nil\r
85 end\r
86 \r
87 \r
88 minetest.register_chatcommand("/about", {\r
89         params = "",\r
90         description = "Get information about the mod",\r
91         func = function(name, param)\r
92                 worldedit.player_notify(name, "WorldEdit " .. worldedit.version_string .. " is available on this server. Type /help to get a list of commands, or get more information at https://github.com/Uberi/MineTest-WorldEdit/")\r
93         end,\r
94 })\r
95 \r
96 -- mostly copied from builtin/chatcommands.lua with minor modifications\r
97 minetest.register_chatcommand("/help", {\r
98         privs = {},\r
99         params = "[all/<cmd>]",\r
100         description = "Get help for WorldEdit commands",\r
101         func = function(name, param)\r
102                 local function is_we_command(cmd)\r
103                         return cmd:sub(0, 1) == "/"\r
104                 end\r
105                 local function format_help_line(cmd, def)\r
106                         local msg = minetest.colorize("#00ffff", "/"..cmd)\r
107                         if def.params and def.params ~= "" then\r
108                                 msg = msg .. " " .. def.params\r
109                         end\r
110                         if def.description and def.description ~= "" then\r
111                                 msg = msg .. ": " .. def.description\r
112                         end\r
113                         return msg\r
114                 end\r
115 \r
116                 if not minetest.check_player_privs(name, "worldedit") then\r
117                         return false, "You are not allowed to use any WorldEdit commands."\r
118                 end\r
119                 if param == "" then\r
120                         local msg = ""\r
121                         local cmds = {}\r
122                         for cmd, def in pairs(minetest.chatcommands) do\r
123                                 if is_we_command(cmd) and minetest.check_player_privs(name, def.privs) then\r
124                                         cmds[#cmds + 1] = cmd:sub(2) -- strip the /\r
125                                 end\r
126                         end\r
127                         table.sort(cmds)\r
128                         return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"\r
129                                         .. "Use '//help <cmd>' to get more information,"\r
130                                         .. " or '//help all' to list everything."\r
131                 elseif param == "all" then\r
132                         local cmds = {}\r
133                         for cmd, def in pairs(minetest.chatcommands) do\r
134                                 if is_we_command(cmd) and minetest.check_player_privs(name, def.privs) then\r
135                                         cmds[#cmds + 1] = format_help_line(cmd, def)\r
136                                 end\r
137                         end\r
138                         table.sort(cmds)\r
139                         return true, "Available commands:\n"..table.concat(cmds, "\n")\r
140                 else\r
141                         return minetest.chatcommands["help"].func(name, "/" .. param)\r
142                 end\r
143         end,\r
144 })\r
145 \r
146 minetest.register_chatcommand("/inspect", {\r
147         params = "on/off/1/0/true/false/yes/no/enable/disable/<blank>",\r
148         description = "Enable or disable node inspection",\r
149         privs = {worldedit=true},\r
150         func = function(name, param)\r
151                 if param == "on" or param == "1" or param == "true" or param == "yes" or param == "enable" or param == "" then\r
152                         worldedit.inspect[name] = true\r
153                         local axis, sign = worldedit.player_axis(name)\r
154                         worldedit.player_notify(name, string.format("inspector: inspection enabled for %s, currently facing the %s axis",\r
155                                 name, axis .. (sign > 0 and "+" or "-")))\r
156                 elseif param == "off" or param == "0" or param == "false" or param == "no" or param == "disable" then\r
157                         worldedit.inspect[name] = nil\r
158                         worldedit.player_notify(name, "inspector: inspection disabled")\r
159                 else\r
160                         worldedit.player_notify(name, "invalid usage: " .. param)\r
161                 end\r
162         end,\r
163 })\r
164 \r
165 local function get_node_rlight(pos)\r
166         local vecs = { -- neighboring nodes\r
167                 {x= 1, y= 0, z= 0},\r
168                 {x=-1, y= 0, z= 0},\r
169                 {x= 0, y= 1, z= 0},\r
170                 {x= 0, y=-1, z= 0},\r
171                 {x= 0, y= 0, z= 1},\r
172                 {x= 0, y= 0, z=-1},\r
173         }\r
174         local ret = 0\r
175         for _, v in ipairs(vecs) do\r
176                 ret = math.max(ret, minetest.get_node_light(vector.add(pos, v)))\r
177         end\r
178         return ret\r
179 end\r
180 \r
181 minetest.register_on_punchnode(function(pos, node, puncher)\r
182         local name = puncher:get_player_name()\r
183         if worldedit.inspect[name] then\r
184                 local axis, sign = worldedit.player_axis(name)\r
185                 message = string.format("inspector: %s at %s (param1=%d, param2=%d, received light=%d) punched facing the %s axis",\r
186                         node.name, minetest.pos_to_string(pos), node.param1, node.param2, get_node_rlight(pos), axis .. (sign > 0 and "+" or "-"))\r
187                 worldedit.player_notify(name, message)\r
188         end\r
189 end)\r
190 \r
191 minetest.register_chatcommand("/reset", {\r
192         params = "",\r
193         description = "Reset the region so that it is empty",\r
194         privs = {worldedit=true},\r
195         func = function(name, param)\r
196                 worldedit.pos1[name] = nil\r
197                 worldedit.pos2[name] = nil\r
198                 worldedit.mark_pos1(name)\r
199                 worldedit.mark_pos2(name)\r
200                 worldedit.set_pos[name] = nil\r
201                 --make sure the user does not try to confirm an operation after resetting pos:\r
202                 reset_pending(name)\r
203                 worldedit.player_notify(name, "region reset")\r
204         end,\r
205 })\r
206 \r
207 minetest.register_chatcommand("/mark", {\r
208         params = "",\r
209         description = "Show markers at the region positions",\r
210         privs = {worldedit=true},\r
211         func = function(name, param)\r
212                 worldedit.mark_pos1(name)\r
213                 worldedit.mark_pos2(name)\r
214                 worldedit.player_notify(name, "region marked")\r
215         end,\r
216 })\r
217 \r
218 minetest.register_chatcommand("/unmark", {\r
219         params = "",\r
220         description = "Hide markers if currently shown",\r
221         privs = {worldedit=true},\r
222         func = function(name, param)\r
223                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
224                 worldedit.pos1[name] = nil\r
225                 worldedit.pos2[name] = nil\r
226                 worldedit.mark_pos1(name)\r
227                 worldedit.mark_pos2(name)\r
228                 worldedit.pos1[name] = pos1\r
229                 worldedit.pos2[name] = pos2\r
230                 worldedit.player_notify(name, "region unmarked")\r
231         end,\r
232 })\r
233 \r
234 minetest.register_chatcommand("/pos1", {\r
235         params = "",\r
236         description = "Set WorldEdit region position 1 to the player's location",\r
237         privs = {worldedit=true},\r
238         func = function(name, param)\r
239                 local pos = minetest.get_player_by_name(name):getpos()\r
240                 pos.x, pos.y, pos.z = math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5)\r
241                 worldedit.pos1[name] = pos\r
242                 worldedit.mark_pos1(name)\r
243                 worldedit.player_notify(name, "position 1 set to " .. minetest.pos_to_string(pos))\r
244         end,\r
245 })\r
246 \r
247 minetest.register_chatcommand("/pos2", {\r
248         params = "",\r
249         description = "Set WorldEdit region position 2 to the player's location",\r
250         privs = {worldedit=true},\r
251         func = function(name, param)\r
252                 local pos = minetest.get_player_by_name(name):getpos()\r
253                 pos.x, pos.y, pos.z = math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5)\r
254                 worldedit.pos2[name] = pos\r
255                 worldedit.mark_pos2(name)\r
256                 worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos))\r
257         end,\r
258 })\r
259 \r
260 minetest.register_chatcommand("/p", {\r
261         params = "set/set1/set2/get",\r
262         description = "Set WorldEdit region, WorldEdit position 1, or WorldEdit position 2 by punching nodes, or display the current WorldEdit region",\r
263         privs = {worldedit=true},\r
264         func = function(name, param)\r
265                 if param == "set" then --set both WorldEdit positions\r
266                         worldedit.set_pos[name] = "pos1"\r
267                         worldedit.player_notify(name, "select positions by punching two nodes")\r
268                 elseif param == "set1" then --set WorldEdit position 1\r
269                         worldedit.set_pos[name] = "pos1only"\r
270                         worldedit.player_notify(name, "select position 1 by punching a node")\r
271                 elseif param == "set2" then --set WorldEdit position 2\r
272                         worldedit.set_pos[name] = "pos2"\r
273                         worldedit.player_notify(name, "select position 2 by punching a node")\r
274                 elseif param == "get" then --display current WorldEdit positions\r
275                         if worldedit.pos1[name] ~= nil then\r
276                                 worldedit.player_notify(name, "position 1: " .. minetest.pos_to_string(worldedit.pos1[name]))\r
277                         else\r
278                                 worldedit.player_notify(name, "position 1 not set")\r
279                         end\r
280                         if worldedit.pos2[name] ~= nil then\r
281                                 worldedit.player_notify(name, "position 2: " .. minetest.pos_to_string(worldedit.pos2[name]))\r
282                         else\r
283                                 worldedit.player_notify(name, "position 2 not set")\r
284                         end\r
285                 else\r
286                         worldedit.player_notify(name, "unknown subcommand: " .. param)\r
287                 end\r
288         end,\r
289 })\r
290 \r
291 minetest.register_chatcommand("/fixedpos", {\r
292         params = "set1/set2 x y z",\r
293         description = "Set a WorldEdit region position to the position at (<x>, <y>, <z>)",\r
294         privs = {worldedit=true},\r
295         func = function(name, param)\r
296                 local found, _, flag, x, y, z = param:find("^(set[12])%s+([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)$")\r
297                 if found == nil then\r
298                         worldedit.player_notify(name, "invalid usage: " .. param)\r
299                         return\r
300                 end\r
301                 local pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}\r
302                 if flag == "set1" then\r
303                         worldedit.pos1[name] = pos\r
304                         worldedit.mark_pos1(name)\r
305                         worldedit.player_notify(name, "position 1 set to " .. minetest.pos_to_string(pos))\r
306                 else --flag == "set2"\r
307                         worldedit.pos2[name] = pos\r
308                         worldedit.mark_pos2(name)\r
309                         worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos))\r
310                 end\r
311         end,\r
312 })\r
313 \r
314 minetest.register_on_punchnode(function(pos, node, puncher)\r
315         local name = puncher:get_player_name()\r
316         if name ~= "" and worldedit.set_pos[name] ~= nil then --currently setting position\r
317                 if worldedit.set_pos[name] == "pos1" then --setting position 1\r
318                         worldedit.pos1[name] = pos\r
319                         worldedit.mark_pos1(name)\r
320                         worldedit.set_pos[name] = "pos2" --set position 2 on the next invocation\r
321                         worldedit.player_notify(name, "position 1 set to " .. minetest.pos_to_string(pos))\r
322                 elseif worldedit.set_pos[name] == "pos1only" then --setting position 1 only\r
323                         worldedit.pos1[name] = pos\r
324                         worldedit.mark_pos1(name)\r
325                         worldedit.set_pos[name] = nil --finished setting positions\r
326                         worldedit.player_notify(name, "position 1 set to " .. minetest.pos_to_string(pos))\r
327                 elseif worldedit.set_pos[name] == "pos2" then --setting position 2\r
328                         worldedit.pos2[name] = pos\r
329                         worldedit.mark_pos2(name)\r
330                         worldedit.set_pos[name] = nil --finished setting positions\r
331                         worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos))\r
332                 elseif worldedit.set_pos[name] == "prob" then --setting Minetest schematic node probabilities\r
333                         worldedit.prob_pos[name] = pos\r
334                         minetest.show_formspec(puncher:get_player_name(), "prob_val_enter", "field[text;;]")\r
335                 end\r
336         end\r
337 end)\r
338 \r
339 minetest.register_chatcommand("/volume", {\r
340         params = "",\r
341         description = "Display the volume of the current WorldEdit region",\r
342         privs = {worldedit=true},\r
343         func = function(name, param)\r
344                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
345                 if pos1 == nil or pos2 == nil then\r
346                         worldedit.player_notify(name, "no region selected")\r
347                         return nil\r
348                 end\r
349 \r
350                 local volume = worldedit.volume(pos1, pos2)\r
351                 local abs = math.abs\r
352                 worldedit.player_notify(name, "current region has a volume of " .. volume .. " nodes ("\r
353                         .. abs(pos2.x - pos1.x) + 1 .. "*"\r
354                         .. abs(pos2.y - pos1.y) + 1 .. "*"\r
355                         .. abs(pos2.z - pos1.z) + 1 .. ")")\r
356         end,\r
357 })\r
358 \r
359 minetest.register_chatcommand("/deleteblocks", {\r
360         params = "",\r
361         description = "remove all MapBlocks (16x16x16) containing the selected area from the map",\r
362         privs = {worldedit=true},\r
363         func = safe_region(function(name, param)\r
364                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
365                 local success = minetest.delete_area(pos1, pos2)\r
366                 if success then\r
367                         worldedit.player_notify(name, "Area deleted.")\r
368                 else\r
369                         worldedit.player_notify(name, "There was an error during deletion of the area.")\r
370                 end\r
371         end),\r
372 })\r
373 \r
374 minetest.register_chatcommand("/set", {\r
375         params = "<node>",\r
376         description = "Set the current WorldEdit region to <node>",\r
377         privs = {worldedit=true},\r
378         func = safe_region(function(name, param)\r
379                 local node = get_node(name, param)\r
380                 if not node then\r
381                         worldedit.player_notify(name, "Could not identify node \"" .. param .. "\"")\r
382                         return\r
383                 end\r
384 \r
385                 local count = worldedit.set(worldedit.pos1[name], worldedit.pos2[name], node)\r
386                 worldedit.player_notify(name, count .. " nodes set")\r
387         end, check_region),\r
388 })\r
389 \r
390 minetest.register_chatcommand("/mix", {\r
391         params = "<node1> ...",\r
392         description = "Fill the current WorldEdit region with a random mix of <node1>, ...",\r
393         privs = {worldedit=true},\r
394         func = safe_region(function(name, param)\r
395                 local nodes = {}\r
396                 for nodename in param:gmatch("[^%s]+") do\r
397                         local node = get_node(name, nodename)\r
398                         if not node then\r
399                                 worldedit.player_notify(name, "Could not identify node \"" .. name .. "\"")\r
400                                 return\r
401                         end\r
402                         nodes[#nodes + 1] = node\r
403                 end\r
404 \r
405                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
406                 local count = worldedit.set(pos1, pos2, nodes)\r
407                 worldedit.player_notify(name, count .. " nodes set")\r
408         end, check_region),\r
409 })\r
410 \r
411 local check_replace = function(name, param)\r
412         local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$")\r
413         if found == nil then\r
414                 worldedit.player_notify(name, "invalid usage: " .. param)\r
415                 return nil\r
416         end\r
417         local newsearchnode = worldedit.normalize_nodename(searchnode)\r
418         if not newsearchnode then\r
419                 worldedit.player_notify(name, "invalid search node name: " .. searchnode)\r
420                 return nil\r
421         end\r
422         local newreplacenode = worldedit.normalize_nodename(replacenode)\r
423         if not newreplacenode then\r
424                 worldedit.player_notify(name, "invalid replace node name: " .. replacenode)\r
425                 return nil\r
426         end\r
427         return check_region(name, param)\r
428 end\r
429 \r
430 minetest.register_chatcommand("/replace", {\r
431         params = "<search node> <replace node>",\r
432         description = "Replace all instances of <search node> with <replace node> in the current WorldEdit region",\r
433         privs = {worldedit=true},\r
434         func = safe_region(function(name, param)\r
435                 local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$")\r
436                 local norm_search_node = worldedit.normalize_nodename(search_node)\r
437                 local norm_replace_node = worldedit.normalize_nodename(replace_node)\r
438                 local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name],\r
439                                 norm_search_node, norm_replace_node)\r
440                 worldedit.player_notify(name, count .. " nodes replaced")\r
441         end, check_replace),\r
442 })\r
443 \r
444 minetest.register_chatcommand("/replaceinverse", {\r
445         params = "<search node> <replace node>",\r
446         description = "Replace all nodes other than <search node> with <replace node> in the current WorldEdit region",\r
447         privs = {worldedit=true},\r
448         func = safe_region(function(name, param)\r
449                 local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$")\r
450                 local norm_search_node = worldedit.normalize_nodename(search_node)\r
451                 local norm_replace_node = worldedit.normalize_nodename(replace_node)\r
452                 local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name],\r
453                                 norm_search_node, norm_replace_node, true)\r
454                 worldedit.player_notify(name, count .. " nodes replaced")\r
455         end, check_replace),\r
456 })\r
457 \r
458 local check_sphere = function(name, param)\r
459         if worldedit.pos1[name] == nil then\r
460                 worldedit.player_notify(name, "no position 1 selected")\r
461                 return nil\r
462         end\r
463         local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
464         if found == nil then\r
465                 worldedit.player_notify(name, "invalid usage: " .. param)\r
466                 return nil\r
467         end\r
468         local node = get_node(name, nodename)\r
469         if not node then return nil end\r
470         return math.ceil((4 * math.pi * (tonumber(radius) ^ 3)) / 3) --volume of sphere\r
471 end\r
472 \r
473 minetest.register_chatcommand("/hollowsphere", {\r
474         params = "<radius> <node>",\r
475         description = "Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",\r
476         privs = {worldedit=true},\r
477         func = safe_region(function(name, param)\r
478                 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
479                 local node = get_node(name, nodename)\r
480                 local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node, true)\r
481                 worldedit.player_notify(name, count .. " nodes added")\r
482         end, check_sphere),\r
483 })\r
484 \r
485 minetest.register_chatcommand("/sphere", {\r
486         params = "<radius> <node>",\r
487         description = "Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",\r
488         privs = {worldedit=true},\r
489         func = safe_region(function(name, param)\r
490                 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
491                 local node = get_node(name, nodename)\r
492                 local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node)\r
493                 worldedit.player_notify(name, count .. " nodes added")\r
494         end, check_sphere),\r
495 })\r
496 \r
497 local check_dome = function(name, param)\r
498         if worldedit.pos1[name] == nil then\r
499                 worldedit.player_notify(name, "no position 1 selected")\r
500                 return nil\r
501         end\r
502         local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
503         if found == nil then\r
504                 worldedit.player_notify(name, "invalid usage: " .. param)\r
505                 return nil\r
506         end\r
507         local node = get_node(name, nodename)\r
508         if not node then return nil end\r
509         return math.ceil((2 * math.pi * (tonumber(radius) ^ 3)) / 3) --volume of dome\r
510 end\r
511 \r
512 minetest.register_chatcommand("/hollowdome", {\r
513         params = "<radius> <node>",\r
514         description = "Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",\r
515         privs = {worldedit=true},\r
516         func = safe_region(function(name, param)\r
517                 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
518                 local node = get_node(name, nodename)\r
519                 local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node, true)\r
520                 worldedit.player_notify(name, count .. " nodes added")\r
521         end, check_dome),\r
522 })\r
523 \r
524 minetest.register_chatcommand("/dome", {\r
525         params = "<radius> <node>",\r
526         description = "Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",\r
527         privs = {worldedit=true},\r
528         func = safe_region(function(name, param)\r
529                 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")\r
530                 local node = get_node(name, nodename)\r
531                 local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node)\r
532                 worldedit.player_notify(name, count .. " nodes added")\r
533         end, check_dome),\r
534 })\r
535 \r
536 local check_cylinder = function(name, param)\r
537         if worldedit.pos1[name] == nil then\r
538                 worldedit.player_notify(name, "no position 1 selected")\r
539                 return nil\r
540         end\r
541         -- two radii\r
542         local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")\r
543         if found == nil then\r
544                 -- single radius\r
545                 found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")\r
546                 radius2 = radius1\r
547         end\r
548         if found == nil then\r
549                 worldedit.player_notify(name, "invalid usage: " .. param)\r
550                 return nil\r
551         end\r
552         local node = get_node(name, nodename)\r
553         if not node then return nil end\r
554         local radius = math.max(tonumber(radius1), tonumber(radius2))\r
555         return math.ceil(math.pi * (radius ^ 2) * tonumber(length))\r
556 end\r
557 \r
558 minetest.register_chatcommand("/hollowcylinder", {\r
559         params = "x/y/z/? <length> <radius1> [radius2] <node>",\r
560         description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node>",\r
561         privs = {worldedit=true},\r
562         func = safe_region(function(name, param)\r
563                 -- two radii\r
564                 local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")\r
565                 if found == nil then\r
566                         -- single radius\r
567                         found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")\r
568                         radius2 = radius1\r
569                 end\r
570                 length = tonumber(length)\r
571                 if axis == "?" then\r
572                         axis, sign = worldedit.player_axis(name)\r
573                         length = length * sign\r
574                 end\r
575                 local node = get_node(name, nodename)\r
576                 local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node, true)\r
577                 worldedit.player_notify(name, count .. " nodes added")\r
578         end, check_cylinder),\r
579 })\r
580 \r
581 minetest.register_chatcommand("/cylinder", {\r
582         params = "x/y/z/? <length> <radius1> [radius2] <node>",\r
583         description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node>",\r
584         privs = {worldedit=true},\r
585         func = safe_region(function(name, param)\r
586                 -- two radii\r
587                 local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")\r
588                 if found == nil then\r
589                         -- single radius\r
590                         found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")\r
591                         radius2 = radius1\r
592                 end\r
593                 length = tonumber(length)\r
594                 if axis == "?" then\r
595                         axis, sign = worldedit.player_axis(name)\r
596                         length = length * sign\r
597                 end\r
598                 local node = get_node(name, nodename)\r
599                 local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node)\r
600                 worldedit.player_notify(name, count .. " nodes added")\r
601         end, check_cylinder),\r
602 })\r
603 \r
604 local check_pyramid = function(name, param)\r
605         if worldedit.pos1[name] == nil then\r
606                 worldedit.player_notify(name, "no position 1 selected")\r
607                 return nil\r
608         end\r
609         local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")\r
610         if found == nil then\r
611                 worldedit.player_notify(name, "invalid usage: " .. param)\r
612                 return nil\r
613         end\r
614         local node = get_node(name, nodename)\r
615         if not node then return nil end\r
616         height = tonumber(height)\r
617         return math.ceil(((height * 2 + 1) ^ 2) * height / 3)\r
618 end\r
619      \r
620 minetest.register_chatcommand("/hollowpyramid", {\r
621         params = "x/y/z/? <height> <node>",\r
622         description = "Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>",\r
623         privs = {worldedit=true},\r
624         func = safe_region(function(name, param)\r
625                 local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")\r
626                 height = tonumber(height)\r
627                 if axis == "?" then\r
628                         axis, sign = worldedit.player_axis(name)\r
629                         height = height * sign\r
630                 end\r
631                 local node = get_node(name, nodename)\r
632                 local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node, true)\r
633                 worldedit.player_notify(name, count .. " nodes added")\r
634         end, check_pyramid),\r
635 })\r
636 \r
637 minetest.register_chatcommand("/pyramid", {\r
638         params = "x/y/z/? <height> <node>",\r
639         description = "Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>",\r
640         privs = {worldedit=true},\r
641         func = safe_region(function(name, param)\r
642                 local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")\r
643                 height = tonumber(height)\r
644                 if axis == "?" then\r
645                         axis, sign = worldedit.player_axis(name)\r
646                         height = height * sign\r
647                 end\r
648                 local node = get_node(name, nodename)\r
649                 local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node)\r
650                 worldedit.player_notify(name, count .. " nodes added")\r
651         end, check_pyramid),\r
652 })\r
653 \r
654 minetest.register_chatcommand("/spiral", {\r
655         params = "<length> <height> <space> <node>",\r
656         description = "Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <space>, composed of <node>",\r
657         privs = {worldedit=true},\r
658         func = safe_region(function(name, param)\r
659                 local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")\r
660                 local node = get_node(name, nodename)\r
661                 local count = worldedit.spiral(worldedit.pos1[name], tonumber(length), tonumber(height), tonumber(space), node)\r
662                 worldedit.player_notify(name, count .. " nodes added")\r
663         end,\r
664         function(name, param)\r
665                 if worldedit.pos1[name] == nil then\r
666                         worldedit.player_notify(name, "no position 1 selected")\r
667                         return nil\r
668                 end\r
669                 local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")\r
670                 if found == nil then\r
671                         worldedit.player_notify(name, "invalid usage: " .. param)\r
672                         return nil\r
673                 end\r
674                 local node = get_node(name, nodename)\r
675                 if not node then return nil end\r
676                 return 1 -- TODO: return an useful value\r
677         end),\r
678 })\r
679 \r
680 minetest.register_chatcommand("/copy", {\r
681         params = "x/y/z/? <amount>",\r
682         description = "Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes",\r
683         privs = {worldedit=true},\r
684         func = safe_region(function(name, param)\r
685                 local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
686                 if found == nil then\r
687                         worldedit.player_notify(name, "invalid usage: " .. param)\r
688                         return\r
689                 end\r
690                 amount = tonumber(amount)\r
691                 if axis == "?" then\r
692                         axis, sign = worldedit.player_axis(name)\r
693                         amount = amount * sign\r
694                 end\r
695 \r
696                 local count = worldedit.copy(worldedit.pos1[name], worldedit.pos2[name], axis, amount)\r
697                 worldedit.player_notify(name, count .. " nodes copied")\r
698         end,\r
699         function(name, param)\r
700                 local volume = check_region(name, param)\r
701                 return volume and volume * 2 or volume\r
702         end),\r
703 })\r
704 \r
705 minetest.register_chatcommand("/move", {\r
706         params = "x/y/z/? <amount>",\r
707         description = "Move the current WorldEdit region along the x/y/z/? axis by <amount> nodes",\r
708         privs = {worldedit=true},\r
709         func = safe_region(function(name, param)\r
710                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
711                 local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
712                 if found == nil then\r
713                         worldedit.player_notify(name, "invalid usage: " .. param)\r
714                         return\r
715                 end\r
716                 amount = tonumber(amount)\r
717                 if axis == "?" then\r
718                         axis, sign = worldedit.player_axis(name)\r
719                         amount = amount * sign\r
720                 end\r
721 \r
722                 local count = worldedit.move(pos1, pos2, axis, amount)\r
723 \r
724                 pos1[axis] = pos1[axis] + amount\r
725                 pos2[axis] = pos2[axis] + amount\r
726                 worldedit.mark_pos1(name)\r
727                 worldedit.mark_pos2(name)\r
728                 worldedit.player_notify(name, count .. " nodes moved")\r
729         end, check_region),\r
730 })\r
731 \r
732 minetest.register_chatcommand("/stack", {\r
733         params = "x/y/z/? <count>",\r
734         description = "Stack the current WorldEdit region along the x/y/z/? axis <count> times",\r
735         privs = {worldedit=true},\r
736         func = safe_region(function(name, param)\r
737                 local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
738                 repetitions = tonumber(repetitions)\r
739                 if axis == "?" then\r
740                         axis, sign = worldedit.player_axis(name)\r
741                         repetitions = repetitions * sign\r
742                 end\r
743                 local count = worldedit.stack(worldedit.pos1[name], worldedit.pos2[name], axis, repetitions)\r
744                 worldedit.player_notify(name, count .. " nodes stacked")\r
745         end,\r
746         function(name, param)\r
747                 local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
748                 if found == nil then\r
749                         worldedit.player_notify(name, "invalid usage: " .. param)\r
750                         return\r
751                 end\r
752                 local count = check_region(name, param)\r
753                 if count then return (tonumber(repetitions) + 1) * count end\r
754                 return nil\r
755         end),\r
756 })\r
757 \r
758 minetest.register_chatcommand("/stack2", {\r
759         params = "<count> <x> <y> <z>",\r
760         description = "Stack the current WorldEdit region <count> times by offset <x>, <y>, <z>",\r
761         privs = {worldedit=true},\r
762         func = function(name, param)\r
763                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
764                 if pos1 == nil or pos2 == nil then\r
765                         worldedit.player_notify(name, "Select a position first!")\r
766                         return\r
767                 end\r
768                 local repetitions, incs = param:match("(%d+)%s*(.+)")\r
769                 if repetitions == nil then\r
770                         worldedit.player_notify(name, "invalid count: " .. param)\r
771                         return\r
772                 end\r
773                 repetitions = tonumber(repetitions)\r
774 \r
775                 local x, y, z = incs:match("([+-]?%d+) ([+-]?%d+) ([+-]?%d+)")\r
776                 if x == nil then\r
777                         worldedit.player_notify(name, "invalid increments: " .. param)\r
778                         return\r
779                 end\r
780                 x, y, z = tonumber(x), tonumber(y), tonumber(z)\r
781 \r
782                 local count = worldedit.volume(pos1, pos2) * repetitions\r
783 \r
784                 return safe_region(function()\r
785                         worldedit.stack2(pos1, pos2, {x=x, y=y, z=z}, repetitions,\r
786                                 function() worldedit.player_notify(name, count .. " nodes stacked") end)\r
787                 end, function()\r
788                         return count\r
789                 end)(name,param) -- more hax --wip: clean this up a little bit\r
790         end\r
791 })\r
792 \r
793 \r
794 minetest.register_chatcommand("/stretch", {\r
795         params = "<stretchx> <stretchy> <stretchz>",\r
796         description = "Scale the current WorldEdit positions and region by a factor of <stretchx>, <stretchy>, <stretchz> along the X, Y, and Z axes, repectively, with position 1 as the origin",\r
797         privs = {worldedit=true},\r
798         func = safe_region(function(name, param)\r
799                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
800                 local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")\r
801                 stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)\r
802                 local count, pos1, pos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)\r
803 \r
804                 --reset markers to scaled positions\r
805                 worldedit.pos1[name] = pos1\r
806                 worldedit.pos2[name] = pos2\r
807                 worldedit.mark_pos1(name)\r
808                 worldedit.mark_pos2(name)\r
809 \r
810                 worldedit.player_notify(name, count .. " nodes stretched")\r
811         end,\r
812         function(name, param)\r
813                 local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")\r
814                 if found == nil then\r
815                         worldedit.player_notify(name, "invalid usage: " .. param)\r
816                         return nil\r
817                 end\r
818                 stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)\r
819                 if stretchx == 0 or stretchy == 0 or stretchz == 0 then\r
820                         worldedit.player_notify(name, "invalid scaling factors: " .. param)\r
821                 end\r
822                 local count = check_region(name, param)\r
823                 if count then return tonumber(stretchx) * tonumber(stretchy) * tonumber(stretchz) * count end\r
824                 return nil\r
825         end),\r
826 })\r
827 \r
828 minetest.register_chatcommand("/transpose", {\r
829         params = "x/y/z/? x/y/z/?",\r
830         description = "Transpose the current WorldEdit region along the x/y/z/? and x/y/z/? axes",\r
831         privs = {worldedit=true},\r
832         func = safe_region(function(name, param)\r
833                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
834                 local found, _, axis1, axis2 = param:find("^([xyz%?])%s+([xyz%?])$")\r
835                 if axis1 == "?" then axis1 = worldedit.player_axis(name) end\r
836                 if axis2 == "?" then axis2 = worldedit.player_axis(name) end\r
837                 local count, pos1, pos2 = worldedit.transpose(pos1, pos2, axis1, axis2)\r
838 \r
839                 --reset markers to transposed positions\r
840                 worldedit.pos1[name] = pos1\r
841                 worldedit.pos2[name] = pos2\r
842                 worldedit.mark_pos1(name)\r
843                 worldedit.mark_pos2(name)\r
844 \r
845                 worldedit.player_notify(name, count .. " nodes transposed")\r
846         end,\r
847         function(name, param)\r
848                 local found, _, axis1, axis2 = param:find("^([xyz%?])%s+([xyz%?])$")\r
849                 if found == nil then\r
850                         worldedit.player_notify(name, "invalid usage: " .. param)\r
851                         return nil\r
852                 end\r
853                 if axis1 == axis2 then\r
854                         worldedit.player_notify(name, "invalid usage: axes must be different")\r
855                         return nil\r
856                 end\r
857                 return check_region(name, param)\r
858         end),\r
859 })\r
860 \r
861 minetest.register_chatcommand("/flip", {\r
862         params = "x/y/z/?",\r
863         description = "Flip the current WorldEdit region along the x/y/z/? axis",\r
864         privs = {worldedit=true},\r
865         func = safe_region(function(name, param)\r
866                 if param == "?" then param = worldedit.player_axis(name) end\r
867                 local count = worldedit.flip(worldedit.pos1[name], worldedit.pos2[name], param)\r
868                 worldedit.player_notify(name, count .. " nodes flipped")\r
869         end,\r
870         function(name, param)\r
871                 if param ~= "x" and param ~= "y" and param ~= "z" and param ~= "?" then\r
872                         worldedit.player_notify(name, "invalid usage: " .. param)\r
873                         return nil\r
874                 end\r
875                 return check_region(name, param)\r
876         end),\r
877 })\r
878 \r
879 minetest.register_chatcommand("/rotate", {\r
880         params = "<axis> <angle>",\r
881         description = "Rotate the current WorldEdit region around the axis <axis> by angle <angle> (90 degree increment)",\r
882         privs = {worldedit=true},\r
883         func = safe_region(function(name, param)\r
884                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
885                 local found, _, axis, angle = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
886                 if axis == "?" then axis = worldedit.player_axis(name) end\r
887                 local count, pos1, pos2 = worldedit.rotate(pos1, pos2, axis, angle)\r
888 \r
889                 --reset markers to rotated positions\r
890                 worldedit.pos1[name] = pos1\r
891                 worldedit.pos2[name] = pos2\r
892                 worldedit.mark_pos1(name)\r
893                 worldedit.mark_pos2(name)\r
894 \r
895                 worldedit.player_notify(name, count .. " nodes rotated")\r
896         end,\r
897         function(name, param)\r
898                 local found, _, axis, angle = param:find("^([xyz%?])%s+([+-]?%d+)$")\r
899                 if found == nil then\r
900                         worldedit.player_notify(name, "invalid usage: " .. param)\r
901                         return nil\r
902                 end\r
903                 if angle % 90 ~= 0 then\r
904                         worldedit.player_notify(name, "invalid usage: angle must be multiple of 90")\r
905                         return nil\r
906                 end\r
907                 return check_region(name, param)\r
908         end),\r
909 })\r
910 \r
911 minetest.register_chatcommand("/orient", {\r
912         params = "<angle>",\r
913         description = "Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment)",\r
914         privs = {worldedit=true},\r
915         func = safe_region(function(name, param)\r
916                 local found, _, angle = param:find("^([+-]?%d+)$")\r
917                 local count = worldedit.orient(worldedit.pos1[name], worldedit.pos2[name], angle)\r
918                 worldedit.player_notify(name, count .. " nodes oriented")\r
919         end,\r
920         function(name, param)\r
921                 local found, _, angle = param:find("^([+-]?%d+)$")\r
922                 if found == nil then\r
923                         worldedit.player_notify(name, "invalid usage: " .. param)\r
924                         return nil\r
925                 end\r
926                 if angle % 90 ~= 0 then\r
927                         worldedit.player_notify(name, "invalid usage: angle must be multiple of 90")\r
928                         return nil\r
929                 end\r
930                 return check_region(name, param)\r
931         end),\r
932 })\r
933 \r
934 minetest.register_chatcommand("/fixlight", {\r
935         params = "",\r
936         description = "Fix the lighting in the current WorldEdit region",\r
937         privs = {worldedit=true},\r
938         func = safe_region(function(name, param)\r
939                 local count = worldedit.fixlight(worldedit.pos1[name], worldedit.pos2[name])\r
940                 worldedit.player_notify(name, count .. " nodes updated")\r
941         end),\r
942 })\r
943 \r
944 minetest.register_chatcommand("/drain", {\r
945         params = "",\r
946         description = "Remove any fluid node within the current WorldEdit region",\r
947         privs = {worldedit=true},\r
948         func = safe_region(function(name, param)\r
949                 -- TODO: make an API function for this\r
950                 local count = 0\r
951                 local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name])\r
952                 for x = pos1.x, pos2.x do\r
953                 for y = pos1.y, pos2.y do\r
954                 for z = pos1.z, pos2.z do\r
955                         local n = minetest.get_node({x=x, y=y, z=z}).name\r
956                         local d = minetest.registered_nodes[n]\r
957                         if d ~= nil and (d["drawtype"] == "liquid" or d["drawtype"] == "flowingliquid") then\r
958                                 minetest.remove_node({x=x, y=y, z=z})\r
959                                 count = count + 1\r
960                         end\r
961                 end\r
962                 end\r
963                 end\r
964                 worldedit.player_notify(name, count .. " nodes updated")\r
965         end),\r
966 })\r
967 \r
968 minetest.register_chatcommand("/hide", {\r
969         params = "",\r
970         description = "Hide all nodes in the current WorldEdit region non-destructively",\r
971         privs = {worldedit=true},\r
972         func = safe_region(function(name, param)\r
973                 local count = worldedit.hide(worldedit.pos1[name], worldedit.pos2[name])\r
974                 worldedit.player_notify(name, count .. " nodes hidden")\r
975         end),\r
976 })\r
977 \r
978 minetest.register_chatcommand("/suppress", {\r
979         params = "<node>",\r
980         description = "Suppress all <node> in the current WorldEdit region non-destructively",\r
981         privs = {worldedit=true},\r
982         func = safe_region(function(name, param)\r
983                 local node = get_node(name, param)\r
984                 local count = worldedit.suppress(worldedit.pos1[name], worldedit.pos2[name], node)\r
985                 worldedit.player_notify(name, count .. " nodes suppressed")\r
986         end, check_region),\r
987 })\r
988 \r
989 minetest.register_chatcommand("/highlight", {\r
990         params = "<node>",\r
991         description = "Highlight <node> in the current WorldEdit region by hiding everything else non-destructively",\r
992         privs = {worldedit=true},\r
993         func = safe_region(function(name, param)\r
994                 local node = get_node(name, param)\r
995                 local count = worldedit.highlight(worldedit.pos1[name], worldedit.pos2[name], node)\r
996                 worldedit.player_notify(name, count .. " nodes highlighted")\r
997         end, check_region),\r
998 })\r
999 \r
1000 minetest.register_chatcommand("/restore", {\r
1001         params = "",\r
1002         description = "Restores nodes hidden with WorldEdit in the current WorldEdit region",\r
1003         privs = {worldedit=true},\r
1004         func = safe_region(function(name, param)\r
1005                 local count = worldedit.restore(worldedit.pos1[name], worldedit.pos2[name])\r
1006                 worldedit.player_notify(name, count .. " nodes restored")\r
1007         end),\r
1008 })\r
1009 \r
1010 minetest.register_chatcommand("/save", {\r
1011         params = "<file>",\r
1012         description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.we\"",\r
1013         privs = {worldedit=true},\r
1014         func = safe_region(function(name, param)\r
1015                 if param == "" then\r
1016                         worldedit.player_notify(name, "invalid usage: " .. param)\r
1017                         return\r
1018                 end\r
1019                 if not check_filename(param) then\r
1020                         worldedit.player_notify(name, "Disallowed file name: " .. param)\r
1021                         return\r
1022                 end\r
1023                 local result, count = worldedit.serialize(worldedit.pos1[name],\r
1024                                 worldedit.pos2[name])\r
1025 \r
1026                 local path = minetest.get_worldpath() .. "/schems"\r
1027                 -- Create directory if it does not already exist\r
1028                 mkdir(path)\r
1029 \r
1030                 local filename = path .. "/" .. param .. ".we"\r
1031                 local file, err = io.open(filename, "wb")\r
1032                 if err ~= nil then\r
1033                         worldedit.player_notify(name, "Could not save file to \"" .. filename .. "\"")\r
1034                         return\r
1035                 end\r
1036                 file:write(result)\r
1037                 file:flush()\r
1038                 file:close()\r
1039 \r
1040                 worldedit.player_notify(name, count .. " nodes saved")\r
1041         end),\r
1042 })\r
1043 \r
1044 minetest.register_chatcommand("/allocate", {\r
1045         params = "<file>",\r
1046         description = "Set the region defined by nodes from \"(world folder)/schems/<file>.we\" as the current WorldEdit region",\r
1047         privs = {worldedit=true},\r
1048         func = function(name, param)\r
1049                 local pos = get_position(name)\r
1050                 if pos == nil then return end\r
1051 \r
1052                 if param == "" then\r
1053                         worldedit.player_notify(name, "invalid usage: " .. param)\r
1054                         return\r
1055                 end\r
1056                 if not check_filename(param) then\r
1057                         worldedit.player_notify(name, "Disallowed file name: " .. param)\r
1058                         return\r
1059                 end\r
1060 \r
1061                 local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"\r
1062                 local file, err = io.open(filename, "rb")\r
1063                 if err ~= nil then\r
1064                         worldedit.player_notify(name, "could not open file \"" .. filename .. "\"")\r
1065                         return\r
1066                 end\r
1067                 local value = file:read("*a")\r
1068                 file:close()\r
1069 \r
1070                 local version = worldedit.read_header(value)\r
1071                 if version == 0 then\r
1072                         worldedit.player_notify(name, "File is invalid!")\r
1073                         return\r
1074                 elseif version > worldedit.LATEST_SERIALIZATION_VERSION then\r
1075                         worldedit.player_notify(name, "File was created with newer version of WorldEdit!")\r
1076                 end\r
1077                 local nodepos1, nodepos2, count = worldedit.allocate(pos, value)\r
1078 \r
1079                 worldedit.pos1[name] = nodepos1\r
1080                 worldedit.mark_pos1(name)\r
1081                 worldedit.pos2[name] = nodepos2\r
1082                 worldedit.mark_pos2(name)\r
1083 \r
1084                 worldedit.player_notify(name, count .. " nodes allocated")\r
1085         end,\r
1086 })\r
1087 \r
1088 minetest.register_chatcommand("/load", {\r
1089         params = "<file>",\r
1090         description = "Load nodes from \"(world folder)/schems/<file>[.we[m]]\" with position 1 of the current WorldEdit region as the origin",\r
1091         privs = {worldedit=true},\r
1092         func = function(name, param)\r
1093                 local pos = get_position(name)\r
1094                 if pos == nil then return end\r
1095 \r
1096                 if param == "" then\r
1097                         worldedit.player_notify(name, "invalid usage: " .. param)\r
1098                         return\r
1099                 end\r
1100                 if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then\r
1101                         worldedit.player_notify(name, "invalid file name: " .. param)\r
1102                         return\r
1103                 end\r
1104 \r
1105                 --find the file in the world path\r
1106                 local testpaths = {\r
1107                         minetest.get_worldpath() .. "/schems/" .. param,\r
1108                         minetest.get_worldpath() .. "/schems/" .. param .. ".we",\r
1109                         minetest.get_worldpath() .. "/schems/" .. param .. ".wem",\r
1110                 }\r
1111                 local file, err\r
1112                 for index, path in ipairs(testpaths) do\r
1113                         file, err = io.open(path, "rb")\r
1114                         if not err then\r
1115                                 break\r
1116                         end\r
1117                 end\r
1118                 if err then\r
1119                         worldedit.player_notify(name, "could not open file \"" .. param .. "\"")\r
1120                         return\r
1121                 end\r
1122                 local value = file:read("*a")\r
1123                 file:close()\r
1124 \r
1125                 local version = worldedit.read_header(value)\r
1126                 if version == 0 then\r
1127                         worldedit.player_notify(name, "File is invalid!")\r
1128                         return\r
1129                 elseif version > worldedit.LATEST_SERIALIZATION_VERSION then\r
1130                         worldedit.player_notify(name, "File was created with newer version of WorldEdit!")\r
1131                         return\r
1132                 end\r
1133 \r
1134                 local count = worldedit.deserialize(pos, value)\r
1135 \r
1136                 worldedit.player_notify(name, count .. " nodes loaded")\r
1137         end,\r
1138 })\r
1139 \r
1140 minetest.register_chatcommand("/lua", {\r
1141         params = "<code>",\r
1142         description = "Executes <code> as a Lua chunk in the global namespace",\r
1143         privs = {worldedit=true, server=true},\r
1144         func = function(name, param)\r
1145                 local err = worldedit.lua(param)\r
1146                 if err then\r
1147                         worldedit.player_notify(name, "code error: " .. err)\r
1148                         minetest.log("action", name.." tried to execute "..param)\r
1149                 else\r
1150                         worldedit.player_notify(name, "code successfully executed", false)\r
1151                         minetest.log("action", name.." executed "..param)\r
1152                 end\r
1153         end,\r
1154 })\r
1155 \r
1156 minetest.register_chatcommand("/luatransform", {\r
1157         params = "<code>",\r
1158         description = "Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region",\r
1159         privs = {worldedit=true, server=true},\r
1160         func = safe_region(function(name, param)\r
1161                 local err = worldedit.luatransform(worldedit.pos1[name], worldedit.pos2[name], param)\r
1162                 if err then\r
1163                         worldedit.player_notify(name, "code error: " .. err, false)\r
1164                         minetest.log("action", name.." tried to execute luatransform "..param)\r
1165                 else\r
1166                         worldedit.player_notify(name, "code successfully executed", false)\r
1167                         minetest.log("action", name.." executed luatransform "..param)\r
1168                 end\r
1169         end),\r
1170 })\r
1171 \r
1172 minetest.register_chatcommand("/mtschemcreate", {\r
1173         params = "<file>",\r
1174         description = "Save the current WorldEdit region using the Minetest "..\r
1175                 "Schematic format to \"(world folder)/schems/<filename>.mts\"",\r
1176         privs = {worldedit=true},\r
1177         func = safe_region(function(name, param)\r
1178                 if param == nil then\r
1179                         worldedit.player_notify(name, "No filename specified")\r
1180                         return\r
1181                 end\r
1182                 if not check_filename(param) then\r
1183                         worldedit.player_notify(name, "Disallowed file name: " .. param)\r
1184                         return\r
1185                 end\r
1186 \r
1187                 local path = minetest.get_worldpath() .. "/schems"\r
1188                 -- Create directory if it does not already exist\r
1189                 mkdir(path)\r
1190 \r
1191                 local filename = path .. "/" .. param .. ".mts"\r
1192                 local ret = minetest.create_schematic(worldedit.pos1[name],\r
1193                                 worldedit.pos2[name], worldedit.prob_list[name],\r
1194                                 filename)\r
1195                 if ret == nil then\r
1196                         worldedit.player_notify(name, "Failed to create Minetest schematic", false)\r
1197                 else\r
1198                         worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)\r
1199                 end\r
1200                 worldedit.prob_list[name] = {}\r
1201         end),\r
1202 })\r
1203 \r
1204 minetest.register_chatcommand("/mtschemplace", {\r
1205         params = "<file>",\r
1206         description = "Load nodes from \"(world folder)/schems/<file>.mts\" with position 1 of the current WorldEdit region as the origin",\r
1207         privs = {worldedit=true},\r
1208         func = function(name, param)\r
1209                 if param == "" then\r
1210                         worldedit.player_notify(name, "no filename specified")\r
1211                         return\r
1212                 end\r
1213                 if not check_filename(param) then\r
1214                         worldedit.player_notify(name, "Disallowed file name: " .. param)\r
1215                         return\r
1216                 end\r
1217 \r
1218                 local pos = get_position(name)\r
1219                 if pos == nil then return end\r
1220 \r
1221                 local path = minetest.get_worldpath() .. "/schems/" .. param .. ".mts"\r
1222                 if minetest.place_schematic(pos, path) == nil then\r
1223                         worldedit.player_notify(name, "failed to place Minetest schematic", false)\r
1224                 else\r
1225                         worldedit.player_notify(name, "placed Minetest schematic " .. param ..\r
1226                                 " at " .. minetest.pos_to_string(pos), false)\r
1227                 end\r
1228         end,\r
1229 })\r
1230 \r
1231 minetest.register_chatcommand("/mtschemprob", {\r
1232         params = "start/finish/get",\r
1233         description = "Begins node probability entry for Minetest schematics, gets the nodes that have probabilities set, or ends node probability entry",\r
1234         privs = {worldedit=true},\r
1235         func = function(name, param)\r
1236                 if param == "start" then --start probability setting\r
1237                         worldedit.set_pos[name] = "prob"\r
1238                         worldedit.prob_list[name] = {}\r
1239                         worldedit.player_notify(name, "select Minetest schematic probability values by punching nodes")\r
1240                 elseif param == "finish" then --finish probability setting\r
1241                         worldedit.set_pos[name] = nil\r
1242                         worldedit.player_notify(name, "finished Minetest schematic probability selection")\r
1243                 elseif param == "get" then --get all nodes that had probabilities set on them\r
1244                         local text = ""\r
1245                         local problist = worldedit.prob_list[name]\r
1246                         if problist == nil then\r
1247                                 return\r
1248                         end\r
1249                         for k,v in pairs(problist) do\r
1250                                 local prob = math.floor(((v.prob / 256) * 100) * 100 + 0.5) / 100\r
1251                                 text = text .. minetest.pos_to_string(v.pos) .. ": " .. prob .. "% | "\r
1252                         end\r
1253                         worldedit.player_notify(name, "currently set node probabilities:")\r
1254                         worldedit.player_notify(name, text)\r
1255                 else\r
1256                         worldedit.player_notify(name, "unknown subcommand: " .. param)\r
1257                 end\r
1258         end,\r
1259 })\r
1260 \r
1261 minetest.register_on_player_receive_fields(function(player, formname, fields)\r
1262         if formname == "prob_val_enter" and not (fields.text == "" or fields.text == nil) then\r
1263                 local name = player:get_player_name()\r
1264                 local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)}\r
1265                 local index = table.getn(worldedit.prob_list[name]) + 1\r
1266                 worldedit.prob_list[name][index] = prob_entry\r
1267         end\r
1268 end)\r
1269 \r
1270 minetest.register_chatcommand("/clearobjects", {\r
1271         params = "",\r
1272         description = "Clears all objects within the WorldEdit region",\r
1273         privs = {worldedit=true},\r
1274         func = safe_region(function(name, param)\r
1275                 local count = worldedit.clear_objects(worldedit.pos1[name], worldedit.pos2[name])\r
1276                 worldedit.player_notify(name, count .. " objects cleared")\r
1277         end),\r
1278 })\r