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