1 minetest.register_privilege("worldedit", "Can use WorldEdit commands")
\r
3 worldedit.set_pos = {}
\r
4 worldedit.inspect = {}
\r
8 if minetest.place_schematic then
\r
9 worldedit.prob_pos = {}
\r
10 worldedit.prob_list = {}
\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
18 local function get_position(name) --position 1 retrieval function for when not using `safe_region`
\r
19 local pos1 = worldedit.pos1[name]
\r
21 worldedit.player_notify(name, "no position 1 selected")
\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
30 worldedit.player_notify(name, "invalid node name: " .. nodename)
\r
36 function worldedit.player_notify(name, message)
\r
37 minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)
\r
40 local function string_endswith(full, part)
\r
41 return full:find(part, 1, true) == #full - #part + 1
\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
49 local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases
\r
50 if minetest.registered_nodes[fullname] or fullname == "air" then -- full name
\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
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
64 if string_endswith(desc, " block") and desc == nodename.." block" then
\r
65 -- fuzzy description match (e.g. "Steel" == "Steel Block")
\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
76 match = key -- substring description match (only if no ambiguities)
\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
88 return "x", dir.x > 0 and 1 or -1
\r
91 return "y", dir.y > 0 and 1 or -1
\r
93 return "z", dir.z > 0 and 1 or -1
\r
96 local function mkdir(path)
\r
97 if minetest.mkdir then
\r
98 minetest.mkdir(path)
\r
100 os.execute('mkdir "' .. path .. '"')
\r
104 local function check_filename(name)
\r
105 return name:find("^[%w%s%^&'@{}%[%],%$=!%-#%(%)%%%.%+~_]+$") ~= nil
\r
109 minetest.register_chatcommand("/about", {
\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
117 -- mostly copied from builtin/chatcommands.lua with minor modifications
\r
118 minetest.register_chatcommand("/help", {
\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
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
131 if def.description and def.description ~= "" then
\r
132 msg = msg .. ": " .. def.description
\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
140 if param == "" then
\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
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
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
160 return true, "Available commands:\n"..table.concat(cmds, "\n")
\r
162 return minetest.chatcommands["help"].func(name, "/" .. param)
\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
181 worldedit.player_notify(name, "invalid usage: " .. param)
\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
196 for _, v in ipairs(vecs) do
\r
197 ret = math.max(ret, minetest.get_node_light(vector.add(pos, v)))
\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
212 minetest.register_chatcommand("/reset", {
\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
228 minetest.register_chatcommand("/mark", {
\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
239 minetest.register_chatcommand("/unmark", {
\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
255 minetest.register_chatcommand("/pos1", {
\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
268 minetest.register_chatcommand("/pos2", {
\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
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
299 worldedit.player_notify(name, "position 1 not set")
\r
301 if worldedit.pos2[name] ~= nil then
\r
302 worldedit.player_notify(name, "position 2: " .. minetest.pos_to_string(worldedit.pos2[name]))
\r
304 worldedit.player_notify(name, "position 2 not set")
\r
307 worldedit.player_notify(name, "unknown subcommand: " .. param)
\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
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
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
360 minetest.register_chatcommand("/volume", {
\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
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
380 minetest.register_chatcommand("/deleteblocks", {
\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
388 worldedit.player_notify(name, "Area deleted.")
\r
390 worldedit.player_notify(name, "There was an error during deletion of the area.")
\r
395 minetest.register_chatcommand("/set", {
\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 return end
\r
403 local count = worldedit.set(worldedit.pos1[name], worldedit.pos2[name], node)
\r
404 worldedit.player_notify(name, count .. " nodes set")
\r
405 end, check_region),
\r
408 minetest.register_chatcommand("/param2", {
\r
409 params = "<param2>",
\r
410 description = "Set param2 of all nodes in the current WorldEdit region to <param2>",
\r
411 privs = {worldedit=true},
\r
412 func = safe_region(function(name, param)
\r
413 local param2 = tonumber(param)
\r
415 worldedit.player_notify(name, "Invalid or missing param2 argument")
\r
417 elseif param2 < 0 or param2 > 255 then
\r
418 worldedit.player_notify(name, "Param2 is out of range (must be between 0 and 255 inclusive)!")
\r
422 local count = worldedit.set_param2(worldedit.pos1[name], worldedit.pos2[name], param2)
\r
423 worldedit.player_notify(name, count .. " nodes altered")
\r
424 end, check_region),
\r
427 minetest.register_chatcommand("/mix", {
\r
428 params = "<node1> ...",
\r
429 description = "Fill the current WorldEdit region with a random mix of <node1>, ...",
\r
430 privs = {worldedit=true},
\r
431 func = safe_region(function(name, param)
\r
433 for nodename in param:gmatch("[^%s]+") do
\r
434 local node = get_node(name, nodename)
\r
435 if not node then return end
\r
436 nodes[#nodes + 1] = node
\r
439 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
440 local count = worldedit.set(pos1, pos2, nodes)
\r
441 worldedit.player_notify(name, count .. " nodes set")
\r
442 end, check_region),
\r
445 local check_replace = function(name, param)
\r
446 local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$")
\r
447 if found == nil then
\r
448 worldedit.player_notify(name, "invalid usage: " .. param)
\r
451 local newsearchnode = worldedit.normalize_nodename(searchnode)
\r
452 if not newsearchnode then
\r
453 worldedit.player_notify(name, "invalid search node name: " .. searchnode)
\r
456 local newreplacenode = worldedit.normalize_nodename(replacenode)
\r
457 if not newreplacenode then
\r
458 worldedit.player_notify(name, "invalid replace node name: " .. replacenode)
\r
461 return check_region(name, param)
\r
464 minetest.register_chatcommand("/replace", {
\r
465 params = "<search node> <replace node>",
\r
466 description = "Replace all instances of <search node> with <replace node> in the current WorldEdit region",
\r
467 privs = {worldedit=true},
\r
468 func = safe_region(function(name, param)
\r
469 local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$")
\r
470 local norm_search_node = worldedit.normalize_nodename(search_node)
\r
471 local norm_replace_node = worldedit.normalize_nodename(replace_node)
\r
472 local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name],
\r
473 norm_search_node, norm_replace_node)
\r
474 worldedit.player_notify(name, count .. " nodes replaced")
\r
475 end, check_replace),
\r
478 minetest.register_chatcommand("/replaceinverse", {
\r
479 params = "<search node> <replace node>",
\r
480 description = "Replace all nodes other than <search node> with <replace node> in the current WorldEdit region",
\r
481 privs = {worldedit=true},
\r
482 func = safe_region(function(name, param)
\r
483 local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$")
\r
484 local norm_search_node = worldedit.normalize_nodename(search_node)
\r
485 local norm_replace_node = worldedit.normalize_nodename(replace_node)
\r
486 local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name],
\r
487 norm_search_node, norm_replace_node, true)
\r
488 worldedit.player_notify(name, count .. " nodes replaced")
\r
489 end, check_replace),
\r
492 local check_cube = function(name, param)
\r
493 if worldedit.pos1[name] == nil then
\r
494 worldedit.player_notify(name, "no position 1 selected")
\r
497 local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
498 if found == nil then
\r
499 worldedit.player_notify(name, "invalid usage: " .. param)
\r
502 local node = get_node(name, nodename)
\r
503 if not node then return nil end
\r
504 return tonumber(w) * tonumber(h) * tonumber(l)
\r
507 minetest.register_chatcommand("/hollowcube", {
\r
508 params = "<width> <height> <length> <node>",
\r
509 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
510 privs = {worldedit=true},
\r
511 func = safe_region(function(name, param)
\r
512 local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
513 local node = get_node(name, nodename)
\r
514 local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node, true)
\r
515 worldedit.player_notify(name, count .. " nodes added")
\r
519 minetest.register_chatcommand("/cube", {
\r
520 params = "<width> <height> <length> <node>",
\r
521 description = "Add a cube with its ground level centered at WorldEdit position 1 with dimensions <width> x <height> x <length>, composed of <node>.",
\r
522 privs = {worldedit=true},
\r
523 func = safe_region(function(name, param)
\r
524 local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
525 local node = get_node(name, nodename)
\r
526 local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node)
\r
527 worldedit.player_notify(name, count .. " nodes added")
\r
531 local check_sphere = function(name, param)
\r
532 if worldedit.pos1[name] == nil then
\r
533 worldedit.player_notify(name, "no position 1 selected")
\r
536 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
537 if found == nil then
\r
538 worldedit.player_notify(name, "invalid usage: " .. param)
\r
541 local node = get_node(name, nodename)
\r
542 if not node then return nil end
\r
543 return math.ceil((4 * math.pi * (tonumber(radius) ^ 3)) / 3) --volume of sphere
\r
546 minetest.register_chatcommand("/hollowsphere", {
\r
547 params = "<radius> <node>",
\r
548 description = "Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",
\r
549 privs = {worldedit=true},
\r
550 func = safe_region(function(name, param)
\r
551 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
552 local node = get_node(name, nodename)
\r
553 local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node, true)
\r
554 worldedit.player_notify(name, count .. " nodes added")
\r
555 end, check_sphere),
\r
558 minetest.register_chatcommand("/sphere", {
\r
559 params = "<radius> <node>",
\r
560 description = "Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",
\r
561 privs = {worldedit=true},
\r
562 func = safe_region(function(name, param)
\r
563 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
564 local node = get_node(name, nodename)
\r
565 local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node)
\r
566 worldedit.player_notify(name, count .. " nodes added")
\r
567 end, check_sphere),
\r
570 local check_dome = function(name, param)
\r
571 if worldedit.pos1[name] == nil then
\r
572 worldedit.player_notify(name, "no position 1 selected")
\r
575 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
576 if found == nil then
\r
577 worldedit.player_notify(name, "invalid usage: " .. param)
\r
580 local node = get_node(name, nodename)
\r
581 if not node then return nil end
\r
582 return math.ceil((2 * math.pi * (tonumber(radius) ^ 3)) / 3) --volume of dome
\r
585 minetest.register_chatcommand("/hollowdome", {
\r
586 params = "<radius> <node>",
\r
587 description = "Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",
\r
588 privs = {worldedit=true},
\r
589 func = safe_region(function(name, param)
\r
590 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
591 local node = get_node(name, nodename)
\r
592 local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node, true)
\r
593 worldedit.player_notify(name, count .. " nodes added")
\r
597 minetest.register_chatcommand("/dome", {
\r
598 params = "<radius> <node>",
\r
599 description = "Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",
\r
600 privs = {worldedit=true},
\r
601 func = safe_region(function(name, param)
\r
602 local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
\r
603 local node = get_node(name, nodename)
\r
604 local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node)
\r
605 worldedit.player_notify(name, count .. " nodes added")
\r
609 local check_cylinder = function(name, param)
\r
610 if worldedit.pos1[name] == nil then
\r
611 worldedit.player_notify(name, "no position 1 selected")
\r
615 local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
616 if found == nil then
\r
618 found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
\r
621 if found == nil then
\r
622 worldedit.player_notify(name, "invalid usage: " .. param)
\r
625 local node = get_node(name, nodename)
\r
626 if not node then return nil end
\r
627 local radius = math.max(tonumber(radius1), tonumber(radius2))
\r
628 return math.ceil(math.pi * (radius ^ 2) * tonumber(length))
\r
631 minetest.register_chatcommand("/hollowcylinder", {
\r
632 params = "x/y/z/? <length> <radius1> [radius2] <node>",
\r
633 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
634 privs = {worldedit=true},
\r
635 func = safe_region(function(name, param)
\r
637 local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
638 if found == nil then
\r
640 found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
\r
643 length = tonumber(length)
\r
644 if axis == "?" then
\r
645 axis, sign = worldedit.player_axis(name)
\r
646 length = length * sign
\r
648 local node = get_node(name, nodename)
\r
649 local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node, true)
\r
650 worldedit.player_notify(name, count .. " nodes added")
\r
651 end, check_cylinder),
\r
654 minetest.register_chatcommand("/cylinder", {
\r
655 params = "x/y/z/? <length> <radius1> [radius2] <node>",
\r
656 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
657 privs = {worldedit=true},
\r
658 func = safe_region(function(name, param)
\r
660 local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
661 if found == nil then
\r
663 found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
\r
666 length = tonumber(length)
\r
667 if axis == "?" then
\r
668 axis, sign = worldedit.player_axis(name)
\r
669 length = length * sign
\r
671 local node = get_node(name, nodename)
\r
672 local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node)
\r
673 worldedit.player_notify(name, count .. " nodes added")
\r
674 end, check_cylinder),
\r
677 local check_pyramid = function(name, param)
\r
678 if worldedit.pos1[name] == nil then
\r
679 worldedit.player_notify(name, "no position 1 selected")
\r
682 local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")
\r
683 if found == nil then
\r
684 worldedit.player_notify(name, "invalid usage: " .. param)
\r
687 local node = get_node(name, nodename)
\r
688 if not node then return nil end
\r
689 height = tonumber(height)
\r
690 return math.ceil(((height * 2 + 1) ^ 2) * height / 3)
\r
693 minetest.register_chatcommand("/hollowpyramid", {
\r
694 params = "x/y/z/? <height> <node>",
\r
695 description = "Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>",
\r
696 privs = {worldedit=true},
\r
697 func = safe_region(function(name, param)
\r
698 local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")
\r
699 height = tonumber(height)
\r
700 if axis == "?" then
\r
701 axis, sign = worldedit.player_axis(name)
\r
702 height = height * sign
\r
704 local node = get_node(name, nodename)
\r
705 local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node, true)
\r
706 worldedit.player_notify(name, count .. " nodes added")
\r
707 end, check_pyramid),
\r
710 minetest.register_chatcommand("/pyramid", {
\r
711 params = "x/y/z/? <height> <node>",
\r
712 description = "Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>",
\r
713 privs = {worldedit=true},
\r
714 func = safe_region(function(name, param)
\r
715 local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")
\r
716 height = tonumber(height)
\r
717 if axis == "?" then
\r
718 axis, sign = worldedit.player_axis(name)
\r
719 height = height * sign
\r
721 local node = get_node(name, nodename)
\r
722 local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node)
\r
723 worldedit.player_notify(name, count .. " nodes added")
\r
724 end, check_pyramid),
\r
727 minetest.register_chatcommand("/spiral", {
\r
728 params = "<length> <height> <space> <node>",
\r
729 description = "Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <space>, composed of <node>",
\r
730 privs = {worldedit=true},
\r
731 func = safe_region(function(name, param)
\r
732 local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
733 local node = get_node(name, nodename)
\r
734 local count = worldedit.spiral(worldedit.pos1[name], tonumber(length), tonumber(height), tonumber(space), node)
\r
735 worldedit.player_notify(name, count .. " nodes added")
\r
737 function(name, param)
\r
738 if worldedit.pos1[name] == nil then
\r
739 worldedit.player_notify(name, "no position 1 selected")
\r
742 local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
\r
743 if found == nil then
\r
744 worldedit.player_notify(name, "invalid usage: " .. param)
\r
747 local node = get_node(name, nodename)
\r
748 if not node then return nil end
\r
749 return 1 -- TODO: return an useful value
\r
753 minetest.register_chatcommand("/copy", {
\r
754 params = "x/y/z/? <amount>",
\r
755 description = "Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes",
\r
756 privs = {worldedit=true},
\r
757 func = safe_region(function(name, param)
\r
758 local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
759 if found == nil then
\r
760 worldedit.player_notify(name, "invalid usage: " .. param)
\r
763 amount = tonumber(amount)
\r
764 if axis == "?" then
\r
765 axis, sign = worldedit.player_axis(name)
\r
766 amount = amount * sign
\r
769 local count = worldedit.copy(worldedit.pos1[name], worldedit.pos2[name], axis, amount)
\r
770 worldedit.player_notify(name, count .. " nodes copied")
\r
772 function(name, param)
\r
773 local volume = check_region(name, param)
\r
774 return volume and volume * 2 or volume
\r
778 minetest.register_chatcommand("/move", {
\r
779 params = "x/y/z/? <amount>",
\r
780 description = "Move the current WorldEdit region along the x/y/z/? axis by <amount> nodes",
\r
781 privs = {worldedit=true},
\r
782 func = safe_region(function(name, param)
\r
783 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
784 local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
785 if found == nil then
\r
786 worldedit.player_notify(name, "invalid usage: " .. param)
\r
789 amount = tonumber(amount)
\r
790 if axis == "?" then
\r
791 axis, sign = worldedit.player_axis(name)
\r
792 amount = amount * sign
\r
795 local count = worldedit.move(pos1, pos2, axis, amount)
\r
797 pos1[axis] = pos1[axis] + amount
\r
798 pos2[axis] = pos2[axis] + amount
\r
799 worldedit.mark_pos1(name)
\r
800 worldedit.mark_pos2(name)
\r
801 worldedit.player_notify(name, count .. " nodes moved")
\r
802 end, check_region),
\r
805 minetest.register_chatcommand("/stack", {
\r
806 params = "x/y/z/? <count>",
\r
807 description = "Stack the current WorldEdit region along the x/y/z/? axis <count> times",
\r
808 privs = {worldedit=true},
\r
809 func = safe_region(function(name, param)
\r
810 local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
811 repetitions = tonumber(repetitions)
\r
812 if axis == "?" then
\r
813 axis, sign = worldedit.player_axis(name)
\r
814 repetitions = repetitions * sign
\r
816 local count = worldedit.stack(worldedit.pos1[name], worldedit.pos2[name], axis, repetitions)
\r
817 worldedit.player_notify(name, count .. " nodes stacked")
\r
819 function(name, param)
\r
820 local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
821 if found == nil then
\r
822 worldedit.player_notify(name, "invalid usage: " .. param)
\r
825 local count = check_region(name, param)
\r
826 if count then return (tonumber(repetitions) + 1) * count end
\r
831 minetest.register_chatcommand("/stack2", {
\r
832 params = "<count> <x> <y> <z>",
\r
833 description = "Stack the current WorldEdit region <count> times by offset <x>, <y>, <z>",
\r
834 privs = {worldedit=true},
\r
835 func = function(name, param)
\r
836 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
837 if pos1 == nil or pos2 == nil then
\r
838 worldedit.player_notify(name, "Select a position first!")
\r
841 local repetitions, incs = param:match("(%d+)%s*(.+)")
\r
842 if repetitions == nil then
\r
843 worldedit.player_notify(name, "invalid count: " .. param)
\r
846 repetitions = tonumber(repetitions)
\r
848 local x, y, z = incs:match("([+-]?%d+) ([+-]?%d+) ([+-]?%d+)")
\r
850 worldedit.player_notify(name, "invalid increments: " .. param)
\r
853 x, y, z = tonumber(x), tonumber(y), tonumber(z)
\r
855 local count = worldedit.volume(pos1, pos2) * repetitions
\r
857 return safe_region(function()
\r
858 worldedit.stack2(pos1, pos2, {x=x, y=y, z=z}, repetitions,
\r
859 function() worldedit.player_notify(name, count .. " nodes stacked") end)
\r
862 end)(name,param) -- more hax --wip: clean this up a little bit
\r
867 minetest.register_chatcommand("/stretch", {
\r
868 params = "<stretchx> <stretchy> <stretchz>",
\r
869 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
870 privs = {worldedit=true},
\r
871 func = safe_region(function(name, param)
\r
872 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
873 local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")
\r
874 stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)
\r
875 local count, pos1, pos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)
\r
877 --reset markers to scaled positions
\r
878 worldedit.pos1[name] = pos1
\r
879 worldedit.pos2[name] = pos2
\r
880 worldedit.mark_pos1(name)
\r
881 worldedit.mark_pos2(name)
\r
883 worldedit.player_notify(name, count .. " nodes stretched")
\r
885 function(name, param)
\r
886 local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")
\r
887 if found == nil then
\r
888 worldedit.player_notify(name, "invalid usage: " .. param)
\r
891 stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)
\r
892 if stretchx == 0 or stretchy == 0 or stretchz == 0 then
\r
893 worldedit.player_notify(name, "invalid scaling factors: " .. param)
\r
895 local count = check_region(name, param)
\r
896 if count then return tonumber(stretchx) * tonumber(stretchy) * tonumber(stretchz) * count end
\r
901 minetest.register_chatcommand("/transpose", {
\r
902 params = "x/y/z/? x/y/z/?",
\r
903 description = "Transpose the current WorldEdit region along the x/y/z/? and x/y/z/? axes",
\r
904 privs = {worldedit=true},
\r
905 func = safe_region(function(name, param)
\r
906 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
907 local found, _, axis1, axis2 = param:find("^([xyz%?])%s+([xyz%?])$")
\r
908 if axis1 == "?" then axis1 = worldedit.player_axis(name) end
\r
909 if axis2 == "?" then axis2 = worldedit.player_axis(name) end
\r
910 local count, pos1, pos2 = worldedit.transpose(pos1, pos2, axis1, axis2)
\r
912 --reset markers to transposed positions
\r
913 worldedit.pos1[name] = pos1
\r
914 worldedit.pos2[name] = pos2
\r
915 worldedit.mark_pos1(name)
\r
916 worldedit.mark_pos2(name)
\r
918 worldedit.player_notify(name, count .. " nodes transposed")
\r
920 function(name, param)
\r
921 local found, _, axis1, axis2 = param:find("^([xyz%?])%s+([xyz%?])$")
\r
922 if found == nil then
\r
923 worldedit.player_notify(name, "invalid usage: " .. param)
\r
926 if axis1 == axis2 then
\r
927 worldedit.player_notify(name, "invalid usage: axes must be different")
\r
930 return check_region(name, param)
\r
934 minetest.register_chatcommand("/flip", {
\r
935 params = "x/y/z/?",
\r
936 description = "Flip the current WorldEdit region along the x/y/z/? axis",
\r
937 privs = {worldedit=true},
\r
938 func = safe_region(function(name, param)
\r
939 if param == "?" then param = worldedit.player_axis(name) end
\r
940 local count = worldedit.flip(worldedit.pos1[name], worldedit.pos2[name], param)
\r
941 worldedit.player_notify(name, count .. " nodes flipped")
\r
943 function(name, param)
\r
944 if param ~= "x" and param ~= "y" and param ~= "z" and param ~= "?" then
\r
945 worldedit.player_notify(name, "invalid usage: " .. param)
\r
948 return check_region(name, param)
\r
952 minetest.register_chatcommand("/rotate", {
\r
953 params = "<axis> <angle>",
\r
954 description = "Rotate the current WorldEdit region around the axis <axis> by angle <angle> (90 degree increment)",
\r
955 privs = {worldedit=true},
\r
956 func = safe_region(function(name, param)
\r
957 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
958 local found, _, axis, angle = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
959 if axis == "?" then axis = worldedit.player_axis(name) end
\r
960 local count, pos1, pos2 = worldedit.rotate(pos1, pos2, axis, angle)
\r
962 --reset markers to rotated positions
\r
963 worldedit.pos1[name] = pos1
\r
964 worldedit.pos2[name] = pos2
\r
965 worldedit.mark_pos1(name)
\r
966 worldedit.mark_pos2(name)
\r
968 worldedit.player_notify(name, count .. " nodes rotated")
\r
970 function(name, param)
\r
971 local found, _, axis, angle = param:find("^([xyz%?])%s+([+-]?%d+)$")
\r
972 if found == nil then
\r
973 worldedit.player_notify(name, "invalid usage: " .. param)
\r
976 if angle % 90 ~= 0 then
\r
977 worldedit.player_notify(name, "invalid usage: angle must be multiple of 90")
\r
980 return check_region(name, param)
\r
984 minetest.register_chatcommand("/orient", {
\r
985 params = "<angle>",
\r
986 description = "Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment)",
\r
987 privs = {worldedit=true},
\r
988 func = safe_region(function(name, param)
\r
989 local found, _, angle = param:find("^([+-]?%d+)$")
\r
990 local count = worldedit.orient(worldedit.pos1[name], worldedit.pos2[name], angle)
\r
991 worldedit.player_notify(name, count .. " nodes oriented")
\r
993 function(name, param)
\r
994 local found, _, angle = param:find("^([+-]?%d+)$")
\r
995 if found == nil then
\r
996 worldedit.player_notify(name, "invalid usage: " .. param)
\r
999 if angle % 90 ~= 0 then
\r
1000 worldedit.player_notify(name, "invalid usage: angle must be multiple of 90")
\r
1003 return check_region(name, param)
\r
1007 minetest.register_chatcommand("/fixlight", {
\r
1009 description = "Fix the lighting in the current WorldEdit region",
\r
1010 privs = {worldedit=true},
\r
1011 func = safe_region(function(name, param)
\r
1012 local count = worldedit.fixlight(worldedit.pos1[name], worldedit.pos2[name])
\r
1013 worldedit.player_notify(name, count .. " nodes updated")
\r
1017 minetest.register_chatcommand("/drain", {
\r
1019 description = "Remove any fluid node within the current WorldEdit region",
\r
1020 privs = {worldedit=true},
\r
1021 func = safe_region(function(name, param)
\r
1022 -- TODO: make an API function for this
\r
1024 local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name])
\r
1025 for x = pos1.x, pos2.x do
\r
1026 for y = pos1.y, pos2.y do
\r
1027 for z = pos1.z, pos2.z do
\r
1028 local n = minetest.get_node({x=x, y=y, z=z}).name
\r
1029 local d = minetest.registered_nodes[n]
\r
1030 if d ~= nil and (d["drawtype"] == "liquid" or d["drawtype"] == "flowingliquid") then
\r
1031 minetest.remove_node({x=x, y=y, z=z})
\r
1037 worldedit.player_notify(name, count .. " nodes updated")
\r
1041 minetest.register_chatcommand("/hide", {
\r
1043 description = "Hide all nodes in the current WorldEdit region non-destructively",
\r
1044 privs = {worldedit=true},
\r
1045 func = safe_region(function(name, param)
\r
1046 local count = worldedit.hide(worldedit.pos1[name], worldedit.pos2[name])
\r
1047 worldedit.player_notify(name, count .. " nodes hidden")
\r
1051 minetest.register_chatcommand("/suppress", {
\r
1052 params = "<node>",
\r
1053 description = "Suppress all <node> in the current WorldEdit region non-destructively",
\r
1054 privs = {worldedit=true},
\r
1055 func = safe_region(function(name, param)
\r
1056 local node = get_node(name, param)
\r
1057 local count = worldedit.suppress(worldedit.pos1[name], worldedit.pos2[name], node)
\r
1058 worldedit.player_notify(name, count .. " nodes suppressed")
\r
1059 end, check_region),
\r
1062 minetest.register_chatcommand("/highlight", {
\r
1063 params = "<node>",
\r
1064 description = "Highlight <node> in the current WorldEdit region by hiding everything else non-destructively",
\r
1065 privs = {worldedit=true},
\r
1066 func = safe_region(function(name, param)
\r
1067 local node = get_node(name, param)
\r
1068 local count = worldedit.highlight(worldedit.pos1[name], worldedit.pos2[name], node)
\r
1069 worldedit.player_notify(name, count .. " nodes highlighted")
\r
1070 end, check_region),
\r
1073 minetest.register_chatcommand("/restore", {
\r
1075 description = "Restores nodes hidden with WorldEdit in the current WorldEdit region",
\r
1076 privs = {worldedit=true},
\r
1077 func = safe_region(function(name, param)
\r
1078 local count = worldedit.restore(worldedit.pos1[name], worldedit.pos2[name])
\r
1079 worldedit.player_notify(name, count .. " nodes restored")
\r
1083 minetest.register_chatcommand("/save", {
\r
1084 params = "<file>",
\r
1085 description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.we\"",
\r
1086 privs = {worldedit=true},
\r
1087 func = safe_region(function(name, param)
\r
1088 if param == "" then
\r
1089 worldedit.player_notify(name, "invalid usage: " .. param)
\r
1092 if not check_filename(param) then
\r
1093 worldedit.player_notify(name, "Disallowed file name: " .. param)
\r
1096 local result, count = worldedit.serialize(worldedit.pos1[name],
\r
1097 worldedit.pos2[name])
\r
1099 local path = minetest.get_worldpath() .. "/schems"
\r
1100 -- Create directory if it does not already exist
\r
1103 local filename = path .. "/" .. param .. ".we"
\r
1104 local file, err = io.open(filename, "wb")
\r
1105 if err ~= nil then
\r
1106 worldedit.player_notify(name, "Could not save file to \"" .. filename .. "\"")
\r
1109 file:write(result)
\r
1113 worldedit.player_notify(name, count .. " nodes saved")
\r
1117 minetest.register_chatcommand("/allocate", {
\r
1118 params = "<file>",
\r
1119 description = "Set the region defined by nodes from \"(world folder)/schems/<file>.we\" as the current WorldEdit region",
\r
1120 privs = {worldedit=true},
\r
1121 func = function(name, param)
\r
1122 local pos = get_position(name)
\r
1123 if pos == nil then return end
\r
1125 if param == "" then
\r
1126 worldedit.player_notify(name, "invalid usage: " .. param)
\r
1129 if not check_filename(param) then
\r
1130 worldedit.player_notify(name, "Disallowed file name: " .. param)
\r
1134 local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"
\r
1135 local file, err = io.open(filename, "rb")
\r
1136 if err ~= nil then
\r
1137 worldedit.player_notify(name, "could not open file \"" .. filename .. "\"")
\r
1140 local value = file:read("*a")
\r
1143 local version = worldedit.read_header(value)
\r
1144 if version == 0 then
\r
1145 worldedit.player_notify(name, "File is invalid!")
\r
1147 elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
\r
1148 worldedit.player_notify(name, "File was created with newer version of WorldEdit!")
\r
1150 local nodepos1, nodepos2, count = worldedit.allocate(pos, value)
\r
1152 worldedit.pos1[name] = nodepos1
\r
1153 worldedit.mark_pos1(name)
\r
1154 worldedit.pos2[name] = nodepos2
\r
1155 worldedit.mark_pos2(name)
\r
1157 worldedit.player_notify(name, count .. " nodes allocated")
\r
1161 minetest.register_chatcommand("/load", {
\r
1162 params = "<file>",
\r
1163 description = "Load nodes from \"(world folder)/schems/<file>[.we[m]]\" with position 1 of the current WorldEdit region as the origin",
\r
1164 privs = {worldedit=true},
\r
1165 func = function(name, param)
\r
1166 local pos = get_position(name)
\r
1167 if pos == nil then return end
\r
1169 if param == "" then
\r
1170 worldedit.player_notify(name, "invalid usage: " .. param)
\r
1173 if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
\r
1174 worldedit.player_notify(name, "invalid file name: " .. param)
\r
1178 --find the file in the world path
\r
1179 local testpaths = {
\r
1180 minetest.get_worldpath() .. "/schems/" .. param,
\r
1181 minetest.get_worldpath() .. "/schems/" .. param .. ".we",
\r
1182 minetest.get_worldpath() .. "/schems/" .. param .. ".wem",
\r
1185 for index, path in ipairs(testpaths) do
\r
1186 file, err = io.open(path, "rb")
\r
1192 worldedit.player_notify(name, "could not open file \"" .. param .. "\"")
\r
1195 local value = file:read("*a")
\r
1198 local version = worldedit.read_header(value)
\r
1199 if version == 0 then
\r
1200 worldedit.player_notify(name, "File is invalid!")
\r
1202 elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
\r
1203 worldedit.player_notify(name, "File was created with newer version of WorldEdit!")
\r
1207 local count = worldedit.deserialize(pos, value)
\r
1209 worldedit.player_notify(name, count .. " nodes loaded")
\r
1213 minetest.register_chatcommand("/lua", {
\r
1214 params = "<code>",
\r
1215 description = "Executes <code> as a Lua chunk in the global namespace",
\r
1216 privs = {worldedit=true, server=true},
\r
1217 func = function(name, param)
\r
1218 local err = worldedit.lua(param)
\r
1220 worldedit.player_notify(name, "code error: " .. err)
\r
1221 minetest.log("action", name.." tried to execute "..param)
\r
1223 worldedit.player_notify(name, "code successfully executed", false)
\r
1224 minetest.log("action", name.." executed "..param)
\r
1229 minetest.register_chatcommand("/luatransform", {
\r
1230 params = "<code>",
\r
1231 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
1232 privs = {worldedit=true, server=true},
\r
1233 func = safe_region(function(name, param)
\r
1234 local err = worldedit.luatransform(worldedit.pos1[name], worldedit.pos2[name], param)
\r
1236 worldedit.player_notify(name, "code error: " .. err, false)
\r
1237 minetest.log("action", name.." tried to execute luatransform "..param)
\r
1239 worldedit.player_notify(name, "code successfully executed", false)
\r
1240 minetest.log("action", name.." executed luatransform "..param)
\r
1245 minetest.register_chatcommand("/mtschemcreate", {
\r
1246 params = "<file>",
\r
1247 description = "Save the current WorldEdit region using the Minetest "..
\r
1248 "Schematic format to \"(world folder)/schems/<filename>.mts\"",
\r
1249 privs = {worldedit=true},
\r
1250 func = safe_region(function(name, param)
\r
1251 if param == nil then
\r
1252 worldedit.player_notify(name, "No filename specified")
\r
1255 if not check_filename(param) then
\r
1256 worldedit.player_notify(name, "Disallowed file name: " .. param)
\r
1260 local path = minetest.get_worldpath() .. "/schems"
\r
1261 -- Create directory if it does not already exist
\r
1264 local filename = path .. "/" .. param .. ".mts"
\r
1265 local ret = minetest.create_schematic(worldedit.pos1[name],
\r
1266 worldedit.pos2[name], worldedit.prob_list[name],
\r
1268 if ret == nil then
\r
1269 worldedit.player_notify(name, "Failed to create Minetest schematic", false)
\r
1271 worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)
\r
1273 worldedit.prob_list[name] = {}
\r
1277 minetest.register_chatcommand("/mtschemplace", {
\r
1278 params = "<file>",
\r
1279 description = "Load nodes from \"(world folder)/schems/<file>.mts\" with position 1 of the current WorldEdit region as the origin",
\r
1280 privs = {worldedit=true},
\r
1281 func = function(name, param)
\r
1282 if param == "" then
\r
1283 worldedit.player_notify(name, "no filename specified")
\r
1286 if not check_filename(param) then
\r
1287 worldedit.player_notify(name, "Disallowed file name: " .. param)
\r
1291 local pos = get_position(name)
\r
1292 if pos == nil then return end
\r
1294 local path = minetest.get_worldpath() .. "/schems/" .. param .. ".mts"
\r
1295 if minetest.place_schematic(pos, path) == nil then
\r
1296 worldedit.player_notify(name, "failed to place Minetest schematic", false)
\r
1298 worldedit.player_notify(name, "placed Minetest schematic " .. param ..
\r
1299 " at " .. minetest.pos_to_string(pos), false)
\r
1304 minetest.register_chatcommand("/mtschemprob", {
\r
1305 params = "start/finish/get",
\r
1306 description = "Begins node probability entry for Minetest schematics, gets the nodes that have probabilities set, or ends node probability entry",
\r
1307 privs = {worldedit=true},
\r
1308 func = function(name, param)
\r
1309 if param == "start" then --start probability setting
\r
1310 worldedit.set_pos[name] = "prob"
\r
1311 worldedit.prob_list[name] = {}
\r
1312 worldedit.player_notify(name, "select Minetest schematic probability values by punching nodes")
\r
1313 elseif param == "finish" then --finish probability setting
\r
1314 worldedit.set_pos[name] = nil
\r
1315 worldedit.player_notify(name, "finished Minetest schematic probability selection")
\r
1316 elseif param == "get" then --get all nodes that had probabilities set on them
\r
1318 local problist = worldedit.prob_list[name]
\r
1319 if problist == nil then
\r
1322 for k,v in pairs(problist) do
\r
1323 local prob = math.floor(((v.prob / 256) * 100) * 100 + 0.5) / 100
\r
1324 text = text .. minetest.pos_to_string(v.pos) .. ": " .. prob .. "% | "
\r
1326 worldedit.player_notify(name, "currently set node probabilities:")
\r
1327 worldedit.player_notify(name, text)
\r
1329 worldedit.player_notify(name, "unknown subcommand: " .. param)
\r
1334 minetest.register_on_player_receive_fields(function(player, formname, fields)
\r
1335 if formname == "prob_val_enter" and not (fields.text == "" or fields.text == nil) then
\r
1336 local name = player:get_player_name()
\r
1337 local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)}
\r
1338 local index = table.getn(worldedit.prob_list[name]) + 1
\r
1339 worldedit.prob_list[name][index] = prob_entry
\r
1343 minetest.register_chatcommand("/clearobjects", {
\r
1345 description = "Clears all objects within the WorldEdit region",
\r
1346 privs = {worldedit=true},
\r
1347 func = safe_region(function(name, param)
\r
1348 local count = worldedit.clear_objects(worldedit.pos1[name], worldedit.pos2[name])
\r
1349 worldedit.player_notify(name, count .. " objects cleared")
\r