From 1d4b145ba1545d9cba7426b8336a87a3ae2f2bb5 Mon Sep 17 00:00:00 2001 From: Joseph Pickard Date: Sun, 14 Feb 2016 07:49:04 -0500 Subject: [PATCH] Added '//expand' and '//contract' commands that expand and contract the user's worldedit selection. Usage: //expand <+/-> //contract <+/-> --- worldedit_commands/init.lua | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 83a127e..eb29f74 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -30,6 +30,18 @@ local function get_node(name, nodename) return node end +-- Gets the individual parameters from a command from the command's parameter string. +local function process_params(paramStr) + local count = 0 + local paramList = {} + for param in paramStr:gmatch("%S+") do + count = count + 1 + paramList[count] = param + end + + return paramList, count +end + function worldedit.player_notify(name, message) minetest.chat_send_player(name, "WorldEdit -!- " .. message, false) end @@ -70,6 +82,30 @@ function worldedit.player_axis(name) return "z", dir.z > 0 and 1 or -1 end +-- Gets the axis letter, and sign (either +1 or -1) from an argument (such as '+x', '-y', etc.). +-- This function can be used for manually specifying an axis and direction as an argument +function worldedit.arg_axis(param) + if 2 == string.len(param) then + local sign = string.sub(param, 1, 1) + local axis = string.sub(param, 2, 2) + if '+' == sign then + sign = 1 + elseif '-' == sign then + sign = -1 + else + return nil + end + + if not ("x" == axis or "y" == axis or "z" == axis) then + return nil + end + + return axis, sign + else + return nil + end +end + local function mkdir(path) if minetest.mkdir then minetest.mkdir(path) @@ -1138,6 +1174,78 @@ minetest.register_chatcommand("/mtschemprob", { end, }) +-- Expands or contracts the region for user contains the parameters passed to the //expand or //contract command. +-- If invert is true, contract instead. +local function expand_region(name, param, invert) + local params, paramCount = process_params(param) + local count = nil + local axis = nil + local sign = nil + + if nil == worldedit.pos1[name] or nil == worldedit.pos2[name] then + worldedit.player_notify(name, "no region selected") + return + end + + if 0 < paramCount then + count = tonumber(params[1]) + else + worldedit.player_notify(name, "not enough arguments") + return + end + + if nil == count then + worldedit.player_notify(name, "not a valid integer: " .. params[1]) + return + end + + if 1 < paramCount then + axis, sign = worldedit.arg_axis(params[2]) + if nil == axis then + worldedit.player_notify(name, "not a valid axis: " .. params[2]) + return + end + else + axis, sign = worldedit.player_axis(name) + end + + if sign < 0 then + count = 0 - count + end + + if invert then + sign = 0 - sign + end + + if (worldedit.pos1[name][axis] > worldedit.pos2[name][axis]) == (0 < sign) then + worldedit.pos1[name][axis] = worldedit.pos1[name][axis] + count + worldedit.mark_pos1(name) + else + worldedit.pos2[name][axis] = worldedit.pos2[name][axis] + count + worldedit.mark_pos2(name) + end +end + +minetest.register_chatcommand("/expand", { + params = " <+/->", + description = "Expands the worldedit selection by the specified amount in the direction the player is facing or the one specified.",, + privs = {worldedit=true}, + func = function(name, param) + return expand_region(name, param, false) + end, +}) + +minetest.register_chatcommand("/contract", { + params = " <+/->", + description = "Contracts the worldedit selection by the specified amount in the direction the player is facing or the one specified.", + privs = {worldedit=true}, + func = function(name, param) + return expand_region(name, param, true) + end, +}) + + minetest.register_on_player_receive_fields( function(player, formname, fields) if (formname == "prob_val_enter") and (fields.text ~= "") then